Skip to content

Commit 80465a4

Browse files
authored
Merge pull request aws#89 from mattsb42-aws/blacken
Blacken!
2 parents 52349d4 + 3c2ae28 commit 80465a4

File tree

89 files changed

+2077
-2865
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2077
-2865
lines changed

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Amazon DynamoDB Encryption Client for Python
1010
:target: https://pypi.org/project/dynamodb-encryption-sdk
1111
:alt: Supported Python Versions
1212

13+
.. image:: https://img.shields.io/badge/code_style-black-000000.svg
14+
:target: https://github.com/ambv/black
15+
:alt: Code style: black
16+
1317
.. image:: https://readthedocs.org/projects/aws-dynamodb-encryption-python/badge/?version=latest
1418
:target: http://aws-dynamodb-encryption-python.readthedocs.io/en/latest/?badge=latest
1519
:alt: Documentation Status

doc/conf.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
11
# pylint: disable=invalid-name
22
"""Sphinx configuration."""
3-
from datetime import datetime
43
import io
54
import os
65
import re
6+
from datetime import datetime
77

8-
VERSION_RE = re.compile(r'''__version__ = ['"]([0-9.]+)['"]''')
8+
VERSION_RE = re.compile(r"""__version__ = ['"]([0-9.]+)['"]""")
99
HERE = os.path.abspath(os.path.dirname(__file__))
1010

1111

1212
def read(*args):
1313
"""Reads complete file contents."""
14-
return io.open(os.path.join(HERE, *args), encoding='utf-8').read()
14+
return io.open(os.path.join(HERE, *args), encoding="utf-8").read()
1515

1616

1717
def get_release():
1818
"""Reads the release (full three-part version number) from this module."""
19-
init = read('..', 'src', 'dynamodb_encryption_sdk', 'identifiers.py')
19+
init = read("..", "src", "dynamodb_encryption_sdk", "identifiers.py")
2020
return VERSION_RE.search(init).group(1)
2121

2222

2323
def get_version():
2424
"""Reads the version (MAJOR.MINOR) from this module."""
2525
_release = get_release()
26-
split_version = _release.split('.')
26+
split_version = _release.split(".")
2727
if len(split_version) == 3:
28-
return '.'.join(split_version[:2])
28+
return ".".join(split_version[:2])
2929
return _release
3030

3131

32-
project = u'dynamodb-encryption-sdk-python'
32+
project = u"dynamodb-encryption-sdk-python"
3333
version = get_version()
3434
release = get_release()
3535

3636
# Add any Sphinx extension module names here, as strings. They can be extensions
3737
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
38-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest',
39-
'sphinx.ext.intersphinx', 'sphinx.ext.todo',
40-
'sphinx.ext.coverage', 'sphinx.ext.autosummary',
41-
'sphinx.ext.napoleon', 'sphinx.ext.viewcode']
38+
extensions = [
39+
"sphinx.ext.autodoc",
40+
"sphinx.ext.doctest",
41+
"sphinx.ext.intersphinx",
42+
"sphinx.ext.todo",
43+
"sphinx.ext.coverage",
44+
"sphinx.ext.autosummary",
45+
"sphinx.ext.napoleon",
46+
"sphinx.ext.viewcode",
47+
]
4248
napoleon_include_special_with_doc = False
4349

4450
# Add any paths that contain templates here, relative to this directory.
45-
templates_path = ['_templates']
51+
templates_path = ["_templates"]
4652

47-
source_suffix = '.rst' # The suffix of source filenames.
48-
master_doc = 'index' # The master toctree document.
53+
source_suffix = ".rst" # The suffix of source filenames.
54+
master_doc = "index" # The master toctree document.
4955

50-
copyright = u'%s, Amazon' % datetime.now().year # pylint: disable=redefined-builtin
56+
copyright = u"%s, Amazon" % datetime.now().year # pylint: disable=redefined-builtin
5157

5258
# List of directories, relative to source directory, that shouldn't be searched
5359
# for source files.
54-
exclude_trees = ['_build']
60+
exclude_trees = ["_build"]
5561

56-
pygments_style = 'sphinx'
62+
pygments_style = "sphinx"
5763

5864
autoclass_content = "both"
59-
autodoc_default_flags = ['show-inheritance', 'members']
60-
autodoc_member_order = 'bysource'
65+
autodoc_default_flags = ["show-inheritance", "members"]
66+
autodoc_member_order = "bysource"
6167

