@@ -20,62 +20,58 @@ public static void getEncryptedDataKeyDescription(
20
20
String tableName , String partitionKey , String partitionKeyVal , String sortKeyName , String sortKeyValue ,
21
21
String expectedKeyProviderId , String expectedKeyProviderInfo , String expectedBranchKeyId , String expectedBranchKeyVersion
22
22
) {
23
- DynamoDbEncryption ddbEnc = DynamoDbEncryption .builder ()
24
- .DynamoDbEncryptionConfig (DynamoDbEncryptionConfig .builder ().build ())
25
- .build ();
26
-
27
- String header_column = "aws_dbe_head" ;
28
23
24
+ // 1. Create a new AWS SDK DynamoDb client. This client will be used to get item from the DynamoDB table
29
25
DynamoDbClient ddb = DynamoDbClient .builder ()
30
- .region (Region .US_WEST_2 )
31
- .build ();
26
+ .build ();
32
27
28
+ // 2. Get item from the DynamoDB table. This item will be used to Get Encrypted DataKey Description
33
29
HashMap <String , AttributeValue > keyToGet = new HashMap <>();
34
30
keyToGet .put (partitionKey , AttributeValue .builder ()
35
31
.s (partitionKeyVal )
36
32
.build ());
37
-
38
33
keyToGet .put (sortKeyName , AttributeValue .builder ()
39
34
.n (sortKeyValue )
40
35
.build ());
41
-
42
- // ddbEnc.GetHeader(ddbEnc.GetHeaderInput.builder().build());
43
36
GetItemRequest request = GetItemRequest .builder ()
44
37
.tableName (tableName )
45
38
.key (keyToGet )
46
39
.build ();
47
-
48
40
Map <String , AttributeValue > returnedItem = ddb .getItem (request ).item ();
49
-
50
41
if (returnedItem .isEmpty ())
51
42
System .out .format ("No item found with the key %s!\n " , partitionKey );
52
43
53
- ByteBuffer header = returnedItem .get (header_column ).b ().asByteBuffer ();
54
-
44
+ // 3. Prepare the input for GetEncryptedDataKeyDescription method.
45
+ // This input can be a DynamoDB item or a header. For now, we are giving input as a DynamoDB item
46
+ // but users can also extract the header from the column "aws_dbe_head" in the DynamoDB table
47
+ // and use it for GetEncryptedDataKeyDescription method.
48
+ DynamoDbEncryption ddbEnc = DynamoDbEncryption .builder ()
49
+ .DynamoDbEncryptionConfig (DynamoDbEncryptionConfig .builder ().build ())
50
+ .build ();
55
51
GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion .builder ()
56
52
.plaintextItem (returnedItem )
57
53
.build ();
58
-
59
- // GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
60
- // .header(header)
61
- // .build();
62
-
63
- // Create input
64
54
software .amazon .cryptography .dbencryptionsdk .dynamodb .model .GetEncryptedDataKeyDescriptionInput input = GetEncryptedDataKeyDescriptionInput .builder ()
65
55
.input (InputUnion )
66
56
.build ();
67
-
68
- // Call GetHeader method
69
57
GetEncryptedDataKeyDescriptionOutput output = ddbEnc .GetEncryptedDataKeyDescription (input );
58
+
59
+ // In the following code, we are giving input as header instead of a complete DynamoDB item
60
+ // This code is provided solely to demo how the alternative approach works. So, it is commented.
70
61
71
- assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).keyProviderId ().equals (expectedKeyProviderId );
62
+ // String header_column = "aws_dbe_head";
63
+ // ByteBuffer header = returnedItem.get(header_column).b().asByteBuffer();
64
+ // GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
65
+ // .header(header)
66
+ // .build();
72
67
68
+ // Assert everything
69
+ assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).keyProviderId ().equals (expectedKeyProviderId );
73
70
if (expectedKeyProviderId .startsWith ("aws-kms" )) {
74
71
assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).keyProviderInfo ().equals (expectedKeyProviderInfo );
75
72
} else {
76
73
assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).keyProviderInfo () == expectedKeyProviderInfo ;
77
74
}
78
-
79
75
if (output .EncryptedDataKeyDescriptionOutput ().get (0 ).keyProviderId ().equals ("aws-kms-hierarchy" )) {
80
76
assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).branchKeyId ().equals (expectedBranchKeyId );
81
77
assert output .EncryptedDataKeyDescriptionOutput ().get (0 ).branchKeyVersion ().equals (expectedBranchKeyVersion );
0 commit comments