s3_sse_metadata_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package s3api
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  5. )
  6. // TestSSECIsEncrypted tests detection of SSE-C encryption from metadata
  7. func TestSSECIsEncrypted(t *testing.T) {
  8. testCases := []struct {
  9. name string
  10. metadata map[string][]byte
  11. expected bool
  12. }{
  13. {
  14. name: "Empty metadata",
  15. metadata: CreateTestMetadata(),
  16. expected: false,
  17. },
  18. {
  19. name: "Valid SSE-C metadata",
  20. metadata: CreateTestMetadataWithSSEC(GenerateTestSSECKey(1)),
  21. expected: true,
  22. },
  23. {
  24. name: "SSE-C algorithm only",
  25. metadata: map[string][]byte{
  26. s3_constants.AmzServerSideEncryptionCustomerAlgorithm: []byte("AES256"),
  27. },
  28. expected: true,
  29. },
  30. {
  31. name: "SSE-C key MD5 only",
  32. metadata: map[string][]byte{
  33. s3_constants.AmzServerSideEncryptionCustomerKeyMD5: []byte("somemd5"),
  34. },
  35. expected: true,
  36. },
  37. {
  38. name: "Other encryption type (SSE-KMS)",
  39. metadata: map[string][]byte{
  40. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  41. },
  42. expected: false,
  43. },
  44. }
  45. for _, tc := range testCases {
  46. t.Run(tc.name, func(t *testing.T) {
  47. result := IsSSECEncrypted(tc.metadata)
  48. if result != tc.expected {
  49. t.Errorf("Expected %v, got %v", tc.expected, result)
  50. }
  51. })
  52. }
  53. }
  54. // TestSSEKMSIsEncrypted tests detection of SSE-KMS encryption from metadata
  55. func TestSSEKMSIsEncrypted(t *testing.T) {
  56. testCases := []struct {
  57. name string
  58. metadata map[string][]byte
  59. expected bool
  60. }{
  61. {
  62. name: "Empty metadata",
  63. metadata: CreateTestMetadata(),
  64. expected: false,
  65. },
  66. {
  67. name: "Valid SSE-KMS metadata",
  68. metadata: map[string][]byte{
  69. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  70. s3_constants.AmzEncryptedDataKey: []byte("encrypted-key"),
  71. },
  72. expected: true,
  73. },
  74. {
  75. name: "SSE-KMS algorithm only",
  76. metadata: map[string][]byte{
  77. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  78. },
  79. expected: true,
  80. },
  81. {
  82. name: "SSE-KMS encrypted data key only",
  83. metadata: map[string][]byte{
  84. s3_constants.AmzEncryptedDataKey: []byte("encrypted-key"),
  85. },
  86. expected: false, // Only encrypted data key without algorithm header should not be considered SSE-KMS
  87. },
  88. {
  89. name: "Other encryption type (SSE-C)",
  90. metadata: map[string][]byte{
  91. s3_constants.AmzServerSideEncryptionCustomerAlgorithm: []byte("AES256"),
  92. },
  93. expected: false,
  94. },
  95. {
  96. name: "SSE-S3 (AES256)",
  97. metadata: map[string][]byte{
  98. s3_constants.AmzServerSideEncryption: []byte("AES256"),
  99. },
  100. expected: false,
  101. },
  102. }
  103. for _, tc := range testCases {
  104. t.Run(tc.name, func(t *testing.T) {
  105. result := IsSSEKMSEncrypted(tc.metadata)
  106. if result != tc.expected {
  107. t.Errorf("Expected %v, got %v", tc.expected, result)
  108. }
  109. })
  110. }
  111. }
  112. // TestSSETypeDiscrimination tests that SSE types don't interfere with each other
  113. func TestSSETypeDiscrimination(t *testing.T) {
  114. // Test SSE-C headers don't trigger SSE-KMS detection
  115. t.Run("SSE-C headers don't trigger SSE-KMS", func(t *testing.T) {
  116. req := CreateTestHTTPRequest("PUT", "/bucket/object", nil)
  117. keyPair := GenerateTestSSECKey(1)
  118. SetupTestSSECHeaders(req, keyPair)
  119. // Should detect SSE-C, not SSE-KMS
  120. if !IsSSECRequest(req) {
  121. t.Error("Should detect SSE-C request")
  122. }
  123. if IsSSEKMSRequest(req) {
  124. t.Error("Should not detect SSE-KMS request for SSE-C headers")
  125. }
  126. })
  127. // Test SSE-KMS headers don't trigger SSE-C detection
  128. t.Run("SSE-KMS headers don't trigger SSE-C", func(t *testing.T) {
  129. req := CreateTestHTTPRequest("PUT", "/bucket/object", nil)
  130. SetupTestSSEKMSHeaders(req, "test-key-id")
  131. // Should detect SSE-KMS, not SSE-C
  132. if IsSSECRequest(req) {
  133. t.Error("Should not detect SSE-C request for SSE-KMS headers")
  134. }
  135. if !IsSSEKMSRequest(req) {
  136. t.Error("Should detect SSE-KMS request")
  137. }
  138. })
  139. // Test metadata discrimination
  140. t.Run("Metadata type discrimination", func(t *testing.T) {
  141. ssecMetadata := CreateTestMetadataWithSSEC(GenerateTestSSECKey(1))
  142. // Should detect as SSE-C, not SSE-KMS
  143. if !IsSSECEncrypted(ssecMetadata) {
  144. t.Error("Should detect SSE-C encrypted metadata")
  145. }
  146. if IsSSEKMSEncrypted(ssecMetadata) {
  147. t.Error("Should not detect SSE-KMS for SSE-C metadata")
  148. }
  149. })
  150. }
  151. // TestSSECParseCorruptedMetadata tests handling of corrupted SSE-C metadata
  152. func TestSSECParseCorruptedMetadata(t *testing.T) {
  153. testCases := []struct {
  154. name string
  155. metadata map[string][]byte
  156. expectError bool
  157. errorMessage string
  158. }{
  159. {
  160. name: "Missing algorithm",
  161. metadata: map[string][]byte{
  162. s3_constants.AmzServerSideEncryptionCustomerKeyMD5: []byte("valid-md5"),
  163. },
  164. expectError: false, // Detection should still work with partial metadata
  165. },
  166. {
  167. name: "Invalid key MD5 format",
  168. metadata: map[string][]byte{
  169. s3_constants.AmzServerSideEncryptionCustomerAlgorithm: []byte("AES256"),
  170. s3_constants.AmzServerSideEncryptionCustomerKeyMD5: []byte("invalid-base64!"),
  171. },
  172. expectError: false, // Detection should work, validation happens later
  173. },
  174. {
  175. name: "Empty values",
  176. metadata: map[string][]byte{
  177. s3_constants.AmzServerSideEncryptionCustomerAlgorithm: []byte(""),
  178. s3_constants.AmzServerSideEncryptionCustomerKeyMD5: []byte(""),
  179. },
  180. expectError: false,
  181. },
  182. }
  183. for _, tc := range testCases {
  184. t.Run(tc.name, func(t *testing.T) {
  185. // Test that detection doesn't panic on corrupted metadata
  186. result := IsSSECEncrypted(tc.metadata)
  187. // The detection should be robust and not crash
  188. t.Logf("Detection result for %s: %v", tc.name, result)
  189. })
  190. }
  191. }
  192. // TestSSEKMSParseCorruptedMetadata tests handling of corrupted SSE-KMS metadata
  193. func TestSSEKMSParseCorruptedMetadata(t *testing.T) {
  194. testCases := []struct {
  195. name string
  196. metadata map[string][]byte
  197. }{
  198. {
  199. name: "Invalid encrypted data key",
  200. metadata: map[string][]byte{
  201. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  202. s3_constants.AmzEncryptedDataKey: []byte("invalid-base64!"),
  203. },
  204. },
  205. {
  206. name: "Invalid encryption context",
  207. metadata: map[string][]byte{
  208. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  209. s3_constants.AmzEncryptionContextMeta: []byte("invalid-json"),
  210. },
  211. },
  212. {
  213. name: "Empty values",
  214. metadata: map[string][]byte{
  215. s3_constants.AmzServerSideEncryption: []byte(""),
  216. s3_constants.AmzEncryptedDataKey: []byte(""),
  217. },
  218. },
  219. }
  220. for _, tc := range testCases {
  221. t.Run(tc.name, func(t *testing.T) {
  222. // Test that detection doesn't panic on corrupted metadata
  223. result := IsSSEKMSEncrypted(tc.metadata)
  224. t.Logf("Detection result for %s: %v", tc.name, result)
  225. })
  226. }
  227. }
  228. // TestSSEMetadataDeserialization tests SSE-KMS metadata deserialization with various inputs
  229. func TestSSEMetadataDeserialization(t *testing.T) {
  230. testCases := []struct {
  231. name string
  232. data []byte
  233. expectError bool
  234. }{
  235. {
  236. name: "Empty data",
  237. data: []byte{},
  238. expectError: true,
  239. },
  240. {
  241. name: "Invalid JSON",
  242. data: []byte("invalid-json"),
  243. expectError: true,
  244. },
  245. {
  246. name: "Valid JSON but wrong structure",
  247. data: []byte(`{"wrong": "structure"}`),
  248. expectError: false, // Our deserialization might be lenient
  249. },
  250. {
  251. name: "Null data",
  252. data: nil,
  253. expectError: true,
  254. },
  255. }
  256. for _, tc := range testCases {
  257. t.Run(tc.name, func(t *testing.T) {
  258. _, err := DeserializeSSEKMSMetadata(tc.data)
  259. if tc.expectError && err == nil {
  260. t.Error("Expected error but got none")
  261. }
  262. if !tc.expectError && err != nil {
  263. t.Errorf("Expected no error but got: %v", err)
  264. }
  265. })
  266. }
  267. }
  268. // TestGeneralSSEDetection tests the general SSE detection that works across types
  269. func TestGeneralSSEDetection(t *testing.T) {
  270. testCases := []struct {
  271. name string
  272. metadata map[string][]byte
  273. expected bool
  274. }{
  275. {
  276. name: "No encryption",
  277. metadata: CreateTestMetadata(),
  278. expected: false,
  279. },
  280. {
  281. name: "SSE-C encrypted",
  282. metadata: CreateTestMetadataWithSSEC(GenerateTestSSECKey(1)),
  283. expected: true,
  284. },
  285. {
  286. name: "SSE-KMS encrypted",
  287. metadata: map[string][]byte{
  288. s3_constants.AmzServerSideEncryption: []byte("aws:kms"),
  289. },
  290. expected: true,
  291. },
  292. {
  293. name: "SSE-S3 encrypted",
  294. metadata: map[string][]byte{
  295. s3_constants.AmzServerSideEncryption: []byte("AES256"),
  296. },
  297. expected: true,
  298. },
  299. }
  300. for _, tc := range testCases {
  301. t.Run(tc.name, func(t *testing.T) {
  302. result := IsAnySSEEncrypted(tc.metadata)
  303. if result != tc.expected {
  304. t.Errorf("Expected %v, got %v", tc.expected, result)
  305. }
  306. })
  307. }
  308. }