Skip to content

Commit 751a27a

Browse files
committed
single-source logic for decrypting a list of items used in both decrypt_multi_get and EncryptedPaginator
1 parent 54fe376 commit 751a27a

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/dynamodb_encryption_sdk/encrypted/client.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
crypto_config_from_kwargs,
2323
decrypt_batch_get_item,
2424
decrypt_get_item,
25+
decrypt_list_of_items,
2526
decrypt_multi_get,
2627
encrypt_batch_write_item,
2728
encrypt_put_item,
@@ -104,8 +105,11 @@ def paginate(self, **kwargs):
104105
crypto_config, ddb_kwargs = self._crypto_config_method(**kwargs)
105106

106107
for page in self._paginator.paginate(**ddb_kwargs):
107-
for pos, value in enumerate(page["Items"]):
108-
page["Items"][pos] = self._decrypt_method(item=value, crypto_config=crypto_config)
108+
page["Items"] = list(
109+
decrypt_list_of_items(
110+
crypto_config=crypto_config, decrypt_method=self._decrypt_method, items=page["Items"]
111+
)
112+
)
109113
yield page
110114

111115

src/dynamodb_encryption_sdk/internal/utils.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from dynamodb_encryption_sdk.transform import dict_to_ddb
3030

3131
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
32-
from typing import Any, Bool, Callable, Dict, Text # noqa pylint: disable=unused-import
32+
from typing import Any, Bool, Callable, Dict, Iterable, Text # noqa pylint: disable=unused-import
3333
except ImportError: # pragma: no cover
3434
# We only actually need these imports when running the mypy checks
3535
pass
@@ -41,6 +41,7 @@
4141
"crypto_config_from_cache",
4242
"decrypt_get_item",
4343
"decrypt_multi_get",
44+
"decrypt_list_of_items",
4445
"decrypt_batch_get_item",
4546
"encrypt_put_item",
4647
"encrypt_batch_write_item",
@@ -171,6 +172,22 @@ def _item_transformer(crypto_transformer):
171172
return lambda x: x
172173

173174

175+
def decrypt_list_of_items(crypto_config, decrypt_method, items):
176+
# type: (CryptoConfig, Callable, Iterable[Any]) -> Iterable[Any]
177+
# TODO: narrow this down
178+
"""Iterate through a list of encrypted items, decrypting each item and yielding the plaintext item.
179+
180+
:param CryptoConfig crypto_config: :class:`CryptoConfig` to use
181+
:param callable decrypt_method: Method to use to decrypt items
182+
:param items: Iterable of encrypted items
183+
:return: Iterable of plaintext items
184+
"""
185+
for value in items:
186+
yield decrypt_method(
187+
item=value, crypto_config=crypto_config.with_item(_item_transformer(decrypt_method)(value))
188+
)
189+
190+
174191
def decrypt_multi_get(decrypt_method, crypto_config_method, read_method, **kwargs):
175192
# type: (Callable, Callable, Callable, **Any) -> Dict
176193
# TODO: narrow this down
@@ -186,11 +203,9 @@ def decrypt_multi_get(decrypt_method, crypto_config_method, read_method, **kwarg
186203
validate_get_arguments(kwargs)
187204
crypto_config, ddb_kwargs = crypto_config_method(**kwargs)
188205
response = read_method(**ddb_kwargs)
189-
for pos in range(len(response["Items"])):
190-
response["Items"][pos] = decrypt_method(
191-
item=response["Items"][pos],
192-
crypto_config=crypto_config.with_item(_item_transformer(decrypt_method)(response["Items"][pos])),
193-
)
206+
response["Items"] = list(
207+
decrypt_list_of_items(crypto_config=crypto_config, decrypt_method=decrypt_method, items=response["Items"])
208+
)
194209
return response
195210

196211

0 commit comments

Comments
 (0)