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