Skip to content

chore(java): Parallelize Java Examples #1940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci_examples_java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ on:
jobs:
testJava:
strategy:
max-parallel: 1
matrix:
java-version: [8, 11, 16, 17]
os: [macos-13]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@
is provided in CLI arguments.
This table must be configured with the following
primary key configuration:
- Partition key is named "partition_key" with type (S)
- Sort key is named "sort_key" with type (N)
- Partition key is named `partitionKeyName` with type (S)
- Sort key is named `sortKeyName` with type (N)
*/
public class BasicPutGetExample {

public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
public static void PutItemGetItem(
final String kmsKeyId,
final String ddbTableName,
final String partitionKeyName,
final String sortKeyName,
final String partitionKeyValue,
final String sortKeyValue
) {
// 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
// For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use.
// We will use the `CreateMrkMultiKeyring` method to create this keyring,
Expand All @@ -52,8 +59,8 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
// - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
// - DO_NOTHING: The attribute is not encrypted and not included in the signature
final Map<String, CryptoAction> attributeActionsOnEncrypt = new HashMap<>();
attributeActionsOnEncrypt.put("partition_key", CryptoAction.SIGN_ONLY); // Our partition attribute must be SIGN_ONLY
attributeActionsOnEncrypt.put("sort_key", CryptoAction.SIGN_ONLY); // Our sort attribute must be SIGN_ONLY
attributeActionsOnEncrypt.put(partitionKeyName, CryptoAction.SIGN_ONLY); // Our partition attribute must be SIGN_ONLY
attributeActionsOnEncrypt.put(sortKeyName, CryptoAction.SIGN_ONLY); // Our sort attribute must be SIGN_ONLY
attributeActionsOnEncrypt.put("attribute1", CryptoAction.ENCRYPT_AND_SIGN);
attributeActionsOnEncrypt.put("attribute2", CryptoAction.SIGN_ONLY);
attributeActionsOnEncrypt.put(":attribute3", CryptoAction.DO_NOTHING);
Expand Down Expand Up @@ -94,8 +101,8 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
final DynamoDbTableEncryptionConfig config = DynamoDbTableEncryptionConfig
.builder()
.logicalTableName(ddbTableName)
.partitionKeyName("partition_key")
.sortKeyName("sort_key")
.partitionKeyName(partitionKeyName)
.sortKeyName(sortKeyName)
.attributeActionsOnEncrypt(attributeActionsOnEncrypt)
.keyring(kmsKeyring)
.allowedUnsignedAttributePrefix(unsignAttrPrefix)
Expand Down Expand Up @@ -141,10 +148,10 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
// client-side, according to our configuration.
final HashMap<String, AttributeValue> item = new HashMap<>();
item.put(
"partition_key",
AttributeValue.builder().s("BasicPutGetExample").build()
partitionKeyName,
AttributeValue.builder().s(partitionKeyValue).build()
);
item.put("sort_key", AttributeValue.builder().n("0").build());
item.put(sortKeyName, AttributeValue.builder().n(sortKeyValue).build());
item.put(
"attribute1",
AttributeValue.builder().s("encrypt and sign me!").build()
Expand All @@ -168,10 +175,10 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
// back the original item.
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
keyToGet.put(
"partition_key",
AttributeValue.builder().s("BasicPutGetExample").build()
partitionKeyName,
AttributeValue.builder().s(partitionKeyValue).build()
);
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
keyToGet.put(sortKeyName, AttributeValue.builder().n(sortKeyValue).build());

final GetItemRequest getRequest = GetItemRequest
.builder()
Expand All @@ -194,13 +201,26 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
}

public static void main(final String[] args) {
if (args.length < 2) {
if (args.length < 6) {
throw new IllegalArgumentException(
"To run this example, include the kmsKeyId as args[0] and ddbTableName as args[1]"
"To run this example, include the kmsKeyId as args[0], ddbTableName as args[1]," +
" partitionKeyName as args[2], sortKeyName as args[3], partitionKeyValue as args[4]" +
" sortKeyValue as args[5]"
);
}
final String kmsKeyId = args[0];
final String ddbTableName = args[1];
PutItemGetItem(kmsKeyId, ddbTableName);
final String partitionKeyName = args[2];
final String sortKeyName = args[3];
final String partitionKeyValue = args[4];
final String sortKeyValue = args[5];
PutItemGetItem(
kmsKeyId,
ddbTableName,
partitionKeyName,
sortKeyName,
partitionKeyValue,
sortKeyValue
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package software.amazon.cryptography.examples;

import java.util.UUID;
import org.testng.annotations.Test;

public class TestBasicPutGetExample {

@Test
public void TestPutGet() {
final String partitionKeyValue = "BasicPutGetExample" + UUID.randomUUID();
BasicPutGetExample.PutItemGetItem(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME
TestUtils.TEST_DDB_TABLE_NAME,
"partition_key",
"sort_key",
partitionKeyValue,
"0"
);
TestUtils.cleanUpDDBItem(
TestUtils.TEST_DDB_TABLE_NAME,
"partition_key",
"sort_key",
partitionKeyValue,
"0"
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package software.amazon.cryptography.examples;

import java.util.HashMap;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;

public class TestUtils {

public static final String TEST_KEYSTORE_NAME = "KeyStoreDdbTable";
Expand Down Expand Up @@ -30,4 +35,40 @@ public class TestUtils {
// Our tests require access to DDB Table with this name
public static final String TEST_DDB_TABLE_NAME =
"DynamoDbEncryptionInterceptorTestTable";

/**
* Deletes an item from a DynamoDB table.
*
* @param tableName The name of the DynamoDB table
* @param partitionKeyName The name of partition key
* @param sortKeyName The name of sort key
* @param partitionKeyValue The value of the partition key
* @param sortKeyValue The value of the sort key (can be null if table doesn't have a sort key)
*/
public static void cleanUpDDBItem(
final String tableName,
final String partitionKeyName,
final String sortKeyName,
final String partitionKeyValue,
final String sortKeyValue
) {
final DynamoDbClient ddb = DynamoDbClient.builder().build();
final HashMap<String, AttributeValue> keyToDelete = new HashMap<>();
keyToDelete.put(
partitionKeyName,
AttributeValue.builder().s(partitionKeyValue).build()
);
if (sortKeyValue != null) {
keyToDelete.put(
sortKeyName,
AttributeValue.builder().n(sortKeyValue).build()
);
}
final DeleteItemRequest deleteRequest = DeleteItemRequest
.builder()
.tableName(tableName)
.key(keyToDelete)
.build();
ddb.deleteItem(deleteRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class MigrationExampleStep1 {
public static void MigrationStep1(
String kmsKeyId,
String ddbTableName,
int sortReadValue
int sortReadValue,
String partitionKey
) {
// 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
// We will use the `CreateMrkMultiKeyring` method to create this keyring,
Expand Down Expand Up @@ -143,7 +144,7 @@ public static void MigrationStep1(
// 7. Put an item into your table using the DynamoDb Enhanced Client.
// This item will be stored in plaintext.
final SimpleClass item = new SimpleClass();
item.setPartitionKey("PlaintextMigrationExample");
item.setPartitionKey(partitionKey);
item.setSortKey(1);
item.setAttribute1("this will be encrypted and signed");
item.setAttribute3("this will never be encrypted nor signed");
Expand All @@ -158,13 +159,13 @@ public static void MigrationStep1(
// during Step 2 or after), then the item will be decrypted client-side
// and surfaced as a plaintext item.
SimpleClass itemToGet = new SimpleClass();
itemToGet.setPartitionKey("PlaintextMigrationExample");
itemToGet.setPartitionKey(partitionKey);
itemToGet.setSortKey(sortReadValue);

SimpleClass returnedItem = table.getItem(itemToGet);

// Demonstrate we get the expected item back
assert returnedItem.getPartitionKey().equals("PlaintextMigrationExample");
assert returnedItem.getPartitionKey().equals(partitionKey);
assert returnedItem
.getAttribute1()
.equals("this will be encrypted and signed");
Expand All @@ -180,6 +181,7 @@ public static void main(final String[] args) {
final String ddbTableName = args[1];
// You can manipulate this value to demonstrate reading records written in other steps
final int sortReadValue = Integer.parseInt(args[2]);
MigrationStep1(kmsKeyId, ddbTableName, sortReadValue);
final String partitionKey = args[3];
MigrationStep1(kmsKeyId, ddbTableName, sortReadValue, partitionKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class MigrationExampleStep2 {
public static void MigrationStep2(
String kmsKeyId,
String ddbTableName,
int sortReadValue
int sortReadValue,
String partitionKey
) {
// 1. Continue to configure your Keyring, Table Schema, legacy attribute actions,
// and allowedUnsignedAttributes, and old DynamoDBEncryptor as you did in Step 1.
Expand Down Expand Up @@ -121,7 +122,7 @@ public static void MigrationStep2(
// 5. Put an item into your table using the DynamoDb Enhanced Client.
// This item will be encrypted.
final SimpleClass item = new SimpleClass();
item.setPartitionKey("PlaintextMigrationExample");
item.setPartitionKey(partitionKey);
item.setSortKey(2);
item.setAttribute1("this will be encrypted and signed");
item.setAttribute3("this will never be encrypted nor signed");
Expand All @@ -136,13 +137,13 @@ public static void MigrationStep2(
// during Step 2 or after), then the DDB enhanced client will decrypt the
// item client-sid and surface it in our code as a plaintext item.
SimpleClass itemToGet = new SimpleClass();
itemToGet.setPartitionKey("PlaintextMigrationExample");
itemToGet.setPartitionKey(partitionKey);
itemToGet.setSortKey(sortReadValue);

SimpleClass returnedItem = table.getItem(itemToGet);

// Demonstrate we get the expected item back
assert returnedItem.getPartitionKey().equals("PlaintextMigrationExample");
assert returnedItem.getPartitionKey().equals(partitionKey);
assert returnedItem
.getAttribute1()
.equals("this will be encrypted and signed");
Expand All @@ -158,6 +159,7 @@ public static void main(final String[] args) {
final String ddbTableName = args[1];
// You can manipulate this value to demonstrate reading records written in other steps
final int sortReadValue = Integer.parseInt(args[2]);
MigrationStep2(kmsKeyId, ddbTableName, sortReadValue);
final String partitionKey = args[3];
MigrationStep2(kmsKeyId, ddbTableName, sortReadValue, partitionKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class MigrationExampleStep3 {
public static void MigrationStep3(
String kmsKeyId,
String ddbTableName,
int sortReadValue
int sortReadValue,
String partitionKey
) {
// 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
// We will use the `CreateMrkMultiKeyring` method to create this keyring,
Expand Down Expand Up @@ -115,7 +116,7 @@ public static void MigrationStep3(
// 7. Put an item into your table using the DynamoDb Enhanced Client.
// This item will be encrypted.
final SimpleClass item = new SimpleClass();
item.setPartitionKey("PlaintextMigrationExample");
item.setPartitionKey(partitionKey);
item.setSortKey(3);
item.setAttribute1("this will be encrypted and signed");
item.setAttribute3("this will never be encrypted nor signed");
Expand All @@ -131,13 +132,13 @@ public static void MigrationStep3(
// during Step 2 or after), then the item will be decrypted client-side
// and surfaced as a plaintext item.
SimpleClass itemToGet = new SimpleClass();
itemToGet.setPartitionKey("PlaintextMigrationExample");
itemToGet.setPartitionKey(partitionKey);
itemToGet.setSortKey(sortReadValue);

SimpleClass returnedItem = table.getItem(itemToGet);

// Demonstrate we get the expected item back
assert returnedItem.getPartitionKey().equals("PlaintextMigrationExample");
assert returnedItem.getPartitionKey().equals(partitionKey);
assert returnedItem
.getAttribute1()
.equals("this will be encrypted and signed");
Expand All @@ -153,6 +154,7 @@ public static void main(final String[] args) {
final String ddbTableName = args[1];
// You can manipulate this value to demonstrate reading records written in other steps
final int sortReadValue = Integer.parseInt(args[2]);
MigrationStep3(kmsKeyId, ddbTableName, sortReadValue);
final String partitionKey = args[3];
MigrationStep3(kmsKeyId, ddbTableName, sortReadValue, partitionKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ write a plaintext record to a table and read that record.
*/
public class MigrationExampleStep0 {

public static void MigrationStep0(String ddbTableName, int sortReadValue) {
public static void MigrationStep0(
String ddbTableName,
int sortReadValue,
String partitionKey
) {
// 1. Create a Table Schema over your annotated class.
// See SimpleClass.java in this directory for a sample annotated class
// for a plaintext item.
Expand Down Expand Up @@ -56,7 +60,7 @@ public static void MigrationStep0(String ddbTableName, int sortReadValue) {
// 3. Put an example item into our DynamoDb table.
// This item will be stored in plaintext.
SimpleClass itemToPut = new SimpleClass();
itemToPut.setPartitionKey("PlaintextMigrationExample");
itemToPut.setPartitionKey(partitionKey);
itemToPut.setSortKey(0);
itemToPut.setAttribute1("this will be encrypted and signed");
itemToPut.setAttribute3("this will never be encrypted nor signed");
Expand All @@ -76,13 +80,13 @@ public static void MigrationStep0(String ddbTableName, int sortReadValue) {
// client-side encrypted items, you will need to configure encrypted reads on
// your enhanced client (this is configured from Step 1 onwards).
SimpleClass itemToGet = new SimpleClass();
itemToGet.setPartitionKey("PlaintextMigrationExample");
itemToGet.setPartitionKey(partitionKey);
itemToGet.setSortKey(sortReadValue);

SimpleClass returnedItem = table.getItem(itemToGet);

// Demonstrate we get the expected item back
assert returnedItem.getPartitionKey().equals("PlaintextMigrationExample");
assert returnedItem.getPartitionKey().equals(partitionKey);
assert returnedItem
.getAttribute1()
.equals("this will be encrypted and signed");
Expand All @@ -97,6 +101,7 @@ public static void main(final String[] args) {
final String ddbTableName = args[0];
// You can manipulate this value to demonstrate reading records written in other steps
final int sortReadValue = Integer.parseInt(args[1]);
MigrationStep0(ddbTableName, sortReadValue);
final String partitionKey = args[2];
MigrationStep0(ddbTableName, sortReadValue, partitionKey);
}
}
Loading
Loading