62-
html_theme = 'sphinx_rtd_theme'
63-
html_static_path = ['_static']
64-
htmlhelp_basename = '%sdoc' % project
68+
html_theme = "sphinx_rtd_theme"
69+
html_static_path = ["_static"]
70+
htmlhelp_basename = "%sdoc" % project
6571

6672
# Example configuration for intersphinx: refer to the Python standard library.
67-
intersphinx_mapping = {'python': ('http://docs.python.org/', None)}
73+
intersphinx_mapping = {"python": ("http://docs.python.org/", None)}
6874

6975
# autosummary
7076
autosummary_generate = True

examples/src/aws_kms_encrypted_client.py

Lines changed: 36 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# language governing permissions and limitations under the License.
1313
"""Example showing use of AWS KMS CMP with EncryptedClient."""
1414
import boto3
15+
1516
from dynamodb_encryption_sdk.encrypted.client import EncryptedClient
1617
from dynamodb_encryption_sdk.identifiers import CryptoAction
1718
from dynamodb_encryption_sdk.material_providers.aws_kms import AwsKmsCryptographicMaterialsProvider
@@ -20,51 +21,41 @@
2021

2122
def encrypt_item(table_name, aws_cmk_id):
2223
"""Demonstrate use of EncryptedClient to transparently encrypt an item."""
23-
index_key = {
24-
'partition_attribute': {'S': 'is this'},
25-
'sort_attribute': {'N': '55'}
26-
}
24+
index_key = {"partition_attribute": {"S": "is this"}, "sort_attribute": {"N": "55"}}
2725
plaintext_item = {
28-
'example': {'S': 'data'},
29-
'some numbers': {'N': '99'},
30-
'and some binary': {'B': b'\x00\x01\x02'},
31-
'leave me': {'S': 'alone'} # We want to ignore this attribute
26+
"example": {"S": "data"},
27+
"some numbers": {"N": "99"},
28+
"and some binary": {"B": b"\x00\x01\x02"},
29+
"leave me": {"S": "alone"}, # We want to ignore this attribute
3230
}
3331
# Collect all of the attributes that will be encrypted (used later).
3432
encrypted_attributes = set(plaintext_item.keys())
35-
encrypted_attributes.remove('leave me')
33+
encrypted_attributes.remove("leave me")
3634
# Collect all of the attributes that will not be encrypted (used later).
3735
unencrypted_attributes = set(index_key.keys())
38-
unencrypted_attributes.add('leave me')
36+
unencrypted_attributes.add("leave me")
3937
# Add the index pairs to the item.
4038
plaintext_item.update(index_key)
4139

4240
# Create a normal client.
43-
client = boto3.client('dynamodb')
41+
client = boto3.client("dynamodb")
4442
# Create a crypto materials provider using the specified AWS KMS key.
4543
aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id)
4644
# Create attribute actions that tells the encrypted client to encrypt all attributes except one.
4745
actions = AttributeActions(
48-
default_action=CryptoAction.ENCRYPT_AND_SIGN,
49-
attribute_actions={
50-
'leave me': CryptoAction.DO_NOTHING
51-
}
46+
default_action=CryptoAction.ENCRYPT_AND_SIGN, attribute_actions={"leave me": CryptoAction.DO_NOTHING}
5247
)
5348
# Use these objects to create an encrypted client.
54-
encrypted_client = EncryptedClient(
55-
client=client,
56-
materials_provider=aws_kms_cmp,
57-
attribute_actions=actions
58-
)
49+
encrypted_client = EncryptedClient(client=client, materials_provider=aws_kms_cmp, attribute_actions=actions)
5950

6051
# Put the item to the table, using the encrypted client to transparently encrypt it.
6152
encrypted_client.put_item(TableName=table_name, Item=plaintext_item)
6253

6354
# Get the encrypted item using the standard client.
64-
encrypted_item = client.get_item(TableName=table_name, Key=index_key)['Item']
55+
encrypted_item = client.get_item(TableName=table_name, Key=index_key)["Item"]
6556

6657
# Get the item using the encrypted client, transparently decyrpting it.
67-
decrypted_item = encrypted_client.get_item(TableName=table_name, Key=index_key)['Item']
58+
decrypted_item = encrypted_client.get_item(TableName=table_name, Key=index_key)["Item"]
6859

