Skip to content

Commit abcc934

Browse files
committed
enable authentication-only RawEn/DecryptionMaterials
1 parent a210236 commit abcc934

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/dynamodb_encryption_sdk/materials/raw.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ class RawEncryptionMaterials(EncryptionMaterials):
5151
"""
5252

5353
_signing_key = attr.ib(validator=attr.validators.instance_of(DelegatedKey))
54-
_encryption_key = attr.ib(validator=attr.validators.instance_of(DelegatedKey))
54+
_encryption_key = attr.ib(
55+
validator=attr.validators.optional(attr.validators.instance_of(DelegatedKey)),
56+
default=None
57+
)
5558
_material_description = attr.ib(
5659
validator=dictionary_validator(six.string_types, six.string_types),
5760
converter=copy.deepcopy,
@@ -60,7 +63,7 @@ class RawEncryptionMaterials(EncryptionMaterials):
6063

6164
def __attrs_post_init__(self):
6265
"""Verify that the encryption key is allowed be used for raw materials."""
63-
if not self._encryption_key.allowed_for_raw_materials:
66+
if self._encryption_key is not None and not self._encryption_key.allowed_for_raw_materials:
6467
raise ValueError('Encryption key type "{}" does not allow use with RawEncryptionMaterials'.format(
6568
type(self._encryption_key)
6669
))
@@ -93,6 +96,9 @@ def encryption_key(self):
9396
:returns: Encryption key
9497
:rtype: dynamodb_encryption_sdk.delegated_keys.DelegatedKey
9598
"""
99+
if self._encryption_key is None:
100+
raise AttributeError('No encryption key available')
101+
96102
return self._encryption_key
97103

98104

@@ -113,7 +119,10 @@ class RawDecryptionMaterials(DecryptionMaterials):
113119
"""
114120

115121
_verification_key = attr.ib(validator=attr.validators.instance_of(DelegatedKey))
116-
_decryption_key = attr.ib(validator=attr.validators.instance_of(DelegatedKey))
122+
_decryption_key = attr.ib(
123+
validator=attr.validators.optional(attr.validators.instance_of(DelegatedKey)),
124+
default=None
125+
)
117126
_material_description = attr.ib(
118127
validator=dictionary_validator(six.string_types, six.string_types),
119128
converter=copy.deepcopy,
@@ -122,7 +131,7 @@ class RawDecryptionMaterials(DecryptionMaterials):
122131

123132
def __attrs_post_init__(self):
124133
"""Verify that the encryption key is allowed be used for raw materials."""
125-
if not self._decryption_key.allowed_for_raw_materials:
134+
if self._decryption_key is not None and not self._decryption_key.allowed_for_raw_materials:
126135
raise ValueError('Decryption key type "{}" does not allow use with RawDecryptionMaterials'.format(
127136
type(self._decryption_key)
128137
))
@@ -155,4 +164,7 @@ def decryption_key(self):
155164
:returns: Decryption key
156165
:rtype: dynamodb_encryption_sdk.delegated_keys.DelegatedKey
157166
"""
167+
if self._decryption_key is None:
168+
raise AttributeError('No decryption key available')
169+
158170
return self._decryption_key

test/functional/materials/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Dummy stub to make linters work better."""

test/functional/materials/test_raw.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Functional test suite for ``dynamodb_encryption_sdk.materials.raw``."""
14+
import pytest
15+
16+
from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey
17+
from dynamodb_encryption_sdk.materials.raw import RawDecryptionMaterials, RawEncryptionMaterials
18+
19+
pytestmark = [pytest.mark.functional, pytest.mark.local]
20+
21+
22+
def test_no_encryption_key():
23+
signing_key = JceNameLocalDelegatedKey.generate('HmacSHA512', 256)
24+
encryption_materials = RawEncryptionMaterials(signing_key=signing_key)
25+
26+
with pytest.raises(AttributeError) as excinfo:
27+
encryption_materials.encryption_key
28+
29+
excinfo.match('No encryption key available')
30+
31+
32+
def test_no_decryption_key():
33+
verification_key = JceNameLocalDelegatedKey.generate('HmacSHA512', 256)
34+
decryption_materials = RawDecryptionMaterials(verification_key=verification_key)
35+
36+
with pytest.raises(AttributeError) as excinfo:
37+
decryption_materials.decryption_key
38+
39+
excinfo.match('No decryption key available')

0 commit comments

Comments
 (0)