|
| 1 | +package com.amazonaws.examples; |
| 2 | + |
| 3 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 4 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; |
| 5 | +import com.amazonaws.services.dynamodbv2.datamodeling.AttributeEncryptor; |
| 6 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; |
| 7 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; |
| 8 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
| 9 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig; |
| 10 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey; |
| 11 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; |
| 12 | +import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DynamoDBEncryptor; |
| 13 | +import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionContext; |
| 14 | +import com.amazonaws.services.dynamodbv2.datamodeling.encryption.EncryptionFlags; |
| 15 | +import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.DirectKmsMaterialProvider; |
| 16 | +import com.amazonaws.services.dynamodbv2.model.AttributeValue; |
| 17 | +import com.amazonaws.services.kms.AWSKMS; |
| 18 | +import com.amazonaws.services.kms.AWSKMSClientBuilder; |
| 19 | + |
| 20 | +import java.security.GeneralSecurityException; |
| 21 | +import java.util.EnumSet; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.utils.EncryptionContextOperators.overrideEncryptionContextTableNameUsingMap; |
| 27 | + |
| 28 | +public class EncryptionContextOverridesWithDynamoDBMapper { |
| 29 | + public static void main(String[] args) throws GeneralSecurityException { |
| 30 | + final String cmkArn = args[0]; |
| 31 | + final String region = args[1]; |
| 32 | + final String encryptionContextTableName = args[2]; |
| 33 | + |
| 34 | + AmazonDynamoDB ddb = null; |
| 35 | + AWSKMS kms = null; |
| 36 | + try { |
| 37 | + ddb = AmazonDynamoDBClientBuilder.standard().withRegion(region).build(); |
| 38 | + kms = AWSKMSClientBuilder.standard().withRegion(region).build(); |
| 39 | + encryptRecord(cmkArn, encryptionContextTableName, ddb, kms); |
| 40 | + } finally { |
| 41 | + if (ddb != null) { |
| 42 | + ddb.shutdown(); |
| 43 | + } |
| 44 | + if (kms != null) { |
| 45 | + kms.shutdown(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void encryptRecord(final String cmkArn, |
| 51 | + final String newEncryptionContextTableName, |
| 52 | + AmazonDynamoDB ddb, |
| 53 | + AWSKMS kms) throws GeneralSecurityException { |
| 54 | + // Sample object to be encrypted |
| 55 | + ExampleItem record = new ExampleItem(); |
| 56 | + record.setPartitionAttribute("is this"); |
| 57 | + record.setSortAttribute(55); |
| 58 | + record.setExample("my data"); |
| 59 | + |
| 60 | + // Set up our configuration and clients |
| 61 | + final DirectKmsMaterialProvider cmp = new DirectKmsMaterialProvider(kms, cmkArn); |
| 62 | + final DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(cmp); |
| 63 | + |
| 64 | + Map<String, String> tableNameEncryptionContextOverrides = new HashMap<>(); |
| 65 | + tableNameEncryptionContextOverrides.put("ExampleTableForEncryptionContextOverrides", newEncryptionContextTableName); |
| 66 | + tableNameEncryptionContextOverrides.put("AnotherExampleTableForEncryptionContextOverrides", "this table doesn't exist"); |
| 67 | + |
| 68 | + // Supply an operator to override the table name used in the encryption context |
| 69 | + encryptor.setEncryptionContextOverrideOperator( |
| 70 | + overrideEncryptionContextTableNameUsingMap(tableNameEncryptionContextOverrides) |
| 71 | + ); |
| 72 | + |
| 73 | + // Mapper Creation |
| 74 | + // Please note the use of SaveBehavior.PUT (SaveBehavior.CLOBBER works as well). |
| 75 | + // Omitting this can result in data-corruption. |
| 76 | + DynamoDBMapperConfig mapperConfig = DynamoDBMapperConfig.builder() |
| 77 | + .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.PUT).build(); |
| 78 | + DynamoDBMapper mapper = new DynamoDBMapper(ddb, mapperConfig, new AttributeEncryptor(encryptor)); |
| 79 | + |
| 80 | + System.out.println("Plaintext Record: " + record.toString()); |
| 81 | + // Save the record to the DynamoDB table |
| 82 | + mapper.save(record); |
| 83 | + |
| 84 | + // Retrieve (and decrypt) it from DynamoDB |
| 85 | + ExampleItem decrypted_record = mapper.load(ExampleItem.class, "is this", 55); |
| 86 | + System.out.println("Decrypted Record: " + decrypted_record.toString()); |
| 87 | + |
| 88 | + // Setup new configuration to decrypt without using an overridden EncryptionContext |
| 89 | + final Map<String, AttributeValue> itemKey = new HashMap<>(); |
| 90 | + itemKey.put("partition_attribute", new AttributeValue().withS("is this")); |
| 91 | + itemKey.put("sort_attribute", new AttributeValue().withN("55")); |
| 92 | + |
| 93 | + final EnumSet<EncryptionFlags> signOnly = EnumSet.of(EncryptionFlags.SIGN); |
| 94 | + final EnumSet<EncryptionFlags> encryptAndSign = EnumSet.of(EncryptionFlags.ENCRYPT, EncryptionFlags.SIGN); |
| 95 | + final Map<String, AttributeValue> encryptedItem = ddb.getItem("ExampleTableForEncryptionContextOverrides", itemKey) |
| 96 | + .getItem(); |
| 97 | + System.out.println("Encrypted Record: " + encryptedItem); |
| 98 | + |
| 99 | + Map<String, Set<EncryptionFlags>> encryptionFlags = new HashMap<>(); |
| 100 | + encryptionFlags.put("partition_attribute", signOnly); |
| 101 | + encryptionFlags.put("sort_attribute", signOnly); |
| 102 | + encryptionFlags.put("example", encryptAndSign); |
| 103 | + |
| 104 | + final DynamoDBEncryptor encryptorWithoutOverrides = DynamoDBEncryptor.getInstance(cmp); |
| 105 | + |
| 106 | + // Decrypt the record without using an overridden EncryptionContext |
| 107 | + encryptorWithoutOverrides.decryptRecord(encryptedItem, |
| 108 | + encryptionFlags, |
| 109 | + new EncryptionContext.Builder().withHashKeyName("partition_attribute") |
| 110 | + .withRangeKeyName("sort_attribute") |
| 111 | + .withTableName(newEncryptionContextTableName) |
| 112 | + .build()); |
| 113 | + System.out.printf("The example item was encrypted using the table name '%s' in the EncryptionContext%n", newEncryptionContextTableName); |
| 114 | + } |
| 115 | + |
| 116 | + @DynamoDBTable(tableName = "ExampleTableForEncryptionContextOverrides") |
| 117 | + public static final class ExampleItem { |
| 118 | + private String partitionAttribute; |
| 119 | + private int sortAttribute; |
| 120 | + private String example; |
| 121 | + |
| 122 | + @DynamoDBHashKey(attributeName = "partition_attribute") |
| 123 | + public String getPartitionAttribute() { |
| 124 | + return partitionAttribute; |
| 125 | + } |
| 126 | + |
| 127 | + public void setPartitionAttribute(String partitionAttribute) { |
| 128 | + this.partitionAttribute = partitionAttribute; |
| 129 | + } |
| 130 | + |
| 131 | + @DynamoDBRangeKey(attributeName = "sort_attribute") |
| 132 | + public int getSortAttribute() { |
| 133 | + return sortAttribute; |
| 134 | + } |
| 135 | + |
| 136 | + public void setSortAttribute(int sortAttribute) { |
| 137 | + this.sortAttribute = sortAttribute; |
| 138 | + } |
| 139 | + |
| 140 | + @DynamoDBAttribute(attributeName = "example") |
| 141 | + public String getExample() { |
| 142 | + return example; |
| 143 | + } |
| 144 | + |
| 145 | + public void setExample(String example) { |
| 146 | + this.example = example; |
| 147 | + } |
| 148 | + |
| 149 | + public String toString() { |
| 150 | + return String.format("{partition_attribute: %s, sort_attribute: %s, example: %s}", |
| 151 | + partitionAttribute, sortAttribute, example); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments