Skip to content

Commit 3428a3d

Browse files
m
2 parents 00013d2 + c7e254a commit 3428a3d

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

test_vector_handlers/src/awses_test_vectors/manifests/master_key.py

+40-27
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"""
88
import attr
99
import six
10-
from aws_encryption_sdk.exceptions import IncorrectMasterKeyError, InvalidKeyIdError
10+
from aws_encryption_sdk.exceptions import InvalidKeyIdError
1111
from aws_encryption_sdk.identifiers import EncryptionKeyType, WrappingAlgorithm
12-
from aws_encryption_sdk.key_providers.base import MasterKeyProvider, MasterKeyProviderConfig # noqa pylint: disable=unused-import
12+
from aws_encryption_sdk.key_providers.base import MasterKeyProvider, MasterKeyProviderConfig
1313
from aws_encryption_sdk.key_providers.kms import ( # noqa pylint: disable=unused-import
1414
DiscoveryFilter,
1515
KMSMasterKey,
@@ -63,31 +63,6 @@
6363
"public": EncryptionKeyType.PUBLIC,
6464
}
6565

66-
class TestVectorsMultiMasterKeyProvider(MasterKeyProvider):
67-
"""
68-
Provider for other MasterKeyProviders.
69-
Allows a "multi" MasterKeyProvider for use in test vectors.
70-
71-
In Python ESDK, MasterKey extends MasterKeyProvider.
72-
However, MasterKey overrides MasterKeyProvider's `decrypt_data_key` method.
73-
From AWS ESDK specification:
74-
"A master key MUST supply itself and MUST NOT supply any other master keys."
75-
https://github.com/awslabs/aws-encryption-sdk-specification/blob/master/framework/master-key-interface.md#get-master-key
76-
77-
78-
"""
79-
80-
_config_class = MasterKeyProviderConfig
81-
provider_id = "aws-test-vectors-multi-master-key-provider"
82-
83-
def __init__(self):
84-
self.key_provider_for_key_id = {}
85-
86-
def add_key(self, key_provider):
87-
self._members.append(key_provider)
88-
89-
def _new_master_key(self, key_id):
90-
raise InvalidKeyIdError()
9166

9267
@attr.s
9368
class MasterKeySpec(object): # pylint: disable=too-many-instance-attributes
@@ -316,6 +291,44 @@ def scenario_spec(self):
316291
return spec
317292

318293

294+
class TestVectorsMultiMasterKeyProvider(MasterKeyProvider):
295+
"""
296+
Provider for other MasterKeyProviders.
297+
Acts as a "multi" MasterKeyProvider for use in test vectors.
298+
299+
There is some disagreement between the spec
300+
and how Python ESDK implements MasterKey;
301+
this class fills that gap.
302+
303+
In the ESDK-Python, MasterKey extends MasterKeyProvider;
304+
i.e. MasterKey "is a" MasterKeyProvider; isinstance(some_master_key, MasterKeyProvider) == True.
305+
306+
From AWS ESDK specification:
307+
"A master key MUST supply itself and MUST NOT supply any other master keys."
308+
https://github.com/awslabs/aws-encryption-sdk-specification/blob/master/framework/master-key-interface.md#get-master-key
309+
310+
The MasterKey class overrides MasterKeyProvider's `decrypt_data_key` method to correct this gap.
311+
However, this modification suggests that this "is a" relationship is not entirely true.
312+
313+
master_key_provider_from_master_key_specs expects to return a MasterKeyProvider, not a MasterKey.
314+
master_key_provider_from_master_key_specs uses this class to always return a MasterKeyProvider
315+
that wraps any MasterKeyProvider or MasterKey loaded from a spec.
316+
"""
317+
318+
_config_class = MasterKeyProviderConfig
319+
provider_id = "aws-test-vectors-multi-master-key-provider"
320+
_members = []
321+
322+
def add_key(self, key_provider):
323+
"""Add a MKP to the list of configured MKPs."""
324+
self._members.append(key_provider)
325+
326+
def _new_master_key(self, key_id):
327+
# This MKP does not have a key associated with it.
328+
# ESDK-Python will find keys in _members.
329+
raise InvalidKeyIdError()
330+
331+
319332
def master_key_provider_from_master_key_specs(keys, master_key_specs):
320333
# type: (KeysManifest, Iterable[MasterKeySpec]) -> MasterKeyProvider
321334
"""Build and combine all master key providers identified by the provided specs and

test_vector_handlers/src/awses_test_vectors/manifests/mpl_keyring.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
GetKeyDescriptionOutput,
2626
TestVectorKeyringInput,
2727
)
28-
from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders
29-
from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig
30-
from aws_cryptographic_material_providers.mpl.references import IKeyring
31-
from aws_cryptographic_material_providers.mpl.models import CreateMultiKeyringInput
28+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
29+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
30+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
31+
from aws_cryptographic_materialproviders.mpl.models import CreateMultiKeyringInput
3232

3333
import _dafny
3434
from smithy_dafny_standard_library.internaldafny.generated import UTF8
@@ -180,7 +180,8 @@ def keyring(self, keys_uri, mode):
180180
# But this seems weird, and we didn't have to do this in Java.
181181
if hasattr(keyring, "_impl"): # pylint: disable=protected-access
182182
if hasattr(keyring._impl, "_keyName"): # pylint: disable=protected-access
183-
if keyring._impl._keyName == UTF8.default__.Encode(_dafny.Seq("rsa-4096-public")).value:
183+
if keyring._impl._keyName == UTF8.default__.Encode(_dafny.Seq("rsa-4096-public")).value \
184+
and mode in ("decrypt-generate", "encrypt"): # pylint: disable=protected-access
184185
if changed_key_name_from_private_to_public:
185186
# pylint: disable=protected-access
186187
keyring._impl._keyName = UTF8.default__.Encode(_dafny.Seq("rsa-4096-private")).value

0 commit comments

Comments
 (0)