Skip to content

Commit 9d99241

Browse files
authored
Fix botocore regional client use to avoid issues in #124 (#125)
* add extra itertools-like permuter helpers * add permuted valid arguments test for AwsKmsCryptographicMaterialsProvider * fix AwsKmsCryptographicMaterialsProvider.__attrs_post_init__ value override fixes #124 * update changelog with #124 fix * specify region_name in test value
1 parent 45f3826 commit 9d99241

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

CHANGELOG.rst

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Changelog
33
*********
44

5+
1.1.2 -- 2019-09-??
6+
===================
7+
8+
Bugfixes
9+
--------
10+
* Fix :class:`AwsKmsCryptographicMaterialsProvider` regional clients override bug
11+
`#124 <https://github.com/aws/aws-dynamodb-encryption-python/issues/124>`_
12+
513
1.1.1 -- 2019-08-29
614
===================
715

src/dynamodb_encryption_sdk/material_providers/aws_kms.py

-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ def __attrs_post_init__(self):
214214
default_algorithm=_DEFAULT_SIGNING_ALGORITHM,
215215
default_key_length=_DEFAULT_SIGNING_KEY_LENGTH,
216216
)
217-
self._regional_clients = (
218-
{}
219-
) # type: Dict[Text, botocore.client.BaseClient] # noqa pylint: disable=attribute-defined-outside-init
220217

221218
def _add_regional_client(self, region_name):
222219
# type: (Text) -> None

test/unit/material_providers/test_aws_kms.py

+34
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
)
3333
from dynamodb_encryption_sdk.structures import EncryptionContext
3434

35+
from ..unit_test_utils import all_possible_combinations_kwargs
36+
3537
pytestmark = [pytest.mark.unit, pytest.mark.local]
3638

3739
_VALID_KEY_INFO_KWARGS = dict(description="some string", algorithm="algorithm name", length=1234)
@@ -218,6 +220,38 @@ def test_loaded_key_infos():
218220
assert cmp._regional_clients == {}
219221

220222

223+
@pytest.mark.parametrize(
224+
"kwargs",
225+
[
226+
pytest.param(val, id=str(val))
227+
for val in all_possible_combinations_kwargs(
228+
dict(),
229+
dict(botocore_session=botocore.session.Session()),
230+
dict(grant_tokens=("sdvoaweih", "auwshefiouawh")),
231+
dict(material_description={"asoiufeoia": "soajfijewi"}),
232+
dict(
233+
regional_clients={
234+
"my-region-1": boto3.session.Session().client(
235+
"kms", region_name="not-a-real-region", endpoint_url="https://not-a-real-url"
236+
)
237+
}
238+
),
239+
)
240+
],
241+
)
242+
def test_kms_cmp_values_set(kwargs):
243+
cmp = AwsKmsCryptographicMaterialsProvider(key_id="example_key_id", **kwargs)
244+
245+
assert cmp._key_id == "example_key_id"
246+
247+
if "botocore_session" in kwargs:
248+
assert cmp._botocore_session == kwargs["botocore_session"]
249+
250+
assert cmp._grant_tokens == kwargs.get("grant_tokens", ())
251+
assert cmp._material_description == kwargs.get("material_description", {})
252+
assert cmp._regional_clients == kwargs.get("regional_clients", {})
253+
254+
221255
def test_add_regional_client_known_region(default_kms_cmp, patch_boto3_session):
222256
default_kms_cmp._regional_clients[sentinel.region] = sentinel.client
223257

test/unit/unit_test_utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Helper utilities for unit tests."""
14+
import itertools
15+
1416
import pytest
1517

1618
from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey
@@ -25,3 +27,16 @@ def wrapped_cmp():
2527
signing_key=signing_key, wrapping_key=wrapping_key, unwrapping_key=wrapping_key
2628
)
2729
return cmp
30+
31+
32+
def all_possible_combinations(*base_values):
33+
combinations = [itertools.combinations(base_values, i) for i in range(1, len(base_values) + 1)]
34+
return itertools.chain(*combinations)
35+
36+
37+
def all_possible_combinations_kwargs(*base_values):
38+
for combo in all_possible_combinations(*base_values):
39+
kwargs = {}
40+
for values in combo:
41+
kwargs.update(values)
42+
yield kwargs

0 commit comments

Comments
 (0)