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