21
21
)
22
22
23
23
class ItemEncryptor :
24
- """Client providing item-level encryption for DynamoDB items / Python dictionaries."""
24
+ """Class providing item-level encryption for DynamoDB items / Python dictionaries."""
25
25
26
26
_internal_client : DynamoDbItemEncryptor
27
27
@@ -39,33 +39,38 @@ def __init__(
39
39
40
40
def encrypt_python_item (
41
41
self ,
42
- plaintext_dict_item : dict [str , Any ]
42
+ plaintext_dict_item : dict [str , Any ],
43
43
) -> EncryptItemOutput :
44
44
"""
45
45
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.
47
51
48
52
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.)
50
57
51
58
Parameters:
52
59
plaintext_dict_item (dict[str, Any]): A standard Python dictionary.
53
60
54
61
Returns:
55
62
EncryptItemOutput: Structure containing the following fields:
56
63
- `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).
59
66
60
67
Example:
61
68
62
69
>>> plaintext_item = {
63
70
... 'some': 'data',
64
71
... 'more': 5
65
72
... }
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)
69
74
"""
70
75
plaintext_ddb_item = dict_to_ddb (plaintext_dict_item )
71
76
encrypted_ddb_item : EncryptItemOutput = self .encrypt_dynamodb_item (plaintext_ddb_item )
@@ -77,20 +82,25 @@ def encrypt_python_item(
77
82
78
83
def encrypt_dynamodb_item (
79
84
self ,
80
- plaintext_dynamodb_item : dict [str , Any ]
85
+ plaintext_dynamodb_item : dict [str , dict [ str , Any ]],
81
86
) -> EncryptItemOutput :
82
87
"""
83
- Encrypt a DynamoDB item .
88
+ Encrypt DynamoDB-formatted JSON .
84
89
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.)
87
96
88
97
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 .
90
99
91
100
Returns:
92
101
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.
94
104
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (`aws_dbe_head` value).
95
105
96
106
Example:
@@ -99,9 +109,7 @@ def encrypt_dynamodb_item(
99
109
... 'some': {'S': 'data'},
100
110
... 'more': {'N': '5'}
101
111
... }
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)
105
113
"""
106
114
return self .encrypt_item (
107
115
EncryptItemInput (
@@ -111,18 +119,21 @@ def encrypt_dynamodb_item(
111
119
112
120
def encrypt_item (
113
121
self ,
114
- encrypt_item_input : EncryptItemInput
122
+ encrypt_item_input : EncryptItemInput ,
115
123
) -> EncryptItemOutput :
116
124
"""
117
125
Encrypt a DynamoDB item.
118
126
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
+
119
130
Parameters:
120
131
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 .
122
133
123
134
Returns:
124
135
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 .
126
137
- `parsed_header` (Optional[ParsedHeader]): The encrypted DynamoDB item's header (`aws_dbe_head` value).
127
138
128
139
Example:
@@ -141,33 +152,38 @@ def encrypt_item(
141
152
142
153
def decrypt_python_item (
143
154
self ,
144
- encrypted_dict_item : dict [str , Any ]
155
+ encrypted_dict_item : dict [str , Any ],
145
156
) -> DecryptItemOutput :
146
157
"""
147
158
Decrypt a Python dictionary.
148
- This will convert the Python dictionary into a DynamoDB item, then decrypt the item.
149
159
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.)
152
170
153
171
Parameters:
154
172
encrypted_dict_item (dict[str, Any]): A standard Python dictionary with encrypted values.
155
173
156
174
Returns:
157
175
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).
161
179
162
180
Example:
163
181
164
- >>> plaintext_item = {
165
- ... 'some': 'data ',
166
- ... 'more': 5
182
+ >>> encrypted_item = {
183
+ ... 'some': b'ENCRYPTED_DATA ',
184
+ ... 'more': b'ENCRYPTED_DATA',
167
185
... }
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)
171
187
"""
172
188
encrypted_ddb_item = dict_to_ddb (encrypted_dict_item )
173
189
plaintext_ddb_item : DecryptItemOutput = self .decrypt_dynamodb_item (encrypted_ddb_item )
@@ -179,20 +195,24 @@ def decrypt_python_item(
179
195
180
196
def decrypt_dynamodb_item (
181
197
self ,
182
- encrypted_dynamodb_item : dict [str , Any ]
198
+ encrypted_dynamodb_item : dict [str , dict [ str , Any ]],
183
199
) -> DecryptItemOutput :
184
200
"""
185
- Decrypt a DynamoDB item .
201
+ Decrypt DynamoDB-formatted JSON .
186
202
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.)
189
209
190
210
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 .
192
212
193
213
Returns:
194
214
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 .
196
216
- `parsed_header` (Optional[ParsedHeader]): The decrypted DynamoDB item's header (`aws_dbe_head` value).
197
217
198
218
Example:
@@ -201,9 +221,7 @@ def decrypt_dynamodb_item(
201
221
... 'some': {'B': b'ENCRYPTED_DATA'},
202
222
... 'more': {'B': b'ENCRYPTED_DATA'}
203
223
... }
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)
207
225
"""
208
226
return self .decrypt_item (
209
227
DecryptItemInput (
@@ -213,18 +231,21 @@ def decrypt_dynamodb_item(
213
231
214
232
def decrypt_item (
215
233
self ,
216
- decrypt_item_input : DecryptItemInput
234
+ decrypt_item_input : DecryptItemInput ,
217
235
) -> DecryptItemOutput :
218
236
"""
219
237
Decrypt a DynamoDB item.
220
238
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
+
221
242
Parameters:
222
243
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 .
224
245
225
246
Returns:
226
247
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 .
228
249
- `parsed_header` (Optional[ParsedHeader]): The decrypted DynamoDB item's header (`aws_dbe_head` value).
229
250
230
251
Example:
0 commit comments