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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
package s3api
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
)
// TestPutObjectWithSSEC tests PUT object with SSE-C through HTTP handler
func TestPutObjectWithSSEC(t *testing.T) {
keyPair := GenerateTestSSECKey(1)
testData := "Hello, SSE-C PUT object!"
// Create HTTP request
req := CreateTestHTTPRequest("PUT", "/test-bucket/test-object", []byte(testData))
SetupTestSSECHeaders(req, keyPair)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Create response recorder
w := CreateTestHTTPResponse()
// Test header validation
err := ValidateSSECHeaders(req)
if err != nil {
t.Fatalf("Header validation failed: %v", err)
}
// Parse SSE-C headers
customerKey, err := ParseSSECHeaders(req)
if err != nil {
t.Fatalf("Failed to parse SSE-C headers: %v", err)
}
if customerKey == nil {
t.Fatal("Expected customer key, got nil")
}
// Verify parsed key matches input
if !bytes.Equal(customerKey.Key, keyPair.Key) {
t.Error("Parsed key doesn't match input key")
}
if customerKey.KeyMD5 != keyPair.KeyMD5 {
t.Errorf("Parsed key MD5 doesn't match: expected %s, got %s", keyPair.KeyMD5, customerKey.KeyMD5)
}
// Simulate setting response headers
w.Header().Set(s3_constants.AmzServerSideEncryptionCustomerAlgorithm, "AES256")
w.Header().Set(s3_constants.AmzServerSideEncryptionCustomerKeyMD5, keyPair.KeyMD5)
// Verify response headers
AssertSSECHeaders(t, w, keyPair)
}
// TestGetObjectWithSSEC tests GET object with SSE-C through HTTP handler
func TestGetObjectWithSSEC(t *testing.T) {
keyPair := GenerateTestSSECKey(1)
// Create HTTP request for GET
req := CreateTestHTTPRequest("GET", "/test-bucket/test-object", nil)
SetupTestSSECHeaders(req, keyPair)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Create response recorder
w := CreateTestHTTPResponse()
// Test that SSE-C is detected for GET requests
if !IsSSECRequest(req) {
t.Error("Should detect SSE-C request for GET with SSE-C headers")
}
// Validate headers
err := ValidateSSECHeaders(req)
if err != nil {
t.Fatalf("Header validation failed: %v", err)
}
// Simulate response with SSE-C headers
w.Header().Set(s3_constants.AmzServerSideEncryptionCustomerAlgorithm, "AES256")
w.Header().Set(s3_constants.AmzServerSideEncryptionCustomerKeyMD5, keyPair.KeyMD5)
w.WriteHeader(http.StatusOK)
// Verify response
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
AssertSSECHeaders(t, w, keyPair)
}
// TestPutObjectWithSSEKMS tests PUT object with SSE-KMS through HTTP handler
func TestPutObjectWithSSEKMS(t *testing.T) {
kmsKey := SetupTestKMS(t)
defer kmsKey.Cleanup()
testData := "Hello, SSE-KMS PUT object!"
// Create HTTP request
req := CreateTestHTTPRequest("PUT", "/test-bucket/test-object", []byte(testData))
SetupTestSSEKMSHeaders(req, kmsKey.KeyID)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Create response recorder
w := CreateTestHTTPResponse()
// Test that SSE-KMS is detected
if !IsSSEKMSRequest(req) {
t.Error("Should detect SSE-KMS request")
}
// Parse SSE-KMS headers
sseKmsKey, err := ParseSSEKMSHeaders(req)
if err != nil {
t.Fatalf("Failed to parse SSE-KMS headers: %v", err)
}
if sseKmsKey == nil {
t.Fatal("Expected SSE-KMS key, got nil")
}
if sseKmsKey.KeyID != kmsKey.KeyID {
t.Errorf("Parsed key ID doesn't match: expected %s, got %s", kmsKey.KeyID, sseKmsKey.KeyID)
}
// Simulate setting response headers
w.Header().Set(s3_constants.AmzServerSideEncryption, "aws:kms")
w.Header().Set(s3_constants.AmzServerSideEncryptionAwsKmsKeyId, kmsKey.KeyID)
// Verify response headers
AssertSSEKMSHeaders(t, w, kmsKey.KeyID)
}
// TestGetObjectWithSSEKMS tests GET object with SSE-KMS through HTTP handler
func TestGetObjectWithSSEKMS(t *testing.T) {
kmsKey := SetupTestKMS(t)
defer kmsKey.Cleanup()
// Create HTTP request for GET (no SSE headers needed for GET)
req := CreateTestHTTPRequest("GET", "/test-bucket/test-object", nil)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Create response recorder
w := CreateTestHTTPResponse()
// Simulate response with SSE-KMS headers (would come from stored metadata)
w.Header().Set(s3_constants.AmzServerSideEncryption, "aws:kms")
w.Header().Set(s3_constants.AmzServerSideEncryptionAwsKmsKeyId, kmsKey.KeyID)
w.WriteHeader(http.StatusOK)
// Verify response
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
AssertSSEKMSHeaders(t, w, kmsKey.KeyID)
}
// TestSSECRangeRequestSupport tests that range requests are now supported for SSE-C
func TestSSECRangeRequestSupport(t *testing.T) {
keyPair := GenerateTestSSECKey(1)
// Create HTTP request with Range header
req := CreateTestHTTPRequest("GET", "/test-bucket/test-object", nil)
req.Header.Set("Range", "bytes=0-100")
SetupTestSSECHeaders(req, keyPair)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Create a mock proxy response with SSE-C headers
proxyResponse := httptest.NewRecorder()
proxyResponse.Header().Set(s3_constants.AmzServerSideEncryptionCustomerAlgorithm, "AES256")
proxyResponse.Header().Set(s3_constants.AmzServerSideEncryptionCustomerKeyMD5, keyPair.KeyMD5)
proxyResponse.Header().Set("Content-Length", "1000")
// Test the detection logic - these should all still work
// Should detect as SSE-C request
if !IsSSECRequest(req) {
t.Error("Should detect SSE-C request")
}
// Should detect range request
if req.Header.Get("Range") == "" {
t.Error("Range header should be present")
}
// The combination should now be allowed and handled by the filer layer
// Range requests with SSE-C are now supported since IV is stored in metadata
}
// TestSSEHeaderConflicts tests conflicting SSE headers
func TestSSEHeaderConflicts(t *testing.T) {
testCases := []struct {
name string
setupFn func(*http.Request)
valid bool
}{
{
name: "SSE-C and SSE-KMS conflict",
setupFn: func(req *http.Request) {
keyPair := GenerateTestSSECKey(1)
SetupTestSSECHeaders(req, keyPair)
SetupTestSSEKMSHeaders(req, "test-key-id")
},
valid: false,
},
{
name: "Valid SSE-C only",
setupFn: func(req *http.Request) {
keyPair := GenerateTestSSECKey(1)
SetupTestSSECHeaders(req, keyPair)
},
valid: true,
},
{
name: "Valid SSE-KMS only",
setupFn: func(req *http.Request) {
SetupTestSSEKMSHeaders(req, "test-key-id")
},
valid: true,
},
{
name: "No SSE headers",
setupFn: func(req *http.Request) {
// No SSE headers
},
valid: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := CreateTestHTTPRequest("PUT", "/test-bucket/test-object", []byte("test"))
tc.setupFn(req)
ssecDetected := IsSSECRequest(req)
sseKmsDetected := IsSSEKMSRequest(req)
// Both shouldn't be detected simultaneously
if ssecDetected && sseKmsDetected {
t.Error("Both SSE-C and SSE-KMS should not be detected simultaneously")
}
// Test validation if SSE-C is detected
if ssecDetected {
err := ValidateSSECHeaders(req)
if tc.valid && err != nil {
t.Errorf("Expected valid SSE-C headers, got error: %v", err)
}
if !tc.valid && err == nil && tc.name == "SSE-C and SSE-KMS conflict" {
// This specific test case should probably be handled at a higher level
t.Log("Conflict detection should be handled by higher-level validation")
}
}
})
}
}
// TestSSECopySourceHeaders tests copy operations with SSE headers
func TestSSECopySourceHeaders(t *testing.T) {
sourceKey := GenerateTestSSECKey(1)
destKey := GenerateTestSSECKey(2)
// Create copy request with both source and destination SSE-C headers
req := CreateTestHTTPRequest("PUT", "/dest-bucket/dest-object", nil)
// Set copy source headers
SetupTestSSECCopyHeaders(req, sourceKey)
// Set destination headers
SetupTestSSECHeaders(req, destKey)
// Set copy source
req.Header.Set("X-Amz-Copy-Source", "/source-bucket/source-object")
SetupTestMuxVars(req, map[string]string{
"bucket": "dest-bucket",
"object": "dest-object",
})
// Parse copy source headers
copySourceKey, err := ParseSSECCopySourceHeaders(req)
if err != nil {
t.Fatalf("Failed to parse copy source headers: %v", err)
}
if copySourceKey == nil {
t.Fatal("Expected copy source key, got nil")
}
if !bytes.Equal(copySourceKey.Key, sourceKey.Key) {
t.Error("Copy source key doesn't match")
}
// Parse destination headers
destCustomerKey, err := ParseSSECHeaders(req)
if err != nil {
t.Fatalf("Failed to parse destination headers: %v", err)
}
if destCustomerKey == nil {
t.Fatal("Expected destination key, got nil")
}
if !bytes.Equal(destCustomerKey.Key, destKey.Key) {
t.Error("Destination key doesn't match")
}
}
// TestSSERequestValidation tests comprehensive request validation
func TestSSERequestValidation(t *testing.T) {
testCases := []struct {
name string
method string
setupFn func(*http.Request)
expectError bool
errorType string
}{
{
name: "Valid PUT with SSE-C",
method: "PUT",
setupFn: func(req *http.Request) {
keyPair := GenerateTestSSECKey(1)
SetupTestSSECHeaders(req, keyPair)
},
expectError: false,
},
{
name: "Valid GET with SSE-C",
method: "GET",
setupFn: func(req *http.Request) {
keyPair := GenerateTestSSECKey(1)
SetupTestSSECHeaders(req, keyPair)
},
expectError: false,
},
{
name: "Invalid SSE-C key format",
method: "PUT",
setupFn: func(req *http.Request) {
req.Header.Set(s3_constants.AmzServerSideEncryptionCustomerAlgorithm, "AES256")
req.Header.Set(s3_constants.AmzServerSideEncryptionCustomerKey, "invalid-key")
req.Header.Set(s3_constants.AmzServerSideEncryptionCustomerKeyMD5, "invalid-md5")
},
expectError: true,
errorType: "InvalidRequest",
},
{
name: "Missing SSE-C key MD5",
method: "PUT",
setupFn: func(req *http.Request) {
keyPair := GenerateTestSSECKey(1)
req.Header.Set(s3_constants.AmzServerSideEncryptionCustomerAlgorithm, "AES256")
req.Header.Set(s3_constants.AmzServerSideEncryptionCustomerKey, keyPair.KeyB64)
// Missing MD5
},
expectError: true,
errorType: "InvalidRequest",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := CreateTestHTTPRequest(tc.method, "/test-bucket/test-object", []byte("test data"))
tc.setupFn(req)
SetupTestMuxVars(req, map[string]string{
"bucket": "test-bucket",
"object": "test-object",
})
// Test header validation
if IsSSECRequest(req) {
err := ValidateSSECHeaders(req)
if tc.expectError && err == nil {
t.Errorf("Expected error for %s, but got none", tc.name)
}
if !tc.expectError && err != nil {
t.Errorf("Expected no error for %s, but got: %v", tc.name, err)
}
}
})
}
}
|