Skip to content

feat: enable use of keyrings #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dbccf9f
chore: add __all__ values for keyring modules
mattsb42-aws Feb 20, 2020
bd1eea9
chore: add versionadded tags for keyrings
mattsb42-aws Feb 20, 2020
17ec28e
feat: plump keyrings into stream handlers and default CMM
mattsb42-aws Feb 21, 2020
263750d
chore: incorporate keyrings into tests
mattsb42-aws Feb 21, 2020
fa1a6ee
add raw AES keyring-MKP compatibility tests
mattsb42-aws Feb 22, 2020
fce9fe1
add raw RSA MKP-keyring compatibility tests
mattsb42-aws Feb 22, 2020
2a69b22
fix: fix integ test to run when default region is set
mattsb42-aws Feb 22, 2020
20872fb
chore: add public-private keyring-MKP compat tests
mattsb42-aws Feb 22, 2020
87fed8a
fix: fix typo
mattsb42-aws Feb 24, 2020
8a27aea
add DefaultCryptographicMaterialsProvider tests for keyrings that ret…
mattsb42-aws Feb 24, 2020
bc8bfb2
feat: enable caching CMM to accept either MKP or keyring
mattsb42-aws Feb 25, 2020
b5da8aa
fix: rename test keyring to avoid name collision
mattsb42-aws Feb 25, 2020
cf6bcdf
chore: caching CMM has too many instance attributes and that's ok
mattsb42-aws Feb 25, 2020
d3fcab7
docs: add versionadded flags to docstrings for keyring values
mattsb42-aws Feb 25, 2020
d4bec46
docs: update docs on encrypt/decrypt helper functions to match underl…
mattsb42-aws Feb 25, 2020
f9713cc
chore: update copyright notices on modified files
mattsb42-aws Feb 25, 2020
1c76c17
docs: render keyring docs
mattsb42-aws Feb 25, 2020
50f529b
docs: clean up keyring method docs
mattsb42-aws Feb 25, 2020
56db42f
fix: fix linting issues
mattsb42-aws Feb 25, 2020
8f83d2b
fix: autoformat
mattsb42-aws Feb 26, 2020
bbb6056
docs: fix typo
mattsb42-aws Feb 27, 2020
bcc6781
fix: fix docs and error message inconsistency
mattsb42-aws Feb 27, 2020
4856ab1
fix: re-order checks to avoid misleading error messages if materials …
mattsb42-aws Feb 27, 2020
a46b80d
chore: test broken paths in default CMM with more algorithm suites
mattsb42-aws Feb 27, 2020
0438910
docs: make docs correctly link to pyca/cryptography docs
mattsb42-aws Mar 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Modules
aws_encryption_sdk.caches.base
aws_encryption_sdk.caches.local
aws_encryption_sdk.caches.null
aws_encryption_sdk.keyrings.base
aws_encryption_sdk.keyrings.multi
aws_encryption_sdk.keyrings.raw
aws_encryption_sdk.key_providers.base
aws_encryption_sdk.key_providers.kms
aws_encryption_sdk.key_providers.raw
Expand Down
48 changes: 24 additions & 24 deletions src/aws_encryption_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""High level AWS Encryption SDK client functions."""
# Below are imported for ease of use by implementors
from aws_encryption_sdk.caches.local import LocalCryptoMaterialsCache # noqa
Expand All @@ -33,6 +23,9 @@ def encrypt(**kwargs):
When using this function, the entire ciphertext message is encrypted into memory before returning
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.

.. versionadded:: 1.5.0
The *keyring* parameter.

.. code:: python

>>> import aws_encryption_sdk
Expand All @@ -49,12 +42,14 @@ def encrypt(**kwargs):
:type config: aws_encryption_sdk.streaming_client.EncryptorConfig
:param source: Source data to encrypt or decrypt
:type source: str, bytes, io.IOBase, or file
:param materials_manager: `CryptoMaterialsManager` from which to obtain cryptographic materials
(either `materials_manager` or `key_provider` required)
:type materials_manager: aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager
:param key_provider: `MasterKeyProvider` from which to obtain data keys for encryption
(either `materials_manager` or `key_provider` required)
:type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
:param CryptoMaterialsManager materials_manager:
Cryptographic materials manager to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param Keyring keyring: Keyring to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param MasterKeyProvider key_provider:
Master key provider to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param int source_length: Length of source data (optional)

.. note::
Expand Down Expand Up @@ -87,6 +82,9 @@ def decrypt(**kwargs):
When using this function, the entire ciphertext message is decrypted into memory before returning
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.

.. versionadded:: 1.5.0
The *keyring* parameter.

.. code:: python

>>> import aws_encryption_sdk
Expand All @@ -103,12 +101,14 @@ def decrypt(**kwargs):
:type config: aws_encryption_sdk.streaming_client.DecryptorConfig
:param source: Source data to encrypt or decrypt
:type source: str, bytes, io.IOBase, or file
:param materials_manager: `CryptoMaterialsManager` from which to obtain cryptographic materials
(either `materials_manager` or `key_provider` required)
:type materials_manager: aws_encryption_sdk.materials_managers.base.CryptoMaterialsManager
:param key_provider: `MasterKeyProvider` from which to obtain data keys for decryption
(either `materials_manager` or `key_provider` required)
:type key_provider: aws_encryption_sdk.key_providers.base.MasterKeyProvider
:param CryptoMaterialsManager materials_manager:
Cryptographic materials manager to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param Keyring keyring: Keyring to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param MasterKeyProvider key_provider:
Master key provider to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param int source_length: Length of source data (optional)

.. note::
Expand Down
21 changes: 9 additions & 12 deletions src/aws_encryption_sdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""Contains exception classes for AWS Encryption SDK."""


