Skip to content

Commit 2bac0de

Browse files
committed
feat: convert AWS KMS keyring and helpers to require a callable rather than an instance of ClientSupplier
1 parent 929c5b9 commit 2bac0de

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/aws_encryption_sdk/keyrings/aws_kms/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import attr
1111
import six
12-
from attr.validators import deep_iterable, instance_of, optional
12+
from attr.validators import deep_iterable, instance_of, is_callable, optional
1313

1414
from aws_encryption_sdk.exceptions import DecryptKeyError, EncryptKeyError
1515
from aws_encryption_sdk.identifiers import AlgorithmSuite
@@ -23,6 +23,7 @@
2323

2424
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
2525
from typing import Any, Dict, Iterable, Union # noqa pylint: disable=unused-import
26+
from .client_suppliers import ClientSupplierType # noqa pylint: disable=unused-import
2627
except ImportError: # pragma: no cover
2728
# We only actually need these imports when running the mypy checks
2829
pass
@@ -77,7 +78,7 @@ class KmsKeyring(Keyring):
7778
:param List[str] grant_tokens: AWS KMS grant tokens to include in requests (optional)
7879
"""
7980

80-
_client_supplier = attr.ib(default=attr.Factory(DefaultClientSupplier), validator=instance_of(ClientSupplier))
81+
_client_supplier = attr.ib(default=attr.Factory(DefaultClientSupplier), validator=is_callable())
8182
_generator_key_id = attr.ib(default=None, validator=optional(instance_of(six.string_types)))
8283
_child_key_ids = attr.ib(
8384
default=attr.Factory(tuple),
@@ -154,7 +155,7 @@ class _AwsKmsSingleCmkKeyring(Keyring):
154155
"""
155156

156157
_key_id = attr.ib(validator=instance_of(six.string_types))
157-
_client_supplier = attr.ib(validator=instance_of(ClientSupplier))
158+
_client_supplier = attr.ib(validator=is_callable())
158159
_grant_tokens = attr.ib(
159160
default=attr.Factory(tuple),
160161
validator=(deep_iterable(member_validator=instance_of(six.string_types)), value_is_not_a_string),
@@ -231,7 +232,7 @@ class _AwsKmsDiscoveryKeyring(Keyring):
231232
:param List[str] grant_tokens: AWS KMS grant tokens to include in requests (optional)
232233
"""
233234

234-
_client_supplier = attr.ib(validator=instance_of(ClientSupplier))
235+
_client_supplier = attr.ib(validator=is_callable())
235236
_grant_tokens = attr.ib(
236237
default=attr.Factory(tuple),
237238
validator=(deep_iterable(member_validator=instance_of(six.string_types)), value_is_not_a_string),
@@ -261,7 +262,7 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
261262

262263

263264
def _try_aws_kms_decrypt(client_supplier, decryption_materials, grant_tokens, encrypted_data_key):
264-
# type: (ClientSupplier, DecryptionMaterials, Iterable[str], EncryptedDataKey) -> DecryptionMaterials
265+
# type: (ClientSupplierType, DecryptionMaterials, Iterable[str], EncryptedDataKey) -> DecryptionMaterials
265266
"""Attempt to call ``kms:Decrypt`` and return the resulting plaintext data key.
266267
267268
Any errors encountered are caught and logged.
@@ -291,7 +292,7 @@ def _try_aws_kms_decrypt(client_supplier, decryption_materials, grant_tokens, en
291292

292293

293294
def _do_aws_kms_decrypt(client_supplier, key_name, encrypted_data_key, encryption_context, grant_tokens):
294-
# type: (ClientSupplier, str, EncryptedDataKey, Dict[str, str], Iterable[str]) -> RawDataKey
295+
# type: (ClientSupplierType, str, EncryptedDataKey, Dict[str, str], Iterable[str]) -> RawDataKey
295296
"""Attempt to call ``kms:Decrypt`` and return the resulting plaintext data key.
296297
297298
Any errors encountered are passed up the chain without comment.
@@ -318,7 +319,7 @@ def _do_aws_kms_decrypt(client_supplier, key_name, encrypted_data_key, encryptio
318319

319320

320321
def _do_aws_kms_encrypt(client_supplier, key_name, plaintext_data_key, encryption_context, grant_tokens):
321-
# type: (ClientSupplier, str, RawDataKey, Dict[str, str], Iterable[str]) -> EncryptedDataKey
322+
# type: (ClientSupplierType, str, RawDataKey, Dict[str, str], Iterable[str]) -> EncryptedDataKey
322323
"""Attempt to call ``kms:Encrypt`` and return the resulting encrypted data key.
323324
324325
Any errors encountered are passed up the chain without comment.
@@ -338,7 +339,7 @@ def _do_aws_kms_encrypt(client_supplier, key_name, plaintext_data_key, encryptio
338339

339340

340341
def _do_aws_kms_generate_data_key(client_supplier, key_name, encryption_context, algorithm, grant_tokens):
341-
# type: (ClientSupplier, str, Dict[str, str], AlgorithmSuite, Iterable[str]) -> (RawDataKey, EncryptedDataKey)
342+
# type: (ClientSupplierType, str, Dict[str, str], AlgorithmSuite, Iterable[str]) -> (RawDataKey, EncryptedDataKey)
342343
"""Attempt to call ``kms:GenerateDataKey`` and return the resulting plaintext and encrypted data keys.
343344
344345
Any errors encountered are passed up the chain without comment.

0 commit comments

Comments
 (0)