Skip to content

Commit 6ff66fa

Browse files
authored
fix AWS KMS encryption context calculation to correctly handle binary values (#45)
* fix AWS KMS encryption context calculation to correctly handle binary values
1 parent 3d68464 commit 6ff66fa

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/dynamodb_encryption_sdk/material_providers/aws_kms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _attribute_to_value(self, attribute):
301301
"""
302302
attribute_type, attribute_value = list(attribute.items())[0]
303303
if attribute_type == 'B':
304-
return base64.b64encode(attribute_value.value).decode(TEXT_ENCODING)
304+
return base64.b64encode(attribute_value).decode(TEXT_ENCODING)
305305
if attribute_type in ('S', 'N'):
306306
return attribute_value
307307
raise ValueError('Attribute of type "{}" cannot be used in KMS encryption context.'.format(attribute_type))

test/integration/material_providers/test_aws_kms.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
# language governing permissions and limitations under the License.
1313
"""Integration tests for ``dynamodb_encryption_sdk.material_providers.aws_kms``."""
1414
import logging
15+
import itertools
1516

1617
import hypothesis
1718
import pytest
1819

1920
from dynamodb_encryption_sdk.encrypted import CryptoConfig
20-
from dynamodb_encryption_sdk.identifiers import USER_AGENT_SUFFIX
21-
from dynamodb_encryption_sdk.structures import EncryptionContext
21+
from dynamodb_encryption_sdk.identifiers import CryptoAction, USER_AGENT_SUFFIX
22+
from dynamodb_encryption_sdk.structures import AttributeActions, EncryptionContext
23+
from dynamodb_encryption_sdk.transform import dict_to_ddb
2224
from ..integration_test_utils import aws_kms_cmp # noqa pylint: disable=unused-import
2325
from ..integration_test_utils import functional_test_utils, hypothesis_strategies
2426

2527
pytestmark = pytest.mark.integ
2628

29+
_primary_key_names = ('partition_key', 'sort_key')
30+
2731

2832
def pytest_generate_tests(metafunc):
2933
functional_test_utils.set_parametrized_actions(metafunc)
@@ -38,6 +42,35 @@ def test_verify_user_agent(aws_kms_cmp, caplog):
3842
assert USER_AGENT_SUFFIX in caplog.text
3943

4044

45+
def _many_items():
46+
values = ('a string', 1234, b'binary \x00\x88 value')
47+
partition_keys = (('partition_key', value) for value in values)
48+
sort_keys = (('sort_key', value) for value in values)
49+
for pairs in itertools.product(partition_keys, sort_keys):
50+
item = dict(pairs)
51+
yield pytest.param(item, id=str(item))
52+
53+
54+
@pytest.mark.parametrize('item', _many_items())
55+
def test_aws_kms_diverse_indexes(aws_kms_cmp, item):
56+
"""Verify that AWS KMS cycle works for items with all possible combinations for primary index attribute types."""
57+
crypto_config = CryptoConfig(
58+
materials_provider=aws_kms_cmp,
59+
encryption_context=EncryptionContext(
60+
partition_key_name='partition_key',
61+
sort_key_name='sort_key',
62+
attributes=dict_to_ddb(item)
63+
),
64+
attribute_actions=AttributeActions(
65+
attribute_actions={
66+
key: CryptoAction.SIGN_ONLY
67+
for key in _primary_key_names
68+
}
69+
)
70+
)
71+
functional_test_utils.cycle_item_check(item, crypto_config)
72+
73+
4174
def test_aws_kms_item_cycle(aws_kms_cmp, parametrized_actions, parametrized_item):
4275
crypto_config = CryptoConfig(
4376
materials_provider=aws_kms_cmp,

test/unit/material_providers/test_aws_kms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def test_select_id(default_kms_cmp):
318318

319319
# TODO: vectorize
320320
@pytest.mark.parametrize('attribute, expected_value', (
321-
({'B': Binary(b'\x00\x01\x02\x03')}, 'AAECAw=='),
321+
({'B': b'\x00\x01\x02\x03'}, 'AAECAw=='),
322322
({'S': 'some string value'}, 'some string value'),
323323
({'N': '55.2'}, '55.2')
324324
))

0 commit comments

Comments
 (0)