-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.java
More file actions
865 lines (758 loc) · 34.6 KB
/
Copy pathTestUtils.java
File metadata and controls
865 lines (758 loc) · 34.6 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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.encryption.s3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.net.Socket;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.amazonaws.services.s3.model.S3Object;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.junit.jupiter.params.provider.Arguments;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import software.amazon.encryption.s3.model.CreateClientInput;
import software.amazon.encryption.s3.model.CreateClientOutput;
import software.amazon.encryption.s3.model.EncryptionAlgorithm;
import software.amazon.encryption.s3.model.GetObjectInput;
import software.amazon.encryption.s3.model.GetObjectOutput;
import software.amazon.encryption.s3.model.KeyMaterial;
import software.amazon.encryption.s3.model.PutObjectInput;
import software.amazon.encryption.s3.model.PutObjectOutput;
import software.amazon.encryption.s3.model.S3ECConfig;
import software.amazon.encryption.s3.model.S3EncryptionClientError;
import software.amazon.smithy.java.aws.client.restjson.RestJsonClientProtocol;
import software.amazon.smithy.java.client.core.ClientConfig;
import software.amazon.smithy.java.client.core.ClientProtocol;
import software.amazon.smithy.java.client.core.endpoint.EndpointResolver;
import software.amazon.encryption.s3.client.S3ECTestServerClient;
import software.amazon.encryption.s3.model.S3ECTestServerApiService;
import software.amazon.smithy.java.http.api.HttpRequest;
import software.amazon.smithy.java.http.api.HttpResponse;
public class TestUtils {
// Version name constants
// Each language can have up to 3 versions:
// vN-Current: Currently released version. Does not support setting commitment policy.
// vN-Transition: Proposed feature release version. Supports reading messages encrypted with key commitment.
// vN+1: Proposed breaking release version. Supports reading/writing messages encrypted with key commitment.
public static final String JAVA_V3_TRANSITION = "Java-V3-Transition";
public static final String JAVA_V4 = "Java-V4";
// No Python S3EC versions are released. Only test V3 as the "vN+1" version.
public static final String PYTHON_V3 = "Python-V3";
public static final String GO_V3_TRANSITION = "Go-V3-Transition";
public static final String GO_V4 = "Go-V4";
public static final String NET_V2_TRANSITION = "NET-V2-Transition";
public static final String NET_V3_TRANSITION = "NET-V3-Transition";
public static final String NET_V4 = "NET-V4";
public static final String CPP_V2_TRANSITION = "CPP-V2-Transition";
public static final String CPP_V3 = "CPP-V3";
public static final String RUBY_V2_TRANSITION = "Ruby-V2-Transition";
public static final String RUBY_V3 = "Ruby-V3";
public static final String PHP_V2_TRANSITION = "PHP-V2-Transition";
public static final String PHP_V3 = "PHP-V3";
// Test configuration constants
public static final String KMS_KEY_ARN = System.getenv("TEST_SERVER_KMS_KEY_ARN") != null ?
System.getenv("TEST_SERVER_KMS_KEY_ARN") : "arn:aws:kms:us-west-2:370957321024:alias/S3EC-Test-Server-Github-KMS-Key";
public static final Region KMS_REGION = Region.getRegion(Regions.fromName("us-west-2"));
public static final String BUCKET = System.getenv("TEST_SERVER_S3_BUCKET") != null ?
System.getenv("TEST_SERVER_S3_BUCKET") : "s3ec-test-server-github-bucket";
// Sets of unsupported features by language
public static final Set<String> ENCRYPTION_CONTEXT_ON_DECRYPT_UNSUPPORTED =
Set.of(PHP_V2_TRANSITION, PHP_V3, NET_V3_TRANSITION, NET_V4);
public static final Set<String> ENCRYPTION_CONTEXT_ON_ENCRYPT_UNSUPPORTED =
Set.of(NET_V3_TRANSITION, NET_V4);
// Languages that reject caller-provided encryption context when the
// wrapping algorithm is KmsV1 ("kms").
public static final Set<String> KMSV1_ENCRYPTION_CONTEXT_UNSUPPORTED =
Set.of(PYTHON_V3);
public static final Set<String> RE_ENCRYPT_SUPPORTED =
Set.of(JAVA_V3_TRANSITION, JAVA_V4);
public static final Set<String> RANGED_GETS_SUPPORTED =
Set.of(
JAVA_V3_TRANSITION, JAVA_V4, CPP_V2_TRANSITION, CPP_V3
);
// Cpp only supports Raw AES
public static final Set<String> RAW_AES_SUPPORTED =
Set.of(JAVA_V3_TRANSITION, JAVA_V4, NET_V3_TRANSITION, NET_V4, RUBY_V2_TRANSITION, RUBY_V3, CPP_V2_TRANSITION, CPP_V3);
public static final Set<String> RAW_RSA_SUPPORTED =
Set.of(JAVA_V3_TRANSITION, JAVA_V4, NET_V3_TRANSITION, NET_V4, RUBY_V2_TRANSITION, RUBY_V3);
// Intersection of RAW_AES_SUPPORTED and RAW_RSA_SUPPORTED
public static final Set<String> RAW_SUPPORTED =
RAW_AES_SUPPORTED.stream()
.filter(RAW_RSA_SUPPORTED::contains)
.collect(Collectors.toSet());
// .NET only supports decrypting instruction files using AES and RSA.
// Python MUST support decrypting KMS instruction files, but does not yet.
public static final Set<String> KMS_INSTRUCTION_FILE_UNSUPPORTED =
Set.of(NET_V2_TRANSITION, NET_V3_TRANSITION, NET_V4);
// Go does not write with instruction files
public static final Set<String> INSTRUCTION_FILE_PUT_UNSUPPORTED =
Set.of(GO_V3_TRANSITION, GO_V4, PYTHON_V3);
// Not implemented yet in Python.
public static final Set<String> INSTRUCTION_FILE_GET_UNSUPPORTED =
Set.of(PYTHON_V3);
// Languages that support custom instruction file suffix on GetObject
// Only Java, Ruby, and PHP servers have been updated with this feature
// This is a current gap.
public static final Set<String> CUSTOM_INSTRUCTION_SUFFIX_GET_SUPPORTED =
Set.of(
JAVA_V3_TRANSITION,
JAVA_V4,
RUBY_V2_TRANSITION,
RUBY_V3,
PHP_V2_TRANSITION,
PHP_V3
);
public static final Set<String> TRANSITION_VERSIONS =
Set.of(
JAVA_V3_TRANSITION,
GO_V3_TRANSITION,
NET_V3_TRANSITION,
CPP_V2_TRANSITION,
PHP_V2_TRANSITION,
RUBY_V2_TRANSITION
);
public static final Set<String> IMPROVED_VERSIONS =
Set.of(
JAVA_V4,
PYTHON_V3,
GO_V4,
NET_V4,
CPP_V3,
PHP_V3,
RUBY_V3
);
private static final Map<String, LanguageServerTarget> serverMap;
static {
final Map<String, LanguageServerTarget> servers = new LinkedHashMap<>();
servers.put(PYTHON_V3, new LanguageServerTarget(PYTHON_V3, "8081"));
servers.put(CPP_V2_TRANSITION, new LanguageServerTarget(CPP_V2_TRANSITION, "8097"));
servers.put(CPP_V3, new LanguageServerTarget(CPP_V3, "8091"));
servers.put(GO_V4, new LanguageServerTarget(GO_V4, "8089"));
servers.put(NET_V4, new LanguageServerTarget(NET_V4, "8090"));
servers.put(RUBY_V3, new LanguageServerTarget(RUBY_V3, "8092"));
servers.put(PHP_V3, new LanguageServerTarget(PHP_V3, "8093"));
servers.put(JAVA_V3_TRANSITION, new LanguageServerTarget(JAVA_V3_TRANSITION, "8094"));
servers.put(GO_V3_TRANSITION, new LanguageServerTarget(GO_V3_TRANSITION, "8095"));
servers.put(RUBY_V2_TRANSITION, new LanguageServerTarget(RUBY_V2_TRANSITION, "8098"));
servers.put(PHP_V2_TRANSITION, new LanguageServerTarget(PHP_V2_TRANSITION, "8099"));
servers.put(JAVA_V4, new LanguageServerTarget(JAVA_V4, "8088"));
servers.put(NET_V3_TRANSITION, new LanguageServerTarget(NET_V3_TRANSITION, "8100"));
serverMap = filterServers(servers);
System.out.println("=== Configured Test Servers ===");
System.out.println("\nServers:");
serverMap.forEach((name, target) -> {
System.out.println(" " + name + " -> " + target.getServerURI());
});
System.out.println("\nTotal servers configured: " + serverMap.size());
System.out.println("================================");
}
public static class LanguageServerTarget {
private final String baseURI = "http://localhost";
private String languageName;
private URI serverURI;
public LanguageServerTarget(String language, String port) {
languageName = language;
serverURI = URI.create(baseURI + ":" + port);
}
public String getLanguageName() {
return languageName;
}
public URI getServerURI() {
return serverURI;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
LanguageServerTarget that = (LanguageServerTarget) o;
return Objects.equals(languageName, that.languageName) && Objects.equals(serverURI, that.serverURI);
}
@Override
public int hashCode() {
return Objects.hash(languageName, serverURI);
}
@Override
public String toString() {
return languageName;
}
}
/**
* Filters the available servers based on system property test.filter.servers
* @param allServers Map of all available servers
* @return Filtered map of servers to use for testing
*/
private static Map<String, LanguageServerTarget> filterServers(Map<String, LanguageServerTarget> allServers) {
final String maybeFilter = System.getProperty("test.filter.servers");
if (maybeFilter == null || maybeFilter.trim().isEmpty()) {
return allServers; // No filtering - use all servers
}
System.out.println("Filtering with: " + maybeFilter);
final String[] filters = Arrays.stream(maybeFilter.split(","))
.map(String::trim)
.map(String::toLowerCase)
.toArray(String[]::new);
return allServers.entrySet().stream()
.filter(entry -> {
String key = entry.getKey().toLowerCase();
System.out.println("Checking server name:" + key);
return Arrays.stream(filters).anyMatch(key::contains);
})
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, // merge function (not really needed)
LinkedHashMap::new // preserve order
));
}
/**
* Gets the map of available server targets for testing
* @return Map of language names to server targets
*/
public static Map<String, LanguageServerTarget> getServerMap() {
return serverMap;
}
/**
* Checks if a server is listening on the specified URI
* @param uri The URI to check
* @return true if server is listening, false otherwise
*/
public static boolean serverListening(URI uri) {
try (Socket ignored = new Socket(uri.getHost(), uri.getPort())) {
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Creates a test server client for the specified language server target
* @param server The language server target
* @return Configured S3ECTestServerClient
*/
public static S3ECTestServerClient testServerClientFor(LanguageServerTarget server) {
S3ECTestServerApiService apiService = S3ECTestServerApiService.instance();
ClientProtocol<HttpRequest, HttpResponse> rest = new RestJsonClientProtocol(apiService.schema().id());
return S3ECTestServerClient.builder()
.endpointResolver(EndpointResolver.staticEndpoint(server.serverURI))
.withConfiguration(ClientConfig.builder()
.service(apiService)
.protocol(rest)
.endpointResolver(EndpointResolver.staticEndpoint(server.serverURI))
.build())
.build();
}
/**
* Converts a metadata map to a list format for Smithy serialization
* Annoyingly, Smithy doesn't provide an interface for map types
* in HTTP headers, so we have to do the serde ourselves
* @param md The metadata map
* @return List representation of the metadata
*/
public static List<String> metadataMapToList(Map<String, String> md) {
List<String> mdAsList = new ArrayList<>(md.size());
for (Map.Entry<String, String> keyValue : md.entrySet()) {
// Using ":" because Smithy will parse "," into a flattened list
mdAsList.add("[" + keyValue.getKey() + "]:[" + keyValue.getValue() + "]");
}
return mdAsList;
}
/**
* Validates that all servers in the server map are running
* @throws RuntimeException if any server is not running
*/
public static void validateServersRunning() {
for (LanguageServerTarget server : serverMap.values()) {
if (!serverListening(server.getServerURI())) {
throw new RuntimeException(String.format("Test Server for %s is not running at endpoint: %s",
server.getLanguageName(), server.getServerURI()));
}
}
}
/**
* Provides a stream of arguments for parameterized tests that test individual clients
* @return Stream of Arguments containing language names for testing
*/
public static Stream<Arguments> clientsForTest() {
return serverMap.values().stream()
.map(Arguments::of);
}
/**
* Get stream of arguments for transition version clients for testing.
*/
public static Stream<Arguments> transitionClientsForTest() {
return serverMap.values().stream()
.filter(target -> TRANSITION_VERSIONS.contains(target.getLanguageName()))
.map(Arguments::of);
}
/**
* Get stream of arguments for improved version clients for testing.
*/
public static Stream<Arguments> improvedClientsForTest() {
return serverMap.values().stream()
.filter(target -> IMPROVED_VERSIONS.contains(target.getLanguageName()))
.map(Arguments::of);
}
/**
* Get stream of arguments for the Python V3 client only.
* Other languages can be added to this set as their commitment policy
* validation is confirmed.
*/
public static Stream<Arguments> pythonV3ClientForTest() {
return serverMap.values().stream()
.filter(target -> PYTHON_V3.equals(target.getLanguageName()))
.map(Arguments::of);
}
/**
* Get stream of arguments for clients that support RAW AES (includes CPP).
*/
public static Stream<Arguments> clientsRawAesForTest() {
Stream<Arguments> improved = improvedClientsForTest()
.filter(target -> RAW_AES_SUPPORTED.contains(((LanguageServerTarget) target.get()[0]).getLanguageName()));
Stream<Arguments> transition = transitionClientsForTest()
.filter(target -> RAW_AES_SUPPORTED.contains(((LanguageServerTarget) target.get()[0]).getLanguageName()));
return Stream.concat(improved, transition);
}
/**
* Get stream of arguments for clients that support RAW RSA (excludes CPP).
*/
public static Stream<Arguments> clientsRawRsaForTest() {
Stream<Arguments> improved = improvedClientsForTest()
.filter(target -> RAW_RSA_SUPPORTED.contains(((LanguageServerTarget) target.get()[0]).getLanguageName()));
Stream<Arguments> transition = transitionClientsForTest()
.filter(target -> RAW_RSA_SUPPORTED.contains(((LanguageServerTarget) target.get()[0]).getLanguageName()));
return Stream.concat(improved, transition);
}
/**
* These functions provide a stream of arguments for parameterized tests.
* @return Stream of Arguments containing pairs of LanguageServerTarget for encryption and decryption
*/
public static Stream<Arguments> encryptImprovedDecryptImproved() {
return improvedClientsForTest()
.flatMap(encrypt -> improvedClientsForTest()
.flatMap(decrypt -> Stream.of(
Arguments.of(encrypt.get()[0], decrypt.get()[0])
)));
}
public static Stream<Arguments> encryptImprovedDecryptTransition() {
return improvedClientsForTest()
.flatMap(encrypt -> transitionClientsForTest()
.flatMap(decrypt -> Stream.of(
Arguments.of(encrypt.get()[0], decrypt.get()[0])
)));
}
public static Stream<Arguments> encryptTransitionDecryptImproved() {
return transitionClientsForTest()
.flatMap(encrypt -> improvedClientsForTest()
.flatMap(decrypt -> Stream.of(
Arguments.of(encrypt.get()[0], decrypt.get()[0])
)));
}
/**
* Provides a stream of arguments for parameterized tests that test cross-language compatibility
* @return Stream of Arguments containing pairs of LanguageServerTarget for encryption and decryption
*/
public static Stream<Arguments> crossLanguageClients() {
return serverMap.values().stream()
.flatMap(t1 -> serverMap.values().stream()
.flatMap(t2 -> Stream.of(
Arguments.of(t1, t2)
)));
}
/**
* For a given string, append a suffix to distinguish it from
* simultaneous test runs.
* @param s The string to append the suffix to
* @return The string with the suffix appended
*/
public static String appendTestSuffix(final String s) {
StringBuilder stringBuilder = new StringBuilder(s);
stringBuilder.append(DateTimeFormat.forPattern("-yyMMdd-hhmmss-").print(new DateTime()));
stringBuilder.append((int) (Math.random() * 100000));
return stringBuilder.toString();
}
private static AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
public static EncryptionAlgorithm GetEncryptionAlgorithm(String objectKey)
{
// Lambda to determine encryption algorithm from a metadata map
java.util.function.Function<Map<String, ?>, Optional<EncryptionAlgorithm>> getAlgorithmFromMap = (map) -> {
if (map.containsKey("x-amz-c")) {
return Optional.of(EncryptionAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY);
} else if (map.containsKey("x-amz-cek-alg")) {
String cek = (String) map.get("x-amz-cek-alg");
if (cek.contains("CBC")) {
return Optional.of(EncryptionAlgorithm.ALG_AES_256_CBC_IV16_NO_KDF);
} else if (cek.contains("GCM")) {
return Optional.of(EncryptionAlgorithm.ALG_AES_256_GCM_IV12_TAG16_NO_KDF);
}
}
return Optional.empty();
};
ObjectMetadata metadata = s3Client.getObjectMetadata(TestUtils.BUCKET, objectKey);
Map<String, String> userMetadata = metadata.getUserMetadata();
// Try to get algorithm from object metadata
Optional<EncryptionAlgorithm> algorithm = getAlgorithmFromMap.apply(userMetadata);
if (algorithm.isPresent()) {
return algorithm.get();
}
// Check instruction file
try {
String instructionFileKey = objectKey + ".instruction";
com.amazonaws.services.s3.model.S3Object instructionFileObject =
s3Client.getObject(TestUtils.BUCKET, instructionFileKey);
// Read instruction file content
java.io.InputStream inputStream = instructionFileObject.getObjectContent();
String instructionFileJson = new String(
inputStream.readAllBytes(),
java.nio.charset.StandardCharsets.UTF_8
);
inputStream.close();
// Parse JSON to get metadata
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
Map<String, Object> instructionFileMap = mapper.readValue(instructionFileJson, Map.class);
// Try to get algorithm from instruction file
algorithm = getAlgorithmFromMap.apply(instructionFileMap);
if (algorithm.isPresent()) {
return algorithm.get();
}
} catch (Exception e) {
// Instruction file doesn't exist or couldn't be read
}
throw new RuntimeException("Could not determine encryption algorithm from object metadata or instruction file!");
}
public static void Encrypt(
S3ECTestServerClient client,
String S3ECId,
String objectKey,
List<String> crossLanguageObjects,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
PutObjectOutput foo = client.putObject(PutObjectInput.builder()
.clientID(S3ECId)
.key(objectKey)
.bucket(TestUtils.BUCKET)
.body(ByteBuffer.wrap(objectKey.getBytes(StandardCharsets.UTF_8)))
.build());
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"When encrypting the EncryptionAlgorithm does not match the expected value: " + expectedEncryptionAlgorithm
);
crossLanguageObjects.add(objectKey);
}
public static void Decrypt(
S3ECTestServerClient client,
String S3ECId,
List<String> crossLanguageObjects,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
// Call 5-parameter version with crossLanguageObjects as expectedPlaintexts
Decrypt(client, S3ECId, crossLanguageObjects, expectedEncryptionAlgorithm, crossLanguageObjects);
}
public static void Decrypt(
S3ECTestServerClient client,
String S3ECId,
List<String> crossLanguageObjects,
EncryptionAlgorithm expectedEncryptionAlgorithm,
List<String> expectedPlaintexts
) {
Decrypt(client, S3ECId, crossLanguageObjects, expectedEncryptionAlgorithm, expectedPlaintexts, null);
}
public static void Decrypt(
S3ECTestServerClient client,
String S3ECId,
List<String> crossLanguageObjects,
EncryptionAlgorithm expectedEncryptionAlgorithm,
List<String> expectedPlaintexts,
String instructionFileSuffix
) {
if (crossLanguageObjects.isEmpty()) {
fail("There is nothing to decrypt");
}
List<String> failures = new ArrayList<>();
for (int i = 0; i < crossLanguageObjects.size(); i++) {
try {
String objectKey = crossLanguageObjects.get(i);
String expectedPlaintext = expectedPlaintexts.get(i);
GetObjectInput.Builder builder = GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey);
// Add custom instruction file suffix if provided
if (instructionFileSuffix != null && !instructionFileSuffix.isEmpty()) {
builder.instructionFileSuffix(instructionFileSuffix);
}
GetObjectOutput output = client.getObject(builder.build());
// Then: Pass
assertEquals(expectedPlaintext, new String(output.getBody().array()));
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"When decrypting the EncryptionAlgorithm does not match the expected value: " + expectedEncryptionAlgorithm
);
} catch (Exception e) {
failures.add(String.format(
"Failed to decrypt object '%s' (index %d): %s - %s",
crossLanguageObjects.get(i), i, e.getClass().getSimpleName(), e.getMessage()
));
}
}
if (!failures.isEmpty()) {
throw new AssertionError(String.format(
"Decryption failed for %d out of %d objects:\n%s",
failures.size(), crossLanguageObjects.size(),
String.join("\n", failures)
));
}
}
/**
* Decrypt helper for C++ clients that require materials description per-operation.
*
* C++ SDK Design: Unlike Java/. NET/etc where materials description is embedded in the
* keyring during client creation, the C++ SDK requires passing materials description
* as a contextMap parameter to each GetObject/PutObject operation.
*
* This helper extracts materials description from KeyMaterial and passes it via the
* Content-Metadata header on each GetObject call, which the C++ server converts to
* the contextMap parameter required by the C++ SDK.
*/
public static void DecryptWithMaterialsDescription(
S3ECTestServerClient client,
String S3ECId,
List<String> crossLanguageObjects,
KeyMaterial keyMaterial,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
DecryptWithMaterialsDescription(client, S3ECId, crossLanguageObjects, keyMaterial,
expectedEncryptionAlgorithm, crossLanguageObjects);
}
/**
* Decrypt helper for C++ clients with custom expected plaintexts.
*/
public static void DecryptWithMaterialsDescription(
S3ECTestServerClient client,
String S3ECId,
List<String> crossLanguageObjects,
KeyMaterial keyMaterial,
EncryptionAlgorithm expectedEncryptionAlgorithm,
List<String> expectedPlaintexts
) {
if (crossLanguageObjects.isEmpty()) {
throw new AssertionError("There is nothing to decrypt");
}
// Extract materials description from KeyMaterial
List<String> metadata = (keyMaterial.getMaterialsDescription() != null)
? metadataMapToList(keyMaterial.getMaterialsDescription())
: new ArrayList<>();
List<String> failures = new ArrayList<>();
for (int i = 0; i < crossLanguageObjects.size(); i++) {
try {
String objectKey = crossLanguageObjects.get(i);
String expectedPlaintext = expectedPlaintexts.get(i);
GetObjectOutput output = client.getObject(GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey)
.metadata(metadata) // Pass materials description for C++
.build());
// Then: Pass
assertEquals(expectedPlaintext, new String(output.getBody().array()));
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"When decrypting the EncryptionAlgorithm does not match the expected value: " + expectedEncryptionAlgorithm
);
} catch (Exception e) {
failures.add(String.format(
"Failed to decrypt object '%s' (index %d): %s - %s",
crossLanguageObjects.get(i), i, e.getClass().getSimpleName(), e.getMessage()
));
}
}
if (!failures.isEmpty()) {
throw new AssertionError(String.format(
"Decryption failed for %d out of %d objects:\n%s",
failures.size(), crossLanguageObjects.size(),
String.join("\n", failures)
));
}
}
/**
* Attempts to encrypt an object and expects the operation to fail with an S3EncryptionClientError.
* This is used for negative tests where the client configuration should prevent encryption
* (e.g., commitment policy violations).
*
* The failure may occur during client creation (CreateClient) or during the PutObject call,
* depending on when the server-side S3EC validates the configuration.
*/
public static void Encrypt_fails(
S3ECTestServerClient client,
S3ECConfig config,
String objectKey
) {
try {
CreateClientOutput clientOutput = client.createClient(CreateClientInput.builder()
.config(config)
.build());
String S3ECId = clientOutput.getClientId();
client.putObject(PutObjectInput.builder()
.clientID(S3ECId)
.key(objectKey)
.bucket(TestUtils.BUCKET)
.body(ByteBuffer.wrap(objectKey.getBytes(StandardCharsets.UTF_8)))
.build());
fail("Encryption should have failed for object: " + objectKey
+ " with config commitmentPolicy=" + config.getCommitmentPolicy()
+ " encryptionAlgorithm=" + config.getEncryptionAlgorithm());
} catch (S3EncryptionClientError e) {
// Expected - the S3EC should reject this configuration
}
}
public static void Decrypt_fails(
S3ECTestServerClient client,
String S3ECId, List<String> crossLanguageObjects,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
if (crossLanguageObjects.isEmpty()) {
throw new AssertionError("There is nothing to decrypt");
}
List<String> successfulDecrypt = new ArrayList<>();
for (String objectKey : crossLanguageObjects) {
try {
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"Before decrypting the EncryptionAlgorithm does not match the expected value: " + expectedEncryptionAlgorithm
);
GetObjectOutput output = client.getObject(GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey)
.build());
// It should fail to decrypt
successfulDecrypt.add(objectKey);
} catch (S3EncryptionClientError e) {
// This is a success
// TODO, add the failure message
}
}
assertEquals(successfulDecrypt.size(), 0, "Decryption should have failed:" + String.join(",", successfulDecrypt));
}
/**
* Perform ranged get operation with specified byte range
*/
public static void RangedGet(
S3ECTestServerClient client,
String S3ECId,
List<String> objectKeys,
long rangeStart,
long rangeEnd,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
if (objectKeys.isEmpty()) {
throw new AssertionError("There is nothing to get");
}
List<String> failures = new ArrayList<>();
for (String objectKey : objectKeys) {
try {
// Get the full object first to know expected content
GetObjectOutput fullOutput = client.getObject(GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey)
.build());
byte[] fullContent = fullOutput.getBody().array();
// Perform ranged get
GetObjectOutput output = client.getObject(GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey)
.range("bytes=" + rangeStart + "-" + rangeEnd)
.build());
// Verify the ranged content matches expected slice
byte[] rangedContent = output.getBody().array();
int startIndex = (int) rangeStart;
int endIndex = (int) Math.min(rangeEnd + 1, fullContent.length); // +1 because HTTP ranges are inclusive
byte[] expectedContent = Arrays.copyOfRange(fullContent, startIndex, endIndex);
assertArrayEquals(expectedContent, rangedContent,
"Ranged get returned unexpected data for:" + objectKey);
// Verify encryption algorithm
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"Encryption algorithm mismatch for " + objectKey
);
} catch (Exception e) {
failures.add(String.format(
"Failed ranged get on '%s': %s - %s",
objectKey, e.getClass().getSimpleName(), e.getMessage()
));
}
}
if (!failures.isEmpty()) {
throw new AssertionError(String.format(
"Ranged get failed for %d out of %d objects:\n%s",
failures.size(), objectKeys.size(),
String.join("\n", failures)
));
}
}
/**
* Perform ranged get operations that are expected to fail
*/
public static void RangedGet_fails(
S3ECTestServerClient client,
String S3ECId,
List<String> objectKeys,
long rangeStart,
long rangeEnd,
EncryptionAlgorithm expectedEncryptionAlgorithm
) {
if (objectKeys.isEmpty()) {
throw new AssertionError("There is nothing to get");
}
List<String> successfulGets = new ArrayList<>();
for (String objectKey : objectKeys) {
try {
assertEquals(
expectedEncryptionAlgorithm,
GetEncryptionAlgorithm(objectKey),
"Encryption algorithm mismatch for " + objectKey
);
GetObjectOutput output = client.getObject(GetObjectInput.builder()
.clientID(S3ECId)
.bucket(TestUtils.BUCKET)
.key(objectKey)
.range("bytes=" + rangeStart + "-" + rangeEnd)
.build());
// Should have failed but didn't
successfulGets.add(objectKey);
} catch (S3EncryptionClientError e) {
// This is expected - the ranged get should fail
}
}
assertEquals(0, successfulGets.size(),
"Ranged get should have failed for: " + String.join(", ", successfulGets));
}
}