@@ -33,8 +33,8 @@ def __init__(self, **kwargs):
33
33
def _get_raw_key (self , key_id ):
34
34
"""Retrieves a static, randomly generated, RSA key for the specified key id.
35
35
36
- :param str key_id: Key ID
37
- :returns: Wrapping key which contains the specified static key
36
+ :param str key_id: User-defined ID for the static key
37
+ :returns: Wrapping key that contains the specified static key
38
38
:rtype: :class:`aws_encryption_sdk.internal.crypto.WrappingKey`
39
39
"""
40
40
try :
@@ -59,33 +59,38 @@ def _get_raw_key(self, key_id):
59
59
60
60
61
61
def cycle_file (key_arn , source_plaintext_filename , botocore_session = None ):
62
- """Encrypts and then decrypts a file under both a KMS Master Key Provider and a custom static Master Key Provider.
62
+ """Encrypts and then decrypts a file using a KMS master key provider and a custom static master
63
+ key provider. Both master key providers are used to encrypt the plaintext file, so either one alone
64
+ can decrypt it.
63
65
64
- :param str key_arn: Amazon Resource Name (Arn ) of the KMS CMK
66
+ :param str key_arn: Amazon Resource Name (ARN ) of the KMS Customer Master Key ( CMK) (http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html)
65
67
:param str source_plaintext_filename: Filename of file to encrypt
66
68
:param botocore_session: existing botocore session instance
67
69
:type botocore_session: botocore.session.Session
68
70
"""
69
-
71
+
72
+ # "Cycled" means encrypted and then decrypted
70
73
ciphertext_filename = source_plaintext_filename + '.encrypted'
71
74
cycled_kms_plaintext_filename = source_plaintext_filename + '.kms.decrypted'
72
75
cycled_static_plaintext_filename = source_plaintext_filename + '.static.decrypted'
73
76
74
- # Create KMS Master Key Provider
77
+ # Create a KMS master key provider
75
78
kms_kwargs = dict (key_ids = [key_arn ])
76
79
if botocore_session is not None :
77
80
kms_kwargs ['botocore_session' ] = botocore_session
78
81
kms_master_key_provider = aws_encryption_sdk .KMSMasterKeyProvider (** kms_kwargs )
79
82
80
- # Create Static Master Key Provider and add to KMS Master Key Provider
83
+ # Create a static master key provider and add a master key to it
81
84
static_key_id = os .urandom (8 )
82
85
static_master_key_provider = StaticRandomMasterKeyProvider ()
83
86
static_master_key_provider .add_master_key (static_key_id )
84
87
85
- # Add Static Master Key Provider to KMS Master Key Provider
88
+ # Add the static master key provider to the KMS master key provider
89
+ # The resulting master key provider uses KMS master keys to generate (and encrypt)
90
+ # data keys and static master keys to create an additional encrypted copy of each data key.
86
91
kms_master_key_provider .add_master_key_provider (static_master_key_provider )
87
92
88
- # Encrypt plaintext with both KMS and Static Master Keys
93
+ # Encrypt plaintext with both KMS and static master keys
89
94
with open (source_plaintext_filename , 'rb' ) as plaintext , open (ciphertext_filename , 'wb' ) as ciphertext :
90
95
with aws_encryption_sdk .stream (
91
96
source = plaintext ,
@@ -95,7 +100,7 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
95
100
for chunk in encryptor :
96
101
ciphertext .write (chunk )
97
102
98
- # Decrypt the ciphertext with the KMS Master Key
103
+ # Decrypt the ciphertext with only the KMS master key
99
104
with open (ciphertext_filename , 'rb' ) as ciphertext , open (cycled_kms_plaintext_filename , 'wb' ) as plaintext :
100
105
with aws_encryption_sdk .stream (
101
106
source = ciphertext ,
@@ -105,7 +110,7 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
105
110
for chunk in kms_decryptor :
106
111
plaintext .write (chunk )
107
112
108
- # Decrypt the ciphertext with the Static Master Key only
113
+ # Decrypt the ciphertext with only the static master key
109
114
with open (ciphertext_filename , 'rb' ) as ciphertext , open (cycled_static_plaintext_filename , 'wb' ) as plaintext :
110
115
with aws_encryption_sdk .stream (
111
116
source = ciphertext ,
@@ -115,11 +120,16 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
115
120
for chunk in static_decryptor :
116
121
plaintext .write (chunk )
117
122
118
- # Validate that the cycled plaintext is identical to the source plaintext
123
+ # Verify that the " cycled" (encrypted, then decrypted) plaintext is identical to the source plaintext
119
124
assert filecmp .cmp (source_plaintext_filename , cycled_kms_plaintext_filename )
120
125
assert filecmp .cmp (source_plaintext_filename , cycled_static_plaintext_filename )
121
126
122
- # Validate that the encryption context used by the decryptor has all the key-pairs from the encryptor
127
+
128
+ # Verify that the encryption context in the decrypt operation includes all key pairs from the
129
+ # encrypt operation.
130
+ #
131
+ # In production, always use a meaningful encryption context. In this sample, we omit the
132
+ # encryption context (no key pairs).
123
133
assert all (
124
134
pair in kms_decryptor .header .encryption_context .items ()
125
135
for pair in encryptor .header .encryption_context .items ()
0 commit comments