6960
# Verify that all of the attributes are different in the encrypted item
7061
for name in encrypted_attributes:
@@ -82,28 +73,16 @@ def encrypt_item(table_name, aws_cmk_id):
8273
def encrypt_batch_items(table_name, aws_cmk_id):
8374
"""Demonstrate use of EncryptedClient to transparently encrypt multiple items in a batch request."""
8475
index_keys = [
85-
{
86-
'partition_attribute': {'S': 'is this'},
87-
'sort_attribute': {'N': '55'}
88-
},
89-
{
90-
'partition_attribute': {'S': 'is this'},
91-
'sort_attribute': {'N': '56'}
92-
},
93-
{
94-
'partition_attribute': {'S': 'is this'},
95-
'sort_attribute': {'N': '57'}
96-
},
97-
{
98-
'partition_attribute': {'S': 'another'},
99-
'sort_attribute': {'N': '55'}
100-
}
76+
{"partition_attribute": {"S": "is this"}, "sort_attribute": {"N": "55"}},
77+
{"partition_attribute": {"S": "is this"}, "sort_attribute": {"N": "56"}},
78+
{"partition_attribute": {"S": "is this"}, "sort_attribute": {"N": "57"}},
79+
{"partition_attribute": {"S": "another"}, "sort_attribute": {"N": "55"}},
10180
]
10281
plaintext_additional_attributes = {
103-
'example': {'S': 'data'},
104-
'some numbers': {'N': '99'},
105-
'and some binary': {'B': b'\x00\x01\x02'},
106-
'leave me': {'S': 'alone'} # We want to ignore this attribute
82+
"example": {"S": "data"},
83+
"some numbers": {"N": "99"},
84+
"and some binary": {"B": b"\x00\x01\x02"},
85+
"leave me": {"S": "alone"}, # We want to ignore this attribute
10786
}
10887
plaintext_items = []
10988
for key in index_keys:
@@ -113,43 +92,34 @@ def encrypt_batch_items(table_name, aws_cmk_id):
11392

11493
# Collect all of the attributes that will be encrypted (used later).
11594
encrypted_attributes = set(plaintext_additional_attributes.keys())
116-
encrypted_attributes.remove('leave me')
95+
encrypted_attributes.remove("leave me")
11796
# Collect all of the attributes that will not be encrypted (used later).
11897
unencrypted_attributes = set(index_keys[0].keys())
119-
unencrypted_attributes.add('leave me')
98+
unencrypted_attributes.add("leave me")
12099

121100
# Create a normal client.
122-
client = boto3.client('dynamodb')
101+
client = boto3.client("dynamodb")
123102
# Create a crypto materials provider using the specified AWS KMS key.
124103
aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id)
125104
# Create attribute actions that tells the encrypted client to encrypt all attributes except one.
126105
actions = AttributeActions(
127-
default_action=CryptoAction.ENCRYPT_AND_SIGN,
128-
attribute_actions={
129-
'leave me': CryptoAction.DO_NOTHING
130-
}
106+
default_action=CryptoAction.ENCRYPT_AND_SIGN, attribute_actions={"leave me": CryptoAction.DO_NOTHING}
131107
)
132108
# Use these objects to create an encrypted client.
133-
encrypted_client = EncryptedClient(
134-
client=client,
135-
materials_provider=aws_kms_cmp,
136-
attribute_actions=actions
137-
)
109+
encrypted_client = EncryptedClient(client=client, materials_provider=aws_kms_cmp, attribute_actions=actions)
138110

139111
# Put the items to the table, using the encrypted client to transparently encrypt them.
140-
encrypted_client.batch_write_item(RequestItems={
141-
table_name: [{'PutRequest': {'Item': item}} for item in plaintext_items]
142-
})
112+
encrypted_client.batch_write_item(
113+
RequestItems={table_name: [{"PutRequest": {"Item": item}} for item in plaintext_items]}
114+
)
143115

144116
# Get the encrypted item using the standard client.
145-
encrypted_items = client.batch_get_item(
146-
RequestItems={table_name: {'Keys': index_keys}}
147-
)['Responses'][table_name]
117+
encrypted_items = client.batch_get_item(RequestItems={table_name: {"Keys": index_keys}})["Responses"][table_name]
148118

