Skip to content

Commit f51160b

Browse files
committed
chore(python): examples for keyrings
1 parent 2475cd7 commit f51160b

21 files changed

+1871
-23
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static void SharedCacheAcrossHierarchicalKeyringsGetItemPutItem(
180180
final IKeyring hierarchicalKeyring1 =
181181
matProv.CreateAwsKmsHierarchicalKeyring(keyringInput1);
182182

183-
// 4. Configure which attributes are encrypted and/or signed when writing new items.
183+
// 5. Configure which attributes are encrypted and/or signed when writing new items.
184184
// For each attribute that may exist on the items we plan to write to our DynamoDbTable,
185185
// we must explicitly configure how they should be treated during item encryption:
186186
// - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
@@ -194,14 +194,14 @@ public static void SharedCacheAcrossHierarchicalKeyringsGetItemPutItem(
194194
CryptoAction.ENCRYPT_AND_SIGN
195195
);
196196

197-
// 5. Get the DDB Client for Hierarchical Keyring 1.
197+
// 6. Get the DDB Client for Hierarchical Keyring 1.
198198
final DynamoDbClient ddbClient1 = GetDdbClient(
199199
ddbTableName,
200200
hierarchicalKeyring1,
201201
attributeActionsOnEncrypt
202202
);
203203

204-
// 6. Encrypt Decrypt roundtrip with ddbClient1
204+
// 7. Encrypt Decrypt roundtrip with ddbClient1
205205
PutGetItems(ddbTableName, ddbClient1);
206206

207207
// Through the above encrypt and decrypt roundtrip, the cache will be populated and
@@ -210,7 +210,7 @@ public static void SharedCacheAcrossHierarchicalKeyringsGetItemPutItem(
210210
// - Same Logical Key Store Name of the Key Store for the Hierarchical Keyring
211211
// - Same Branch Key ID
212212

213-
// 7. Configure your KeyStore resource keystore2.
213+
// 8. Configure your KeyStore resource keystore2.
214214
// This SHOULD be the same configuration that you used
215215
// to initially create and populate your physical KeyStore.
216216
// Note that keyStoreTableName is the physical Key Store,
@@ -243,13 +243,13 @@ public static void SharedCacheAcrossHierarchicalKeyringsGetItemPutItem(
243243
)
244244
.build();
245245

246-
// 8. Create the Hierarchical Keyring HK2 with Key Store instance K2, the shared Cache
246+
// 9. Create the Hierarchical Keyring HK2 with Key Store instance K2, the shared Cache
247247
// and the same partitionId and BranchKeyId used in HK1 because we want to share cache entries
248248
// (and experience cache HITS).
249249

250-
// Please make sure that you read the guidance on how to set Partition ID, Logical Key Store Name and
251-
// Branch Key ID at the top of this example before creating Hierarchical Keyrings with a Shared Cache.
252-
// partitionId for this example is a random UUID
250+
Please make sure that you read the guidance on how to set Partition ID, Logical Key Store Name and
251+
Branch Key ID at the top of this example before creating Hierarchical Keyrings with a Shared Cache.
252+
partitionId for this example is a random UUID
253253
final CreateAwsKmsHierarchicalKeyringInput keyringInput2 =
254254
CreateAwsKmsHierarchicalKeyringInput
255255
.builder()
@@ -262,14 +262,14 @@ public static void SharedCacheAcrossHierarchicalKeyringsGetItemPutItem(
262262
final IKeyring hierarchicalKeyring2 =
263263
matProv.CreateAwsKmsHierarchicalKeyring(keyringInput2);
264264

265-
// 9. Get the DDB Client for Hierarchical Keyring 2.
265+
// 10. Get the DDB Client for Hierarchical Keyring 2.
266266
final DynamoDbClient ddbClient2 = GetDdbClient(
267267
ddbTableName,
268268
hierarchicalKeyring2,
269269
attributeActionsOnEncrypt
270270
);
271271

272-
// 10. Encrypt Decrypt roundtrip with ddbClient2
272+
// 11. Encrypt Decrypt roundtrip with ddbClient2
273273
PutGetItems(ddbTableName, ddbClient2);
274274
}
275275

@@ -352,12 +352,12 @@ public static void PutGetItems(
352352
String ddbTableName,
353353
DynamoDbClient ddbClient
354354
) {
355-
// Put an item into our table using the given ddb client.
356-
// Before the item gets sent to DynamoDb, it will be encrypted
357-
// client-side, according to our configuration.
358-
// This example creates a Hierarchical Keyring for a single BranchKeyId. You can, however, use a
359-
// BranchKeyIdSupplier as per your use-case. See the HierarchicalKeyringsExample.java for more
360-
// information.
355+
Put an item into our table using the given ddb client.
356+
Before the item gets sent to DynamoDb, it will be encrypted
357+
client-side, according to our configuration.
358+
This example creates a Hierarchical Keyring for a single BranchKeyId. You can, however, use a
359+
BranchKeyIdSupplier as per your use-case. See the HierarchicalKeyringsExample.java for more
360+
information.
361361
final HashMap<String, AttributeValue> item = new HashMap<>();
362362
item.put("partition_key", AttributeValue.builder().s("id").build());
363363
item.put("sort_key", AttributeValue.builder().n("0").build());
@@ -377,12 +377,12 @@ public static void PutGetItems(
377377
// Demonstrate that PutItem succeeded
378378
assert 200 == putResponse.sdkHttpResponse().statusCode();
379379

380-
// Get the item back from our table using the same client.
381-
// The client will decrypt the item client-side, and return
382-
// back the original item.
383-
// This example creates a Hierarchical Keyring for a single BranchKeyId. You can, however, use a
384-
// BranchKeyIdSupplier as per your use-case. See the HierarchicalKeyringsExample.java for more
385-
// information.
380+
Get the item back from our table using the same client.
381+
The client will decrypt the item client-side, and return
382+
back the original item.
383+
This example creates a Hierarchical Keyring for a single BranchKeyId. You can, however, use a
384+
BranchKeyIdSupplier as per your use-case. See the HierarchicalKeyringsExample.java for more
385+
information.
386386
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
387387
keyToGet.put("partition_key", AttributeValue.builder().s("id").build());
388388
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
The Hierarchical Keyring Example and Searchable Encryption Examples
5+
rely on the existence of a DDB-backed key store with pre-existing
6+
branch key material or beacon key material.
7+
8+
This example demonstrates configuring a KeyStore and then
9+
using a helper method to create the DDB table that will be
10+
used to persist branch keys and beacons keys for this KeyStore.
11+
12+
This table creation should occur within your control plane. This
13+
only needs to occur once. While not demonstrated in this example,
14+
you should additionally use the `VersionKey` API on the KeyStore
15+
to periodically rotate your branch key material.
16+
"""
17+
18+
import boto3
19+
from aws_cryptographic_material_providers.keystore.client import KeyStore
20+
from aws_cryptographic_material_providers.keystore.config import KeyStoreConfig
21+
from aws_cryptographic_material_providers.keystore.models import (
22+
CreateKeyStoreInput,
23+
KMSConfigurationKmsKeyArn,
24+
)
25+
26+
27+
def keystore_create_table(
28+
keystore_table_name: str,
29+
logical_keystore_name: str,
30+
kms_key_arn: str
31+
):
32+
"""Create KeyStore Table Example.
33+
34+
:param keystore_table_name: The name of the DynamoDB table to create
35+
:param logical_keystore_name: The logical name for this keystore
36+
:param kms_key_arn: The ARN of the KMS key to use for protecting branch keys
37+
"""
38+
# 1. Configure your KeyStore resource.
39+
# `ddb_table_name` is the name you want for the DDB table that
40+
# will back your keystore.
41+
# `kms_key_arn` is the KMS Key that will protect your branch keys and beacon keys
42+
# when they are stored in your DDB table.
43+
keystore = KeyStore(
44+
config=KeyStoreConfig(
45+
ddb_client=boto3.client('dynamodb'),
46+
ddb_table_name=keystore_table_name,
47+
logical_key_store_name=logical_keystore_name,
48+
kms_client=boto3.client('kms'),
49+
kms_configuration=KMSConfigurationKmsKeyArn(kms_key_arn),
50+
)
51+
)
52+
53+
# 2. Create the DynamoDb table that will store the branch keys and beacon keys.
54+
# This checks if the correct table already exists at `ddb_table_name`
55+
# by using the DescribeTable API. If no table exists,
56+
# it will create one. If a table exists, it will verify
57+
# the table's configuration and will error if the configuration is incorrect.
58+
keystore.create_key_store(input=CreateKeyStoreInput())
59+
# It may take a couple of minutes for the table to become ACTIVE,
60+
# at which point it is ready to store branch and beacon keys.
61+
# See the Create KeyStore Key Example for how to populate
62+
# this table.

Examples/runtimes/python/DynamoDBEncryption/src/item_encryptor/encrypt_decrypt_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646

4747

48-
def encrypt_decrypt_example(kms_key_id: str, ddb_table_name: str) -> None:
48+
def encrypt_decrypt_example(kms_key_id: str, ddb_table_name: str):
4949
"""Encrypt and decrypt an item with an ItemEncryptor."""
5050
# 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data.
5151
# For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Stub to allow relative imports of examples from tests."""

0 commit comments

Comments
 (0)