Skip to content

Commit 42b503e

Browse files
auto commit
1 parent 9c3e159 commit 42b503e

File tree

3 files changed

+80
-19
lines changed

3 files changed

+80
-19
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626
is provided in CLI arguments.
2727
This table must be configured with the following
2828
primary key configuration:
29-
- Partition key is named "partition_key" with type (S)
30-
- Sort key is named "sort_key" with type (N)
29+
- Partition key is named `partitionKeyName` with type (S)
30+
- Sort key is named `sortKeyName` with type (N)
3131
*/
3232
public class BasicPutGetExample {
3333

3434
public static void PutItemGetItem(
35-
String kmsKeyId,
36-
String ddbTableName,
37-
String PartitionKeyName
35+
final String kmsKeyId,
36+
final String ddbTableName,
37+
final String partitionKeyName,
38+
final String sortKeyName,
39+
final String partitionKeyValue,
40+
final String sortKeyValue
3841
) {
3942
// 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
4043
// For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use.
@@ -57,8 +60,8 @@ public static void PutItemGetItem(
5760
// - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
5861
// - DO_NOTHING: The attribute is not encrypted and not included in the signature
5962
final Map<String, CryptoAction> attributeActionsOnEncrypt = new HashMap<>();
60-
attributeActionsOnEncrypt.put("partition_key", CryptoAction.SIGN_ONLY); // Our partition attribute must be SIGN_ONLY
61-
attributeActionsOnEncrypt.put("sort_key", CryptoAction.SIGN_ONLY); // Our sort attribute must be SIGN_ONLY
63+
attributeActionsOnEncrypt.put(partitionKeyName, CryptoAction.SIGN_ONLY); // Our partition attribute must be SIGN_ONLY
64+
attributeActionsOnEncrypt.put(sortKeyName, CryptoAction.SIGN_ONLY); // Our sort attribute must be SIGN_ONLY
6265
attributeActionsOnEncrypt.put("attribute1", CryptoAction.ENCRYPT_AND_SIGN);
6366
attributeActionsOnEncrypt.put("attribute2", CryptoAction.SIGN_ONLY);
6467
attributeActionsOnEncrypt.put(":attribute3", CryptoAction.DO_NOTHING);
@@ -99,8 +102,8 @@ public static void PutItemGetItem(
99102
final DynamoDbTableEncryptionConfig config = DynamoDbTableEncryptionConfig
100103
.builder()
101104
.logicalTableName(ddbTableName)
102-
.partitionKeyName("partition_key")
103-
.sortKeyName("sort_key")
105+
.partitionKeyName(partitionKeyName)
106+
.sortKeyName(sortKeyName)
104107
.attributeActionsOnEncrypt(attributeActionsOnEncrypt)
105108
.keyring(kmsKeyring)
106109
.allowedUnsignedAttributePrefix(unsignAttrPrefix)
@@ -146,10 +149,10 @@ public static void PutItemGetItem(
146149
// client-side, according to our configuration.
147150
final HashMap<String, AttributeValue> item = new HashMap<>();
148151
item.put(
149-
"partition_key",
150-
AttributeValue.builder().s(PartitionKeyName).build()
152+
partitionKeyName,
153+
AttributeValue.builder().s(partitionKeyValue).build()
151154
);
152-
item.put("sort_key", AttributeValue.builder().n("0").build());
155+
item.put(sortKeyName, AttributeValue.builder().n(sortKeyValue).build());
153156
item.put(
154157
"attribute1",
155158
AttributeValue.builder().s("encrypt and sign me!").build()
@@ -173,10 +176,10 @@ public static void PutItemGetItem(
173176
// back the original item.
174177
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
175178
keyToGet.put(
176-
"partition_key",
177-
AttributeValue.builder().s(PartitionKeyName).build()
179+
partitionKeyName,
180+
AttributeValue.builder().s(partitionKeyValue).build()
178181
);
179-
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
182+
keyToGet.put(sortKeyName, AttributeValue.builder().n(sortKeyValue).build());
180183

181184
final GetItemRequest getRequest = GetItemRequest
182185
.builder()
@@ -199,13 +202,19 @@ public static void PutItemGetItem(
199202
}
200203

201204
public static void main(final String[] args) {
202-
if (args.length < 2) {
205+
if (args.length < 6) {
203206
throw new IllegalArgumentException(
204-
"To run this example, include the kmsKeyId as args[0] and ddbTableName as args[1]"
207+
"To run this example, include the kmsKeyId as args[0], ddbTableName as args[1],"
208+
+ " partitionKeyName as args[2], sortKeyName as args[3], partitionKeyValue as args[4]"
209+
+ " sortKeyValue as args[5]"
205210
);
206211
}
207212
final String kmsKeyId = args[0];
208213
final String ddbTableName = args[1];
209-
PutItemGetItem(kmsKeyId, ddbTableName, "BasicPutGetExample");
214+
final String partitionKeyName = args[2];
215+
final String sortKeyName = args[3];
216+
final String partitionKeyValue = args[4];
217+
final String sortKeyValue = args[5];
218+
PutItemGetItem(kmsKeyId, ddbTableName, partitionKeyName, sortKeyName, partitionKeyValue, sortKeyValue);
210219
}
211220
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/TestBasicPutGetExample.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ public class TestBasicPutGetExample {
77

88
@Test
99
public void TestPutGet() {
10+
final String partitionKeyValue = "BasicPutGetExample" + UUID.randomUUID();
1011
BasicPutGetExample.PutItemGetItem(
1112
TestUtils.TEST_KMS_KEY_ID,
1213
TestUtils.TEST_DDB_TABLE_NAME,
13-
"BasicPutGetExample" + UUID.randomUUID()
14+
"partition_key",
15+
"sort_key",
16+
partitionKeyValue,
17+
"0"
18+
);
19+
TestUtils.cleanUpDDBItem(
20+
TestUtils.TEST_DDB_TABLE_NAME,
21+
"partition_key",
22+
"sort_key",
23+
partitionKeyValue,
24+
"0"
1425
);
1526
}
1627
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/TestUtils.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package software.amazon.cryptography.examples;
22

3+
import java.util.HashMap;
4+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
5+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
6+
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
7+
38
public class TestUtils {
49

510
public static final String TEST_KEYSTORE_NAME = "KeyStoreDdbTable";
@@ -30,4 +35,40 @@ public class TestUtils {
3035
// Our tests require access to DDB Table with this name
3136
public static final String TEST_DDB_TABLE_NAME =
3237
"DynamoDbEncryptionInterceptorTestTable";
38+
39+
/**
40+
* Deletes an item from a DynamoDB table.
41+
*
42+
* @param tableName The name of the DynamoDB table
43+
* @param partitionKeyName The name of partition key
44+
* @param sortKeyName The name of sort key
45+
* @param partitionKeyValue The value of the partition key
46+
* @param sortKeyValue The value of the sort key (can be null if table doesn't have a sort key)
47+
*/
48+
public static void cleanUpDDBItem(
49+
final String tableName,
50+
final String partitionKeyName,
51+
final String sortKeyName,
52+
final String partitionKeyValue,
53+
final String sortKeyValue
54+
) {
55+
final DynamoDbClient ddb = DynamoDbClient.builder().build();
56+
final HashMap<String, AttributeValue> keyToDelete = new HashMap<>();
57+
keyToDelete.put(
58+
partitionKeyName,
59+
AttributeValue.builder().s(partitionKeyValue).build()
60+
);
61+
if (sortKeyValue != null) {
62+
keyToDelete.put(
63+
sortKeyName,
64+
AttributeValue.builder().n(sortKeyValue).build()
65+
);
66+
}
67+
final DeleteItemRequest deleteRequest = DeleteItemRequest
68+
.builder()
69+
.tableName(tableName)
70+
.key(keyToDelete)
71+
.build();
72+
ddb.deleteItem(deleteRequest);
73+
}
3374
}

0 commit comments

Comments
 (0)