Skip to content

Commit 83f4ad4

Browse files
mattsb42-awslizroth
authored andcommitted
Merge pull request #9 from juneb/scrub-code-comments
Scrub Python code comments
1 parent a259b71 commit 83f4ad4

4 files changed

+48
-28
lines changed

README.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ An example of a CMM is the default CMM, which is automatically generated anywher
4444
key provider. The default CMM collects encrypted data keys from all master keys referenced by the master key
4545
provider.
4646

47-
An example of a more advanced CMM is the caching CMM, which caches cryptographic materials provided by a another CMM.
47+
An example of a more advanced CMM is the caching CMM, which caches cryptographic materials provided by another CMM.
4848

4949
Master Key Providers
5050
--------------------
@@ -57,12 +57,13 @@ To encrypt data in this client, a ``MasterKeyProvider`` object must contain at l
5757

5858
Master Keys
5959
-----------
60-
Master keys provide data keys.
60+
Master keys generate, encrypt, and decrypt data keys.
6161
An example of a master key is a `KMS customer master key (CMK)`_.
6262

6363
Data Keys
6464
---------
65-
Data Keys are the actual encryption keys which are used to encrypt your data.
65+
Data keys are the encryption keys that are used to encrypt your data. If your algorithm suite
66+
uses a key derivation function, the data key is used to generate the key that directly encrypts the data.
6667

6768
*****
6869
Usage

examples/src/basic_encryption.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
def cycle_string(key_arn, source_plaintext, botocore_session=None):
2020
"""Encrypts and then decrypts a string under a KMS customer master key (CMK).
2121
22-
:param str key_arn: Amazon Resource Name (Arn) of the KMS CMK
22+
:param str key_arn: Amazon Resource Name (ARN) of the KMS CMK
2323
:param bytes source_plaintext: Data to encrypt
2424
:param botocore_session: existing botocore session instance
2525
:type botocore_session: botocore.session.Session
2626
"""
27-
# Create the KMS Master Key Provider
27+
# Create a KMS master key provider
2828
kms_kwargs = dict(key_ids=[key_arn])
2929
if botocore_session is not None:
3030
kms_kwargs['botocore_session'] = botocore_session
3131
master_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(**kms_kwargs)
3232

33-
# Encrypt the source plaintext
33+
# Encrypt the plaintext source data
3434
ciphertext, encryptor_header = aws_encryption_sdk.encrypt(
3535
source=source_plaintext,
3636
key_provider=master_key_provider
@@ -43,10 +43,14 @@ def cycle_string(key_arn, source_plaintext, botocore_session=None):
4343
key_provider=master_key_provider
4444
)
4545

46-
# Validate that the cycled plaintext is identical to the source plaintext
46+
# Verify that the "cycled" (encrypted, then decrypted) plaintext is identical to the source plaintext
4747
assert cycled_plaintext == source_plaintext
4848

