s3_sse_kms_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "strings"
  7. "testing"
  8. "github.com/seaweedfs/seaweedfs/weed/kms"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  10. )
  11. func TestSSEKMSEncryptionDecryption(t *testing.T) {
  12. kmsKey := SetupTestKMS(t)
  13. defer kmsKey.Cleanup()
  14. // Test data
  15. testData := "Hello, SSE-KMS world! This is a test of envelope encryption."
  16. testReader := strings.NewReader(testData)
  17. // Create encryption context
  18. encryptionContext := BuildEncryptionContext("test-bucket", "test-object", false)
  19. // Encrypt the data
  20. encryptedReader, sseKey, err := CreateSSEKMSEncryptedReader(testReader, kmsKey.KeyID, encryptionContext)
  21. if err != nil {
  22. t.Fatalf("Failed to create encrypted reader: %v", err)
  23. }
  24. // Verify SSE key metadata
  25. if sseKey.KeyID != kmsKey.KeyID {
  26. t.Errorf("Expected key ID %s, got %s", kmsKey.KeyID, sseKey.KeyID)
  27. }
  28. if len(sseKey.EncryptedDataKey) == 0 {
  29. t.Error("Encrypted data key should not be empty")
  30. }
  31. if sseKey.EncryptionContext == nil {
  32. t.Error("Encryption context should not be nil")
  33. }
  34. // Read the encrypted data
  35. encryptedData, err := io.ReadAll(encryptedReader)
  36. if err != nil {
  37. t.Fatalf("Failed to read encrypted data: %v", err)
  38. }
  39. // Verify the encrypted data is different from original
  40. if string(encryptedData) == testData {
  41. t.Error("Encrypted data should be different from original data")
  42. }
  43. // The encrypted data should be same size as original (IV is stored in metadata, not in stream)
  44. if len(encryptedData) != len(testData) {
  45. t.Errorf("Encrypted data should be same size as original: expected %d, got %d", len(testData), len(encryptedData))
  46. }
  47. // Decrypt the data
  48. decryptedReader, err := CreateSSEKMSDecryptedReader(bytes.NewReader(encryptedData), sseKey)
  49. if err != nil {
  50. t.Fatalf("Failed to create decrypted reader: %v", err)
  51. }
  52. // Read the decrypted data
  53. decryptedData, err := io.ReadAll(decryptedReader)
  54. if err != nil {
  55. t.Fatalf("Failed to read decrypted data: %v", err)
  56. }
  57. // Verify the decrypted data matches the original
  58. if string(decryptedData) != testData {
  59. t.Errorf("Decrypted data does not match original.\nExpected: %s\nGot: %s", testData, string(decryptedData))
  60. }
  61. }
  62. func TestSSEKMSKeyValidation(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. keyID string
  66. wantValid bool
  67. }{
  68. {
  69. name: "Valid UUID key ID",
  70. keyID: "12345678-1234-1234-1234-123456789012",
  71. wantValid: true,
  72. },
  73. {
  74. name: "Valid alias",
  75. keyID: "alias/my-test-key",
  76. wantValid: true,
  77. },
  78. {
  79. name: "Valid ARN",
  80. keyID: "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012",
  81. wantValid: true,
  82. },
  83. {
  84. name: "Valid alias ARN",
  85. keyID: "arn:aws:kms:us-east-1:123456789012:alias/my-test-key",
  86. wantValid: true,
  87. },
  88. {
  89. name: "Valid test key format",
  90. keyID: "invalid-key-format",
  91. wantValid: true, // Now valid - following Minio's permissive approach
  92. },
  93. {
  94. name: "Valid short key",
  95. keyID: "12345678-1234",
  96. wantValid: true, // Now valid - following Minio's permissive approach
  97. },
  98. {
  99. name: "Invalid - leading space",
  100. keyID: " leading-space",
  101. wantValid: false,
  102. },
  103. {
  104. name: "Invalid - trailing space",
  105. keyID: "trailing-space ",
  106. wantValid: false,
  107. },
  108. {
  109. name: "Invalid - empty",
  110. keyID: "",
  111. wantValid: false,
  112. },
  113. {
  114. name: "Invalid - internal spaces",
  115. keyID: "invalid key id",
  116. wantValid: false,
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. valid := isValidKMSKeyID(tt.keyID)
  122. if valid != tt.wantValid {
  123. t.Errorf("isValidKMSKeyID(%s) = %v, want %v", tt.keyID, valid, tt.wantValid)
  124. }
  125. })
  126. }
  127. }
  128. func TestSSEKMSMetadataSerialization(t *testing.T) {
  129. // Create test SSE key
  130. sseKey := &SSEKMSKey{
  131. KeyID: "test-key-id",
  132. EncryptedDataKey: []byte("encrypted-data-key"),
  133. EncryptionContext: map[string]string{
  134. "aws:s3:arn": "arn:aws:s3:::test-bucket/test-object",
  135. },
  136. BucketKeyEnabled: true,
  137. }
  138. // Serialize metadata
  139. serialized, err := SerializeSSEKMSMetadata(sseKey)
  140. if err != nil {
  141. t.Fatalf("Failed to serialize SSE-KMS metadata: %v", err)
  142. }
  143. // Verify it's valid JSON
  144. var jsonData map[string]interface{}
  145. if err := json.Unmarshal(serialized, &jsonData); err != nil {
  146. t.Fatalf("Serialized data is not valid JSON: %v", err)
  147. }
  148. // Deserialize metadata
  149. deserializedKey, err := DeserializeSSEKMSMetadata(serialized)
  150. if err != nil {
  151. t.Fatalf("Failed to deserialize SSE-KMS metadata: %v", err)
  152. }
  153. // Verify the deserialized data matches original
  154. if deserializedKey.KeyID != sseKey.KeyID {
  155. t.Errorf("KeyID mismatch: expected %s, got %s", sseKey.KeyID, deserializedKey.KeyID)
  156. }
  157. if !bytes.Equal(deserializedKey.EncryptedDataKey, sseKey.EncryptedDataKey) {
  158. t.Error("EncryptedDataKey mismatch")
  159. }
  160. if len(deserializedKey.EncryptionContext) != len(sseKey.EncryptionContext) {
  161. t.Error("EncryptionContext length mismatch")
  162. }
  163. for k, v := range sseKey.EncryptionContext {
  164. if deserializedKey.EncryptionContext[k] != v {
  165. t.Errorf("EncryptionContext mismatch for key %s: expected %s, got %s", k, v, deserializedKey.EncryptionContext[k])
  166. }
  167. }
  168. if deserializedKey.BucketKeyEnabled != sseKey.BucketKeyEnabled {
  169. t.Errorf("BucketKeyEnabled mismatch: expected %v, got %v", sseKey.BucketKeyEnabled, deserializedKey.BucketKeyEnabled)
  170. }
  171. }
  172. func TestBuildEncryptionContext(t *testing.T) {
  173. tests := []struct {
  174. name string
  175. bucket string
  176. object string
  177. useBucketKey bool
  178. expectedARN string
  179. }{
  180. {
  181. name: "Object-level encryption",
  182. bucket: "test-bucket",
  183. object: "test-object",
  184. useBucketKey: false,
  185. expectedARN: "arn:aws:s3:::test-bucket/test-object",
  186. },
  187. {
  188. name: "Bucket-level encryption",
  189. bucket: "test-bucket",
  190. object: "test-object",
  191. useBucketKey: true,
  192. expectedARN: "arn:aws:s3:::test-bucket",
  193. },
  194. {
  195. name: "Nested object path",
  196. bucket: "my-bucket",
  197. object: "folder/subfolder/file.txt",
  198. useBucketKey: false,
  199. expectedARN: "arn:aws:s3:::my-bucket/folder/subfolder/file.txt",
  200. },
  201. }
  202. for _, tt := range tests {
  203. t.Run(tt.name, func(t *testing.T) {
  204. context := BuildEncryptionContext(tt.bucket, tt.object, tt.useBucketKey)
  205. if context == nil {
  206. t.Fatal("Encryption context should not be nil")
  207. }
  208. arn, exists := context[kms.EncryptionContextS3ARN]
  209. if !exists {
  210. t.Error("Encryption context should contain S3 ARN")
  211. }
  212. if arn != tt.expectedARN {
  213. t.Errorf("Expected ARN %s, got %s", tt.expectedARN, arn)
  214. }
  215. })
  216. }
  217. }
  218. func TestKMSErrorMapping(t *testing.T) {
  219. tests := []struct {
  220. name string
  221. kmsError *kms.KMSError
  222. expectedErr string
  223. }{
  224. {
  225. name: "Key not found",
  226. kmsError: &kms.KMSError{
  227. Code: kms.ErrCodeNotFoundException,
  228. Message: "Key not found",
  229. },
  230. expectedErr: "KMSKeyNotFoundException",
  231. },
  232. {
  233. name: "Access denied",
  234. kmsError: &kms.KMSError{
  235. Code: kms.ErrCodeAccessDenied,
  236. Message: "Access denied",
  237. },
  238. expectedErr: "KMSAccessDeniedException",
  239. },
  240. {
  241. name: "Key unavailable",
  242. kmsError: &kms.KMSError{
  243. Code: kms.ErrCodeKeyUnavailable,
  244. Message: "Key is disabled",
  245. },
  246. expectedErr: "KMSKeyDisabledException",
  247. },
  248. }
  249. for _, tt := range tests {
  250. t.Run(tt.name, func(t *testing.T) {
  251. errorCode := MapKMSErrorToS3Error(tt.kmsError)
  252. // Get the actual error description
  253. apiError := s3err.GetAPIError(errorCode)
  254. if apiError.Code != tt.expectedErr {
  255. t.Errorf("Expected error code %s, got %s", tt.expectedErr, apiError.Code)
  256. }
  257. })
  258. }
  259. }
  260. // TestLargeDataEncryption tests encryption/decryption of larger data streams
  261. func TestSSEKMSLargeDataEncryption(t *testing.T) {
  262. kmsKey := SetupTestKMS(t)
  263. defer kmsKey.Cleanup()
  264. // Create a larger test dataset (1MB)
  265. testData := strings.Repeat("This is a test of SSE-KMS with larger data streams. ", 20000)
  266. testReader := strings.NewReader(testData)
  267. // Create encryption context
  268. encryptionContext := BuildEncryptionContext("large-bucket", "large-object", false)
  269. // Encrypt the data
  270. encryptedReader, sseKey, err := CreateSSEKMSEncryptedReader(testReader, kmsKey.KeyID, encryptionContext)
  271. if err != nil {
  272. t.Fatalf("Failed to create encrypted reader: %v", err)
  273. }
  274. // Read the encrypted data
  275. encryptedData, err := io.ReadAll(encryptedReader)
  276. if err != nil {
  277. t.Fatalf("Failed to read encrypted data: %v", err)
  278. }
  279. // Decrypt the data
  280. decryptedReader, err := CreateSSEKMSDecryptedReader(bytes.NewReader(encryptedData), sseKey)
  281. if err != nil {
  282. t.Fatalf("Failed to create decrypted reader: %v", err)
  283. }
  284. // Read the decrypted data
  285. decryptedData, err := io.ReadAll(decryptedReader)
  286. if err != nil {
  287. t.Fatalf("Failed to read decrypted data: %v", err)
  288. }
  289. // Verify the decrypted data matches the original
  290. if string(decryptedData) != testData {
  291. t.Errorf("Decrypted data length: %d, original data length: %d", len(decryptedData), len(testData))
  292. t.Error("Decrypted large data does not match original")
  293. }
  294. t.Logf("Successfully encrypted/decrypted %d bytes of data", len(testData))
  295. }
  296. // TestValidateSSEKMSKey tests the ValidateSSEKMSKey function, which correctly handles empty key IDs
  297. func TestValidateSSEKMSKey(t *testing.T) {
  298. tests := []struct {
  299. name string
  300. sseKey *SSEKMSKey
  301. wantErr bool
  302. }{
  303. {
  304. name: "nil SSE-KMS key",
  305. sseKey: nil,
  306. wantErr: true,
  307. },
  308. {
  309. name: "empty key ID (valid - represents default KMS key)",
  310. sseKey: &SSEKMSKey{
  311. KeyID: "",
  312. EncryptionContext: map[string]string{"test": "value"},
  313. BucketKeyEnabled: false,
  314. },
  315. wantErr: false,
  316. },
  317. {
  318. name: "valid UUID key ID",
  319. sseKey: &SSEKMSKey{
  320. KeyID: "12345678-1234-1234-1234-123456789012",
  321. EncryptionContext: map[string]string{"test": "value"},
  322. BucketKeyEnabled: true,
  323. },
  324. wantErr: false,
  325. },
  326. {
  327. name: "valid alias",
  328. sseKey: &SSEKMSKey{
  329. KeyID: "alias/my-test-key",
  330. EncryptionContext: map[string]string{},
  331. BucketKeyEnabled: false,
  332. },
  333. wantErr: false,
  334. },
  335. {
  336. name: "valid flexible key ID format",
  337. sseKey: &SSEKMSKey{
  338. KeyID: "invalid-format",
  339. EncryptionContext: map[string]string{},
  340. BucketKeyEnabled: false,
  341. },
  342. wantErr: false, // Now valid - following Minio's permissive approach
  343. },
  344. }
  345. for _, tt := range tests {
  346. t.Run(tt.name, func(t *testing.T) {
  347. err := ValidateSSEKMSKey(tt.sseKey)
  348. if (err != nil) != tt.wantErr {
  349. t.Errorf("ValidateSSEKMSKey() error = %v, wantErr %v", err, tt.wantErr)
  350. }
  351. })
  352. }
  353. }