1212# language governing permissions and limitations under the License.
1313"""Example showing use of AWS KMS CMP with EncryptedClient."""
1414import boto3
15+
1516from dynamodb_encryption_sdk .encrypted .client import EncryptedClient
1617from dynamodb_encryption_sdk .identifiers import CryptoAction
1718from dynamodb_encryption_sdk .material_providers .aws_kms import AwsKmsCryptographicMaterialsProvider
2021
2122def encrypt_item (table_name , aws_cmk_id ):
2223 """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" }}
2725 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
3230 }
3331 # Collect all of the attributes that will be encrypted (used later).
3432 encrypted_attributes = set (plaintext_item .keys ())
35- encrypted_attributes .remove (' leave me' )
33+ encrypted_attributes .remove (" leave me" )
3634 # Collect all of the attributes that will not be encrypted (used later).
3735 unencrypted_attributes = set (index_key .keys ())
38- unencrypted_attributes .add (' leave me' )
36+ unencrypted_attributes .add (" leave me" )
3937 # Add the index pairs to the item.
4038 plaintext_item .update (index_key )
4139
4240 # Create a normal client.
43- client = boto3 .client (' dynamodb' )
41+ client = boto3 .client (" dynamodb" )
4442 # Create a crypto materials provider using the specified AWS KMS key.
4543 aws_kms_cmp = AwsKmsCryptographicMaterialsProvider (key_id = aws_cmk_id )
4644 # Create attribute actions that tells the encrypted client to encrypt all attributes except one.
4745 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 }
5247 )
5348 # 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 )
5950
6051 # Put the item to the table, using the encrypted client to transparently encrypt it.
6152 encrypted_client .put_item (TableName = table_name , Item = plaintext_item )
6253
6354 # 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" ]
6556
6657 # 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" ]
6859
6960 # Verify that all of the attributes are different in the encrypted item
7061 for name in encrypted_attributes :
@@ -82,28 +73,16 @@ def encrypt_item(table_name, aws_cmk_id):
8273def encrypt_batch_items (table_name , aws_cmk_id ):
8374 """Demonstrate use of EncryptedClient to transparently encrypt multiple items in a batch request."""
8475 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" }},
10180 ]
10281 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
10786 }
10887 plaintext_items = []
10988 for key in index_keys :
@@ -113,43 +92,34 @@ def encrypt_batch_items(table_name, aws_cmk_id):
11392
11493 # Collect all of the attributes that will be encrypted (used later).
11594 encrypted_attributes = set (plaintext_additional_attributes .keys ())
116- encrypted_attributes .remove (' leave me' )
95+ encrypted_attributes .remove (" leave me" )
11796 # Collect all of the attributes that will not be encrypted (used later).
11897 unencrypted_attributes = set (index_keys [0 ].keys ())
119- unencrypted_attributes .add (' leave me' )
98+ unencrypted_attributes .add (" leave me" )
12099
121100 # Create a normal client.
122- client = boto3 .client (' dynamodb' )
101+ client = boto3 .client (" dynamodb" )
123102 # Create a crypto materials provider using the specified AWS KMS key.
124103 aws_kms_cmp = AwsKmsCryptographicMaterialsProvider (key_id = aws_cmk_id )
125104 # Create attribute actions that tells the encrypted client to encrypt all attributes except one.
126105 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 }
131107 )
132108 # 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 )
138110
139111 # 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+ )
143115
144116 # 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 ]
148118
149119 # 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+ ]
153123
154124 def _select_index_from_item (item ):
155125 """Find the index keys that match this item."""
@@ -178,6 +148,6 @@ def _select_item_from_index(index, all_items):
178148 assert decrypted_item [name ] == encrypted_item [name ] == plaintext_item [name ]
179149
180150 # 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