Skip to content

Commit d73839a

Browse files
Add comments to example
1 parent 75ac9a4 commit d73839a

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/GetEncryptedDataKeyDescriptionExample.java

+20-24
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,58 @@ public static void getEncryptedDataKeyDescription(
2020
String tableName, String partitionKey, String partitionKeyVal, String sortKeyName, String sortKeyValue,
2121
String expectedKeyProviderId, String expectedKeyProviderInfo, String expectedBranchKeyId, String expectedBranchKeyVersion
2222
) {
23-
DynamoDbEncryption ddbEnc = DynamoDbEncryption.builder()
24-
.DynamoDbEncryptionConfig(DynamoDbEncryptionConfig.builder().build())
25-
.build();
26-
27-
String header_column = "aws_dbe_head";
2823

24+
// 1. Create a new AWS SDK DynamoDb client. This client will be used to get item from the DynamoDB table
2925
DynamoDbClient ddb = DynamoDbClient.builder()
30-
.region(Region.US_WEST_2)
31-
.build();
26+
.build();
3227

28+
// 2. Get item from the DynamoDB table. This item will be used to Get Encrypted DataKey Description
3329
HashMap<String, AttributeValue> keyToGet = new HashMap<>();
3430
keyToGet.put(partitionKey, AttributeValue.builder()
3531
.s(partitionKeyVal)
3632
.build());
37-
3833
keyToGet.put(sortKeyName, AttributeValue.builder()
3934
.n(sortKeyValue)
4035
.build());
41-
42-
// ddbEnc.GetHeader(ddbEnc.GetHeaderInput.builder().build());
4336
GetItemRequest request = GetItemRequest.builder()
4437
.tableName(tableName)
4538
.key(keyToGet)
4639
.build();
47-
4840
Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
49-
5041
if (returnedItem.isEmpty())
5142
System.out.format("No item found with the key %s!\n", partitionKey);
5243

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();
5551
GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
5652
.plaintextItem(returnedItem)
5753
.build();
58-
59-
// GetEncryptedDataKeyDescriptionUnion InputUnion = GetEncryptedDataKeyDescriptionUnion.builder()
60-
// .header(header)
61-
// .build();
62-
63-
// Create input
6454
software.amazon.cryptography.dbencryptionsdk.dynamodb.model.GetEncryptedDataKeyDescriptionInput input = GetEncryptedDataKeyDescriptionInput.builder()
6555
.input(InputUnion)
6656
.build();
67-
68-
// Call GetHeader method
6957
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.
7061

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();
7267

68+
// Assert everything
69+
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderId().equals(expectedKeyProviderId);
7370
if(expectedKeyProviderId.startsWith("aws-kms")) {
7471
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderInfo().equals(expectedKeyProviderInfo);
7572
} else {
7673
assert output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderInfo() == expectedKeyProviderInfo;
7774
}
78-
7975
if(output.EncryptedDataKeyDescriptionOutput().get(0).keyProviderId().equals("aws-kms-hierarchy")) {
8076
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyId().equals(expectedBranchKeyId);
8177
assert output.EncryptedDataKeyDescriptionOutput().get(0).branchKeyVersion().equals(expectedBranchKeyVersion);

Examples/runtimes/net/src/GetEncryptedDataKeyDescriptionExample.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static async Task GetEncryptedDataKeyDescription()
1414
var kmsKeyId = TestUtils.TEST_KMS_KEY_ID;
1515
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
1616
var ddbEnc = new DynamoDbEncryption(new DynamoDbEncryptionConfig());
17-
string header_column = "aws_dbe_head";
1817

1918
// 1. Define keys that will be used to retrieve item from the DynamoDB table.
2019
var keyToGet = new Dictionary<String, AttributeValue>
@@ -32,14 +31,22 @@ public static async Task GetEncryptedDataKeyDescription()
3231
};
3332
GetItemResponse getResponse = await ddb.GetItemAsync(getRequest);
3433

35-
// Demonstrate that PutItem succeeded
34+
// Demonstrate that GetItem succeeded
3635
Debug.Assert(getResponse.HttpStatusCode == HttpStatusCode.OK);
3736

3837
// 3. Extract the item from the dynamoDB table and prepare input for the GetEncryptedDataKeyDescription method.
39-
// Here, we are sending header as the input by getting it from "aws_dbe_head" attribute but you can send a complete DDB item as well.
38+
// Here, we are sending dynamodb item but you can also input the header itself by extracting the header from
39+
// "aws_dbe_head" attribute in the dynamoDB item. The part of the code where we send input as the header is commented.
4040
var returnedItem = getResponse.Item;
4141
GetEncryptedDataKeyDescriptionUnion InputUnion = new GetEncryptedDataKeyDescriptionUnion();
42-
InputUnion.Header = returnedItem[header_column].B;
42+
InputUnion.PlaintextItem = returnedItem;
43+
44+
// The code below shows how we can send header as the input to the DynamoDB. This code is written to demo the
45+
// alternative approach. So, it is commented.
46+
47+
// string header_column = "aws_dbe_head";
48+
// InputUnion.Header = returnedItem[header_column].B;
49+
4350
GetEncryptedDataKeyDescriptionInput Input = new GetEncryptedDataKeyDescriptionInput();
4451
Input.Input = InputUnion;
4552
GetEncryptedDataKeyDescriptionOutput output = ddbEnc.GetEncryptedDataKeyDescription(Input);

0 commit comments

Comments
 (0)