Skip to content

Commit 0ef759b

Browse files
m
1 parent 81be9b6 commit 0ef759b

File tree

2 files changed

+67
-49
lines changed

2 files changed

+67
-49
lines changed

DynamoDbEncryption/runtimes/python/src/aws_database_encryption_sdk/encryptor/item.py

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222

2323
class ItemEncryptor:
24-
"""Client providing item-level encryption for DynamoDB items / Python dictionaries."""
24+
"""Class providing item-level encryption for DynamoDB items / Python dictionaries."""
2525

2626
_internal_client: DynamoDbItemEncryptor
2727

@@ -39,33 +39,38 @@ def __init__(
3939

4040
def encrypt_python_item(
4141
self,
42-
plaintext_dict_item: dict[str, Any]
42+
plaintext_dict_item: dict[str, Any],
4343
) -> EncryptItemOutput:
4444
"""
4545
Encrypt a Python dictionary.
46-
This method will convert the Python dictionary into a DynamoDB item, then encrypt the item.
46+
47+
This method will transform the Python dictionary into DynamoDB JSON,
48+
encrypt the DynamoDB JSON,
49+
transform the encrypted DynamoDB JSON into an encrypted Python dictionary,
50+
then return the encrypted Python dictionary.
4751
4852
boto3 DynamoDB Tables and Resources expect items formatted as native Python dictionaries.
49-
Use this method to encrypt an item you intend to store using a boto3 DynamoDB Table or Resource interface.
53+
Use this method to encrypt an item if you intend to pass the encrypted item
54+
to a boto3 DynamoDB Table or Resource interface to store it.
55+
(Alternatively, you can use this library's EncryptedTable or EncryptedResource interfaces
56+
to transparently encrypt items without an intermediary ItemEncryptor.)
5057
5158
Parameters:
5259
plaintext_dict_item (dict[str, Any]): A standard Python dictionary.
5360
5461
Returns:
5562
EncryptItemOutput: Structure containing the following fields:
5663
- `encrypted_item` (dict[str, Any]): The encrypted Python dictionary.
57-
**Note:** The item was encrypted as a DynamoDB item, then converted back to a native Python item.
58-
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (`aws_dbe_head` value).
64+
**Note:** The item was encrypted as DynamoDB JSON, then transformed to a Python dictionary.
65+
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (parsed `aws_dbe_head` value).
5966
6067
Example:
6168
6269
>>> plaintext_item = {
6370
... 'some': 'data',
6471
... 'more': 5
6572
... }
66-
>>> encrypted_item, header = item_encryptor.encrypt_python_item(
67-
... plaintext_dict_item = plaintext_item,
68-
... )
73+
>>> encrypted_item, header = item_encryptor.encrypt_python_item(plaintext_item)
6974
"""
7075
plaintext_ddb_item = dict_to_ddb(plaintext_dict_item)
7176
encrypted_ddb_item: EncryptItemOutput = self.encrypt_dynamodb_item(plaintext_ddb_item)
@@ -77,20 +82,25 @@ def encrypt_python_item(
7782

7883
def encrypt_dynamodb_item(
7984
self,
80-
plaintext_dynamodb_item: dict[str, Any]
85+
plaintext_dynamodb_item: dict[str, dict[str, Any]],
8186
) -> EncryptItemOutput:
8287
"""
83-
Encrypt a DynamoDB item.
88+
Encrypt DynamoDB-formatted JSON.
8489
85-
boto3 DynamoDB clients expect items formatted as DynamoDB items.
86-
Use this method to encrypt an item you intend to store using a boto3 DynamoDB client.
90+
boto3 DynamoDB clients expect items formatted as DynamoDB JSON:
91+
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html
92+
Use this method to encrypt an item if you intend to pass the encrypted item
93+
to a boto3 DynamoDB client to store it.
94+
(Alternatively, you can use this library's EncryptedClient interface
95+
to transparently encrypt items without an intermediary ItemEncryptor.)
8796
8897
Parameters:
89-
plaintext_dynamodb_item (dict[str, Any]): A dictionary representing a DynamoDB item.
98+
plaintext_dynamodb_item (dict[str, dict[str, Any]]): The item to encrypt formatted as DynamoDB JSON.
9099
91100
Returns:
92101
EncryptItemOutput: Structure containing the following fields:
93-
- `encrypted_item` (dict[str, Any]): The encrypted DynamoDB item.
102+
- `encrypted_item` (dict[str, Any]): A dictionary containing the encrypted DynamoDB item
103+
formatted as DynamoDB JSON.
94104
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (`aws_dbe_head` value).
95105
96106
Example:
@@ -99,9 +109,7 @@ def encrypt_dynamodb_item(
99109
... 'some': {'S': 'data'},
100110
... 'more': {'N': '5'}
101111
... }
102-
>>> encrypted_item, header = item_encryptor.encrypt_dynamodb_item(
103-
... plaintext_dynamodb_item = plaintext_item
104-
... )
112+
>>> encrypted_item, header = item_encryptor.encrypt_dynamodb_item(plaintext_item)
105113
"""
106114
return self.encrypt_item(
107115
EncryptItemInput(
@@ -111,18 +119,21 @@ def encrypt_dynamodb_item(
111119

112120
def encrypt_item(
113121
self,
114-
encrypt_item_input: EncryptItemInput
122+
encrypt_item_input: EncryptItemInput,
115123
) -> EncryptItemOutput:
116124
"""
117125
Encrypt a DynamoDB item.
118126
127+
The input item should contain a dictionary formatted as DynamoDB JSON:
128+
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html
129+
119130
Parameters:
120131
encrypt_item_input (EncryptItemInput): Structure containing the following field:
121-
- `plaintext_item` (dict[str, Any]): The plaintext DynamoDB item.
132+
- `plaintext_item` (dict[str, Any]): The item to encrypt formatted as DynamoDB JSON.
122133
123134
Returns:
124135
EncryptItemOutput: Structure containing the following fields:
125-
- `encrypted_item` (dict[str, Any]): The encrypted DynamoDB item.
136+
- `encrypted_item` (dict[str, Any]): The encrypted DynamoDB item formatted as DynamoDB JSON.
126137
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (`aws_dbe_head` value).
127138
128139
Example:
@@ -141,33 +152,38 @@ def encrypt_item(
141152

142153
def decrypt_python_item(
143154
self,
144-
encrypted_dict_item: dict[str, Any]
155+
encrypted_dict_item: dict[str, Any],
145156
) -> DecryptItemOutput:
146157
"""
147158
Decrypt a Python dictionary.
148-
This will convert the Python dictionary into a DynamoDB item, then decrypt the item.
149159
150-
boto3 DynamoDB Tables and Resources expect items formatted as native Python dictionaries.
151-
Use this method to decrypt an item you retrieved using a boto3 DynamoDB Table or Resource interface.
160+
This method will transform the Python dictionary into DynamoDB JSON,
161+
decrypt the DynamoDB JSON,
162+
transform the plaintext DynamoDB JSON into a plaintext Python dictionary,
163+
then return the plaintext Python dictionary.
164+
165+
boto3 DynamoDB Tables and Resources return items formatted as native Python dictionaries.
166+
Use this method to decrypt an item if you retrieve the encrypted item
167+
from a boto3 DynamoDB Table or Resource interface.
168+
(Alternatively, you can use this library's EncryptedTable or EncryptedResource interfaces
169+
to transparently decrypt items without an intermediary ItemEncryptor.)
152170
153171
Parameters:
154172
encrypted_dict_item (dict[str, Any]): A standard Python dictionary with encrypted values.
155173
156174
Returns:
157175
DecryptItemOutput: Structure containing the following fields:
158-
- `encrypted_item` (dict[str, Any]): The decrypted Python dictionary.
159-
**Note:** The item was decrypted as a DynamoDB item, then converted back to a native Python item.
160-
- `parsed_header` (Optional[ParsedHeader]): The decrypted DynamoDB item's header (`aws_dbe_head` value).
176+
- `plaintext_item` (dict[str, Any]): The decrypted Python dictionary.
177+
**Note:** The item was decrypted as DynamoDB JSON, then transformed to a Python dictionary.
178+
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (parsed `aws_dbe_head` value).
161179
162180
Example:
163181
164-
>>> plaintext_item = {
165-
... 'some': 'data',
166-
... 'more': 5
182+
>>> encrypted_item = {
183+
... 'some': b'ENCRYPTED_DATA',
184+
... 'more': b'ENCRYPTED_DATA',
167185
... }
168-
>>> encrypted_item = item_encryptor.encrypt_python_item(
169-
... plaintext_dict_item = plaintext_item,
170-
... )
186+
>>> plaintext_item, header = item_encryptor.decrypt_python_item(encrypted_item)
171187
"""
172188
encrypted_ddb_item = dict_to_ddb(encrypted_dict_item)
173189
plaintext_ddb_item: DecryptItemOutput = self.decrypt_dynamodb_item(encrypted_ddb_item)
@@ -179,20 +195,24 @@ def decrypt_python_item(
179195

180196
def decrypt_dynamodb_item(
181197
self,
182-
encrypted_dynamodb_item: dict[str, Any]
198+
encrypted_dynamodb_item: dict[str, dict[str, Any]],
183199
) -> DecryptItemOutput:
184200
"""
185-
Decrypt a DynamoDB item.
201+
Decrypt DynamoDB-formatted JSON.
186202
187-
boto3 DynamoDB clients expect items formatted as DynamoDB items.
188-
Use this method to decrypt an item you retrieved using a boto3 DynamoDB client.
203+
boto3 DynamoDB clients return items formatted as DynamoDB JSON:
204+
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html
205+
Use this method to decrypt an item if you retrieved the encrypted item
206+
from a boto3 DynamoDB client.
207+
(Alternatively, you can use this library's EncryptedClient interface
208+
to transparently decrypt items without an intermediary ItemEncryptor.)
189209
190210
Parameters:
191-
encrypted_ddb_item (dict[str, Any]): A dictionary representing an encrypted DynamoDB item.
211+
encrypted_ddb_item (dict[str, dict[str, Any]]): The item to decrypt formatted as DynamoDB JSON.
192212
193213
Returns:
194214
DecryptItemOutput: Structure containing the following fields:
195-
- `plaintext_item` (dict[str, Any]): The plaintext DynamoDB item.
215+
- `plaintext_item` (dict[str, Any]): The plaintext DynamoDB item formatted as DynamoDB JSON.
196216
- `parsed_header` (Optional[ParsedHeader]): The decrypted DynamoDB item's header (`aws_dbe_head` value).
197217
198218
Example:
@@ -201,9 +221,7 @@ def decrypt_dynamodb_item(
201221
... 'some': {'B': b'ENCRYPTED_DATA'},
202222
... 'more': {'B': b'ENCRYPTED_DATA'}
203223
... }
204-
>>> decrypted_item = item_encryptor.decrypt_dynamodb_item(
205-
... encrypted_ddb_item = encrypted_item,
206-
... )
224+
>>> decrypted_item, header = item_encryptor.decrypt_dynamodb_item(encrypted_item)
207225
"""
208226
return self.decrypt_item(
209227
DecryptItemInput(
@@ -213,18 +231,21 @@ def decrypt_dynamodb_item(
213231

214232
def decrypt_item(
215233
self,
216-
decrypt_item_input: DecryptItemInput
234+
decrypt_item_input: DecryptItemInput,
217235
) -> DecryptItemOutput:
218236
"""
219237
Decrypt a DynamoDB item.
220238
239+
The input item should contain a dictionary formatted as DynamoDB JSON:
240+
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html
241+
221242
Parameters:
222243
decrypt_item_input (DecryptItemInput): Structure containing the following field:
223-
- `encrypted_item` (dict[str, Any]): The encrypted DynamoDB item.
244+
- `encrypted_item` (dict[str, Any]): The item to decrypt formatted as DynamoDB JSON.
224245
225246
Returns:
226247
DecryptItemOutput: Structure containing the following fields:
227-
- `plaintext_item` (dict[str, Any]): The plaintext DynamoDB item.
248+
- `plaintext_item` (dict[str, Any]): The decrypted DynamoDB item formatted as DynamoDB JSON.
228249
- `parsed_header` (Optional[ParsedHeader]): The decrypted DynamoDB item's header (`aws_dbe_head` value).
229250
230251
Example:

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
import re
4-
from collections import namedtuple
5-
63
from boto3.exceptions import (
74
DynamoDBNeedsConditionError,
85
)

0 commit comments

Comments
 (0)