29
29
from dynamodb_encryption_sdk .transform import dict_to_ddb
30
30
31
31
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
33
33
except ImportError : # pragma: no cover
34
34
# We only actually need these imports when running the mypy checks
35
35
pass
41
41
"crypto_config_from_cache" ,
42
42
"decrypt_get_item" ,
43
43
"decrypt_multi_get" ,
44
+ "decrypt_list_of_items" ,
44
45
"decrypt_batch_get_item" ,
45
46
"encrypt_put_item" ,
46
47
"encrypt_batch_write_item" ,
@@ -171,6 +172,22 @@ def _item_transformer(crypto_transformer):
171
172
return lambda x : x
172
173
173
174
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
+
174
191
def decrypt_multi_get (decrypt_method , crypto_config_method , read_method , ** kwargs ):
175
192
# type: (Callable, Callable, Callable, **Any) -> Dict
176
193
# TODO: narrow this down
@@ -186,11 +203,9 @@ def decrypt_multi_get(decrypt_method, crypto_config_method, read_method, **kwarg
186
203
validate_get_arguments (kwargs )
187
204
crypto_config , ddb_kwargs = crypto_config_method (** kwargs )
188
205
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
+ )
194
209
return response
195
210
196
211
0 commit comments