Skip to content

Commit 9e3d100

Browse files
committed
Add Cleanup Utility
1 parent 93860fe commit 9e3d100

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
Test cleanup utilities for DynamoDB Encryption SDK.
5+
6+
This module provides utilities for cleaning up resources after running tests.
7+
NOTE: This is only a test utility and should not be used in production code.
8+
It is specifically designed for cleaning up test resources after test execution.
9+
"""
10+
import boto3
11+
12+
BRANCH_KEY_IDENTIFIER_FIELD = "branch-key-id"
13+
TYPE_FIELD = "type"
14+
15+
16+
def delete_branch_key(
17+
identifier: str,
18+
table_name: str,
19+
ddb_client: boto3.client,
20+
) -> bool:
21+
"""
22+
Delete all branch key items with the given identifier.
23+
24+
Args:
25+
identifier: Branch key identifier to delete
26+
table_name: DynamoDB table name
27+
ddb_client: DynamoDB client to use
28+
29+
Returns:
30+
True if all items were deleted, False if more than 100 items exist
31+
32+
Raises:
33+
ValueError: If an item is not a branch key
34+
35+
"""
36+
if ddb_client is None:
37+
ddb_client = boto3.client("dynamodb")
38+
39+
# Query for items with matching identifier
40+
query_response = ddb_client.query(
41+
TableName=table_name,
42+
KeyConditionExpression="#pk = :pk",
43+
ExpressionAttributeNames={"#pk": BRANCH_KEY_IDENTIFIER_FIELD},
44+
ExpressionAttributeValues={":pk": {"S": identifier}},
45+
)
46+
47+
items = query_response.get("Items", [])
48+
if not items:
49+
return True
50+
51+
# Create delete requests for each item
52+
delete_items = []
53+
for item in items:
54+
if TYPE_FIELD not in item:
55+
raise ValueError("Item is not a branch key")
56+
57+
delete_item = {
58+
"Delete": {
59+
"Key": {BRANCH_KEY_IDENTIFIER_FIELD: {"S": identifier}, TYPE_FIELD: item[TYPE_FIELD]},
60+
"TableName": table_name,
61+
}
62+
}
63+
delete_items.append(delete_item)
64+
65+
if not delete_items:
66+
return True
67+
68+
# DynamoDB transactions are limited to 100 items
69+
if len(delete_items) > 100:
70+
delete_items = delete_items[:100]
71+
72+
# Execute the delete transaction
73+
ddb_client.transact_write_items(TransactItems=delete_items)
74+
75+
# Return False if we had to truncate the deletion
76+
return len(items) <= 100

Examples/runtimes/python/DynamoDBEncryption/test/keyring/test_hierarchical_keyring_example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from ...src.create_keystore_key_example import keystore_create_key
99
from ...src.keyring.hierarchical_keyring_example import hierarchical_keyring_get_item_put_item
10+
from ..cleanup import delete_branch_key
1011
from ..test_utils import (
1112
TEST_DDB_TABLE_NAME,
1213
TEST_KEYSTORE_KMS_KEY_ID,
@@ -30,3 +31,7 @@ def test_hierarchical_keyring_example():
3031
hierarchical_keyring_get_item_put_item(
3132
TEST_DDB_TABLE_NAME, key_id1, key_id2, TEST_KEYSTORE_NAME, TEST_LOGICAL_KEYSTORE_NAME, TEST_KEYSTORE_KMS_KEY_ID
3233
)
34+
35+
# Cleanup Branch Key
36+
delete_branch_key(key_id1, TEST_KEYSTORE_NAME, None)
37+
delete_branch_key(key_id2, TEST_KEYSTORE_NAME, None)

Examples/runtimes/python/DynamoDBEncryption/test/keyring/test_shared_cache_across_hierarchical_keyrings_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ...src.keyring.shared_cache_across_hierarchical_keyrings_example import (
1010
shared_cache_across_hierarchical_keyrings_example,
1111
)
12+
from ..cleanup import delete_branch_key
1213
from ..test_utils import (
1314
TEST_DDB_TABLE_NAME,
1415
TEST_KEYSTORE_KMS_KEY_ID,
@@ -37,3 +38,6 @@ def test_shared_cache_across_hierarchical_keyrings_example():
3738
TEST_PARTITION_ID,
3839
TEST_KEYSTORE_KMS_KEY_ID,
3940
)
41+
42+
# Cleanup Branch Key
43+
delete_branch_key(key_id, TEST_KEYSTORE_NAME, None)

Examples/runtimes/python/DynamoDBEncryption/test/test_create_keystore_key_example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from ..src.create_keystore_key_example import keystore_create_key
7+
from .cleanup import delete_branch_key
78
from .test_utils import TEST_KEYSTORE_KMS_KEY_ID, TEST_KEYSTORE_NAME, TEST_LOGICAL_KEYSTORE_NAME
89

910
pytestmark = [pytest.mark.examples]
@@ -14,4 +15,6 @@ def test_create_keystore_key_example():
1415
key_id = keystore_create_key(TEST_KEYSTORE_NAME, TEST_LOGICAL_KEYSTORE_NAME, TEST_KEYSTORE_KMS_KEY_ID)
1516

1617
assert key_id is not None
17-
# TODO: Cleanup Branch Key Items
18+
19+
# Cleanup Branch Key
20+
delete_branch_key(key_id, TEST_KEYSTORE_NAME, None)

0 commit comments

Comments
 (0)