40
40
}
41
41
42
42
43
- def create_kms_client (aws_region = "us-west-2" ):
44
- """Create an AWS KMS client.
45
-
46
- Usage: create_kms_client(aws_region)
47
- :param aws_region: AWS region to use for KMS client.
48
- :type aws_region: string
49
- """
50
- # Create a boto3 client for KMS.
51
- kms_client = boto3 .client ('kms' , region_name = aws_region )
52
-
53
- return kms_client
54
-
55
-
56
43
def create_keyring (
57
- kms_key_id : str
44
+ kms_key_id : str ,
45
+ aws_region = "us-west-2"
58
46
):
59
47
"""Demonstrate how to create an AWS KMS keyring.
60
48
@@ -66,7 +54,7 @@ def create_keyring(
66
54
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
67
55
"""
68
56
# Create a boto3 client for KMS.
69
- kms_client = create_kms_client ( )
57
+ kms_client = boto3 . client ( 'kms' , region_name = aws_region )
70
58
71
59
# Create a KMS keyring
72
60
mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
@@ -85,53 +73,6 @@ def create_keyring(
85
73
return keyring
86
74
87
75
88
- def encrypt_using_keyring (
89
- plaintext_data : bytes ,
90
- keyring : IKeyring ,
91
- client : aws_encryption_sdk .EncryptionSDKClient
92
- ):
93
- """Demonstrate how to encrypt plaintext data using an AWS KMS keyring.
94
-
95
- Usage: encrypt_using_keyring(plaintext_data, keyring)
96
- :param plaintext_data: plaintext data you want to encrypt
97
- :type: bytes
98
- :param keyring: Keyring to use for encryption.
99
- :type keyring: IKeyring
100
- :param client: AWS Encryption SDK client.
101
- :type client: aws_encryption_sdk.EncryptionSDKClient
102
- """
103
- ciphertext_data , _ = client .encrypt (
104
- source = plaintext_data ,
105
- keyring = keyring ,
106
- encryption_context = DEFAULT_ENCRYPTION_CONTEXT
107
- )
108
-
109
- return ciphertext_data
110
-
111
-
112
- def decrypt_using_keyring (
113
- ciphertext_data : bytes ,
114
- keyring : IKeyring ,
115
- client : aws_encryption_sdk .EncryptionSDKClient
116
- ):
117
- """Demonstrate how to decrypt ciphertext data using an AWS KMS keyring.
118
-
119
- Usage: decrypt_using_keyring(ciphertext_data, keyring)
120
- :param ciphertext_data: ciphertext data you want to decrypt
121
- :type: bytes
122
- :param keyring: Keyring to use for decryption.
123
- :type keyring: IKeyring
124
- :param client: AWS Encryption SDK client.
125
- :type client: aws_encryption_sdk.EncryptionSDKClient
126
- """
127
- decrypted_plaintext_data , _ = client .decrypt (
128
- source = ciphertext_data ,
129
- keyring = keyring
130
- )
131
-
132
- return decrypted_plaintext_data
133
-
134
-
135
76
def create_key_provider (
136
77
kms_key_id : str
137
78
):
@@ -152,53 +93,6 @@ def create_key_provider(
152
93
return key_provider
153
94
154
95
155
- def encrypt_using_key_provider (
156
- plaintext_data : bytes ,
157
- key_provider : aws_encryption_sdk .key_providers .base .MasterKeyProvider ,
158
- client : aws_encryption_sdk .EncryptionSDKClient
159
- ):
160
- """Demonstrate how to encrypt plaintext data using an AWS KMS master key provider.
161
-
162
- Usage: encrypt_using_key_provider(plaintext_data, key_provider)
163
- :param plaintext_data: plaintext data you want to encrypt
164
- :type: bytes
165
- :param key_provider: Master key provider to use for encryption.
166
- :type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
167
- :param client: AWS Encryption SDK client.
168
- :type client: aws_encryption_sdk.EncryptionSDKClient
169
- """
170
- ciphertext_data , _ = client .encrypt (
171
- source = plaintext_data ,
172
- key_provider = key_provider ,
173
- encryption_context = DEFAULT_ENCRYPTION_CONTEXT
174
- )
175
-
176
- return ciphertext_data
177
-
178
-
179
- def decrypt_using_key_provider (
180
- ciphertext_data : bytes ,
181
- key_provider : aws_encryption_sdk .key_providers .base .MasterKeyProvider ,
182
- client : aws_encryption_sdk .EncryptionSDKClient
183
- ):
184
- """Demonstrate how to decrypt ciphertext data using an AWS KMS master key provider.
185
-
186
- Usage: decrypt_using_key_provider(ciphertext_data, key_provider)
187
- :param ciphertext_data: ciphertext data you want to decrypt
188
- :type: bytes
189
- :param key_provider: Master key provider to use for decryption.
190
- :type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
191
- :param client: AWS Encryption SDK client.
192
- :type client: aws_encryption_sdk.EncryptionSDKClient
193
- """
194
- decrypted_plaintext_data , _ = client .decrypt (
195
- source = ciphertext_data ,
196
- key_provider = key_provider
197
- )
198
-
199
- return decrypted_plaintext_data
200
-
201
-
202
96
def migration_aws_kms_key (
203
97
kms_key_id : str
204
98
):
@@ -221,17 +115,17 @@ def migration_aws_kms_key(
221
115
aws_kms_master_key_provider = create_key_provider (kms_key_id = kms_key_id )
222
116
223
117
# 2a. Encrypt EXAMPLE_DATA using AWS KMS Keyring
224
- ciphertext_keyring = encrypt_using_keyring (
225
- plaintext_data = EXAMPLE_DATA ,
118
+ ciphertext_keyring , _ = client . encrypt (
119
+ source = EXAMPLE_DATA ,
226
120
keyring = aws_kms_keyring ,
227
- client = client
121
+ encryption_context = DEFAULT_ENCRYPTION_CONTEXT
228
122
)
229
123
230
124
# 2b. Encrypt EXAMPLE_DATA using AWS KMS Master Key Provider
231
- ciphertext_mkp = encrypt_using_key_provider (
232
- plaintext_data = EXAMPLE_DATA ,
125
+ ciphertext_mkp , _ = client . encrypt (
126
+ source = EXAMPLE_DATA ,
233
127
key_provider = aws_kms_master_key_provider ,
234
- client = client
128
+ encryption_context = DEFAULT_ENCRYPTION_CONTEXT
235
129
)
236
130
237
131
# Note: The ciphertexts obtained by encrypting EXAMPLE_DATA using keyring and MKP
@@ -241,16 +135,14 @@ def migration_aws_kms_key(
241
135
242
136
# 3. Decrypt the ciphertext_keyring using both the keyring and MKP and ensure the
243
137
# resulting plaintext is the same and also equal to EXAMPLE_DATA
244
- decrypted_ciphertext_keyring_using_keyring = decrypt_using_keyring (
245
- ciphertext_data = ciphertext_keyring ,
246
- keyring = aws_kms_keyring ,
247
- client = client
138
+ decrypted_ciphertext_keyring_using_keyring , _ = client .decrypt (
139
+ source = ciphertext_keyring ,
140
+ keyring = aws_kms_keyring
248
141
)
249
142
250
- decrypted_ciphertext_keyring_using_mkp = decrypt_using_key_provider (
251
- ciphertext_data = ciphertext_keyring ,
252
- key_provider = aws_kms_master_key_provider ,
253
- client = client
143
+ decrypted_ciphertext_keyring_using_mkp , _ = client .decrypt (
144
+ source = ciphertext_keyring ,
145
+ key_provider = aws_kms_master_key_provider
254
146
)
255
147
256
148
assert decrypted_ciphertext_keyring_using_keyring == decrypted_ciphertext_keyring_using_mkp \
@@ -259,16 +151,14 @@ def migration_aws_kms_key(
259
151
260
152
# 4. Decrypt the ciphertext_mkp using both the keyring and MKP and ensure the
261
153
# resulting plaintext is the same and also equal to EXAMPLE_DATA
262
- decrypted_ciphertext_mkp_using_keyring = decrypt_using_keyring (
263
- ciphertext_data = ciphertext_mkp ,
264
- keyring = aws_kms_keyring ,
265
- client = client
154
+ decrypted_ciphertext_mkp_using_keyring , _ = client .decrypt (
155
+ source = ciphertext_mkp ,
156
+ keyring = aws_kms_keyring
266
157
)
267
158
268
- decrypted_ciphertext_mkp_using_mkp = decrypt_using_key_provider (
269
- ciphertext_data = ciphertext_mkp ,
270
- key_provider = aws_kms_master_key_provider ,
271
- client = client
159
+ decrypted_ciphertext_mkp_using_mkp , _ = client .decrypt (
160
+ source = ciphertext_mkp ,
161
+ key_provider = aws_kms_master_key_provider
272
162
)
273
163
274
164
assert decrypted_ciphertext_mkp_using_keyring == decrypted_ciphertext_mkp_using_mkp \
0 commit comments