Skip to content

Commit 3ada1aa

Browse files
Add an example using the DynamoDBMapper
1 parent 0e1e0cf commit 3ada1aa

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.amazonaws.examples;
2+
3+
import java.security.GeneralSecurityException;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
9+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
10+
import com.amazonaws.services.dynamodbv2.datamodeling.AttributeEncryptor;
11+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
12+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
13+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
14+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
15+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.SaveBehavior;
16+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
17+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
18+
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotTouch;
19+
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DynamoDBEncryptor;
20+
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.providers.DirectKmsMaterialProvider;
21+
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
22+
import com.amazonaws.services.kms.AWSKMS;
23+
import com.amazonaws.services.kms.AWSKMSClientBuilder;
24+
25+
/**
26+
* This demonstrates how to use the {@link DynamoDBMapper} with the {@link AttributeEncryptor}
27+
* to encrypt your data. Before you can use this you need to set up a table called "ExampleTable"
28+
* to hold the encrypted data.
29+
*/
30+
public class AwsKmsEncryptedObject {
31+
public static void main(String[] args) throws GeneralSecurityException {
32+
final String cmkArn = args[0];
33+
final String region = args[1];
34+
35+
encryptRecord(cmkArn, region);
36+
}
37+
38+
public static void encryptRecord(final String cmkArn, final String region) {
39+
// Sample object to be encrypted
40+
DataPoJo record = new DataPoJo();
41+
record.setPartitionAttribute("is this");
42+
record.setSortAttribute(55);
43+
record.setExample("data");
44+
record.setSomeNumbers(99);
45+
record.setSomeBinary(new byte[]{0x00, 0x01, 0x02});
46+
record.setLeaveMe("alone");
47+
48+
// Set up our configuration and clients
49+
final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.standard().withRegion(region).build();
50+
final AWSKMS kms = AWSKMSClientBuilder.standard().withRegion(region).build();
51+
final DirectKmsMaterialProvider cmp = new DirectKmsMaterialProvider(kms, cmkArn);
52+
// Encryptor creation
53+
final DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(cmp);
54+
// Mapper Creation
55+
DynamoDBMapperConfig mapperConfig = DynamoDBMapperConfig.builder().withSaveBehavior(SaveBehavior.CLOBBER).build();
56+
DynamoDBMapper mapper = new DynamoDBMapper(ddb, mapperConfig, new AttributeEncryptor(encryptor));
57+
58+
System.out.println("Plaintext Record: " + record);
59+
// Save the item to the DynamoDB table
60+
mapper.save(record);
61+
62+
// Retrieve the encrypted item (directly without decrypting) from Dynamo so we can see it in our example
63+
final Map<String, AttributeValue> itemKey = new HashMap<>();
64+
itemKey.put("partition_attribute", new AttributeValue().withS("is this"));
65+
itemKey.put("sort_attribute", new AttributeValue().withN("55"));
66+
System.out.println("Encrypted Record: " + ddb.getItem("ExampleTable", itemKey).getItem());
67+
68+
// Retrieve (and decrypt) it from DynamoDB
69+
DataPoJo decrypted_record = mapper.load(DataPoJo.class, "is this", 55);
70+
System.out.println("Decrypted Record: " + decrypted_record);
71+
}
72+
73+
@DynamoDBTable(tableName = "ExampleTable")
74+
public static final class DataPoJo {
75+
private String partitionAttribute;
76+
private int sortAttribute;
77+
private String example;
78+
private long someNumbers;
79+
private byte[] someBinary;
80+
private String leaveMe;
81+
82+
@DynamoDBHashKey(attributeName = "partition_attribute")
83+
public String getPartitionAttribute() {
84+
return partitionAttribute;
85+
}
86+
87+
public void setPartitionAttribute(String partitionAttribute) {
88+
this.partitionAttribute = partitionAttribute;
89+
}
90+
91+
@DynamoDBRangeKey(attributeName = "sort_attribute")
92+
public int getSortAttribute() {
93+
return sortAttribute;
94+
}
95+
96+
public void setSortAttribute(int sortAttribute) {
97+
this.sortAttribute = sortAttribute;
98+
}
99+
100+
@DynamoDBAttribute(attributeName = "example")
101+
public String getExample() {
102+
return example;
103+
}
104+
105+
public void setExample(String example) {
106+
this.example = example;
107+
}
108+
109+
@DynamoDBAttribute(attributeName = "some numbers")
110+
public long getSomeNumbers() {
111+
return someNumbers;
112+
}
113+
114+
public void setSomeNumbers(long someNumbers) {
115+
this.someNumbers = someNumbers;
116+
}
117+
118+
@DynamoDBAttribute(attributeName = "and some binary")
119+
public byte[] getSomeBinary() {
120+
return someBinary;
121+
}
122+
123+
public void setSomeBinary(byte[] someBinary) {
124+
this.someBinary = someBinary;
125+
}
126+
127+
@DynamoDBAttribute(attributeName = "leave me")
128+
@DoNotTouch
129+
public String getLeaveMe() {
130+
return leaveMe;
131+
}
132+
133+
public void setLeaveMe(String leaveMe) {
134+
this.leaveMe = leaveMe;
135+
}
136+
137+
@Override
138+
public String toString() {
139+
return "DataPoJo [partitionAttribute=" + partitionAttribute + ", sortAttribute="
140+
+ sortAttribute + ", example=" + example + ", someNumbers=" + someNumbers
141+
+ ", someBinary=" + Arrays.toString(someBinary) + ", leaveMe=" + leaveMe + "]";
142+
}
143+
144+
145+
}
146+
}

0 commit comments

Comments
 (0)