Expand Down Expand Up @@ -87,6 +77,13 @@ class SignatureKeyError(AWSEncryptionSDKClientError):
"""


class InvalidCryptographicMaterialsError(AWSEncryptionSDKClientError):
"""Exception class for errors encountered when attempting to validate cryptographic materials.

.. versionadded:: 1.5.0
"""


class ActionNotAllowedError(AWSEncryptionSDKClientError):
"""Exception class for errors encountered when attempting to perform unallowed actions."""

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

__all__ = ("Keyring",)


class Keyring(object):
"""Parent interface for Keyring classes.
Expand All @@ -34,10 +26,9 @@ def on_encrypt(self, encryption_materials):
# type: (EncryptionMaterials) -> EncryptionMaterials
"""Generate a data key if not present and encrypt it using any available wrapping key.

:param encryption_materials: Encryption materials for the keyring to modify.
:type encryption_materials: aws_encryption_sdk.materials_managers.EncryptionMaterials
:param EncryptionMaterials encryption_materials: Encryption materials for keyring to modify.
:returns: Optionally modified encryption materials.
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
:rtype: EncryptionMaterials
:raises NotImplementedError: if method is not implemented
"""
raise NotImplementedError("Keyring does not implement on_encrypt function")
Expand All @@ -46,12 +37,10 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
# type: (DecryptionMaterials, Iterable[EncryptedDataKey]) -> DecryptionMaterials
"""Attempt to decrypt the encrypted data keys.

:param decryption_materials: Decryption materials for the keyring to modify.
:type decryption_materials: aws_encryption_sdk.materials_managers.DecryptionMaterials
:param encrypted_data_keys: List of encrypted data keys.
:type: Iterable of :class:`aws_encryption_sdk.structures.EncryptedDataKey`
:param DecryptionMaterials decryption_materials: Decryption materials for keyring to modify.
:param List[EncryptedDataKey] encrypted_data_keys: List of encrypted data keys.
:returns: Optionally modified decryption materials.
:rtype: aws_encryption_sdk.materials_managers.DecryptionMaterials
:rtype: DecryptionMaterials
:raises NotImplementedError: if method is not implemented
"""
raise NotImplementedError("Keyring does not implement on_decrypt function")
38 changes: 14 additions & 24 deletions src/aws_encryption_sdk/keyrings/multi.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""Resources required for Multi Keyrings."""
import itertools

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

__all__ = ("MultiKeyring",)


@attr.s
class MultiKeyring(Keyring):
"""Public class for Multi Keyring.

:param generator: Generator keyring used to generate data encryption key (optional)
:type generator: Keyring
:param list children: List of keyrings used to encrypt the data encryption key (optional)
.. versionadded:: 1.5.0

:param Keyring generator: Generator keyring used to generate data encryption key (optional)
:param List[Keyring] children: List of keyrings used to encrypt the data encryption key (optional)
:raises EncryptKeyError: if encryption of data key fails for any reason
"""

generator = attr.ib(default=None, validator=optional(instance_of(Keyring)))
children = attr.ib(
default=attr.Factory(tuple), validator=optional(deep_iterable(member_validator=instance_of(Keyring)))
)
generator = attr.ib(default=None, validator=optional(instance_of(Keyring)))

def __attrs_post_init__(self):
# type: () -> None
Expand All @@ -62,10 +55,9 @@ def on_encrypt(self, encryption_materials):
"""Generate a data key using generator keyring
and encrypt it using any available wrapping key in any child keyring.

:param encryption_materials: Encryption materials for keyring to modify.
:type encryption_materials: aws_encryption_sdk.materials_managers.EncryptionMaterials
:param EncryptionMaterials encryption_materials: Encryption materials for keyring to modify.
:returns: Optionally modified encryption materials.
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
:rtype: EncryptionMaterials
:raises EncryptKeyError: if unable to encrypt data key.
"""
# Check if generator keyring is not provided and data key is not generated
Expand Down Expand Up @@ -94,12 +86,10 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
# type: (DecryptionMaterials, Iterable[EncryptedDataKey]) -> DecryptionMaterials
"""Attempt to decrypt the encrypted data keys.

:param decryption_materials: Decryption materials for keyring to modify.
:type decryption_materials: aws_encryption_sdk.materials_managers.DecryptionMaterials
:param encrypted_data_keys: List of encrypted data keys.
:type: List of `aws_encryption_sdk.structures.EncryptedDataKey`
:param DecryptionMaterials decryption_materials: Decryption materials for keyring to modify.
:param List[EncryptedDataKey] encrypted_data_keys: List of encrypted data keys.
:returns: Optionally modified decryption materials.
:rtype: aws_encryption_sdk.materials_managers.DecryptionMaterials
:rtype: DecryptionMaterials
"""
# Call on_decrypt on all keyrings till decryption is successful
for keyring in self._decryption_keyrings:
Expand Down
Loading