s3api_copy_validation.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package s3api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  6. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  7. )
  8. // CopyValidationError represents validation errors during copy operations
  9. type CopyValidationError struct {
  10. Code s3err.ErrorCode
  11. Message string
  12. }
  13. func (e *CopyValidationError) Error() string {
  14. return e.Message
  15. }
  16. // ValidateCopyEncryption performs comprehensive validation of copy encryption parameters
  17. func ValidateCopyEncryption(srcMetadata map[string][]byte, headers http.Header) error {
  18. // Validate SSE-C copy requirements
  19. if err := validateSSECCopyRequirements(srcMetadata, headers); err != nil {
  20. return err
  21. }
  22. // Validate SSE-KMS copy requirements
  23. if err := validateSSEKMSCopyRequirements(srcMetadata, headers); err != nil {
  24. return err
  25. }
  26. // Validate incompatible encryption combinations
  27. if err := validateEncryptionCompatibility(headers); err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. // validateSSECCopyRequirements validates SSE-C copy header requirements
  33. func validateSSECCopyRequirements(srcMetadata map[string][]byte, headers http.Header) error {
  34. srcIsSSEC := IsSSECEncrypted(srcMetadata)
  35. hasCopyHeaders := hasSSECCopyHeaders(headers)
  36. hasSSECHeaders := hasSSECHeaders(headers)
  37. // If source is SSE-C encrypted, copy headers are required
  38. if srcIsSSEC && !hasCopyHeaders {
  39. return &CopyValidationError{
  40. Code: s3err.ErrInvalidRequest,
  41. Message: "SSE-C encrypted source requires copy source encryption headers",
  42. }
  43. }
  44. // If copy headers are provided, source must be SSE-C encrypted
  45. if hasCopyHeaders && !srcIsSSEC {
  46. return &CopyValidationError{
  47. Code: s3err.ErrInvalidRequest,
  48. Message: "SSE-C copy headers provided but source is not SSE-C encrypted",
  49. }
  50. }
  51. // Validate copy header completeness
  52. if hasCopyHeaders {
  53. if err := validateSSECCopyHeaderCompleteness(headers); err != nil {
  54. return err
  55. }
  56. }
  57. // Validate destination SSE-C headers if present
  58. if hasSSECHeaders {
  59. if err := validateSSECHeaderCompleteness(headers); err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. // validateSSEKMSCopyRequirements validates SSE-KMS copy requirements
  66. func validateSSEKMSCopyRequirements(srcMetadata map[string][]byte, headers http.Header) error {
  67. dstIsSSEKMS := IsSSEKMSRequest(&http.Request{Header: headers})
  68. // Validate KMS key ID format if provided
  69. if dstIsSSEKMS {
  70. keyID := headers.Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId)
  71. if keyID != "" && !isValidKMSKeyID(keyID) {
  72. return &CopyValidationError{
  73. Code: s3err.ErrKMSKeyNotFound,
  74. Message: fmt.Sprintf("Invalid KMS key ID format: %s", keyID),
  75. }
  76. }
  77. }
  78. // Validate encryption context format if provided
  79. if contextHeader := headers.Get(s3_constants.AmzServerSideEncryptionContext); contextHeader != "" {
  80. if !dstIsSSEKMS {
  81. return &CopyValidationError{
  82. Code: s3err.ErrInvalidRequest,
  83. Message: "Encryption context can only be used with SSE-KMS",
  84. }
  85. }
  86. // Validate base64 encoding and JSON format
  87. if err := validateEncryptionContext(contextHeader); err != nil {
  88. return &CopyValidationError{
  89. Code: s3err.ErrInvalidRequest,
  90. Message: fmt.Sprintf("Invalid encryption context: %v", err),
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. // validateEncryptionCompatibility validates that encryption methods are not conflicting
  97. func validateEncryptionCompatibility(headers http.Header) error {
  98. hasSSEC := hasSSECHeaders(headers)
  99. hasSSEKMS := headers.Get(s3_constants.AmzServerSideEncryption) == "aws:kms"
  100. hasSSES3 := headers.Get(s3_constants.AmzServerSideEncryption) == "AES256"
  101. // Count how many encryption methods are specified
  102. encryptionCount := 0
  103. if hasSSEC {
  104. encryptionCount++
  105. }
  106. if hasSSEKMS {
  107. encryptionCount++
  108. }
  109. if hasSSES3 {
  110. encryptionCount++
  111. }
  112. // Only one encryption method should be specified
  113. if encryptionCount > 1 {
  114. return &CopyValidationError{
  115. Code: s3err.ErrInvalidRequest,
  116. Message: "Multiple encryption methods specified - only one is allowed",
  117. }
  118. }
  119. return nil
  120. }
  121. // validateSSECCopyHeaderCompleteness validates that all required SSE-C copy headers are present
  122. func validateSSECCopyHeaderCompleteness(headers http.Header) error {
  123. algorithm := headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerAlgorithm)
  124. key := headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKey)
  125. keyMD5 := headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKeyMD5)
  126. if algorithm == "" {
  127. return &CopyValidationError{
  128. Code: s3err.ErrInvalidRequest,
  129. Message: "SSE-C copy customer algorithm header is required",
  130. }
  131. }
  132. if key == "" {
  133. return &CopyValidationError{
  134. Code: s3err.ErrInvalidRequest,
  135. Message: "SSE-C copy customer key header is required",
  136. }
  137. }
  138. if keyMD5 == "" {
  139. return &CopyValidationError{
  140. Code: s3err.ErrInvalidRequest,
  141. Message: "SSE-C copy customer key MD5 header is required",
  142. }
  143. }
  144. // Validate algorithm
  145. if algorithm != "AES256" {
  146. return &CopyValidationError{
  147. Code: s3err.ErrInvalidRequest,
  148. Message: fmt.Sprintf("Unsupported SSE-C algorithm: %s", algorithm),
  149. }
  150. }
  151. return nil
  152. }
  153. // validateSSECHeaderCompleteness validates that all required SSE-C headers are present
  154. func validateSSECHeaderCompleteness(headers http.Header) error {
  155. algorithm := headers.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm)
  156. key := headers.Get(s3_constants.AmzServerSideEncryptionCustomerKey)
  157. keyMD5 := headers.Get(s3_constants.AmzServerSideEncryptionCustomerKeyMD5)
  158. if algorithm == "" {
  159. return &CopyValidationError{
  160. Code: s3err.ErrInvalidRequest,
  161. Message: "SSE-C customer algorithm header is required",
  162. }
  163. }
  164. if key == "" {
  165. return &CopyValidationError{
  166. Code: s3err.ErrInvalidRequest,
  167. Message: "SSE-C customer key header is required",
  168. }
  169. }
  170. if keyMD5 == "" {
  171. return &CopyValidationError{
  172. Code: s3err.ErrInvalidRequest,
  173. Message: "SSE-C customer key MD5 header is required",
  174. }
  175. }
  176. // Validate algorithm
  177. if algorithm != "AES256" {
  178. return &CopyValidationError{
  179. Code: s3err.ErrInvalidRequest,
  180. Message: fmt.Sprintf("Unsupported SSE-C algorithm: %s", algorithm),
  181. }
  182. }
  183. return nil
  184. }
  185. // Helper functions for header detection
  186. func hasSSECCopyHeaders(headers http.Header) bool {
  187. return headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerAlgorithm) != "" ||
  188. headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKey) != "" ||
  189. headers.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKeyMD5) != ""
  190. }
  191. func hasSSECHeaders(headers http.Header) bool {
  192. return headers.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm) != "" ||
  193. headers.Get(s3_constants.AmzServerSideEncryptionCustomerKey) != "" ||
  194. headers.Get(s3_constants.AmzServerSideEncryptionCustomerKeyMD5) != ""
  195. }
  196. // validateEncryptionContext validates the encryption context header format
  197. func validateEncryptionContext(contextHeader string) error {
  198. // This would validate base64 encoding and JSON format
  199. // Implementation would decode base64 and parse JSON
  200. // For now, just check it's not empty
  201. if contextHeader == "" {
  202. return fmt.Errorf("encryption context cannot be empty")
  203. }
  204. return nil
  205. }
  206. // ValidateCopySource validates the copy source path and permissions
  207. func ValidateCopySource(copySource string, srcBucket, srcObject string) error {
  208. if copySource == "" {
  209. return &CopyValidationError{
  210. Code: s3err.ErrInvalidCopySource,
  211. Message: "Copy source header is required",
  212. }
  213. }
  214. if srcBucket == "" {
  215. return &CopyValidationError{
  216. Code: s3err.ErrInvalidCopySource,
  217. Message: "Source bucket cannot be empty",
  218. }
  219. }
  220. if srcObject == "" {
  221. return &CopyValidationError{
  222. Code: s3err.ErrInvalidCopySource,
  223. Message: "Source object cannot be empty",
  224. }
  225. }
  226. return nil
  227. }
  228. // ValidateCopyDestination validates the copy destination
  229. func ValidateCopyDestination(dstBucket, dstObject string) error {
  230. if dstBucket == "" {
  231. return &CopyValidationError{
  232. Code: s3err.ErrInvalidRequest,
  233. Message: "Destination bucket cannot be empty",
  234. }
  235. }
  236. if dstObject == "" {
  237. return &CopyValidationError{
  238. Code: s3err.ErrInvalidRequest,
  239. Message: "Destination object cannot be empty",
  240. }
  241. }
  242. return nil
  243. }
  244. // MapCopyValidationError maps validation errors to appropriate S3 error codes
  245. func MapCopyValidationError(err error) s3err.ErrorCode {
  246. if validationErr, ok := err.(*CopyValidationError); ok {
  247. return validationErr.Code
  248. }
  249. return s3err.ErrInvalidRequest
  250. }