|
36 | 36 | import com.amazonaws.services.dynamodbv2.datamodeling.encryption.materials.EncryptionMaterials; |
37 | 37 | import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.EncryptionMaterialsProvider; |
38 | 38 | import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.SymmetricStaticProvider; |
39 | | -import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.store.MetaStore; |
40 | | -import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.store.ProviderStore; |
41 | 39 | import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; |
42 | 40 |
|
43 | 41 | public class MetaStoreTests { |
44 | | - private static final String TABLE_NAME = "keystoreTable"; |
| 42 | + private static final String SOURCE_TABLE_NAME = "keystoreTable"; |
| 43 | + private static final String DESTINATION_TABLE_NAME = "keystoreDestinationTable"; |
45 | 44 | private static final String MATERIAL_NAME = "material"; |
46 | 45 | private static final SecretKey AES_KEY = new SecretKeySpec(new byte[] { 0, |
47 | 46 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, "AES"); |
| 47 | + private static final SecretKey TARGET_AES_KEY = new SecretKeySpec(new byte[] { 0, |
| 48 | + 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 }, "AES"); |
48 | 49 | private static final SecretKey HMAC_KEY = new SecretKeySpec(new byte[] { 0, |
49 | 50 | 1, 2, 3, 4, 5, 6, 7 }, "HmacSHA256"); |
| 51 | + private static final SecretKey TARGET_HMAC_KEY = new SecretKeySpec(new byte[] { 0, |
| 52 | + 2, 4, 6, 8, 10, 12, 14 }, "HmacSHA256"); |
50 | 53 | private static final EncryptionMaterialsProvider BASE_PROVIDER = new SymmetricStaticProvider(AES_KEY, HMAC_KEY); |
| 54 | + private static final EncryptionMaterialsProvider TARGET_BASE_PROVIDER = new SymmetricStaticProvider(TARGET_AES_KEY, TARGET_HMAC_KEY); |
51 | 55 | private static final DynamoDBEncryptor ENCRYPTOR = DynamoDBEncryptor.getInstance(BASE_PROVIDER); |
| 56 | + private static final DynamoDBEncryptor TARGET_ENCRYPTOR = DynamoDBEncryptor.getInstance(TARGET_BASE_PROVIDER); |
52 | 57 |
|
53 | 58 | private AmazonDynamoDB client; |
54 | | - private ProviderStore store; |
| 59 | + private AmazonDynamoDB targetClient; |
| 60 | + private MetaStore store; |
| 61 | + private MetaStore targetStore; |
55 | 62 | private EncryptionContext ctx; |
56 | 63 |
|
57 | 64 | @Before |
58 | 65 | public void setup() { |
59 | 66 | client = synchronize(DynamoDBEmbedded.create(), AmazonDynamoDB.class); |
60 | | - MetaStore.createTable(client, TABLE_NAME, new ProvisionedThroughput(1L, 1L)); |
61 | | - store = new MetaStore(client, TABLE_NAME, ENCRYPTOR); |
| 67 | + targetClient = synchronize(DynamoDBEmbedded.create(), AmazonDynamoDB.class); |
| 68 | + MetaStore.createTable(client, SOURCE_TABLE_NAME, new ProvisionedThroughput(1L, 1L)); |
| 69 | + //Creating Targeted DynamoDB Object |
| 70 | + MetaStore.createTable(targetClient, DESTINATION_TABLE_NAME, new ProvisionedThroughput(1L, 1L)); |
| 71 | + store = new MetaStore(client, SOURCE_TABLE_NAME, ENCRYPTOR); |
| 72 | + targetStore = new MetaStore(targetClient, DESTINATION_TABLE_NAME, TARGET_ENCRYPTOR); |
62 | 73 | ctx = new EncryptionContext.Builder().build(); |
63 | 74 | } |
64 | 75 |
|
@@ -172,6 +183,28 @@ public void getOrCreateCollision() { |
172 | 183 | assertEquals(eMat.getSigningKey(), dMat.getVerificationKey()); |
173 | 184 | } |
174 | 185 |
|
| 186 | + @Test |
| 187 | + public void replicateIntermediateKeysTest() { |
| 188 | + assertEquals(-1, store.getMaxVersion(MATERIAL_NAME)); |
| 189 | + |
| 190 | + final EncryptionMaterialsProvider prov1 = store.getOrCreate(MATERIAL_NAME, 0); |
| 191 | + assertEquals(0, store.getMaxVersion(MATERIAL_NAME)); |
| 192 | + |
| 193 | + store.replicate(MATERIAL_NAME, 0, targetStore); |
| 194 | + assertEquals(0, targetStore.getMaxVersion(MATERIAL_NAME)); |
| 195 | + |
| 196 | + final EncryptionMaterials eMat = prov1.getEncryptionMaterials(ctx); |
| 197 | + final DecryptionMaterials dMat = targetStore.getProvider(MATERIAL_NAME, 0).getDecryptionMaterials(ctx(eMat)); |
| 198 | + |
| 199 | + assertEquals(eMat.getEncryptionKey(), dMat.getDecryptionKey()); |
| 200 | + assertEquals(eMat.getSigningKey(), dMat.getVerificationKey()); |
| 201 | + } |
| 202 | + |
| 203 | + @Test(expected = IndexOutOfBoundsException.class) |
| 204 | + public void replicateIntermediateKeysWhenMaterialNotFoundTest() { |
| 205 | + store.replicate(MATERIAL_NAME, 0, targetStore); |
| 206 | + } |
| 207 | + |
175 | 208 | @Test |
176 | 209 | public void newProviderCollision() throws InterruptedException { |
177 | 210 | final SlowNewProvider slowProv = new SlowNewProvider(); |
@@ -207,7 +240,7 @@ private static EncryptionContext ctx(final EncryptionMaterials mat) { |
207 | 240 |
|
208 | 241 | private class SlowNewProvider extends Thread { |
209 | 242 | public volatile EncryptionMaterialsProvider result; |
210 | | - public ProviderStore slowStore = new MetaStore(client, TABLE_NAME, ENCRYPTOR) { |
| 243 | + public ProviderStore slowStore = new MetaStore(client, SOURCE_TABLE_NAME, ENCRYPTOR) { |
211 | 244 | @Override |
212 | 245 | public EncryptionMaterialsProvider newProvider(final String materialName) { |
213 | 246 | final long nextId = getMaxVersion(materialName) + 1; |
|
0 commit comments