Skip to content

Commit 13d4ee7

Browse files
author
Anirav Kareddy
committed
I created the MaterialsDescription class to help identify AES + RSA keys for reEncryptInstructionFile feature. I also included simple test cases to verify that the builder for the MaterialsDescription class is working as well as validation checks to ensure that MaterialsDescription is provided in the case that reEncryptInstructionFile is true
1 parent 99077dc commit 13d4ee7

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

src/main/java/software/amazon/encryption/s3/materials/AesKeyring.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ protected Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies() {
177177
return decryptDataKeyStrategies;
178178
}
179179

180-
public static class Builder extends S3Keyring.Builder<AesKeyring, Builder> {
180+
public MaterialsDescription getMaterialsDescription() {
181+
return _materialsDescription;
182+
}
183+
184+
public static class Builder extends S3Keyring.Builder<AesKeyring, Builder> {
181185
private SecretKey _wrappingKey;
182186

183187
private Builder() {

src/main/java/software/amazon/encryption/s3/materials/RsaKeyring.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ protected Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies() {
197197
return decryptDataKeyStrategies;
198198
}
199199

200-
public static class Builder extends S3Keyring.Builder<S3Keyring, Builder> {
200+
public MaterialsDescription getMaterialsDescription() {
201+
return _materialsDescription;
202+
}
203+
204+
public static class Builder extends S3Keyring.Builder<S3Keyring, Builder> {
201205
private PartialRsaKeyPair _partialRsaKeyPair;
202206

203207
private Builder() {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package software.amazon.encryption.s3.materials;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import software.amazon.encryption.s3.S3EncryptionClientException;
6+
7+
import javax.crypto.KeyGenerator;
8+
import javax.crypto.SecretKey;
9+
import java.security.KeyPair;
10+
import java.security.KeyPairGenerator;
11+
import java.security.NoSuchAlgorithmException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
17+
import static org.junit.jupiter.api.Assertions.assertNull;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
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+
}
34+
@Test
35+
public void testSimpleMaterialsDescription() {
36+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
37+
.put("version", "1.0")
38+
.build();
39+
assertEquals("1.0", materialsDescription.getDescription().get("version"));
40+
assertEquals(1, materialsDescription.getDescription().size());
41+
try {
42+
materialsDescription.getDescription().put("version", "2.0");
43+
throw new RuntimeException("Expected UnsupportedOperationException");
44+
} catch (UnsupportedOperationException e) {
45+
assertNull(e.getMessage());
46+
}
47+
try {
48+
materialsDescription.getDescription().clear();
49+
throw new RuntimeException("Expected UnsupportedOperationException");
50+
} catch (UnsupportedOperationException e) {
51+
assertNull(e.getMessage());
52+
}
53+
}
54+
@Test
55+
public void testMaterialsDescriptionPutAll() {
56+
Map<String, String> description = new HashMap<>();
57+
description.put("version", "1.0");
58+
description.put("next-version", "2.0");
59+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
60+
.putAll(description)
61+
.build();
62+
assertEquals(2, materialsDescription.getDescription().size());
63+
assertTrue(materialsDescription.getDescription().containsKey("version"));
64+
assertTrue(materialsDescription.getDescription().containsKey("next-version"));
65+
assertEquals("1.0", materialsDescription.getDescription().get("version"));
66+
assertEquals("2.0", materialsDescription.getDescription().get("next-version"));
67+
}
68+
@Test
69+
public void testMaterialsDescriptionAesKeyring() {
70+
AesKeyring aesKeyring = AesKeyring.builder()
71+
.wrappingKey(AES_KEY)
72+
.reEncryptInstructionFile(true)
73+
.materialsDescription(MaterialsDescription.builder()
74+
.put("version", "1.0")
75+
.put("admin", "yes")
76+
.build())
77+
.build();
78+
assertNotNull(aesKeyring.getMaterialsDescription());
79+
assertEquals("1.0", aesKeyring.getMaterialsDescription().getDescription().get("version"));
80+
assertEquals("yes", aesKeyring.getMaterialsDescription().getDescription().get("admin"));
81+
assertEquals(2, aesKeyring.getMaterialsDescription().getDescription().size());
82+
83+
}
84+
@Test
85+
public void testMaterialsDescriptionRsaKeyring() {
86+
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
87+
RsaKeyring rsaKeyring = RsaKeyring.builder()
88+
.wrappingKeyPair(keyPair)
89+
.reEncryptInstructionFile(true)
90+
.materialsDescription(MaterialsDescription.builder()
91+
.put("version", "1.0")
92+
.put("admin", "yes")
93+
.build())
94+
.build();
95+
assertNotNull(rsaKeyring);
96+
assertEquals("1.0", rsaKeyring.getMaterialsDescription().getDescription().get("version"));
97+
assertEquals("yes", rsaKeyring.getMaterialsDescription().getDescription().get("admin"));
98+
assertEquals(2, rsaKeyring.getMaterialsDescription().getDescription().size());
99+
100+
}
101+
@Test
102+
public void testMaterialsDescriptionRsaKeyringWithNoReEncrypt() {
103+
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
104+
try {
105+
RsaKeyring.builder()
106+
.wrappingKeyPair(keyPair)
107+
.reEncryptInstructionFile(true)
108+
.build();
109+
throw new RuntimeException("Expected failure!");
110+
} catch (S3EncryptionClientException e) {
111+
assertTrue(e.getMessage().contains("Materials description must be provided for re-encrypt instruction file!"));
112+
}
113+
}
114+
@Test
115+
public void testMaterialsDescriptionAesKeyringWithNoReEncrypt() {
116+
try {
117+
AesKeyring.builder()
118+
.wrappingKey(AES_KEY)
119+
.reEncryptInstructionFile(true)
120+
.build();
121+
throw new RuntimeException("Expected fa");
122+
} catch (S3EncryptionClientException e) {
123+
assertTrue(e.getMessage().contains("Materials description must be provided for re-encrypt instruction file!"));
124+
}
125+
}
126+
127+
}

0 commit comments

Comments
 (0)