Skip to content

Commit 9a7d2a8

Browse files
item encryptor
1 parent 50fffe6 commit 9a7d2a8

File tree

3 files changed

+1131
-118
lines changed

3 files changed

+1131
-118
lines changed

db-esdk-performance-testing/benchmarks/config/test-scenarios.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ data_sizes:
99
- 10240 # 10KB
1010
medium:
1111
- 102400 # 100KB
12-
- 400000 # 400KB
12+
- 512000 # 500KB
13+
- 1048576 # 1MB
14+
large:
15+
- 10485760 # 10MB
16+
- 52428800 # 50MB
17+
- 104857600 # 100MB
1318

1419
# Quick test configuration (reduced test set for faster execution)
1520
quick_config:

db-esdk-performance-testing/benchmarks/java/src/main/java/com/amazon/esdk/benchmark/ESDKBenchmark.java

Lines changed: 34 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
3737
import software.amazon.awssdk.services.dynamodb.model.WriteRequest;
3838
import software.amazon.cryptography.dbencryptionsdk.dynamodb.DynamoDbEncryptionInterceptor;
39+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.DynamoDbItemEncryptor;
40+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.DecryptItemInput;
41+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.DynamoDbItemEncryptorConfig;
42+
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.model.EncryptItemInput;
3943
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTableEncryptionConfig;
4044
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTablesEncryptionConfig;
4145
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.model.CryptoAction;
@@ -55,7 +59,7 @@
5559
public final class ESDKBenchmark {
5660

5761
final Config config;
58-
final DynamoDbClient ddbClient;
62+
final DynamoDbItemEncryptor itemEncryptor;
5963
final IKeyring keyring;
6064
final String tableName;
6165
// System information
@@ -74,10 +78,10 @@ public ESDKBenchmark(final String configPath) throws Exception {
7478

7579
// Setup DB-ESDK with local DynamoDB
7680
this.keyring = setupKeyring();
77-
this.ddbClient = setupDynamoDbClient();
81+
this.itemEncryptor = setupItemEncryptorClient();
7882

7983
// Create table if it doesn't exist
80-
createTableIfNotExists();
84+
// createTableIfNotExists();
8185

8286
System.out.println(
8387
"Initialized DB-ESDK Benchmark - CPU cores: " +
@@ -115,7 +119,7 @@ private IKeyring setupKeyring() throws Exception {
115119
return matProv.CreateRawAesKeyring(keyringInput);
116120
}
117121

118-
private DynamoDbClient setupDynamoDbClient() {
122+
private DynamoDbItemEncryptor setupItemEncryptorClient() {
119123
// Configure attribute actions for encryption
120124
final Map<String, CryptoAction> attributeActionsOnEncrypt = new HashMap<>();
121125
attributeActionsOnEncrypt.put("partition_key", CryptoAction.SIGN_ONLY);
@@ -125,8 +129,7 @@ private DynamoDbClient setupDynamoDbClient() {
125129
attributeActionsOnEncrypt.put(":attribute3", CryptoAction.DO_NOTHING);
126130

127131
// Configure table encryption
128-
final Map<String, DynamoDbTableEncryptionConfig> tableConfigs = new HashMap<>();
129-
final DynamoDbTableEncryptionConfig tableConfig = DynamoDbTableEncryptionConfig
132+
final DynamoDbItemEncryptorConfig tableConfig = DynamoDbItemEncryptorConfig
130133
.builder()
131134
.logicalTableName(tableName)
132135
.partitionKeyName("partition_key")
@@ -136,136 +139,50 @@ private DynamoDbClient setupDynamoDbClient() {
136139
.allowedUnsignedAttributePrefix(":")
137140
.algorithmSuiteId(DBEAlgorithmSuiteId.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384)
138141
.build();
139-
tableConfigs.put(tableName, tableConfig);
140142

141-
// Create encryption interceptor
142-
final DynamoDbEncryptionInterceptor encryptionInterceptor = DynamoDbEncryptionInterceptor
143+
final DynamoDbItemEncryptor itemEncryptor = DynamoDbItemEncryptor
143144
.builder()
144-
.config(
145-
DynamoDbTablesEncryptionConfig
146-
.builder()
147-
.tableEncryptionConfigs(tableConfigs)
148-
.build()
149-
)
150-
.build();
151-
152-
// Create DynamoDB client with local endpoint
153-
return DynamoDbClient
154-
.builder()
155-
.endpointOverride(URI.create("http://localhost:8000"))
156-
.overrideConfiguration(
157-
ClientOverrideConfiguration
158-
.builder()
159-
.addExecutionInterceptor(encryptionInterceptor)
160-
.build()
161-
)
145+
.DynamoDbItemEncryptorConfig(tableConfig)
162146
.build();
163-
}
164-
165-
private void createTableIfNotExists() {
166-
try {
167-
// Check if table exists
168-
ddbClient.describeTable(DescribeTableRequest.builder()
169-
.tableName(tableName)
170-
.build());
171-
System.out.println("Table " + tableName + " already exists");
172-
} catch (ResourceNotFoundException e) {
173-
// Table doesn't exist, create it
174-
System.out.println("Creating table " + tableName + "...");
175-
ddbClient.createTable(CreateTableRequest.builder()
176-
.tableName(tableName)
177-
.keySchema(
178-
KeySchemaElement.builder()
179-
.attributeName("partition_key")
180-
.keyType(KeyType.HASH)
181-
.build(),
182-
KeySchemaElement.builder()
183-
.attributeName("sort_key")
184-
.keyType(KeyType.RANGE)
185-
.build()
186-
)
187-
.attributeDefinitions(
188-
AttributeDefinition.builder()
189-
.attributeName("partition_key")
190-
.attributeType(ScalarAttributeType.S)
191-
.build(),
192-
AttributeDefinition.builder()
193-
.attributeName("sort_key")
194-
.attributeType(ScalarAttributeType.N)
195-
.build()
196-
)
197-
.billingMode(BillingMode.PAY_PER_REQUEST)
198-
.build());
199-
System.out.println("Table " + tableName + " created successfully");
200-
}
147+
148+
return itemEncryptor;
201149
}
202150

203151
/**
204152
* Run a single batch put-get cycle and measure performance
205153
*/
206-
public BatchPutGetResult runBatchPutGetCycle(final byte[] data) {
154+
public Result runBatchPutGetCycle(final byte[] data) {
207155
// Create 25 items with same data, different sort_key
208-
final List<WriteRequest> writeRequests = new ArrayList<>();
209-
final List<Map<String, AttributeValue>> keys = new ArrayList<>();
210-
211-
for (int i = 0; i < 25; i++) {
212-
final Map<String, AttributeValue> item = new HashMap<>();
156+
final Map<String, AttributeValue> item = new HashMap<>();
213157
item.put("partition_key", AttributeValue.builder().s("benchmark-test").build());
214-
item.put("sort_key", AttributeValue.builder().n(String.valueOf(i)).build());
158+
item.put("sort_key", AttributeValue.builder().n(String.valueOf(0)).build());
215159
item.put("attribute1", AttributeValue.builder()
216160
.m(Map.of("data", AttributeValue.builder().b(SdkBytes.fromByteArray(data)).build()))
217161
.build());
218162
item.put("attribute2", AttributeValue.builder().s("sign me!").build());
219163
item.put(":attribute3", AttributeValue.builder().s("ignore me!").build());
220164

221-
writeRequests.add(WriteRequest.builder()
222-
.putRequest(PutRequest.builder().item(item).build())
223-
.build());
224-
225-
// Prepare key for batch get
226-
final Map<String, AttributeValue> key = new HashMap<>();
227-
key.put("partition_key", AttributeValue.builder().s("benchmark-test").build());
228-
key.put("sort_key", AttributeValue.builder().n(String.valueOf(i)).build());
229-
keys.add(key);
230-
}
231-
232165
// Measure batch write
233-
final long batchWriteStart = System.nanoTime();
234-
final BatchWriteItemResponse writeResponse = ddbClient.batchWriteItem(
235-
BatchWriteItemRequest.builder()
236-
.requestItems(Map.of(tableName, writeRequests))
237-
.build()
238-
);
239-
final long batchWriteTime = System.nanoTime() - batchWriteStart;
166+
final long encryptStart = System.nanoTime();
167+
final Map<String, AttributeValue> encryptedItem = itemEncryptor
168+
.EncryptItem(
169+
EncryptItemInput.builder().plaintextItem(item).build()
170+
)
171+
.encryptedItem();
172+
final long encryptTime = System.nanoTime() - encryptStart;
240173

241174
// Measure batch get
242-
final long batchGetStart = System.nanoTime();
243-
final BatchGetItemResponse getResponse = ddbClient.batchGetItem(
244-
BatchGetItemRequest.builder()
245-
.requestItems(Map.of(tableName, KeysAndAttributes.builder()
246-
.keys(keys)
247-
.consistentRead(true)
248-
.build()))
249-
.build()
250-
);
251-
final long batchGetTime = System.nanoTime() - batchGetStart;
252-
253-
// Verify 25 items retrieved
254-
final List<Map<String, AttributeValue>> returnedItems = getResponse.responses().get(tableName);
255-
if (returnedItems.size() != 25) {
256-
throw new RuntimeException("Expected 25 items, got " + returnedItems.size());
257-
}
258-
259-
// Verify data integrity for first item
260-
final Map<String, AttributeValue> firstItem = returnedItems.get(0);
261-
final AttributeValue attr1 = firstItem.get("attribute1");
262-
if (attr1 == null || attr1.m() == null || attr1.m().get("data") == null) {
263-
throw new RuntimeException("Data verification failed");
264-
}
175+
final long decryptStart = System.nanoTime();
176+
final Map<String, AttributeValue> decryptedItem = itemEncryptor
177+
.DecryptItem(
178+
DecryptItemInput.builder().encryptedItem(encryptedItem).build()
179+
)
180+
.plaintextItem();
181+
final long decryptTime = System.nanoTime() - decryptStart;
265182

266-
return new BatchPutGetResult(
267-
batchWriteTime / 1_000_000.0, // Convert to milliseconds
268-
batchGetTime / 1_000_000.0
183+
return new Result(
184+
encryptTime / 1_000_000.0, // Convert to milliseconds
185+
decryptTime / 1_000_000.0
269186
);
270187
}
271188

@@ -335,7 +252,7 @@ public List<TestResult> runAllBenchmarks() {
335252
return allResults;
336253
}
337254

338-
public record BatchPutGetResult(
255+
public record Result(
339256
double putLatencyMs,
340257
double getLatencyMs
341258
) {}

0 commit comments

Comments
 (0)