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 6 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 @@ -30,7 +30,11 @@
*/
public class BasicPutGetExample {

public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
public static void PutItemGetItem(
String kmsKeyId,
String ddbTableName,
String PartitionKeyName
) {
// 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 Down Expand Up @@ -142,7 +146,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
final HashMap<String, AttributeValue> item = new HashMap<>();
item.put(
"partition_key",
AttributeValue.builder().s("BasicPutGetExample").build()
AttributeValue.builder().s(PartitionKeyName).build()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to delete this at some point, so that the table doesn't grow arbitrarily large.

);
item.put("sort_key", AttributeValue.builder().n("0").build());
item.put(
Expand All @@ -169,7 +173,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
keyToGet.put(
"partition_key",
AttributeValue.builder().s("BasicPutGetExample").build()
AttributeValue.builder().s(PartitionKeyName).build()
);
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());

Expand Down Expand Up @@ -201,6 +205,6 @@ public static void main(final String[] args) {
}
final String kmsKeyId = args[0];
final String ddbTableName = args[1];
PutItemGetItem(kmsKeyId, ddbTableName);
PutItemGetItem(kmsKeyId, ddbTableName, "BasicPutGetExample");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.amazon.cryptography.examples;

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

public class TestBasicPutGetExample {
Expand All @@ -8,7 +9,8 @@ public class TestBasicPutGetExample {
public void TestPutGet() {
BasicPutGetExample.PutItemGetItem(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME
TestUtils.TEST_DDB_TABLE_NAME,
"BasicPutGetExample" + UUID.randomUUID()
);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package software.amazon.cryptography.examples.migration.awsdbe;
package software.amazon.cryptography.examples.migration;

import java.util.UUID;

public class TestUtils {

Expand All @@ -9,4 +11,7 @@ public class TestUtils {
// Our tests require access to DDB Table with this name
public static final String TEST_DDB_TABLE_NAME =
"DynamoDbEncryptionInterceptorTestTable";

public static final String PARTITION_KEY =
"PlaintextMigrationExample" + UUID.randomUUID();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep1;
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep2;
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep3;
import software.amazon.cryptography.examples.migration.TestUtils;
import software.amazon.cryptography.examples.plaintext.MigrationExampleStep0;

public class TestMigrationExampleStep1 {
Expand All @@ -14,42 +15,52 @@ public void TestMigrationStep1() {
MigrationExampleStep1.MigrationStep1(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
1
1,
TestUtils.PARTITION_KEY
);

// Given: Step 0 has succeeded
MigrationExampleStep0.MigrationStep0(TestUtils.TEST_DDB_TABLE_NAME, 0);
MigrationExampleStep0.MigrationStep0(
TestUtils.TEST_DDB_TABLE_NAME,
0,
TestUtils.PARTITION_KEY
);
// When: Execute Step 1 with sortReadValue=0, Then: Success (i.e. can read plaintext values)
MigrationExampleStep1.MigrationStep1(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
0
0,
TestUtils.PARTITION_KEY
);

// Given: Step 2 has succeeded
MigrationExampleStep2.MigrationStep2(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
2
2,
TestUtils.PARTITION_KEY
);
// When: Execute Step 1 with sortReadValue=2, Then: Success (i.e. can read encrypted values)
MigrationExampleStep1.MigrationStep1(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
2
2,
TestUtils.PARTITION_KEY
);

// Given: Step 3 has succeeded
MigrationExampleStep3.MigrationStep3(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
3
3,
TestUtils.PARTITION_KEY
);
// When: Execute Step 1 with sortReadValue=3, Then: Success (i.e. can read encrypted values)
MigrationExampleStep1.MigrationStep1(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
3
3,
TestUtils.PARTITION_KEY
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep1;
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep2;
import software.amazon.cryptography.examples.awsdbe.MigrationExampleStep3;
import software.amazon.cryptography.examples.migration.TestUtils;
import software.amazon.cryptography.examples.plaintext.MigrationExampleStep0;

public class TestMigrationExampleStep2 {
Expand All @@ -14,42 +15,52 @@ public void TestMigrationStep2() {
MigrationExampleStep2.MigrationStep2(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
2
2,
TestUtils.PARTITION_KEY
);

// Given: Step 0 has succeeded
MigrationExampleStep0.MigrationStep0(TestUtils.TEST_DDB_TABLE_NAME, 0);
MigrationExampleStep0.MigrationStep0(
TestUtils.TEST_DDB_TABLE_NAME,
0,
TestUtils.PARTITION_KEY
);
// When: Execute Step 2 with sortReadValue=0, Then: Success (i.e. can read plaintext values)
MigrationExampleStep2.MigrationStep2(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
0
0,
TestUtils.PARTITION_KEY
);

// Given: Step 1 has succeeded
MigrationExampleStep1.MigrationStep1(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
1
1,
TestUtils.PARTITION_KEY
);
// When: Execute Step 2 with sortReadValue=1, Then: Success (i.e. can read encrypted values)
MigrationExampleStep2.MigrationStep2(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
1
1,
TestUtils.PARTITION_KEY
);

// Given: Step 3 has succeeded
MigrationExampleStep3.MigrationStep3(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
3
3,
TestUtils.PARTITION_KEY
);
// When: Execute Step 2 with sortReadValue=3, Then: Success (i.e. can read encrypted values)
MigrationExampleStep2.MigrationStep2(
TestUtils.TEST_KMS_KEY_ID,
TestUtils.TEST_DDB_TABLE_NAME,
3
3,
TestUtils.PARTITION_KEY
);
}
}
Loading
Loading