Skip to content

Commit ace02a2

Browse files
author
Lucas McDonald
committed
client coverage
1 parent 4383614 commit ace02a2

File tree

6 files changed

+199
-163
lines changed

6 files changed

+199
-163
lines changed

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/encrypted/client.py

Lines changed: 115 additions & 147 deletions
Large diffs are not rendered by default.

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/encrypted/table.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,18 @@ def _copy_missing_sdk_output_fields_to_response(self, sdk_output, response, outp
179179
def update_item(self, **kwargs):
180180
raise NotImplementedError('"update_item" is not yet implemented')
181181

182-
def batch_writer(self, overwrite_by_pkeys=None):
183-
"""Create a batch writer object.
182+
def batch_writer(
183+
self,
184+
overwrite_by_pkeys: list[str] | None
185+
):
186+
"""Create a batch writer object that will transparently encrypt requests to DynamoDB.
184187
185188
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/table/batch_writer.html
186189
187-
:type overwrite_by_pkeys: list(string)
188-
:param overwrite_by_pkeys: De-duplicate request items in buffer if match new request
189-
item on specified primary keys. i.e ``["partition_key1", "sort_key2", "sort_key3"]``
190+
Parameters:
191+
overwrite_by_pkeys: De-duplicate request items in buffer if match new request
192+
item on specified primary keys. i.e ``["partition_key1", "sort_key2", "sort_key3"]``
190193
"""
191-
print("making encrypted client")
192194
encrypted_client = EncryptedClient(
193195
client = self._table.meta.client,
194196
encryption_config = self._encryption_config,

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/internal/boto3_conversions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,5 @@ def batch_write_item_item_collection_metrics(self, item_collection_metrics):
251251
table_value = self.item_collection_metrics(table_value)
252252
item_collection_metrics_out[table_name] = table_value
253253
return item_collection_metrics_out
254+
255+
def

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/internal/resource_to_client.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,41 @@ def expression(self, condition_expression, expression_attribute_names, expressio
8181
# Expressions provided to tables can also already be string-like.
8282
# Assume the user has provided something string-like, and let Smithy-Python/DBESDK internals raise exceptions if not.
8383
print(f"probably a string: {condition_expression=}")
84-
return condition_expression
84+
return condition_expression
85+
86+
def batch_write_item_request_items(self, tables):
87+
"""Transform a batch write request's items to DynamoDB format."""
88+
output_tables = []
89+
table_names = list(tables.keys())
90+
for table_name in table_names:
91+
requests = tables[table_name]
92+
output_requests = []
93+
for request in requests:
94+
request_name_list = list(request.keys())
95+
if len(request_name_list) > 1:
96+
raise ValueError("Invalid JSON format")
97+
request_name = request_name_list[0]
98+
if request_name == "PutRequest":
99+
dict_request = dict_to_ddb(request[request_name]["Item"])
100+
boto3_request = {"Item": dict_request}
101+
elif request_name == "DeleteRequest":
102+
# Delete requests are based on Keys, which are expected to already be in DynamoDB JSON.
103+
# Only Items can be in Python dictionary JSON.
104+
boto3_request = request[request_name]
105+
else:
106+
raise ValueError(f"Unknown batch_write_items method key: {request_name}")
107+
output_requests.append({request_name: boto3_request})
108+
output_tables.append({table_name: output_requests})
109+
return output_tables
110+
111+
def batch_get_request(self, **kwargs):
112+
"""Transform a batch get request to DynamoDB format."""
113+
dynamodb_input = kwargs.copy()
114+
tables = dynamodb_input["RequestItems"]
115+
116+
for table_name, table_data in tables.items():
117+
dynamodb_input["RequestItems"][table_name]["Keys"] = [
118+
self.key_to_attribute_value_map(key) for key in table_data["Keys"]
119+
]
120+
121+
return dynamodb_input

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/transform.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,47 @@
1313

1414

1515
def dict_to_ddb(item: Dict[str, Any]) -> Dict[str, Any]:
16-
"""Converts a native Python dictionary to a raw DynamoDB item.
16+
"""Converts a native Python dictionary to a DynamoDB-JSON item.
1717
18-
:param dict item: Native item
19-
:returns: DynamoDB item
18+
Args:
19+
item (Dict[str, Any]): Native Python dictionary.
20+
21+
Returns:
22+
Dict[str, Any]: DynamoDB-formatted item.
2023
"""
2124
serializer = TypeSerializer()
2225
return {key: serializer.serialize(value) for key, value in item.items()}
2326

27+
def list_of_dict_to_list_of_ddb(items: list[Dict[str, Any]]) -> list[Dict[str, Any]]:
28+
"""Converts a list of Python dictionaries into a list of DynamoDB-JSON formatted items.
29+
30+
Args:
31+
items (List[Dict[str, Any]]): List of native Python dictionaries.
32+
33+
Returns:
34+
List[Dict[str, Any]]: List of DynamoDB-formatted items.
35+
"""
36+
return [dict_to_ddb(item) for item in items]
2437

2538
def ddb_to_dict(item: Dict[str, Any]) -> Dict[str, Any]:
26-
"""Converts a raw DynamoDB item to a native Python dictionary.
39+
"""Converts a DynamoDB-JSON item to a native Python dictionary.
40+
41+
Args:
42+
item (Dict[str, Any]): DynamoDB-formatted item.
2743
28-
:param dict item: DynamoDB item
29-
:returns: Native item
44+
Returns:
45+
Dict[str, Any]: Native Python dictionary.
3046
"""
3147
deserializer = TypeDeserializer()
3248
return {key: deserializer.deserialize(value) for key, value in item.items()}
49+
50+
def list_of_ddb_to_list_of_dict(items: list[Dict[str, Any]]) -> list[Dict[str, Any]]:
51+
"""Converts a list of DynamoDB-JSON formatted items to a list of Python dictionaries.
52+
53+
Args:
54+
items (List[Dict[str, Any]]): List of DynamoDB-formatted items.
55+
56+
Returns:
57+
List[Dict[str, Any]]: List of native Python dictionaries.
58+
"""
59+
return [ddb_to_dict(item) for item in items]

Examples/runtimes/python/DynamoDBEncryption/src/encrypted_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ def encrypted_client_put_get_example(
151151
# The client will decrypt the item client-side, and return
152152
# back the original item.
153153
key_to_get = {
154-
"partition_key": "BasicPutGetExample",
155-
"sort_key": 0,
154+
"partition_key": {"S": "BasicPutGetExample"},
155+
"sort_key": {"N": "0"}
156156
}
157157

158158
get_item_request = {
@@ -167,4 +167,4 @@ def encrypted_client_put_get_example(
167167
print(f"{item_to_encrypt=}")
168168
print(f"{get_item_response['Item']=}")
169169
# assert get_item_response["Item"] == item_to_encrypt
170-
assert get_item_response["Item"]["attribute1"] == {"S" : "encrypt and sign me!"}
170+
assert get_item_response["Item"]["attribute1"] == "encrypt and sign me!"

0 commit comments

Comments
 (0)