Skip to content

Commit 04be22b

Browse files
committed
Add DirectKmsMaterialProviderTest and MetaStoreTests
Add the internal tests for ExtraDataSupplier in MetaStore
1 parent 3c319a6 commit 04be22b

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/providers/DirectKmsMaterialProviderTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
2121
import com.amazonaws.services.dynamodbv2.testing.FakeKMS;
2222
import com.amazonaws.services.kms.AWSKMS;
23+
import com.amazonaws.services.kms.model.DecryptRequest;
24+
import com.amazonaws.services.kms.model.DecryptResult;
2325
import com.amazonaws.services.kms.model.GenerateDataKeyRequest;
2426
import com.amazonaws.services.kms.model.GenerateDataKeyResult;
2527
import com.amazonaws.util.Base64;
@@ -50,7 +52,7 @@ public class DirectKmsMaterialProviderTest {
5052

5153
@BeforeMethod
5254
public void setUp() {
53-
description = new HashMap<String, String>();
55+
description = new HashMap<>();
5456
description.put("TestKey", "test value");
5557
description = Collections.unmodifiableMap(description);
5658
ctx = new EncryptionContext.Builder().build();
@@ -87,7 +89,7 @@ public void simple() throws GeneralSecurityException {
8789
public void simpleWithKmsEc() throws GeneralSecurityException {
8890
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId);
8991

90-
Map<String, AttributeValue> attrVals = new HashMap<String, AttributeValue>();
92+
Map<String, AttributeValue> attrVals = new HashMap<>();
9193
attrVals.put("hk", new AttributeValue("HashKeyValue"));
9294
attrVals.put("rk", new AttributeValue("RangeKeyValue"));
9395

@@ -116,7 +118,7 @@ public void simpleWithKmsEc() throws GeneralSecurityException {
116118
public void simpleWithKmsEc2() throws GeneralSecurityException {
117119
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId);
118120

119-
Map<String, AttributeValue> attrVals = new HashMap<String, AttributeValue>();
121+
Map<String, AttributeValue> attrVals = new HashMap<>();
120122
attrVals.put("hk", new AttributeValue().withN("10"));
121123
attrVals.put("rk", new AttributeValue().withN("20"));
122124

@@ -145,7 +147,7 @@ public void simpleWithKmsEc2() throws GeneralSecurityException {
145147
public void simpleWithKmsEc3() throws GeneralSecurityException {
146148
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId);
147149

148-
Map<String, AttributeValue> attrVals = new HashMap<String, AttributeValue>();
150+
Map<String, AttributeValue> attrVals = new HashMap<>();
149151
attrVals.put("hk",
150152
new AttributeValue().withB(ByteBuffer.wrap("Foo".getBytes(StandardCharsets.UTF_8))));
151153
attrVals.put("rk",
@@ -198,7 +200,7 @@ public void testRefresh() {
198200

199201
@Test
200202
public void explicitContentKeyAlgorithm() throws GeneralSecurityException {
201-
Map<String, String> desc = new HashMap<String, String>();
203+
Map<String, String> desc = new HashMap<>();
202204
desc.put(WrappedRawMaterials.CONTENT_KEY_ALGORITHM, "AES");
203205

204206
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId, desc);
@@ -215,7 +217,7 @@ public void explicitContentKeyAlgorithm() throws GeneralSecurityException {
215217

216218
@Test
217219
public void explicitContentKeyLength128() throws GeneralSecurityException {
218-
Map<String, String> desc = new HashMap<String, String>();
220+
Map<String, String> desc = new HashMap<>();
219221
desc.put(WrappedRawMaterials.CONTENT_KEY_ALGORITHM, "AES/128");
220222

221223
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId, desc);
@@ -234,7 +236,7 @@ public void explicitContentKeyLength128() throws GeneralSecurityException {
234236

235237
@Test
236238
public void explicitContentKeyLength256() throws GeneralSecurityException {
237-
Map<String, String> desc = new HashMap<String, String>();
239+
Map<String, String> desc = new HashMap<>();
238240
desc.put(WrappedRawMaterials.CONTENT_KEY_ALGORITHM, "AES/256");
239241

240242
DirectKmsMaterialProvider prov = new DirectKmsMaterialProvider(kms, keyId, desc);
@@ -336,7 +338,7 @@ public GenerateDataKeyResult generateDataKey(GenerateDataKeyRequest r) {
336338
}
337339

338340
private static class ExtendedKmsMaterialProvider extends DirectKmsMaterialProvider {
339-
protected final String encryptionKeyIdAttributeName;
341+
private final String encryptionKeyIdAttributeName;
340342

341343
public ExtendedKmsMaterialProvider(AWSKMS kms, String encryptionKeyId, String encryptionKeyIdAttributeName) {
342344
super(kms, encryptionKeyId);
@@ -365,6 +367,16 @@ protected void validateEncryptionKeyId(String encryptionKeyId, EncryptionContext
365367
throw new DynamoDBMappingException("encryption key ids do not match.");
366368
}
367369
}
370+
371+
@Override
372+
protected DecryptResult decrypt(DecryptRequest request, EncryptionContext context) {
373+
return super.decrypt(request, context);
374+
}
375+
376+
@Override
377+
protected GenerateDataKeyResult generateDataKey(GenerateDataKeyRequest request, EncryptionContext context) {
378+
return super.generateDataKey(request, context);
379+
}
368380
}
369381

370382
private static EncryptionContext ctx(EncryptionMaterials mat) {

src/test/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/providers/store/MetaStoreTests.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.EncryptionMaterialsProvider;
2222
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.SymmetricStaticProvider;
2323
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
24+
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
2425
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
2526
import org.testng.annotations.BeforeMethod;
2627
import org.testng.annotations.Test;
@@ -31,6 +32,10 @@
3132
import java.lang.reflect.InvocationTargetException;
3233
import java.lang.reflect.Method;
3334
import java.lang.reflect.Proxy;
35+
import java.util.HashMap;
36+
import java.util.HashSet;
37+
import java.util.Map;
38+
import java.util.Set;
3439

3540
import static org.testng.AssertJUnit.assertEquals;
3641
import static org.testng.AssertJUnit.assertNotNull;
@@ -59,6 +64,28 @@ public class MetaStoreTests {
5964
private MetaStore targetStore;
6065
private EncryptionContext ctx;
6166

67+
private static class TestExtraDataSupplier implements MetaStore.ExtraDataSupplier {
68+
69+
private final Map<String, AttributeValue> attributeValueMap;
70+
private final Set<String> signedOnlyFieldNames;
71+
72+
public TestExtraDataSupplier(final Map<String, AttributeValue> attributeValueMap,
73+
final Set<String> signedOnlyFieldNames) {
74+
this.attributeValueMap = attributeValueMap;
75+
this.signedOnlyFieldNames = signedOnlyFieldNames;
76+
}
77+
78+
@Override
79+
public Map<String, AttributeValue> getAttributes(String materialName, long version) {
80+
return this.attributeValueMap;
81+
}
82+
83+
@Override
84+
public Set<String> getSignedOnlyFieldNames() {
85+
return this.signedOnlyFieldNames;
86+
}
87+
}
88+
6289
@BeforeMethod
6390
public void setup() {
6491
client = synchronize(DynamoDBEmbedded.create(), AmazonDynamoDB.class);
@@ -181,6 +208,34 @@ public void getOrCreateCollision() {
181208
assertEquals(eMat.getSigningKey(), dMat.getVerificationKey());
182209
}
183210

211+
@Test
212+
public void getOrCreateWithContextSupplier() {
213+
final Map<String, AttributeValue> attributeValueMap = new HashMap<>();
214+
attributeValueMap.put("CustomKeyId", new AttributeValue().withS("testCustomKeyId"));
215+
attributeValueMap.put("KeyToken", new AttributeValue().withS("testKeyToken"));
216+
217+
final Set<String> signedOnlyAttributes = new HashSet<>();
218+
signedOnlyAttributes.add("CustomKeyId");
219+
220+
final TestExtraDataSupplier extraDataSupplier = new TestExtraDataSupplier(
221+
attributeValueMap, signedOnlyAttributes);
222+
223+
final MetaStore metaStore = new MetaStore(client, SOURCE_TABLE_NAME, ENCRYPTOR, extraDataSupplier);
224+
225+
assertEquals(-1, metaStore.getMaxVersion(MATERIAL_NAME));
226+
final EncryptionMaterialsProvider prov1 = metaStore.getOrCreate(MATERIAL_NAME, 0);
227+
assertEquals(0, metaStore.getMaxVersion(MATERIAL_NAME));
228+
final EncryptionMaterialsProvider prov2 = metaStore.getOrCreate(MATERIAL_NAME, 0);
229+
230+
final EncryptionMaterials eMat = prov1.getEncryptionMaterials(ctx);
231+
final SecretKey encryptionKey = eMat.getEncryptionKey();
232+
assertNotNull(encryptionKey);
233+
234+
final DecryptionMaterials dMat = prov2.getDecryptionMaterials(ctx(eMat));
235+
assertEquals(encryptionKey, dMat.getDecryptionKey());
236+
assertEquals(eMat.getSigningKey(), dMat.getVerificationKey());
237+
}
238+
184239
@Test
185240
public void replicateIntermediateKeysTest() {
186241
assertEquals(-1, store.getMaxVersion(MATERIAL_NAME));
@@ -231,6 +286,20 @@ public void invalidVersion() {
231286
store.getProvider(MATERIAL_NAME, 1000);
232287
}
233288

289+
@Test(expected = IllegalArgumentException.class)
290+
public void invalidSignedOnlyField() {
291+
final Map<String, AttributeValue> attributeValueMap = new HashMap<>();
292+
attributeValueMap.put("enc", new AttributeValue().withS("testEncryptionKey"));
293+
294+
final Set<String> signedOnlyAttributes = new HashSet<>();
295+
signedOnlyAttributes.add("enc");
296+
297+
final TestExtraDataSupplier extraDataSupplier = new TestExtraDataSupplier(
298+
attributeValueMap, signedOnlyAttributes);
299+
300+
new MetaStore(client, SOURCE_TABLE_NAME, ENCRYPTOR, extraDataSupplier);
301+
}
302+
234303
private static EncryptionContext ctx(final EncryptionMaterials mat) {
235304
return new EncryptionContext.Builder()
236305
.withMaterialDescription(mat.getMaterialDescription()).build();

0 commit comments

Comments
 (0)