Skip to content

Commit 4686fbf

Browse files
committed
feat: restructure default client supplier to pass through config to cache rather than accepting a cache
1 parent 6c2881a commit 4686fbf

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

src/aws_encryption_sdk/keyrings/aws_kms/client_cache.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from botocore.exceptions import BotoCoreError
1717
from botocore.session import Session as BotocoreSession
1818

19-
from aws_encryption_sdk.identifiers import USER_AGENT_SUFFIX
20-
2119
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
2220
from typing import Dict # noqa pylint: disable=unused-import
2321
except ImportError: # pragma: no cover
@@ -36,17 +34,14 @@ class ClientCache(object):
3634
3735
.. versionadded:: 1.5.0
3836
39-
:param botocore_session: Botocore session to use when creating clients (optional)
37+
:param botocore_session: Botocore session to use when creating clients
4038
:type botocore_session: botocore.session.Session
4139
:param client_config: Config to use when creating client
4240
:type client_config: botocore.config.Config
4341
"""
4442

45-
_botocore_session = attr.ib(default=attr.Factory(BotocoreSession), validator=instance_of(BotocoreSession))
46-
_client_config = attr.ib(
47-
default=attr.Factory(functools.partial(BotocoreConfig, user_agent_extra=USER_AGENT_SUFFIX)),
48-
validator=instance_of(BotocoreConfig),
49-
)
43+
_botocore_session = attr.ib(validator=instance_of(BotocoreSession))
44+
_client_config = attr.ib(validator=instance_of(BotocoreConfig))
5045

5146
def __attrs_post_init__(self):
5247
"""Set up internal cache."""

src/aws_encryption_sdk/keyrings/aws_kms/client_suppliers.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
.. versionadded:: 1.5.0
66
77
"""
8+
import functools
89
import logging
910

1011
import attr
1112
import six
1213
from attr.validators import deep_iterable, instance_of, is_callable, optional
1314
from botocore.client import BaseClient
15+
from botocore.config import Config as BotocoreConfig
16+
from botocore.session import Session as BotocoreSession
1417

1518
from aws_encryption_sdk.exceptions import UnknownRegionError
19+
from aws_encryption_sdk.identifiers import USER_AGENT_SUFFIX
1620
from aws_encryption_sdk.internal.validators import value_is_not_a_string
1721

1822
from .client_cache import ClientCache
@@ -58,11 +62,36 @@ class DefaultClientSupplier(ClientSupplier):
5862
5963
.. versionadded:: 1.5.0
6064
61-
:param botocore_session: botocore session to use when creating clients (optional)
65+
If you want clients to have special credentials or other configuration,
66+
you can provide those with custom ``botocore`` Session and/or `Config`_ instances.
67+
68+
.. _Config: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
69+
70+
.. code-block:: python
71+
72+
from aws_encryption_sdk.keyrings.aws_kms.client_supplier import DefaultClientSupplier
73+
from botocore.session import Session
74+
from botocore.config import Config
75+
76+
my_client_supplier = DefaultClientSupplier(
77+
botocore_session=Session(**_get_custom_credentials()),
78+
client_config=Config(connect_timeout=10),
79+
)
80+
81+
:param botocore_session: Botocore session to use when creating clients (optional)
6282
:type botocore_session: botocore.session.Session
83+
:param client_config: Config to use when creating client (optional)
84+
:type client_config: botocore.config.Config
6385
"""
6486

65-
_client_cache = attr.ib(default=attr.Factory(ClientCache), validator=instance_of(ClientCache))
87+
_botocore_session = attr.ib(default=attr.Factory(BotocoreSession), validator=instance_of(BotocoreSession))
88+
_client_config = attr.ib(
89+
default=attr.Factory(functools.partial(BotocoreConfig, user_agent_extra=USER_AGENT_SUFFIX)),
90+
validator=instance_of(BotocoreConfig),
91+
)
92+
93+
def __attrs_post_init__(self):
94+
self._client_cache = ClientCache(botocore_session=self._botocore_session, client_config=self._client_config)
6695

6796
def __call__(self, region_name):
6897
# type: (Union[None, str]) -> BaseClient

test/functional/keyrings/aws_kms/test_client_cache.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Functional tests for ``aws_encryption_sdk.keyrings.aws_kms.client_cache``."""
44
import pytest
5+
from botocore.config import Config
6+
from botocore.session import Session
57

68
from aws_encryption_sdk.keyrings.aws_kms.client_cache import ClientCache
79

810
pytestmark = [pytest.mark.functional, pytest.mark.local]
911

1012

1113
def test_client_cache_caches_clients():
12-
cache = ClientCache()
14+
cache = ClientCache(botocore_session=Session(), client_config=Config())
1315

1416
initial_client = cache.client("us-west-2", "kms")
1517

@@ -20,7 +22,7 @@ def test_client_cache_caches_clients():
2022

2123

2224
def test_client_cache_new_client():
23-
cache = ClientCache()
25+
cache = ClientCache(botocore_session=Session(), client_config=Config())
2426

2527
initial_client = cache.client("us-west-2", "kms")
2628

test/functional/keyrings/aws_kms/test_client_suppliers.py

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Functional tests for ``aws_encryption_sdk.keyrings.aws_kms.client_suppliers``."""
44
import pytest
5+
from botocore.config import Config
6+
from botocore.session import Session
57

68
from aws_encryption_sdk.exceptions import UnknownRegionError
79
from aws_encryption_sdk.keyrings.aws_kms.client_suppliers import (
@@ -34,6 +36,16 @@ def test_default_supplier_uses_cache():
3436
assert test is expected
3537

3638

39+
def test_default_supplier_passes_through_configs():
40+
session = Session()
41+
config = Config()
42+
43+
test = DefaultClientSupplier(botocore_session=session, client_config=config)
44+
45+
assert test._client_cache._botocore_session is session
46+
assert test._client_cache._client_config is config
47+
48+
3749
@pytest.mark.parametrize(
3850
"kwargs",
3951
(

test/integration/keyrings/aws_kms/test_client_cache.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Integration tests for ``aws_encryption_sdk.keyrings.aws_kms.client_cache``."""
44
import pytest
5+
from botocore.config import Config
56
from botocore.exceptions import BotoCoreError
67
from botocore.session import Session
78

@@ -11,7 +12,7 @@
1112

1213

1314
def test_client_cache_removes_bad_client():
14-
cache = ClientCache()
15+
cache = ClientCache(botocore_session=Session(), client_config=Config())
1516
fake_region = "us-fake-1"
1617

1718
initial_client = cache.client(fake_region, "kms")
@@ -25,7 +26,7 @@ def test_client_cache_removes_bad_client():
2526

2627

2728
def test_regional_client_does_not_modify_botocore_session():
28-
cache = ClientCache(botocore_session=Session())
29+
cache = ClientCache(botocore_session=Session(), client_config=Config())
2930
fake_region = "us-fake-1"
3031

3132
assert cache._botocore_session.get_config_variable("region") != fake_region
@@ -34,7 +35,7 @@ def test_regional_client_does_not_modify_botocore_session():
3435

3536

3637
def test_client_cache_remove_bad_client_when_already_removed():
37-
cache = ClientCache()
38+
cache = ClientCache(botocore_session=Session(), client_config=Config())
3839
fake_region = "us-fake-1"
3940

4041
initial_client = cache.client(fake_region, "kms")

0 commit comments

Comments
 (0)