Skip to content

Commit 7645d79

Browse files
committed
Made suggested changes in multi keyring
1 parent aa9145f commit 7645d79

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/aws_encryption_sdk/keyring/multi_keyring.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Resources required for Multi Keyrings."""
14+
import itertools
15+
1416
import attr
1517
from attr.validators import deep_iterable, instance_of, optional
1618

17-
from aws_encryption_sdk.exceptions import EncryptKeyError
19+
from aws_encryption_sdk.exceptions import EncryptKeyError, GenerateKeyError
1820
from aws_encryption_sdk.keyring.base import DecryptionMaterials, EncryptedDataKey, EncryptionMaterials, Keyring
1921

2022
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
@@ -35,17 +37,19 @@ class MultiKeyring(Keyring):
3537
"""
3638

3739
children = attr.ib(
38-
default=None,
39-
validator=optional(deep_iterable(member_validator=instance_of(Keyring), iterable_validator=instance_of(list))),
40+
default=attr.Factory(tuple), validator=optional(deep_iterable(member_validator=instance_of(Keyring)))
4041
)
4142
generator = attr.ib(default=None, validator=optional(instance_of(Keyring)))
4243

4344
def __attrs_post_init__(self):
4445
# type: () -> None
4546
"""Prepares initial values not handled by attrs."""
46-
neither_generator_nor_children = self.generator is None and self.children is None
47+
neither_generator_nor_children = self.generator is None and not self.children
4748
if neither_generator_nor_children:
48-
raise TypeError("At least one of generator or children should be provided")
49+
raise TypeError("At least one of generator or children must be provided")
50+
51+
_generator = (self.generator,) if self.generator is not None else ()
52+
self._decryption_keyrings = itertools.chain(_generator, self.children)
4953

5054
def on_encrypt(self, encryption_materials):
5155
# type: (EncryptionMaterials) -> EncryptionMaterials
@@ -71,12 +75,11 @@ def on_encrypt(self, encryption_materials):
7175

7276
# Check if data key is generated
7377
if not encryption_materials.data_encryption_key:
74-
raise EncryptKeyError("Unable to generate data encryption key.")
78+
raise GenerateKeyError("Unable to generate data encryption key.")
7579

7680
# Call on_encrypt on all other keyrings
77-
if self.children is not None:
78-
for keyring in self.children:
79-
encryption_materials = keyring.on_encrypt(encryption_materials)
81+
for keyring in self.children:
82+
encryption_materials = keyring.on_encrypt(encryption_materials)
8083

8184
return encryption_materials
8285

@@ -95,17 +98,10 @@ def on_decrypt(self, decryption_materials, encrypted_data_keys):
9598
if decryption_materials.data_encryption_key:
9699
return decryption_materials
97100

98-
# Call on_decrypt on generator keyring if it is provided
99-
if self.generator is not None:
100-
decryption_materials = self.generator.on_decrypt(decryption_materials, encrypted_data_keys)
101+
# Call on_decrypt on all keyrings till decryption is successful
102+
for keyring in self._decryption_keyrings:
103+
decryption_materials = keyring.on_decrypt(decryption_materials, encrypted_data_keys)
101104
if decryption_materials.data_encryption_key:
102105
return decryption_materials
103106

104-
# Call on_decrypt on all keyrings till decryption is successful
105-
if self.children is not None:
106-
for keyring in self.children:
107-
decryption_materials = keyring.on_decrypt(decryption_materials, encrypted_data_keys)
108-
if decryption_materials.data_encryption_key:
109-
return decryption_materials
110-
111107
return decryption_materials

test/unit/test_multi_keyrings.py

Whitespace-only changes.

0 commit comments

Comments
 (0)