Skip to content

Commit 189cdfc

Browse files
m
1 parent ff29a05 commit 189cdfc

17 files changed

+100
-127
lines changed

examples/src/aws_kms_discovery_keyring_example.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,12 @@ def encrypt_and_decrypt_with_keyring(
155155
# If all calls to KMS fail, the decryption fails.
156156
plaintext_bytes, dec_header = client.decrypt(
157157
source=ciphertext,
158-
keyring=discovery_keyring
158+
keyring=discovery_keyring,
159+
# Verify that the encryption context in the result contains the
160+
# encryption context supplied to the encryptData method
161+
encryption_context=encryption_context,
159162
)
160163

161-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
162-
# (This is an example for demonstration; you do not need to do this in your own code.)
163-
for k, v in encryption_context.items():
164-
assert v == dec_header.encryption_context[k], \
165-
"Encryption context does not match expected values"
166-
167-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
168-
# (This is an example for demonstration; you do not need to do this in your own code.)
169-
assert plaintext_bytes == EXAMPLE_DATA, \
170-
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
171-
172164
# 11. Demonstrate that if a discovery keyring (Bob's) doesn't have the correct AWS Account ID's,
173165
# the decrypt will fail with an error message
174166
# Note that this assumes Account ID used here ('888888888888') is different than the one used
@@ -192,7 +184,7 @@ def encrypt_and_decrypt_with_keyring(
192184
try:
193185
plaintext_bytes, _ = client.decrypt(
194186
source=ciphertext,
195-
keyring=discovery_keyring_bob
187+
keyring=discovery_keyring_bob,
196188
)
197189

198190
raise AssertionError("Decrypt using discovery keyring with wrong AWS Account ID should"

examples/src/aws_kms_discovery_multi_keyring_example.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,8 @@ def encrypt_and_decrypt_with_keyring(
153153
# KMS Discovery Keyrings will attempt to decrypt Multi Region Keys (MRKs) and regular KMS Keys.
154154
plaintext_bytes, dec_header = client.decrypt(
155155
source=ciphertext,
156-
keyring=discovery_multi_keyring
156+
keyring=discovery_multi_keyring,
157+
# Verify that the encryption context in the result contains the
158+
# encryption context supplied to the encryptData method
159+
encryption_context=encryption_context,
157160
)
158-
159-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
160-
# (This is an example for demonstration; you do not need to do this in your own code.)
161-
for k, v in encryption_context.items():
162-
assert v == dec_header.encryption_context[k], \
163-
"Encryption context does not match expected values"
164-
165-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
166-
# (This is an example for demonstration; you do not need to do this in your own code.)
167-
assert plaintext_bytes == EXAMPLE_DATA, \
168-
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/aws_kms_keyring_example.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,14 @@ def encrypt_and_decrypt_with_keyring(
9797
"Ciphertext and plaintext data are the same. Invalid encryption"
9898

9999
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
100-
plaintext_bytes, dec_header = client.decrypt(
100+
plaintext_bytes, _ = client.decrypt(
101101
source=ciphertext,
102-
keyring=kms_keyring
102+
keyring=kms_keyring,
103+
# Verify that the encryption context in the result contains the
104+
# encryption context supplied to the encryptData method
105+
encryption_context=encryption_context,
103106
)
104107

105-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
106-
# (This is an example for demonstration; you do not need to do this in your own code.)
107-
for k, v in encryption_context.items():
108-
assert v == dec_header.encryption_context[k], \
109-
"Encryption context does not match expected values"
110-
111108
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
112109
# (This is an example for demonstration; you do not need to do this in your own code.)
113110
assert plaintext_bytes == EXAMPLE_DATA, \

examples/src/aws_kms_mrk_discovery_keyring_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,12 @@ def encrypt_and_decrypt_with_keyring(
165165
# 7. Decrypt your encrypted data using the discovery keyring.
166166
plaintext_bytes, dec_header = client.decrypt(
167167
source=ciphertext,
168-
keyring=decrypt_discovery_keyring
168+
keyring=decrypt_discovery_keyring,
169+
# Verify that the encryption context in the result contains the
170+
# encryption context supplied to the encryptData method
171+
encryption_context=encryption_context,
169172
)
170173

171-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
172-
# (This is an example for demonstration; you do not need to do this in your own code.)
173-
for k, v in encryption_context.items():
174-
assert v == dec_header.encryption_context[k], \
175-
"Encryption context does not match expected values"
176-
177174
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
178175
# (This is an example for demonstration; you do not need to do this in your own code.)
179176
assert plaintext_bytes == EXAMPLE_DATA

examples/src/aws_kms_mrk_discovery_multi_keyring_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,12 @@ def encrypt_and_decrypt_with_keyring(
174174
# Multi Region Keys (MRKs) and regular KMS Keys.
175175
plaintext_bytes, dec_header = client.decrypt(
176176
source=ciphertext,
177-
keyring=decrypt_discovery_keyring
177+
keyring=decrypt_discovery_keyring,
178+
# Verify that the encryption context in the result contains the
179+
# encryption context supplied to the encryptData method
180+
encryption_context=encryption_context,
178181
)
179182

180-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
181-
# (This is an example for demonstration; you do not need to do this in your own code.)
182-
for k, v in encryption_context.items():
183-
assert v == dec_header.encryption_context[k], \
184-
"Encryption context does not match expected values"
185-
186183
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
187184
# (This is an example for demonstration; you do not need to do this in your own code.)
188185
assert plaintext_bytes == EXAMPLE_DATA

examples/src/aws_kms_mrk_keyring_example.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,14 @@ def encrypt_and_decrypt_with_keyring(
132132
)
133133

134134
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
135-
plaintext_bytes, dec_header = client.decrypt(
135+
plaintext_bytes, _ = client.decrypt(
136136
source=ciphertext,
137-
keyring=decrypt_keyring
137+
keyring=decrypt_keyring,
138+
# Verify that the encryption context in the result contains the
139+
# encryption context supplied to the encryptData method
140+
encryption_context=encryption_context,
138141
)
139142

140-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
141-
# (This is an example for demonstration; you do not need to do this in your own code.)
142-
for k, v in encryption_context.items():
143-
assert v == dec_header.encryption_context[k], \
144-
"Encryption context does not match expected values"
145-
146143
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
147144
# (This is an example for demonstration; you do not need to do this in your own code.)
148145
assert plaintext_bytes == EXAMPLE_DATA, \

examples/src/aws_kms_mrk_multi_keyring_example.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,12 @@ def encrypt_and_decrypt_with_keyring(
126126
# the first available KMS key on the keyring that is capable of decrypting the data.
127127
plaintext_bytes, dec_header = client.decrypt(
128128
source=ciphertext,
129-
keyring=kms_mrk_multi_keyring
129+
keyring=kms_mrk_multi_keyring,
130+
# Verify that the encryption context in the result contains the
131+
# encryption context supplied to the encryptData method
132+
encryption_context=encryption_context,
130133
)
131134

132-
# 7. Demonstrate that the encryption context is correct in the decrypted message header
133-
# (This is an example for demonstration; you do not need to do this in your own code.)
134-
for k, v in encryption_context.items():
135-
assert v == dec_header.encryption_context[k], \
136-
"Encryption context does not match expected values"
137-
138135
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
139136
# (This is an example for demonstration; you do not need to do this in your own code.)
140137
assert plaintext_bytes == EXAMPLE_DATA, \
@@ -161,15 +158,12 @@ def encrypt_and_decrypt_with_keyring(
161158
# 10. Decrypt your encrypted data using the second region AwsKmsMrkKeyring
162159
plaintext_bytes_second_region, dec_header_second_region = client.decrypt(
163160
source=ciphertext,
164-
keyring=second_region_mrk_keyring
161+
keyring=second_region_mrk_keyring,
162+
# Verify that the encryption context in the result contains the
163+
# encryption context supplied to the encryptData method
164+
encryption_context=encryption_context,
165165
)
166166

167-
# 11. Demonstrate that the encryption context is correct in the decrypted message header
168-
# (This is an example for demonstration; you do not need to do this in your own code.)
169-
for k, v in encryption_context.items():
170-
assert v == dec_header_second_region.encryption_context[k], \
171-
"Encryption context does not match expected values"
172-
173167
# 12. Demonstrate that the decrypted plaintext is identical to the original plaintext.
174168
# (This is an example for demonstration; you do not need to do this in your own code.)
175169
assert plaintext_bytes_second_region == EXAMPLE_DATA

examples/src/aws_kms_multi_keyring_example.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def encrypt_and_decrypt_with_keyring(
133133
# 6a. Decrypt your encrypted data using the same multi_keyring you used on encrypt.
134134
plaintext_bytes_multi_keyring, _ = client.decrypt(
135135
source=ciphertext,
136-
keyring=kms_multi_keyring
136+
keyring=kms_multi_keyring,
137+
# Verify that the encryption context in the result contains the
138+
# encryption context supplied to the encryptData method
139+
encryption_context=encryption_context,
137140
)
138141

139142
# 6b. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -164,7 +167,10 @@ def encrypt_and_decrypt_with_keyring(
164167
# 7c. Decrypt your encrypted data using the default_region_kms_keyring.
165168
plaintext_bytes_default_region_kms_keyring, _ = client.decrypt(
166169
source=ciphertext,
167-
keyring=default_region_kms_keyring
170+
keyring=default_region_kms_keyring,
171+
# Verify that the encryption context in the result contains the
172+
# encryption context supplied to the encryptData method
173+
encryption_context=encryption_context,
168174
)
169175

170176
# 7d. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -192,7 +198,10 @@ def encrypt_and_decrypt_with_keyring(
192198
# 8c. Decrypt your encrypted data using the second_region_kms_keyring.
193199
plaintext_bytes_second_region_kms_keyring, _ = client.decrypt(
194200
source=ciphertext,
195-
keyring=second_region_kms_keyring
201+
keyring=second_region_kms_keyring,
202+
# Verify that the encryption context in the result contains the
203+
# encryption context supplied to the encryptData method
204+
encryption_context=encryption_context,
196205
)
197206

198207
# 8d. Demonstrate that the decrypted plaintext is identical to the original plaintext.

examples/src/aws_kms_rsa_keyring_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,12 @@ def encrypt_and_decrypt_with_keyring(
105105
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
106106
plaintext_bytes, dec_header = client.decrypt(
107107
source=ciphertext,
108-
keyring=kms_rsa_keyring
108+
keyring=kms_rsa_keyring,
109+
# Verify that the encryption context in the result contains the
110+
# encryption context supplied to the encryptData method
111+
encryption_context=encryption_context,
109112
)
110113

111-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
112-
# (This is an example for demonstration; you do not need to do this in your own code.)
113-
for k, v in encryption_context.items():
114-
assert v == dec_header.encryption_context[k], \
115-
"Encryption context does not match expected values"
116-
117114
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
118115
# (This is an example for demonstration; you do not need to do this in your own code.)
119116
assert plaintext_bytes == EXAMPLE_DATA, \

examples/src/default_cryptographic_materials_manager_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,12 @@ def encrypt_and_decrypt_with_default_cmm(
111111
# 7. Decrypt your encrypted data using the same cmm you used on encrypt.
112112
plaintext_bytes, dec_header = client.decrypt(
113113
source=ciphertext,
114-
materials_manager=cmm
114+
materials_manager=cmm,
115+
# Verify that the encryption context in the result contains the
116+
# encryption context supplied to the encryptData method
117+
encryption_context=encryption_context,
115118
)
116119

117-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
118-
# (This is an example for demonstration; you do not need to do this in your own code.)
119-
for k, v in encryption_context.items():
120-
assert v == dec_header.encryption_context[k], \
121-
"Encryption context does not match expected values"
122-
123120
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
124121
# (This is an example for demonstration; you do not need to do this in your own code.)
125122
assert plaintext_bytes == EXAMPLE_DATA, \

examples/src/file_streaming_example.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ def encrypt_and_decrypt_with_keyring(
134134
for chunk in decryptor:
135135
pt_file.write(chunk)
136136

137-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
138-
# (This is an example for demonstration; you do not need to do this in your own code.)
139-
for k, v in encryption_context.items():
140-
assert v == decryptor.header.encryption_context[k], \
141-
"Encryption context does not match expected values"
142-
143137
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
144138
# (This is an example for demonstration; you do not need to do this in your own code.)
145139
assert filecmp.cmp(plaintext_filename, decrypted_filename), \

examples/src/hierarchical_keyring_example.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def encrypt_and_decrypt_with_keyring(
200200
try:
201201
client.decrypt(
202202
source=ciphertext_a,
203-
keyring=hierarchical_keyring_b
203+
keyring=hierarchical_keyring_b,
204+
# Verify that the encryption context in the result contains the
205+
# encryption context supplied to the encryptData method
206+
encryption_context=encryption_context_a,
204207
)
205208
except AWSEncryptionSDKClientError:
206209
pass
@@ -210,7 +213,10 @@ def encrypt_and_decrypt_with_keyring(
210213
try:
211214
client.decrypt(
212215
source=ciphertext_b,
213-
keyring=hierarchical_keyring_a
216+
keyring=hierarchical_keyring_a,
217+
# Verify that the encryption context in the result contains the
218+
# encryption context supplied to the encryptData method
219+
encryption_context=encryption_context_b,
214220
)
215221
except AWSEncryptionSDKClientError:
216222
pass
@@ -219,13 +225,20 @@ def encrypt_and_decrypt_with_keyring(
219225
# and that the decrypted data matches the input data.
220226
plaintext_bytes_a, _ = client.decrypt(
221227
source=ciphertext_a,
222-
keyring=hierarchical_keyring_a
228+
keyring=hierarchical_keyring_a,
229+
# Verify that the encryption context in the result contains the
230+
# encryption context supplied to the encryptData method
231+
encryption_context=encryption_context_a,
223232
)
224233
assert plaintext_bytes_a == EXAMPLE_DATA, \
225234
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
235+
226236
plaintext_bytes_b, _ = client.decrypt(
227237
source=ciphertext_b,
228-
keyring=hierarchical_keyring_b
238+
keyring=hierarchical_keyring_b,
239+
# Verify that the encryption context in the result contains the
240+
# encryption context supplied to the encryptData method
241+
encryption_context=encryption_context_b,
229242
)
230243
assert plaintext_bytes_b == EXAMPLE_DATA, \
231244
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/migration/migration_set_commitment_policy_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,12 @@ def encrypt_and_decrypt_with_keyring(
107107
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
108108
plaintext_bytes, dec_header = client.decrypt(
109109
source=ciphertext,
110-
keyring=kms_keyring
110+
keyring=kms_keyring,
111+
# Verify that the encryption context in the result contains the
112+
# encryption context supplied to the encryptData method
113+
encryption_context=encryption_context,
111114
)
112115

113-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
114-
# (This is an example for demonstration; you do not need to do this in your own code.)
115-
for k, v in encryption_context.items():
116-
assert v == dec_header.encryption_context[k], \
117-
"Encryption context does not match expected values"
118-
119116
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
120117
# (This is an example for demonstration; you do not need to do this in your own code.)
121118
assert plaintext_bytes == EXAMPLE_DATA, \

examples/src/multi_keyring_example.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def encrypt_and_decrypt_with_keyring(
164164
# 10a. Decrypt your encrypted data using the same multi_keyring you used on encrypt.
165165
plaintext_bytes_multi_keyring, _ = client.decrypt(
166166
source=ciphertext,
167-
keyring=multi_keyring
167+
keyring=multi_keyring,
168+
# Verify that the encryption context in the result contains the
169+
# encryption context supplied to the encryptData method
170+
encryption_context=encryption_context,
168171
)
169172

170173
# 10b. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -182,7 +185,10 @@ def encrypt_and_decrypt_with_keyring(
182185
# 11a. Decrypt your encrypted data using the kms_keyring.
183186
plaintext_bytes_kms_keyring, _ = client.decrypt(
184187
source=ciphertext,
185-
keyring=kms_keyring
188+
keyring=kms_keyring,
189+
# Verify that the encryption context in the result contains the
190+
# encryption context supplied to the encryptData method
191+
encryption_context=encryption_context,
186192
)
187193

188194
# 11b. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -197,7 +203,10 @@ def encrypt_and_decrypt_with_keyring(
197203
# 12a. Decrypt your encrypted data using the raw_aes_keyring.
198204
plaintext_bytes_raw_aes_keyring, _ = client.decrypt(
199205
source=ciphertext,
200-
keyring=raw_aes_keyring
206+
keyring=raw_aes_keyring,
207+
# Verify that the encryption context in the result contains the
208+
# encryption context supplied to the encryptData method
209+
encryption_context=encryption_context,
201210
)
202211

203212
# 12b. Demonstrate that the decrypted plaintext is identical to the original plaintext.

examples/src/raw_aes_keyring_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,12 @@ def encrypt_and_decrypt_with_keyring():
109109
# 8. Decrypt your encrypted data using the same keyring you used on encrypt.
110110
plaintext_bytes, dec_header = client.decrypt(
111111
source=ciphertext,
112-
keyring=raw_aes_keyring
112+
keyring=raw_aes_keyring,
113+
# Verify that the encryption context in the result contains the
114+
# encryption context supplied to the encryptData method
115+
encryption_context=encryption_context,
113116
)
114117

115-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
116-
# (This is an example for demonstration; you do not need to do this in your own code.)
117-
for k, v in encryption_context.items():
118-
assert v == dec_header.encryption_context[k], \
119-
"Encryption context does not match expected values"
120-
121118
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
122119
# (This is an example for demonstration; you do not need to do this in your own code.)
123120
assert plaintext_bytes == EXAMPLE_DATA, \

0 commit comments

Comments
 (0)