Skip to content

fix(raw-keyrings): Raise when passed a key_namespace of "aws-kms" #286

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 3 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions src/aws_encryption_sdk/keyrings/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class RawAESKeyring(Keyring):
.. versionadded:: 1.5.0

:param str key_namespace: String defining the keyring.

.. note::
key_namespace MUST NOT equal "aws-kms".

:param bytes key_name: Key ID
:param bytes wrapping_key: Encryption key with which to wrap plaintext data key.

Expand All @@ -98,6 +102,9 @@ def __attrs_post_init__(self):
)
}

if self.key_namespace == "aws-kms":
raise ValueError('Key namespace MUST NOT be "aws-kms"')

try:
self._wrapping_algorithm = key_size_to_wrapping_algorithm[len(self._wrapping_key)]
except KeyError:
Expand Down Expand Up @@ -245,6 +252,10 @@ class RawRSAKeyring(Keyring):
.. versionadded:: 1.5.0

:param str key_namespace: String defining the keyring ID

.. note::
key_namespace MUST NOT equal "aws-kms".

:param bytes key_name: Key ID
:param cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey private_wrapping_key:
Private encryption key with which to wrap plaintext data key (optional)
Expand Down Expand Up @@ -280,6 +291,9 @@ def __attrs_post_init__(self):
"""Prepares initial values not handled by attrs."""
self._key_provider = MasterKeyInfo(provider_id=self.key_namespace, key_info=self.key_name)

if self.key_namespace == "aws-kms":
raise ValueError('Key namespace MUST NOT be "aws-kms"')

if self._public_wrapping_key is None and self._private_wrapping_key is None:
raise TypeError("At least one of public key or private key must be provided.")

Expand Down
16 changes: 16 additions & 0 deletions test/functional/keyrings/raw/test_raw_aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,19 @@ def test_key_info_prefix_vectors(wrapping_algorithm):
)
== _KEY_ID + b"\x00\x00\x00\x80\x00\x00\x00\x0c"
)


def test_must_not_accept_aws_kms():

# Initializing attributes
key_namespace = "aws-kms"
key_name = _KEY_ID

# Attempt to instantiate a raw AES keyring
with pytest.raises(ValueError) as excinfo:
RawAESKeyring(
key_namespace=key_namespace, key_name=key_name, wrapping_key=_WRAPPING_KEY,
)

# Check the error message
excinfo.match('Key namespace MUST NOT be "aws-kms"')
15 changes: 15 additions & 0 deletions test/functional/keyrings/raw/test_raw_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,18 @@ def test_keypair_must_match():
)

excinfo.match("Private and public wrapping keys MUST be from the same keypair.")


def test_must_not_accept_aws_kms():
bad_key_namespace = "aws-kms"

with pytest.raises(ValueError) as excinfo:
RawRSAKeyring(
key_namespace=bad_key_namespace,
key_name=_KEY_ID,
wrapping_algorithm=_WRAPPING_ALGORITHM,
private_wrapping_key=_PRIVATE_WRAPPING_KEY,
public_wrapping_key=_PUBLIC_WRAPPING_KEY,
)

excinfo.match('Key namespace MUST NOT be "aws-kms"')