10
10
import com .amazonaws .services .dynamodbv2 .datamodeling .DynamoDBRangeKey ;
11
11
import com .amazonaws .services .dynamodbv2 .datamodeling .DynamoDBTable ;
12
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 ;
13
15
import com .amazonaws .services .dynamodbv2 .datamodeling .encryption .providers .DirectKmsMaterialProvider ;
14
16
import com .amazonaws .services .dynamodbv2 .model .AttributeValue ;
15
17
import com .amazonaws .services .kms .AWSKMS ;
16
18
import com .amazonaws .services .kms .AWSKMSClientBuilder ;
17
19
18
20
import java .security .GeneralSecurityException ;
21
+ import java .util .EnumSet ;
19
22
import java .util .HashMap ;
20
23
import java .util .Map ;
24
+ import java .util .Set ;
21
25
22
26
import static com .amazonaws .services .dynamodbv2 .datamodeling .encryption .utils .EncryptionContextOperators .overrideEncryptionContextTableNameUsingMap ;
23
27
@@ -27,30 +31,41 @@ public static void main(String[] args) throws GeneralSecurityException {
27
31
final String region = args [1 ];
28
32
final String encryptionContextTableName = args [2 ];
29
33
30
- encryptRecord (cmkArn , region , encryptionContextTableName );
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
+ }
31
48
}
32
49
33
50
public static void encryptRecord (final String cmkArn ,
34
- final String region ,
35
- final String newEncryptionContextTableName ) {
51
+ final String newEncryptionContextTableName ,
52
+ AmazonDynamoDB ddb ,
53
+ AWSKMS kms ) throws GeneralSecurityException {
36
54
// Sample object to be encrypted
37
55
ExampleItem record = new ExampleItem ();
38
56
record .setPartitionAttribute ("is this" );
39
57
record .setSortAttribute (55 );
40
58
record .setExample ("my data" );
41
59
42
60
// Set up our configuration and clients
43
- final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder .standard ().withRegion (region ).build ();
44
- final AWSKMS kms = AWSKMSClientBuilder .standard ().withRegion (region ).build ();
45
61
final DirectKmsMaterialProvider cmp = new DirectKmsMaterialProvider (kms , cmkArn );
46
- // Encryptor creation
47
62
final DynamoDBEncryptor encryptor = DynamoDBEncryptor .getInstance (cmp );
48
63
49
64
Map <String , String > tableNameEncryptionContextOverrides = new HashMap <>();
50
65
tableNameEncryptionContextOverrides .put ("ExampleTableForEncryptionContextOverrides" , newEncryptionContextTableName );
51
66
tableNameEncryptionContextOverrides .put ("AnotherExampleTableForEncryptionContextOverrides" , "this table doesn't exist" );
52
67
53
- // Here we supply an operator to override the table name used in the encryption context
68
+ // Supply an operator to override the table name used in the encryption context
54
69
encryptor .setEncryptionContextOverrideOperator (
55
70
overrideEncryptionContextTableNameUsingMap (tableNameEncryptionContextOverrides )
56
71
);
@@ -62,20 +77,40 @@ public static void encryptRecord(final String cmkArn,
62
77
.withSaveBehavior (DynamoDBMapperConfig .SaveBehavior .CLOBBER ).build ();
63
78
DynamoDBMapper mapper = new DynamoDBMapper (ddb , mapperConfig , new AttributeEncryptor (encryptor ));
64
79
65
- System .out .println ("Plaintext Record: " + record );
80
+ System .out .println ("Plaintext Record: " + record . toString () );
66
81
// Save the record to the DynamoDB table
67
82
mapper .save (record );
68
83
69
- // Retrieve the encrypted record (directly without decrypting) from Dynamo so we can see it in our example
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
70
89
final Map <String , AttributeValue > itemKey = new HashMap <>();
71
90
itemKey .put ("partition_attribute" , new AttributeValue ().withS ("is this" ));
72
91
itemKey .put ("sort_attribute" , new AttributeValue ().withN ("55" ));
73
- System .out .println ("Encrypted Record: " + ddb .getItem ("ExampleTableForEncryptionContextOverrides" ,
74
- itemKey ).getItem ());
75
92
76
- // Retrieve (and decrypt) it from DynamoDB
77
- ExampleItem decrypted_record = mapper .load (ExampleItem .class , "is this" , 55 );
78
- System .out .println ("Decrypted Record: " + decrypted_record );
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 );
79
114
}
80
115
81
116
@ DynamoDBTable (tableName = "ExampleTableForEncryptionContextOverrides" )
@@ -110,6 +145,11 @@ public String getExample() {
110
145
public void setExample (String example ) {
111
146
this .example = example ;
112
147
}
148
+
149
+ public String toString () {
150
+ return String .format ("{partition_attribute: %s, sort_attribute: %s, example: %s}" ,
151
+ partitionAttribute , sortAttribute , example );
152
+ }
113
153
}
114
154
115
155
}
0 commit comments