Skip to content

Commit 09b6134

Browse files
Add GetEncryptedDataKeyDescription Example
1 parent 5d0a9fb commit 09b6134

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package software.amazon.cryptography.examples;
2+
3+
import software.amazon.awssdk.regions.Region;
4+
import software.amazon.awssdk.services.dynamodb.model.*;
5+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
6+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.DynamoDbEncryption;
7+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbEncryptionConfig;
8+
9+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.GetEncryptedDataKeyDescriptionUnion;
10+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.GetEncryptedDataKeyDescriptionInput;
11+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.GetEncryptedDataKeyDescriptionOutput;
12+
import java.nio.ByteBuffer;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import java.util.*;
17+
18+
public class GetEncryptedDataKeyDescriptionExample{
19+
public static void getEncryptedDataKeyDescription(
20+
String tableName, String partitionKey, String partitionKeyVal, String sortKeyName, String sortKeyValue,
21+
String expectedKeyProviderId, String expectedKeyProviderInfo, String expectedBranchKeyId, String expectedBranchKeyVersion
22+
) {
23+
DynamoDbEncryption ddbEnc = DynamoDbEncryption.builder()
24+
.DynamoDbEncryptionConfig(DynamoDbEncryptionConfig.builder().build())
25+
.build();
26+
27+
String header_column = "aws_dbe_head";
28+
29+
DynamoDbClient ddb = DynamoDbClient.builder()
30+
.region(Region.US_WEST_2)
31+
.build();
32+
33+
HashMap<String, AttributeValue> keyToGet = new HashMap<>();
34+
keyToGet.put(partitionKey, AttributeValue.builder()
35+
.s(partitionKeyVal)
36+
.build());
37+
38+
keyToGet.put(sortKeyName, AttributeValue.builder()
39+
.n(sortKeyValue)
40+
.build());
41+
42+
// ddbEnc.GetHeader(ddbEnc.GetHeaderInput.builder().build());
43+
GetItemRequest request = GetItemRequest.builder()
44+
.tableName(tableName)
45+
.key(keyToGet)
46+
.build();
47+
48+
Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
49+
50+
if (returnedItem.isEmpty())
51+
System.out.format("No item found with the key %s!\n", partitionKey);
52+
53+
ByteBuffer header = returnedItem.get(header_column).b().asByteBuffer();
54+
55+
GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
56+
.plaintextItem(returnedItem)
57+
.build();
58+
59+
// GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
60+
// .header(header)
61+
// .build();
62+
63+
// Create input
64+
software.amazon.cryptography.dbencryptionsdk.dynamodb.model.GetEncryptedDataKeyDescriptionInput input = GetEncryptedDataKeyDescriptionInput.builder()
65+
.input(InputUnion)
66+
.build();
67+
68+
// Call GetHeader method
69+
GetEncryptedDataKeyDescriptionOutput output = ddbEnc.GetEncryptedDataKeyDescription(input);
70+
71+
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderId().equals(expectedKeyProviderId);
72+
73+
if(expectedKeyProviderId.startsWith("aws-kms")) {
74+
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderInfo().equals(expectedKeyProviderInfo);
75+
} else {
76+
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderInfo() == expectedKeyProviderInfo;
77+
}
78+
79+
if(output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderId().equals("aws-kms-hierarchy")) {
80+
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyId().equals(expectedBranchKeyId);
81+
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyVersion().equals(expectedBranchKeyVersion);
82+
} else {
83+
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyId() == expectedBranchKeyId;
84+
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyVersion() == expectedBranchKeyVersion;
85+
}
86+
}
87+
public static void main(final String[] args) {
88+
if (args.length < 9) {
89+
throw new IllegalArgumentException("To run this example, include the tableName, partitionKey, partitionKeyVal,"
90+
+ "sortKeyName, sortKeyValue, expectedKeyProviderId, expectedKeyProviderInfo, expectedBranchKeyId and expectedBranchKeyVersion in args");
91+
}
92+
final String tableName = args[0];
93+
final String partitionKey = args[1];
94+
final String partitionKeyVal = args[2];
95+
final String sortKeyName = args[3];
96+
final String sortKeyValue = args[4];
97+
final String expectedKeyProviderId = args[5];
98+
final String expectedKeyProviderInfo = args[6];
99+
final String expectedBranchKeyId = args[7];
100+
final String expectedBranchKeyVersion = args[8];
101+
getEncryptedDataKeyDescription(tableName, partitionKey, partitionKeyVal, sortKeyName, sortKeyValue, expectedKeyProviderId, expectedKeyProviderInfo, expectedBranchKeyId, expectedBranchKeyVersion);
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package software.amazon.cryptography.examples;
2+
3+
import org.testng.annotations.Test;
4+
5+
public class TestGetEncryptedDataKeyDescriptionExample {
6+
@Test
7+
public void TestGetEncryptedDataKeyDescription() {
8+
GetEncryptedDataKeyDescriptionExample.getEncryptedDataKeyDescription(TestUtils.TEST_DDB_TABLE_NAME,"partition_key","BasicPutGetExample","sort_key","0","aws-kms",TestUtils.TEST_KMS_KEY_ID,null,null);
9+
}
10+
}

0 commit comments

Comments
 (0)