-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathS3EncryptionClientRangedGetCompatibilityTest.java
More file actions
628 lines (536 loc) · 27.5 KB
/
S3EncryptionClientRangedGetCompatibilityTest.java
File metadata and controls
628 lines (536 loc) · 27.5 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3;
import com.amazonaws.services.kms.AWSKMS;
import com.amazonaws.services.kms.AWSKMSClientBuilder;
import com.amazonaws.services.s3.AmazonS3Encryption;
import com.amazonaws.services.s3.AmazonS3EncryptionClient;
import com.amazonaws.services.s3.AmazonS3EncryptionClientBuilder;
import com.amazonaws.services.s3.model.CryptoConfiguration;
import com.amazonaws.services.s3.model.EncryptionMaterials;
import com.amazonaws.services.s3.model.EncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_REGION;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject;
/**
* This class is an integration test for Unauthenticated Ranged Get for AES/CBC and AES/GCM modes
*/
public class S3EncryptionClientRangedGetCompatibilityTest {
private static SecretKey AES_KEY;
private static KeyPair RSA_KEY_PAIR;
@BeforeAll
public static void setUp() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
AES_KEY = keyGen.generateKey();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
RSA_KEY_PAIR = keyPairGen.generateKeyPair();
}
static Object[] keyMaterialProvider() {
return new Object[] {
AES_KEY,
RSA_KEY_PAIR,
KMS_KEY_ID,
};
}
private static S3EncryptionClient.Builder addKeyMaterialToV3Client(S3EncryptionClient.Builder builder, Object keyMaterial) {
if (keyMaterial instanceof SecretKey) {
builder.aesKey((SecretKey) keyMaterial);
} else if (keyMaterial instanceof KeyPair) {
builder.rsaKeyPair((KeyPair) keyMaterial);
} else if (keyMaterial instanceof String) {
builder.kmsKeyId((String) keyMaterial);
}
return builder;
}
private static S3AsyncEncryptionClient.Builder addKeyMaterialToV3Client(S3AsyncEncryptionClient.Builder builder, Object keyMaterial) {
if (keyMaterial instanceof SecretKey) {
builder.aesKey((SecretKey) keyMaterial);
} else if (keyMaterial instanceof KeyPair) {
builder.rsaKeyPair((KeyPair) keyMaterial);
} else if (keyMaterial instanceof String) {
builder.kmsKeyId((String) keyMaterial);
}
return builder;
}
private static AmazonS3EncryptionClientBuilder addKeyMaterialToV1Client(AmazonS3EncryptionClientBuilder builder, Object keyMaterial) {
EncryptionMaterials materials;
if (keyMaterial instanceof SecretKey) {
materials = new EncryptionMaterials((SecretKey) keyMaterial);
builder.withEncryptionMaterials(new StaticEncryptionMaterialsProvider(materials));
} else if (keyMaterial instanceof KeyPair) {
materials = new EncryptionMaterials((KeyPair) keyMaterial);
builder.withEncryptionMaterials(new StaticEncryptionMaterialsProvider(materials));
}
if (keyMaterial instanceof String) {
AWSKMS kmsClient = AWSKMSClientBuilder.standard()
.withRegion(KMS_REGION.toString())
.build();
builder.withKmsClient(kmsClient);
EncryptionMaterialsProvider materialsProvider = new KMSEncryptionMaterialsProvider((String) keyMaterial);
builder.withEncryptionMaterials(materialsProvider);
}
return builder;
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AsyncAesGcmV3toV3RangedGet(Object keyMaterial) {
final String objectKey = appendTestSuffix("async-aes-gcm-v3-to-v3-ranged-get");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// Async Client
S3AsyncEncryptionClient.Builder clientBuilder = S3AsyncEncryptionClient.builder()
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3AsyncClient asyncClient = clientBuilder.build();
asyncClient.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), AsyncRequestBody.fromString(input)).join();
// Valid Range
ResponseBytes<GetObjectResponse> objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=10-20")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
String output = objectResponse.asUtf8String();
assertEquals("klmnopqrst0", output);
// Valid start index within input and end index out of range, returns object from start index to End of Stream
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=190-300")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals("KLMNOPQRST", output);
// Valid start index within input and without specifying end index of range, returns object from start index to End of Stream
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=40-")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input.substring(40), output);
// Invalid range with only specifying the end index, returns entire object
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=-40")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range start index range greater than ending index, returns entire object
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=100-50")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range format, returns entire object
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("10-20")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range starting index and ending index greater than object length but within Cipher Block size, returns empty object
objectResponse = asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=216-217")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals("", output);
// Cleanup
deleteObject(BUCKET, objectKey, asyncClient);
asyncClient.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AsyncFailsOnRangeWhenLegacyModeDisabled(Object keyMaterial) {
final String objectKey = appendTestSuffix("fails-when-on-range-when-legacy-disabled");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// V3 Client
S3AsyncEncryptionClient.Builder clientBuilder = S3AsyncEncryptionClient.builder();
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3AsyncClient asyncClient = clientBuilder.build();
asyncClient.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), AsyncRequestBody.fromString(input)).join();
assertThrows(S3EncryptionClientException.class, () -> asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=10-20")
.key(objectKey), AsyncResponseTransformer.toBytes()).join());
// Cleanup
deleteObject(BUCKET, objectKey, asyncClient);
asyncClient.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AsyncAesCbcV1toV3RangedGet(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-cbc-v1-to-v3-ranged-get-async");
// V1 Client
CryptoConfiguration v1CryptoConfig =
new CryptoConfiguration();
AmazonS3EncryptionClientBuilder v1ClientBuilder = AmazonS3EncryptionClient.encryptionBuilder()
.withCryptoConfiguration(v1CryptoConfig);
addKeyMaterialToV1Client(v1ClientBuilder, keyMaterial);
AmazonS3Encryption v1Client = v1ClientBuilder.build();
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
v1Client.putObject(BUCKET, objectKey, input);
// V3 Client
S3AsyncEncryptionClient.Builder clientBuilder = S3AsyncEncryptionClient.builder()
.enableLegacyWrappingAlgorithms(true)
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3AsyncClient v3Client = clientBuilder.build();
// Valid Range
ResponseBytes<GetObjectResponse> objectResponse;
objectResponse = v3Client.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=10-20")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
String output;
output = objectResponse.asUtf8String();
assertEquals("klmnopqrst0", output);
// Valid start index within input and end index out of range, returns object from start index to End of Stream
// This causes a spurious NPE to be logged when debug logging is enabled.
objectResponse = v3Client.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=190-300")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals("KLMNOPQRST", output);
// Invalid range start index range greater than ending index, returns entire object
objectResponse = v3Client.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=100-50")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range format, returns entire object
objectResponse = v3Client.getObject(builder -> builder
.bucket(BUCKET)
.range("10-20")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range starting index and ending index greater than object length but within Cipher Block size, returns empty object
// This causes a spurious NPE to be logged when debug logging is enabled.
objectResponse = v3Client.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=216-217")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
output = objectResponse.asUtf8String();
assertEquals("", output);
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void failsOnRangeWhenLegacyModeDisabled(Object keyMaterial) {
final String objectKey = appendTestSuffix("fails-when-on-range-when-legacy-disabled");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// V3 Client
S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder();
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3Client v3Client = clientBuilder.build();
v3Client.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), RequestBody.fromString(input));
// Asserts
assertThrows(S3EncryptionClientException.class, () -> v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.key(objectKey)
.range("bytes=10-20")));
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AesGcmV3toV3RangedGet(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-gcm-v3-to-v3-ranged-get");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// V3 Client
S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder()
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3Client v3Client = clientBuilder.build();
v3Client.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), RequestBody.fromString(input));
// Valid Range
ResponseBytes<GetObjectResponse> objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=10-20")
.key(objectKey));
String output = objectResponse.asUtf8String();
assertEquals("klmnopqrst0", output);
// Valid start index within input and end index out of range, returns object from start index to End of Stream
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=190-300")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals("KLMNOPQRST", output);
// Valid start index within input and without specifying end index of range, returns object from start index to End of Stream
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=40-")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input.substring(40), output);
// Invalid range with only specifying the end index, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=-40")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range start index range greater than ending index, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=100-50")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range format, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("10-20")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range starting index and ending index greater than object length but within Cipher Block size, returns empty object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=216-217")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals("", output);
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AesGcmV3toV3FailsRangeExceededObjectLength(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-gcm-v3-to-v3-ranged-get-out-of-range");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// V3 Client
S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder()
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3Client v3Client = clientBuilder.build();
v3Client.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), RequestBody.fromString(input));
// Invalid range exceed object length, Throws S3EncryptionClientException wrapped with S3Exception
assertThrows(S3EncryptionClientException.class, () -> v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=300-400")
.key(objectKey)));
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AsyncAesGcmV3toV3FailsRangeExceededObjectLength(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-gcm-v3-to-v3-ranged-get-out-of-range");
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
// Async Client
S3AsyncEncryptionClient.Builder clientBuilder = S3AsyncEncryptionClient.builder()
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3AsyncClient asyncClient = clientBuilder.build();
asyncClient.putObject(PutObjectRequest.builder()
.bucket(BUCKET)
.key(objectKey)
.build(), AsyncRequestBody.fromString(input)).join();
try {
// Invalid range exceed object length, Throws S3Exception nested inside CompletionException
asyncClient.getObject(builder -> builder
.bucket(BUCKET)
.range("bytes=300-400")
.key(objectKey), AsyncResponseTransformer.toBytes()).join();
} catch (CompletionException e) {
assertEquals(S3Exception.class, e.getCause().getClass());
}
// Cleanup
deleteObject(BUCKET, objectKey, asyncClient);
asyncClient.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AesCbcV1toV3RangedGet(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-cbc-v1-to-v3-ranged-get");
// V1 Client
CryptoConfiguration v1CryptoConfig =
new CryptoConfiguration();
AmazonS3EncryptionClientBuilder v1ClientBuilder = AmazonS3EncryptionClient.encryptionBuilder()
.withCryptoConfiguration(v1CryptoConfig);
addKeyMaterialToV1Client(v1ClientBuilder, keyMaterial);
AmazonS3Encryption v1Client = v1ClientBuilder.build();
// This string is 200 characters/bytes long
// Due to padding, its ciphertext will be 208 bytes
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
v1Client.putObject(BUCKET, objectKey, input);
// V3 Client
S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder()
.enableLegacyWrappingAlgorithms(true)
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3Client v3Client = clientBuilder.build();
// Valid Range
ResponseBytes<GetObjectResponse> objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=10-20")
.key(objectKey));
String output = objectResponse.asUtf8String();
assertEquals("klmnopqrst0", output);
// Valid start index within input and end index out of range, returns object from start index to End of Stream
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=190-300")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals("KLMNOPQRST", output);
// Invalid range start index range greater than ending index, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=100-50")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Valid start index within input and without specifying end index of range, returns object from start index to End of Stream
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=40-")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input.substring(40), output);
// Invalid range with only specifying the end index, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=-40")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range format, returns entire object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("10-20")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals(input, output);
// Invalid range starting index and ending index greater than object length
// but within Cipher Block size, returns empty object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=216-217")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals("", output);
// Invalid range starting index and ending index greater than object length
// but within Cipher Block size, returns empty object
objectResponse = v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=216-218")
.key(objectKey));
output = objectResponse.asUtf8String();
assertEquals("", output);
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
@ParameterizedTest
@MethodSource("keyMaterialProvider")
public void AesCbcV1toV3FailsRangeExceededObjectLength(Object keyMaterial) {
final String objectKey = appendTestSuffix("aes-cbc-v1-to-v3-ranged-get-out-of-range");
// V1 Client
CryptoConfiguration v1CryptoConfig =
new CryptoConfiguration();
AmazonS3EncryptionClientBuilder v1ClientBuilder = AmazonS3EncryptionClient.encryptionBuilder()
.withCryptoConfiguration(v1CryptoConfig);
addKeyMaterialToV1Client(v1ClientBuilder, keyMaterial);
AmazonS3Encryption v1Client = v1ClientBuilder.build();
final String input = "0bcdefghijklmnopqrst0BCDEFGHIJKLMNOPQRST" +
"1bcdefghijklmnopqrst1BCDEFGHIJKLMNOPQRST" +
"2bcdefghijklmnopqrst2BCDEFGHIJKLMNOPQRST" +
"3bcdefghijklmnopqrst3BCDEFGHIJKLMNOPQRST" +
"4bcdefghijklmnopqrst4BCDEFGHIJKLMNOPQRST";
v1Client.putObject(BUCKET, objectKey, input);
// V3 Client
S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder()
.enableLegacyWrappingAlgorithms(true)
.enableLegacyUnauthenticatedModes(true);
addKeyMaterialToV3Client(clientBuilder, keyMaterial);
S3Client v3Client = clientBuilder.build();
// Invalid range exceed object length, Throws S3EncryptionClientException wrapped with S3Exception
assertThrows(S3EncryptionClientException.class, () -> v3Client.getObjectAsBytes(builder -> builder
.bucket(BUCKET)
.range("bytes=300-400")
.key(objectKey)));
// Cleanup
deleteObject(BUCKET, objectKey, v3Client);
v3Client.close();
}
}