Skip to content

Commit 5cd05db

Browse files
author
Anirav Kareddy
committed
correctly using encryption context via overrideConfiguration
1 parent 5b0cbce commit 5cd05db

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

src/test/java/software/amazon/encryption/s3/S3EncryptionClientMatDescTest.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import java.security.NoSuchAlgorithmException;
2424
import java.security.SecureRandom;
2525
import java.util.Base64;
26+
import java.util.HashMap;
27+
import java.util.Map;
2628

2729
import static org.junit.jupiter.api.Assertions.assertEquals;
2830
import static org.junit.jupiter.api.Assertions.assertNotNull;
2931
import static org.junit.jupiter.api.Assertions.assertNull;
32+
import static software.amazon.encryption.s3.S3EncryptionClient.withAdditionalConfiguration;
3033
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET;
3134
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
3235
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject;
@@ -46,32 +49,6 @@ public static void setUp() throws NoSuchAlgorithmException {
4649
RSA_KEY_PAIR = keyPairGen.generateKeyPair();
4750
}
4851

49-
@Test
50-
public void testMaterialsDescriptionAesKeyring() {
51-
AesKeyring aesKeyring = AesKeyring.builder()
52-
.wrappingKey(AES_KEY)
53-
.materialsDescription(MaterialsDescription.builder()
54-
.put("version", "1.0")
55-
.put("admin", "yes")
56-
.build())
57-
.build();
58-
assertNotNull(aesKeyring);
59-
}
60-
61-
@Test
62-
public void testMaterialsDescriptionRsaKeyring() {
63-
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
64-
RsaKeyring rsaKeyring = RsaKeyring.builder()
65-
.wrappingKeyPair(keyPair)
66-
.materialsDescription(MaterialsDescription.builder()
67-
.put("version", "1.0")
68-
.put("admin", "yes")
69-
.build())
70-
.build();
71-
assertNotNull(rsaKeyring);
72-
73-
}
74-
7552
@Test
7653
public void testAesMaterialsDescriptionInObjectMetadata() {
7754
AesKeyring aesKeyring = AesKeyring.builder()
@@ -267,13 +244,13 @@ public void testAesKeyringMatDescOverridesPutObjectEncryptionContext() {
267244

268245
final String input = "Testing Materials Description in Instruction File and not Encryption Context!";
269246
final String objectKey = appendTestSuffix("test-aes-materials-description-in-instruction-file-and-not-encryption-context");
270-
final String encryptionContext = "{\"admin\":\"yes\"}";
247+
final Map<String, String> encryptionContext = new HashMap<String, String>();
248+
encryptionContext.put("admin", "yes");
271249

272250
client.putObject(builder -> builder
273251
.bucket(BUCKET)
274252
.key(objectKey)
275-
.serverSideEncryption(ServerSideEncryption.AWS_KMS)
276-
.ssekmsEncryptionContext(Base64.getEncoder().encodeToString(encryptionContext.getBytes(StandardCharsets.UTF_8)))
253+
.overrideConfiguration(withAdditionalConfiguration(encryptionContext))
277254
.build(), RequestBody.fromString(input)
278255
);
279256
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder
@@ -313,13 +290,13 @@ public void testRsaKeyringMatDescOverridesPutObjectEncryptionContext() {
313290
.build();
314291
final String input = "Testing Materials Description in Instruction File and not Encryption Context!";
315292
final String objectKey = appendTestSuffix("test-rsa-materials-description-in-instruction-file-and-not-encryption-context");
316-
final String encryptionContext = "{\"admin\":\"yes\"}";
293+
final Map<String, String> encryptionContext = new HashMap<String, String>();
294+
encryptionContext.put("admin", "yes");
317295

318296
client.putObject(builder -> builder
319297
.bucket(BUCKET)
320298
.key(objectKey)
321-
.serverSideEncryption(ServerSideEncryption.AWS_KMS)
322-
.ssekmsEncryptionContext(Base64.getEncoder().encodeToString(encryptionContext.getBytes(StandardCharsets.UTF_8)))
299+
.overrideConfiguration(withAdditionalConfiguration(encryptionContext))
323300
.build(), RequestBody.fromString(input)
324301
);
325302
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder

src/test/java/software/amazon/encryption/s3/materials/MaterialsDescriptionTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
package software.amazon.encryption.s3.materials;
22

3+
import org.junit.jupiter.api.BeforeAll;
34
import org.junit.jupiter.api.Test;
5+
6+
import javax.crypto.KeyGenerator;
7+
import javax.crypto.SecretKey;
8+
import java.security.KeyPair;
9+
import java.security.KeyPairGenerator;
10+
import java.security.NoSuchAlgorithmException;
411
import java.util.HashMap;
512
import java.util.Map;
613

714
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
816
import static org.junit.jupiter.api.Assertions.assertNull;
917
import static org.junit.jupiter.api.Assertions.assertTrue;
1018
import static org.junit.jupiter.api.Assertions.fail;
1119

1220
public class MaterialsDescriptionTest {
21+
private static SecretKey AES_KEY;
22+
private static KeyPair RSA_KEY_PAIR;
23+
24+
@BeforeAll
25+
public static void setUp() throws NoSuchAlgorithmException {
26+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
27+
keyGen.init(256);
28+
AES_KEY = keyGen.generateKey();
29+
30+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
31+
keyPairGen.initialize(2048);
32+
RSA_KEY_PAIR = keyPairGen.generateKeyPair();
33+
}
1334

1435
@Test
1536
public void testSimpleMaterialsDescription() {
@@ -46,4 +67,30 @@ public void testMaterialsDescriptionPutAll() {
4667
assertEquals("2.0", materialsDescription.getMaterialsDescription().get("next-version"));
4768
}
4869

70+
@Test
71+
public void testMaterialsDescriptionAesKeyring() {
72+
AesKeyring aesKeyring = AesKeyring.builder()
73+
.wrappingKey(AES_KEY)
74+
.materialsDescription(MaterialsDescription.builder()
75+
.put("version", "1.0")
76+
.put("admin", "yes")
77+
.build())
78+
.build();
79+
assertNotNull(aesKeyring);
80+
}
81+
82+
@Test
83+
public void testMaterialsDescriptionRsaKeyring() {
84+
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
85+
RsaKeyring rsaKeyring = RsaKeyring.builder()
86+
.wrappingKeyPair(keyPair)
87+
.materialsDescription(MaterialsDescription.builder()
88+
.put("version", "1.0")
89+
.put("admin", "yes")
90+
.build())
91+
.build();
92+
assertNotNull(rsaKeyring);
93+
94+
}
95+
4996
}

0 commit comments

Comments
 (0)