149119
# Get the item using the encrypted client, transparently decyrpting it.
150-
decrypted_items = encrypted_client.batch_get_item(
151-
RequestItems={table_name: {'Keys': index_keys}}
152-
)['Responses'][table_name]
120+
decrypted_items = encrypted_client.batch_get_item(RequestItems={table_name: {"Keys": index_keys}})["Responses"][
121+
table_name
122+
]
153123

154124
def _select_index_from_item(item):
155125
"""Find the index keys that match this item."""
@@ -178,6 +148,6 @@ def _select_item_from_index(index, all_items):
178148
assert decrypted_item[name] == encrypted_item[name] == plaintext_item[name]
179149

180150
# Clean up the item
181-
encrypted_client.batch_write_item(RequestItems={
182-
table_name: [{'DeleteRequest': {'Key': key}} for key in index_keys]
183-
})
151+
encrypted_client.batch_write_item(
152+
RequestItems={table_name: [{"DeleteRequest": {"Key": key}} for key in index_keys]}
153+
)

examples/src/aws_kms_encrypted_item.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""Example showing use of AWS KMS CMP with item encryption functions directly."""
1414
import boto3
1515
from boto3.dynamodb.types import Binary
16+
1617
from dynamodb_encryption_sdk.encrypted import CryptoConfig
1718
from dynamodb_encryption_sdk.encrypted.item import decrypt_python_item, encrypt_python_item
1819
from dynamodb_encryption_sdk.identifiers import CryptoAction
@@ -23,27 +24,24 @@
2324

2425
def encrypt_item(table_name, aws_cmk_id):
2526
"""Demonstrate use of EncryptedTable to transparently encrypt an item."""
26-
index_key = {
27-
'partition_attribute': 'is this',
28-
'sort_attribute': 55
29-
}
27+
index_key = {"partition_attribute": "is this", "sort_attribute": 55}
3028
plaintext_item = {
31-
'example': 'data',
32-
'some numbers': 99,
33-
'and some binary': Binary(b'\x00\x01\x02'),
34-
'leave me': 'alone' # We want to ignore this attribute
29+
"example": "data",
30+
"some numbers": 99,
31+
"and some binary": Binary(b"\x00\x01\x02"),
32+
"leave me": "alone", # We want to ignore this attribute
3533
}
3634
# Collect all of the attributes that will be encrypted (used later).
3735
encrypted_attributes = set(plaintext_item.keys())
38-
encrypted_attributes.remove('leave me')
36+
encrypted_attributes.remove("leave me")
3937
# Collect all of the attributes that will not be encrypted (used later).
4038
unencrypted_attributes = set(index_key.keys())
41-
unencrypted_attributes.add('leave me')
39+
unencrypted_attributes.add("leave me")
4240
# Add the index pairs to the item.
4341
plaintext_item.update(index_key)
4442

4543
# Create a normal table resource.
46-
table = boto3.resource('dynamodb').Table(table_name)
44+
table = boto3.resource("dynamodb").Table(table_name)
4745

4846
# Use the TableInfo helper to collect information about the indexes.
4947
table_info = TableInfo(name=table_name)
@@ -60,24 +58,21 @@ def encrypt_item(table_name, aws_cmk_id):
6058
# are the primary index attributes.
6159
# These attributes need to be in the form of a DynamoDB JSON structure, so first
6260
# convert the standard dictionary.
63-
attributes=dict_to_ddb(index_key)
61+
attributes=dict_to_ddb(index_key),
6462
)
6563

6664
# Create attribute actions that tells the encrypted table to encrypt all attributes,
6765
# only sign the primary index attributes, and ignore the one identified attribute to
6866
# ignore.
6967
actions = AttributeActions(
70-
default_action=CryptoAction.ENCRYPT_AND_SIGN,
71-
attribute_actions={'leave me': CryptoAction.DO_NOTHING}
68+
default_action=CryptoAction.ENCRYPT_AND_SIGN, attribute_actions={"leave me": CryptoAction.DO_NOTHING}
7269
)
7370
actions.set_index_keys(*table_info.protected_index_keys())
7471

7572
# Build the crypto config to use for this item.
7673
# When using the higher-level helpers, this is handled for you.
7774
crypto_config = CryptoConfig(
78-
materials_provider=aws_kms_cmp,
79-
encryption_context=encryption_context,
80-
attribute_actions=actions
75+
materials_provider=aws_kms_cmp, encryption_context=encryption_context, attribute_actions=actions
8176
)
8277

8378
# Encrypt the plaintext item directly

0 commit comments

Comments
 (0)