49-
# Validate that the encryption context used by the decryptor has all the key-pairs from the encryptor
49+
# Verify that the encryption context used in the decrypt operation includes all key pairs from
50+
# the encrypt operation. (The SDK can add pairs, so don't require an exact match.)
51+
#
52+
# In production, always use a meaningful encryption context. In this sample, we omit the
53+
# encryption context (no key pairs).
5054
assert all(
5155
pair in decrypted_header.encryption_context.items()
5256
for pair in encryptor_header.encryption_context.items()

examples/src/basic_file_encryption_with_multiple_providers.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def __init__(self, **kwargs): # pylint: disable=unused-argument
3838
def _get_raw_key(self, key_id):
3939
"""Retrieves a static, randomly generated, RSA key for the specified key id.
4040
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
4343
:rtype: :class:`aws_encryption_sdk.internal.crypto.WrappingKey`
4444
"""
4545
try:
@@ -64,32 +64,38 @@ def _get_raw_key(self, key_id):
6464

6565

6666
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.
6870
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)
7073
:param str source_plaintext_filename: Filename of file to encrypt
7174
:param botocore_session: existing botocore session instance
7275
:type botocore_session: botocore.session.Session
7376
"""
77+
# "Cycled" means encrypted and then decrypted
7478
ciphertext_filename = source_plaintext_filename + '.encrypted'
7579
cycled_kms_plaintext_filename = source_plaintext_filename + '.kms.decrypted'
7680
cycled_static_plaintext_filename = source_plaintext_filename + '.static.decrypted'
7781

78-
# Create KMS Master Key Provider
82+
# Create a KMS master key provider
7983
kms_kwargs = dict(key_ids=[key_arn])
8084
if botocore_session is not None:
8185
kms_kwargs['botocore_session'] = botocore_session
8286
kms_master_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(**kms_kwargs)
8387

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
8589
static_key_id = os.urandom(8)
8690
static_master_key_provider = StaticRandomMasterKeyProvider()
8791
static_master_key_provider.add_master_key(static_key_id)
8892

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.
9096
kms_master_key_provider.add_master_key_provider(static_master_key_provider)
9197

92-
# Encrypt plaintext with both KMS and Static Master Keys
98+
# Encrypt plaintext with both KMS and static master keys
9399
with open(source_plaintext_filename, 'rb') as plaintext, open(ciphertext_filename, 'wb') as ciphertext:
94100
with aws_encryption_sdk.stream(
95101
source=plaintext,
@@ -99,7 +105,7 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
99105
for chunk in encryptor:
100106
ciphertext.write(chunk)
101107

102-
# Decrypt the ciphertext with the KMS Master Key
108+
# Decrypt the ciphertext with only the KMS master key
103109
with open(ciphertext_filename, 'rb') as ciphertext, open(cycled_kms_plaintext_filename, 'wb') as plaintext:
104110
with aws_encryption_sdk.stream(
105111
source=ciphertext,
@@ -109,7 +115,7 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
109115
for chunk in kms_decryptor:
110116
plaintext.write(chunk)
111117

112-
# Decrypt the ciphertext with the Static Master Key only
118+
# Decrypt the ciphertext with only the static master key
113119
with open(ciphertext_filename, 'rb') as ciphertext, open(cycled_static_plaintext_filename, 'wb') as plaintext:
114120
with aws_encryption_sdk.stream(
115121
source=ciphertext,
@@ -119,11 +125,15 @@ def cycle_file(key_arn, source_plaintext_filename, botocore_session=None):
119125
for chunk in static_decryptor:
120126
plaintext.write(chunk)
121127

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
123129
assert filecmp.cmp(source_plaintext_filename, cycled_kms_plaintext_filename)
124130
assert filecmp.cmp(source_plaintext_filename, cycled_static_plaintext_filename)
125131

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).
127137
assert all(
128138
pair in kms_decryptor.header.encryption_context.items()
129139
for pair in encryptor.header.encryption_context.items()

examples/src/basic_file_encryption_with_raw_key_provider.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class StaticRandomMasterKeyProvider(RawMasterKeyProvider):
24-
"""Randomly generates and provides 256-bit keys consistently per unique key id."""
24+
"""Randomly generates 256-bit keys for each unique key ID."""
2525

2626
provider_id = 'static-random'
2727

@@ -30,10 +30,10 @@ def __init__(self, **kwargs): # pylint: disable=unused-argument
3030
self._static_keys = {}
3131

3232
def _get_raw_key(self, key_id):
33-
"""Retrieves a static, randomly generated, symmetric key for the specified key id.
33+
"""Returns a static, randomly-generated symmetric key for the specified key ID.
3434
3535
:param str key_id: Key ID
36-
:returns: Wrapping key which contains the specified static key
36+
:returns: Wrapping key that contains the specified static key
3737
:rtype: :class:`aws_encryption_sdk.internal.crypto.WrappingKey`
3838
"""
3939
try:
@@ -49,19 +49,19 @@ def _get_raw_key(self, key_id):
4949

5050

5151
def cycle_file(source_plaintext_filename):
52-
"""Encrypts and then decrypts a file under a custom static Master Key Provider.
52+
"""Encrypts and then decrypts a file under a custom static master key provider.
5353
5454
:param str source_plaintext_filename: Filename of file to encrypt
5555
"""
56-
# Create the Static Random Master Key Provider
56+
# Create a static random master key provider
5757
key_id = os.urandom(8)
5858
master_key_provider = StaticRandomMasterKeyProvider()
5959
master_key_provider.add_master_key(key_id)
6060

6161
ciphertext_filename = source_plaintext_filename + '.encrypted'
6262
cycled_plaintext_filename = source_plaintext_filename + '.decrypted'
6363

64-
# Encrypt the source plaintext
64+
# Encrypt the plaintext source data
6565
with open(source_plaintext_filename, 'rb') as plaintext, open(ciphertext_filename, 'wb') as ciphertext:
6666
with aws_encryption_sdk.stream(
6767
mode='e',
@@ -81,10 +81,15 @@ def cycle_file(source_plaintext_filename):
8181
for chunk in decryptor:
8282
plaintext.write(chunk)
8383

84-
# Validate that the cycled plaintext is identical to the source plaintext
84+
# Verify that the "cycled" (encrypted, then decrypted) plaintext is identical to the source
85+
# plaintext
8586
assert filecmp.cmp(source_plaintext_filename, cycled_plaintext_filename)
8687

87-
# Validate that the encryption context used by the decryptor has all the key-pairs from the encryptor
88+
# Verify that the encryption context used in the decrypt operation includes all key pairs from
89+
# the encrypt operation
90+
#
91+
# In production, always use a meaningful encryption context. In this sample, we omit the
92+
# encryption context (no key pairs).
8893
assert all(
8994
pair in decryptor.header.encryption_context.items()
9095
for pair in encryptor.header.encryption_context.items()

0 commit comments

Comments
 (0)