12
12
# language governing permissions and limitations under the License.
13
13
"""Example showing use of AWS KMS CMP with EncryptedClient."""
14
14
import boto3
15
+
15
16
from dynamodb_encryption_sdk .encrypted .client import EncryptedClient
16
17
from dynamodb_encryption_sdk .identifiers import CryptoAction
17
18
from dynamodb_encryption_sdk .material_providers .aws_kms import AwsKmsCryptographicMaterialsProvider
20
21
21
22
def encrypt_item (table_name , aws_cmk_id ):
22
23
"""Demonstrate use of EncryptedClient to transparently encrypt an item."""
23
- index_key = {
24
- 'partition_attribute' : {'S' : 'is this' },
25
- 'sort_attribute' : {'N' : '55' }
26
- }
24
+ index_key = {"partition_attribute" : {"S" : "is this" }, "sort_attribute" : {"N" : "55" }}
27
25
plaintext_item = {
28
- ' example' : {'S' : ' data' },
29
- ' some numbers' : {'N' : '99' },
30
- ' and some binary' : {'B' : b' \x00 \x01 \x02 ' },
31
- ' leave me' : {'S' : ' alone' } # We want to ignore this attribute
26
+ " example" : {"S" : " data" },
27
+ " some numbers" : {"N" : "99" },
28
+ " and some binary" : {"B" : b" \x00 \x01 \x02 " },
29
+ " leave me" : {"S" : " alone" }, # We want to ignore this attribute
32
30
}
33
31
# Collect all of the attributes that will be encrypted (used later).
34
32
encrypted_attributes = set (plaintext_item .keys ())
35
- encrypted_attributes .remove (' leave me' )
33
+ encrypted_attributes .remove (" leave me" )
36
34
# Collect all of the attributes that will not be encrypted (used later).
37
35
unencrypted_attributes = set (index_key .keys ())
38
- unencrypted_attributes .add (' leave me' )
36
+ unencrypted_attributes .add (" leave me" )
39
37
# Add the index pairs to the item.
40
38
plaintext_item .update (index_key )
41
39
42
40
# Create a normal client.
43
- client = boto3 .client (' dynamodb' )
41
+ client = boto3 .client (" dynamodb" )
44
42
# Create a crypto materials provider using the specified AWS KMS key.
45
43
aws_kms_cmp = AwsKmsCryptographicMaterialsProvider (key_id = aws_cmk_id )
46
44
# Create attribute actions that tells the encrypted client to encrypt all attributes except one.
47
45
actions = AttributeActions (
48
- default_action = CryptoAction .ENCRYPT_AND_SIGN ,
49
- attribute_actions = {
50
- 'leave me' : CryptoAction .DO_NOTHING
51
- }
46
+ default_action = CryptoAction .ENCRYPT_AND_SIGN , attribute_actions = {"leave me" : CryptoAction .DO_NOTHING }
52
47
)
53
48
# Use these objects to create an encrypted client.
54
- encrypted_client = EncryptedClient (
55
- client = client ,
56
- materials_provider = aws_kms_cmp ,
57
- attribute_actions = actions
58
- )
49
+ encrypted_client = EncryptedClient (client = client , materials_provider = aws_kms_cmp , attribute_actions = actions )
59
50
60
51
# Put the item to the table, using the encrypted client to transparently encrypt it.
61
52
encrypted_client .put_item (TableName = table_name , Item = plaintext_item )
62
53
63
54
# Get the encrypted item using the standard client.
64
- encrypted_item = client .get_item (TableName = table_name , Key = index_key )[' Item' ]
55
+ encrypted_item = client .get_item (TableName = table_name , Key = index_key )[" Item" ]
65
56
66
57
# Get the item using the encrypted client, transparently decyrpting it.
67
- decrypted_item = encrypted_client .get_item (TableName = table_name , Key = index_key )[' Item' ]
58
+ decrypted_item = encrypted_client .get_item (TableName = table_name , Key = index_key )[" Item" ]
68
59
69
60
# Verify that all of the attributes are different in the encrypted item
70
61
for name in encrypted_attributes :
@@ -82,28 +73,16 @@ def encrypt_item(table_name, aws_cmk_id):
82
73
def encrypt_batch_items (table_name , aws_cmk_id ):
83
74
"""Demonstrate use of EncryptedClient to transparently encrypt multiple items in a batch request."""
84
75
index_keys = [
85
- {
86
- 'partition_attribute' : {'S' : 'is this' },
87
- 'sort_attribute' : {'N' : '55' }
88
- },
89
- {
90
- 'partition_attribute' : {'S' : 'is this' },
91
- 'sort_attribute' : {'N' : '56' }
92
- },
93
- {
94
- 'partition_attribute' : {'S' : 'is this' },
95
- 'sort_attribute' : {'N' : '57' }
96
- },
97
- {
98
- 'partition_attribute' : {'S' : 'another' },
99
- 'sort_attribute' : {'N' : '55' }
100
- }
76
+ {"partition_attribute" : {"S" : "is this" }, "sort_attribute" : {"N" : "55" }},
77
+ {"partition_attribute" : {"S" : "is this" }, "sort_attribute" : {"N" : "56" }},
78
+ {"partition_attribute" : {"S" : "is this" }, "sort_attribute" : {"N" : "57" }},
79
+ {"partition_attribute" : {"S" : "another" }, "sort_attribute" : {"N" : "55" }},
101
80
]
102
81
plaintext_additional_attributes = {
103
- ' example' : {'S' : ' data' },
104
- ' some numbers' : {'N' : '99' },
105
- ' and some binary' : {'B' : b' \x00 \x01 \x02 ' },
106
- ' leave me' : {'S' : ' alone' } # We want to ignore this attribute
82
+ " example" : {"S" : " data" },
83
+ " some numbers" : {"N" : "99" },
84
+ " and some binary" : {"B" : b" \x00 \x01 \x02 " },
85
+ " leave me" : {"S" : " alone" }, # We want to ignore this attribute
107
86
}
108
87
plaintext_items = []
109
88
for key in index_keys :
@@ -113,43 +92,34 @@ def encrypt_batch_items(table_name, aws_cmk_id):
113
92
114
93
# Collect all of the attributes that will be encrypted (used later).
115
94
encrypted_attributes = set (plaintext_additional_attributes .keys ())
116
- encrypted_attributes .remove (' leave me' )
95
+ encrypted_attributes .remove (" leave me" )
117
96
# Collect all of the attributes that will not be encrypted (used later).
118
97
unencrypted_attributes = set (index_keys [0 ].keys ())
119
- unencrypted_attributes .add (' leave me' )
98
+ unencrypted_attributes .add (" leave me" )
120
99
121
100
# Create a normal client.
122
- client = boto3 .client (' dynamodb' )
101
+ client = boto3 .client (" dynamodb" )
123
102
# Create a crypto materials provider using the specified AWS KMS key.
124
103
aws_kms_cmp = AwsKmsCryptographicMaterialsProvider (key_id = aws_cmk_id )
125
104
# Create attribute actions that tells the encrypted client to encrypt all attributes except one.
126
105
actions = AttributeActions (
127
- default_action = CryptoAction .ENCRYPT_AND_SIGN ,
128
- attribute_actions = {
129
- 'leave me' : CryptoAction .DO_NOTHING
130
- }
106
+ default_action = CryptoAction .ENCRYPT_AND_SIGN , attribute_actions = {"leave me" : CryptoAction .DO_NOTHING }
131
107
)
132
108
# Use these objects to create an encrypted client.
133
- encrypted_client = EncryptedClient (
134
- client = client ,
135
- materials_provider = aws_kms_cmp ,
136
- attribute_actions = actions
137
- )
109
+ encrypted_client = EncryptedClient (client = client , materials_provider = aws_kms_cmp , attribute_actions = actions )
138
110
139
111
# Put the items to the table, using the encrypted client to transparently encrypt them.
140
- encrypted_client .batch_write_item (RequestItems = {
141
- table_name : [{' PutRequest' : {' Item' : item }} for item in plaintext_items ]
142
- } )
112
+ encrypted_client .batch_write_item (
113
+ RequestItems = { table_name : [{" PutRequest" : {" Item" : item }} for item in plaintext_items ]}
114
+ )
143
115
144
116
# Get the encrypted item using the standard client.
145
- encrypted_items = client .batch_get_item (
146
- RequestItems = {table_name : {'Keys' : index_keys }}
147
- )['Responses' ][table_name ]
117
+ encrypted_items = client .batch_get_item (RequestItems = {table_name : {"Keys" : index_keys }})["Responses" ][table_name ]
148
118
149
119
# Get the item using the encrypted client, transparently decyrpting it.
150
- decrypted_items = encrypted_client .batch_get_item (
151
- RequestItems = { table_name : { 'Keys' : index_keys }}
152
- )[ 'Responses' ][ table_name ]
120
+ decrypted_items = encrypted_client .batch_get_item (RequestItems = { table_name : { "Keys" : index_keys }})[ "Responses" ][
121
+ table_name
122
+ ]
153
123
154
124
def _select_index_from_item (item ):
155
125
"""Find the index keys that match this item."""
@@ -178,6 +148,6 @@ def _select_item_from_index(index, all_items):
178
148
assert decrypted_item [name ] == encrypted_item [name ] == plaintext_item [name ]
179
149
180
150
# Clean up the item
181
- encrypted_client .batch_write_item (RequestItems = {
182
- table_name : [{' DeleteRequest' : {' Key' : key }} for key in index_keys ]
183
- } )
151
+ encrypted_client .batch_write_item (
152
+ RequestItems = { table_name : [{" DeleteRequest" : {" Key" : key }} for key in index_keys ]}
153
+ )
0 commit comments