Skip to content

Commit 9ec35ef

Browse files
authored
feat: enable use of keyrings (#216)
* chore: add __all__ values for keyring modules * chore: add versionadded tags for keyrings * feat: plump keyrings into stream handlers and default CMM * chore: incorporate keyrings into tests * add raw AES keyring-MKP compatibility tests * add raw RSA MKP-keyring compatibility tests * fix: fix integ test to run when default region is set * chore: add public-private keyring-MKP compat tests * fix: fix typo * add DefaultCryptographicMaterialsProvider tests for keyrings that return incomplete or broken materials * feat: enable caching CMM to accept either MKP or keyring * fix: rename test keyring to avoid name collision * chore: caching CMM has too many instance attributes and that's ok * docs: add versionadded flags to docstrings for keyring values * docs: update docs on encrypt/decrypt helper functions to match underlying docs * chore: update copyright notices on modified files * docs: render keyring docs * docs: clean up keyring method docs * fix: fix linting issues * fix: autoformat * docs: fix typo * fix: fix docs and error message inconsistency * fix: re-order checks to avoid misleading error messages if materials are invalid * chore: test broken paths in default CMM with more algorithm suites * docs: make docs correctly link to pyca/cryptography docs
1 parent 952bce4 commit 9ec35ef

File tree

16 files changed

+956
-411
lines changed

16 files changed

+956
-411
lines changed

doc/conf.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def get_version():
6767
htmlhelp_basename = "%sdoc" % project
6868

6969
# Example configuration for intersphinx: refer to the Python standard library.
70-
intersphinx_mapping = {"http://docs.python.org/": None}
70+
intersphinx_mapping = {
71+
"python": ("http://docs.python.org/3/", None),
72+
"cryptography": ("https://cryptography.io/en/latest/", None),
73+
}
7174

7275
# autosummary
7376
autosummary_generate = True

doc/index.rst

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Modules
1414
aws_encryption_sdk.caches.base
1515
aws_encryption_sdk.caches.local
1616
aws_encryption_sdk.caches.null
17+
aws_encryption_sdk.keyrings.base
18+
aws_encryption_sdk.keyrings.multi
19+
aws_encryption_sdk.keyrings.raw
1720
aws_encryption_sdk.key_providers.base
1821
aws_encryption_sdk.key_providers.kms
1922
aws_encryption_sdk.key_providers.raw

src/aws_encryption_sdk/__init__.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License"). You
4-
# may not use this file except in compliance with the License. A copy of
5-
# the License is located at
6-
#
7-
# http://aws.amazon.com/apache2.0/
8-
#
9-
# or in the "license" file accompanying this file. This file is
10-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11-
# ANY KIND, either express or implied. See the License for the specific
12-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
133
"""High level AWS Encryption SDK client functions."""
144
# Below are imported for ease of use by implementors
155
from aws_encryption_sdk.caches.local import LocalCryptoMaterialsCache # noqa
@@ -33,6 +23,9 @@ def encrypt(**kwargs):
3323
When using this function, the entire ciphertext message is encrypted into memory before returning
3424
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.
3525
26+
.. versionadded:: 1.5.0
27+
The *keyring* parameter.
28+
3629
.. code:: python
3730
3831
>>> import aws_encryption_sdk
@@ -49,12 +42,14 @@ def encrypt(**kwargs):
4942
:type config: aws_encryption_sdk.streaming_client.EncryptorConfig
5043
:param source: Source data to encrypt or decrypt
5144
:type source: str, bytes, io.IOBase, or file
52-
:param materials_manager: `CryptoMaterialsManager` from which to obtain cryptographic materials
53-
(either `materials_manager` or `key_provider` required)
54-
:type materials_manager: aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager
55-
:param key_provider: `MasterKeyProvider` from which to obtain data keys for encryption
56-
(either `materials_manager` or `key_provider` required)
57-
:type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
45+
:param CryptoMaterialsManager materials_manager:
46+
Cryptographic materials manager to use for encryption
47+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
48+
:param Keyring keyring: Keyring to use for encryption
49+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
50+
:param MasterKeyProvider key_provider:
51+
Master key provider to use for encryption
52+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
5853
:param int source_length: Length of source data (optional)
5954
6055
.. note::
@@ -87,6 +82,9 @@ def decrypt(**kwargs):
8782
When using this function, the entire ciphertext message is decrypted into memory before returning
8883
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.
8984
85+
.. versionadded:: 1.5.0
86+
The *keyring* parameter.
87+
9088
.. code:: python
9189
9290
>>> import aws_encryption_sdk
@@ -103,12 +101,14 @@ def decrypt(**kwargs):
103101
:type config: aws_encryption_sdk.streaming_client.DecryptorConfig
104102
:param source: Source data to encrypt or decrypt
105103
:type source: str, bytes, io.IOBase, or file
106-
:param materials_manager: `CryptoMaterialsManager` from which to obtain cryptographic materials
107-
(either `materials_manager` or `key_provider` required)
108-
:type materials_manager: aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager
109-
:param key_provider: `MasterKeyProvider` from which to obtain data keys for decryption
110-
(either `materials_manager` or `key_provider` required)
111-
:type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
104+
:param CryptoMaterialsManager materials_manager:
105+
Cryptographic materials manager to use for encryption
106+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
107+
:param Keyring keyring: Keyring to use for encryption
108+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
109+
:param MasterKeyProvider key_provider:
110+
Master key provider to use for encryption
111+
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
112112
:param int source_length: Length of source data (optional)
113113
114114
.. note::

src/aws_encryption_sdk/exceptions.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License"). You
4-
# may not use this file except in compliance with the License. A copy of
5-
# the License is located at
6-
#
7-
# http://aws.amazon.com/apache2.0/
8-
#
9-
# or in the "license" file accompanying this file. This file is
10-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11-
# ANY KIND, either express or implied. See the License for the specific
12-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
133
"""Contains exception classes for AWS Encryption SDK."""
144

155

@@ -87,6 +77,13 @@ class SignatureKeyError(AWSEncryptionSDKClientError):
8777
"""
8878

8979

80+
class InvalidCryptographicMaterialsError(AWSEncryptionSDKClientError):
81+
"""Exception class for errors encountered when attempting to validate cryptographic materials.
82+
83+
.. versionadded:: 1.5.0
84+
"""
85+
86+
9087
class ActionNotAllowedError(AWSEncryptionSDKClientError):
9188
"""Exception class for errors encountered when attempting to perform unallowed actions."""
9289

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License"). You
4-
# may not use this file except in compliance with the License. A copy of
5-
# the License is located at
6-
#
7-
# http://aws.amazon.com/apache2.0/
8-
#
9-
# or in the "license" file accompanying this file. This file is
10-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11-
# ANY KIND, either express or implied. See the License for the specific
12-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
133
"""Base class interface for Keyrings."""
144
from aws_encryption_sdk.materials_managers import ( # only used for mypy; pylint: disable=unused-import
155
DecryptionMaterials,
@@ -23,6 +13,8 @@
2313
# We only actually need these imports when running the mypy checks
2414
pass
2515

16+
__all__ = ("Keyring",)
17+
2618

2719
class Keyring(object):
2820
"""Parent interface for Keyring classes.
@@ -34,10 +26,9 @@ def on_encrypt(self, encryption_materials):
3426
# type: (EncryptionMaterials) -> EncryptionMaterials
3527
"""Generate a data key if not present and encrypt it using any available wrapping key.
3628
37-
:param encryption_materials: Encryption materials for the keyring to modify.
38-
:type encryption_materials: aws_encryption_sdk.materials_managers.EncryptionMaterials
29+
:param EncryptionMaterials encryption_materials: Encryption materials for keyring to modify.
3930
:returns: Optionally modified encryption materials.
40-
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
31+
:rtype: EncryptionMaterials
4132
:raises NotImplementedError: if method is not implemented
4233
"""
4334
raise NotImplementedError("Keyring does not implement on_encrypt function")
@@ -46,12 +37,10 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
4637
# type: (DecryptionMaterials, Iterable[EncryptedDataKey]) -> DecryptionMaterials
4738
"""Attempt to decrypt the encrypted data keys.
4839
49-
:param decryption_materials: Decryption materials for the keyring to modify.
50-
:type decryption_materials: aws_encryption_sdk.materials_managers.DecryptionMaterials
51-
:param encrypted_data_keys: List of encrypted data keys.
52-
:type: Iterable of :class:`aws_encryption_sdk.structures.EncryptedDataKey`
40+
:param DecryptionMaterials decryption_materials: Decryption materials for keyring to modify.
41+
:param List[EncryptedDataKey] encrypted_data_keys: List of encrypted data keys.
5342
:returns: Optionally modified decryption materials.
54-
:rtype: aws_encryption_sdk.materials_managers.DecryptionMaterials
43+
:rtype: DecryptionMaterials
5544
:raises NotImplementedError: if method is not implemented
5645
"""
5746
raise NotImplementedError("Keyring does not implement on_decrypt function")

src/aws_encryption_sdk/keyrings/multi.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License"). You
4-
# may not use this file except in compliance with the License. A copy of
5-
# the License is located at
6-
#
7-
# http://aws.amazon.com/apache2.0/
8-
#
9-
# or in the "license" file accompanying this file. This file is
10-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11-
# ANY KIND, either express or implied. See the License for the specific
12-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
133
"""Resources required for Multi Keyrings."""
144
import itertools
155

@@ -31,21 +21,24 @@
3121
# We only actually need these imports when running the mypy checks
3222
pass
3323

24+
__all__ = ("MultiKeyring",)
25+
3426

3527
@attr.s
3628
class MultiKeyring(Keyring):
3729
"""Public class for Multi Keyring.
3830
39-
:param generator: Generator keyring used to generate data encryption key (optional)
40-
:type generator: Keyring
41-
:param list children: List of keyrings used to encrypt the data encryption key (optional)
31+
.. versionadded:: 1.5.0
32+
33+
:param Keyring generator: Generator keyring used to generate data encryption key (optional)
34+
:param List[Keyring] children: List of keyrings used to encrypt the data encryption key (optional)
4235
:raises EncryptKeyError: if encryption of data key fails for any reason
4336
"""
4437

38+
generator = attr.ib(default=None, validator=optional(instance_of(Keyring)))
4539
children = attr.ib(
4640
default=attr.Factory(tuple), validator=optional(deep_iterable(member_validator=instance_of(Keyring)))
4741
)
48-
generator = attr.ib(default=None, validator=optional(instance_of(Keyring)))
4942

5043
def __attrs_post_init__(self):
5144
# type: () -> None
@@ -62,10 +55,9 @@ def on_encrypt(self, encryption_materials):
6255
"""Generate a data key using generator keyring
6356
and encrypt it using any available wrapping key in any child keyring.
6457
65-
:param encryption_materials: Encryption materials for keyring to modify.
66-
:type encryption_materials: aws_encryption_sdk.materials_managers.EncryptionMaterials
58+
:param EncryptionMaterials encryption_materials: Encryption materials for keyring to modify.
6759
:returns: Optionally modified encryption materials.
68-
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
60+
:rtype: EncryptionMaterials
6961
:raises EncryptKeyError: if unable to encrypt data key.
7062
"""
7163
# Check if generator keyring is not provided and data key is not generated
@@ -94,12 +86,10 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
9486
# type: (DecryptionMaterials, Iterable[EncryptedDataKey]) -> DecryptionMaterials
9587
"""Attempt to decrypt the encrypted data keys.
9688
97-
:param decryption_materials: Decryption materials for keyring to modify.
98-
:type decryption_materials: aws_encryption_sdk.materials_managers.DecryptionMaterials
99-
:param encrypted_data_keys: List of encrypted data keys.
100-
:type: List of `aws_encryption_sdk.structures.EncryptedDataKey`
89+
:param DecryptionMaterials decryption_materials: Decryption materials for keyring to modify.
90+
:param List[EncryptedDataKey] encrypted_data_keys: List of encrypted data keys.
10191
:returns: Optionally modified decryption materials.
102-
:rtype: aws_encryption_sdk.materials_managers.DecryptionMaterials
92+
:rtype: DecryptionMaterials
10393
"""
10494
# Call on_decrypt on all keyrings till decryption is successful
10595
for keyring in self._decryption_keyrings:

0 commit comments

Comments
 (0)