boto3-refresh-session 2.0.5__py3-none-any.whl → 7.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of boto3-refresh-session might be problematic. Click here for more details.

@@ -1,14 +1,28 @@
1
- from __future__ import annotations
1
+ # This Source Code Form is subject to the terms of the Mozilla Public
2
+ # License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ # file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+
5
+ """STS assume-role refreshable session implementation."""
2
6
 
3
7
  __all__ = ["STSRefreshableSession"]
4
8
 
5
- from typing import Any
9
+ from typing import Callable
6
10
 
7
- from ..exceptions import BRSWarning
8
- from ..session import BaseRefreshableSession
9
- from ..utils import AssumeRoleParams, STSClientParams, TemporaryCredentials
11
+ from ..exceptions import BRSConfigurationError, BRSValidationError, BRSWarning
12
+ from ..utils import (
13
+ MFA_SERIAL_PATTERN,
14
+ ROLE_ARN_PATTERN,
15
+ ROLE_SESSION_NAME_PATTERN,
16
+ AssumeRoleParams,
17
+ BaseRefreshableSession,
18
+ Identity,
19
+ STSClientParams,
20
+ TemporaryCredentials,
21
+ refreshable_session,
22
+ )
10
23
 
11
24
 
25
+ @refreshable_session
12
26
  class STSRefreshableSession(BaseRefreshableSession, registry_key="sts"):
13
27
  """A :class:`boto3.session.Session` object that automatically refreshes
14
28
  temporary AWS credentials using an IAM role that is assumed via STS.
@@ -17,16 +31,55 @@ class STSRefreshableSession(BaseRefreshableSession, registry_key="sts"):
17
31
  ----------
18
32
  assume_role_kwargs : AssumeRoleParams
19
33
  Required keyword arguments for :meth:`STS.Client.assume_role` (i.e.
20
- boto3 STS client).
34
+ boto3 STS client). ``RoleArn`` is required. ``RoleSessionName`` will
35
+ default to 'boto3-refresh-session' if not provided.
36
+
37
+ For MFA authentication, two modalities are supported:
38
+
39
+ 1. **Dynamic tokens (recommended)**: Provide ``SerialNumber`` in
40
+ ``assume_role_kwargs`` and pass ``mfa_token_provider`` callable.
41
+ The provider callable will be invoked on each refresh to obtain
42
+ fresh MFA tokens. Do not include ``TokenCode`` in this case.
43
+
44
+ 2. **Static/injectable tokens**: Provide both ``SerialNumber`` and
45
+ ``TokenCode`` in ``assume_role_kwargs``. You are responsible for
46
+ updating ``assume_role_kwargs["TokenCode"]`` before the token
47
+ expires.
48
+ sts_client_kwargs : STSClientParams, optional
49
+ Optional keyword arguments for the :class:`STS.Client` object. Do not
50
+ provide values for ``service_name`` as they are unnecessary. Default
51
+ is None.
52
+ mfa_token_provider : Callable[[], str], optional
53
+ An optional callable that returns a string representing a fresh MFA
54
+ token code. If provided, this will be called during each credential
55
+ refresh to obtain a new token, which overrides any ``TokenCode`` in
56
+ ``assume_role_kwargs``. When using this parameter, ``SerialNumber``
57
+ must be provided in ``assume_role_kwargs``. Default is None.
58
+ mfa_token_provider_kwargs : dict, optional
59
+ Optional keyword arguments to pass to the ``mfa_token_provider``
60
+ callable. Default is None.
21
61
  defer_refresh : bool, optional
22
62
  If ``True`` then temporary credentials are not automatically refreshed
23
63
  until they are explicitly needed. If ``False`` then temporary
24
64
  credentials refresh immediately upon expiration. It is highly
25
65
  recommended that you use ``True``. Default is ``True``.
26
- sts_client_kwargs : STSClientParams, optional
27
- Optional keyword arguments for the :class:`STS.Client` object. Do not
28
- provide values for ``service_name`` as they are unnecessary. Default
29
- is None.
66
+ advisory_timeout : int, optional
67
+ USE THIS ARGUMENT WITH CAUTION!!!
68
+
69
+ Botocore will attempt to refresh credentials early according to
70
+ this value (in seconds), but will continue using the existing
71
+ credentials if refresh fails. Default is 15 minutes (900 seconds).
72
+ mandatory_timeout : int, optional
73
+ USE THIS ARGUMENT WITH CAUTION!!!
74
+
75
+ Botocore requires a successful refresh before continuing. If
76
+ refresh fails in this window (in seconds), API calls may fail.
77
+ Default is 10 minutes (600 seconds).
78
+ cache_clients : bool, optional
79
+ If ``True`` then clients created by this session will be cached and
80
+ reused for subsequent calls to :meth:`client()` with the same
81
+ parameter signatures. Due to the memory overhead of clients, the
82
+ default is ``True`` in order to protect system resources.
30
83
 
31
84
  Other Parameters
32
85
  ----------------
@@ -38,17 +91,136 @@ class STSRefreshableSession(BaseRefreshableSession, registry_key="sts"):
38
91
  def __init__(
39
92
  self,
40
93
  assume_role_kwargs: AssumeRoleParams,
41
- defer_refresh: bool | None = None,
42
94
  sts_client_kwargs: STSClientParams | None = None,
95
+ mfa_token_provider: Callable[[], str] | None = None,
96
+ mfa_token_provider_kwargs: dict | None = None,
43
97
  **kwargs,
44
98
  ):
45
- super().__init__(**kwargs)
99
+ # ensuring 'refresh_method' is not set manually
100
+ if "refresh_method" in kwargs:
101
+ BRSWarning.warn(
102
+ "'refresh_method' cannot be set manually. "
103
+ "Reverting to 'sts-assume-role'."
104
+ )
105
+ del kwargs["refresh_method"]
106
+
107
+ # verifying 'RoleArn' is provided in 'assume_role_kwargs'
108
+ if "RoleArn" not in assume_role_kwargs:
109
+ raise BRSConfigurationError(
110
+ "'RoleArn' must be provided in 'assume_role_kwargs'!",
111
+ param="RoleArn",
112
+ )
113
+
114
+ # verifying 'RoleArn' format
115
+ if not ROLE_ARN_PATTERN.match(assume_role_kwargs["RoleArn"]):
116
+ raise BRSValidationError(
117
+ "'RoleArn' in 'assume_role_kwargs' is not a valid AWS "
118
+ "Role ARN!",
119
+ param="RoleArn",
120
+ value=assume_role_kwargs.get("RoleArn"),
121
+ )
122
+
123
+ # setting default 'RoleSessionName' if not provided
124
+ if "RoleSessionName" not in assume_role_kwargs:
125
+ BRSWarning.warn(
126
+ "'RoleSessionName' not provided in "
127
+ "'assume_role_kwargs'! Defaulting to "
128
+ "'boto3-refresh-session'."
129
+ )
130
+ assume_role_kwargs["RoleSessionName"] = "boto3-refresh-session"
131
+
132
+ # verifying 'RoleSessionName' format
133
+ if not ROLE_SESSION_NAME_PATTERN.match(
134
+ assume_role_kwargs["RoleSessionName"]
135
+ ):
136
+ raise BRSValidationError(
137
+ "'RoleSessionName' in 'assume_role_kwargs' is not valid! "
138
+ "It must be 2-64 characters long and can contain only "
139
+ "alphanumeric characters and the following symbols: "
140
+ "'+=,.@-'.",
141
+ param="RoleSessionName",
142
+ value=assume_role_kwargs.get("RoleSessionName"),
143
+ )
144
+
145
+ # store MFA token provider
146
+ try:
147
+ # verifying type of mfa_token_provider
148
+ assert (
149
+ isinstance(mfa_token_provider, Callable)
150
+ or mfa_token_provider is None
151
+ )
152
+ self.mfa_token_provider = mfa_token_provider
153
+ except AssertionError as err:
154
+ raise BRSValidationError(
155
+ "'mfa_token_provider' must be a callable that returns a "
156
+ "string representing an MFA token code!",
157
+ param="mfa_token_provider",
158
+ ) from err
159
+
160
+ # storing mfa_token_provider_kwargs
161
+ self.mfa_token_provider_kwargs = (
162
+ mfa_token_provider_kwargs if mfa_token_provider_kwargs else {}
163
+ )
164
+
165
+ # verifying 'SerialNumber' format if provided
166
+ if "SerialNumber" in assume_role_kwargs:
167
+ if not MFA_SERIAL_PATTERN.match(
168
+ assume_role_kwargs["SerialNumber"]
169
+ ):
170
+ raise BRSValidationError(
171
+ "'SerialNumber' in 'assume_role_kwargs' is not a valid "
172
+ "AWS MFA device ARN!",
173
+ param="SerialNumber",
174
+ value=assume_role_kwargs.get("SerialNumber"),
175
+ )
176
+
177
+ # ensure SerialNumber is set appropriately with mfa_token_provider
178
+ if (
179
+ self.mfa_token_provider
180
+ and "SerialNumber" not in assume_role_kwargs
181
+ ):
182
+ raise BRSConfigurationError(
183
+ "'SerialNumber' must be provided in 'assume_role_kwargs' "
184
+ "when using 'mfa_token_provider'!",
185
+ param="SerialNumber",
186
+ )
187
+
188
+ # ensure SerialNumber and TokenCode are set without mfa_token_provider
189
+ if (
190
+ self.mfa_token_provider is None
191
+ and (
192
+ "SerialNumber" in assume_role_kwargs
193
+ and "TokenCode" not in assume_role_kwargs
194
+ )
195
+ or (
196
+ "SerialNumber" not in assume_role_kwargs
197
+ and "TokenCode" in assume_role_kwargs
198
+ )
199
+ ):
200
+ raise BRSConfigurationError(
201
+ "'SerialNumber' and 'TokenCode' must be provided in "
202
+ "'assume_role_kwargs' when 'mfa_token_provider' is not set!",
203
+ param="SerialNumber/TokenCode",
204
+ )
205
+
206
+ # warn if TokenCode provided with mfa_token_provider
207
+ if self.mfa_token_provider and "TokenCode" in assume_role_kwargs:
208
+ BRSWarning.warn(
209
+ "'TokenCode' provided in 'assume_role_kwargs' will be "
210
+ "ignored and overridden by 'mfa_token_provider' on each "
211
+ "refresh."
212
+ )
213
+
214
+ # initializing assume role kwargs attribute
46
215
  self.assume_role_kwargs = assume_role_kwargs
47
216
 
217
+ # initializing BRSSession
218
+ super().__init__(refresh_method="sts-assume-role", **kwargs)
219
+
48
220
  if sts_client_kwargs is not None:
49
221
  # overwriting 'service_name' if if appears in sts_client_kwargs
50
222
  if "service_name" in sts_client_kwargs:
51
- BRSWarning(
223
+ BRSWarning.warn(
52
224
  "'sts_client_kwargs' cannot contain values for "
53
225
  "'service_name'. Reverting to service_name = 'sts'."
54
226
  )
@@ -59,17 +231,32 @@ class STSRefreshableSession(BaseRefreshableSession, registry_key="sts"):
59
231
  else:
60
232
  self._sts_client = self.client(service_name="sts")
61
233
 
62
- # mounting refreshable credentials
63
- self.initialize(
64
- credentials_method=self._get_credentials,
65
- defer_refresh=defer_refresh is not False,
66
- refresh_method="sts-assume-role",
67
- )
68
-
69
234
  def _get_credentials(self) -> TemporaryCredentials:
70
- temporary_credentials = self._sts_client.assume_role(
71
- **self.assume_role_kwargs
72
- )["Credentials"]
235
+ params = dict(self.assume_role_kwargs)
236
+
237
+ # override TokenCode with fresh token from provider if configured
238
+ if self.mfa_token_provider:
239
+ params["TokenCode"] = self.mfa_token_provider(
240
+ **self.mfa_token_provider_kwargs
241
+ )
242
+
243
+ # validating TokenCode format
244
+ if (token_code := params.get("TokenCode")) is not None:
245
+ if (
246
+ not isinstance(token_code, str)
247
+ or len(token_code) != 6
248
+ or not token_code.isdigit()
249
+ ):
250
+ raise BRSValidationError(
251
+ "'TokenCode' must be a 6-digit string per AWS MFA "
252
+ "token specifications!",
253
+ param="TokenCode",
254
+ )
255
+
256
+ temporary_credentials = self._sts_client.assume_role(**params)[
257
+ "Credentials"
258
+ ]
259
+
73
260
  return {
74
261
  "access_key": temporary_credentials.get("AccessKeyId"),
75
262
  "secret_key": temporary_credentials.get("SecretAccessKey"),
@@ -77,12 +264,12 @@ class STSRefreshableSession(BaseRefreshableSession, registry_key="sts"):
77
264
  "expiry_time": temporary_credentials.get("Expiration").isoformat(),
78
265
  }
79
266
 
80
- def get_identity(self) -> dict[str, Any]:
267
+ def get_identity(self) -> Identity:
81
268
  """Returns metadata about the identity assumed.
82
269
 
83
270
  Returns
84
271
  -------
85
- dict[str, Any]
272
+ Identity
86
273
  Dict containing caller identity according to AWS STS.
87
274
  """
88
275
 
@@ -1,37 +1,17 @@
1
+ # This Source Code Form is subject to the terms of the Mozilla Public
2
+ # License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ # file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+
5
+ """Public factory for constructing refreshable boto3 sessions."""
6
+
1
7
  from __future__ import annotations
2
8
 
3
9
  __all__ = ["RefreshableSession"]
4
10
 
5
11
  from typing import get_args
6
12
 
7
- from .exceptions import BRSError
8
- from .utils import BRSSession, CredentialProvider, Method, Registry
9
-
10
-
11
- class BaseRefreshableSession(
12
- Registry[Method],
13
- CredentialProvider,
14
- BRSSession,
15
- registry_key="__sentinel__",
16
- ):
17
- """Abstract base class for implementing refreshable AWS sessions.
18
-
19
- Provides a common interface and factory registration mechanism
20
- for subclasses that generate temporary credentials using various
21
- AWS authentication methods (e.g., STS).
22
-
23
- Subclasses must implement ``_get_credentials()`` and ``get_identity()``.
24
- They should also register themselves using the ``method=...`` argument
25
- to ``__init_subclass__``.
26
-
27
- Parameters
28
- ----------
29
- registry : dict[str, type[BaseRefreshableSession]]
30
- Class-level registry mapping method names to registered session types.
31
- """
32
-
33
- def __init__(self, **kwargs):
34
- super().__init__(**kwargs)
13
+ from .exceptions import BRSValidationError
14
+ from .utils import BaseRefreshableSession, Method
35
15
 
36
16
 
37
17
  class RefreshableSession:
@@ -52,6 +32,28 @@ class RefreshableSession:
52
32
  method : Method
53
33
  The authentication and refresh method to use for the session. Must
54
34
  match a registered method name. Default is "sts".
35
+ defer_refresh : bool, optional
36
+ If ``True`` then temporary credentials are not automatically refreshed
37
+ until they are explicitly needed. If ``False`` then temporary
38
+ credentials refresh immediately upon expiration. It is highly
39
+ recommended that you use ``True``. Default is ``True``.
40
+ advisory_timeout : int, optional
41
+ USE THIS ARGUMENT WITH CAUTION!!!
42
+
43
+ Botocore will attempt to refresh credentials early according to
44
+ this value (in seconds), but will continue using the existing
45
+ credentials if refresh fails. Default is 15 minutes (900 seconds).
46
+ mandatory_timeout : int, optional
47
+ USE THIS ARGUMENT WITH CAUTION!!!
48
+
49
+ Botocore requires a successful refresh before continuing. If
50
+ refresh fails in this window (in seconds), API calls may fail.
51
+ Default is 10 minutes (600 seconds).
52
+ cache_clients : bool, optional
53
+ If ``True`` then clients created by this session will be cached and
54
+ reused for subsequent calls to :meth:`client()` with the same
55
+ parameter signatures. Due to the memory overhead of clients, the
56
+ default is ``True`` in order to protect system resources.
55
57
 
56
58
  Other Parameters
57
59
  ----------------
@@ -62,18 +64,20 @@ class RefreshableSession:
62
64
  See Also
63
65
  --------
64
66
  boto3_refresh_session.methods.custom.CustomRefreshableSession
67
+ boto3_refresh_session.methods.iot.x509.IOTX509RefreshableSession
65
68
  boto3_refresh_session.methods.sts.STSRefreshableSession
66
- boto3_refresh_session.methods.ecs.ECSRefreshableSession
67
69
  """
68
70
 
69
71
  def __new__(
70
72
  cls, method: Method = "sts", **kwargs
71
73
  ) -> BaseRefreshableSession:
72
74
  if method not in (methods := cls.get_available_methods()):
73
- raise BRSError(
75
+ raise BRSValidationError(
74
76
  f"{method!r} is an invalid method parameter. "
75
77
  "Available methods are "
76
- f"{', '.join(repr(meth) for meth in methods)}."
78
+ f"{', '.join(repr(meth) for meth in methods)}.",
79
+ param="method",
80
+ value=method,
77
81
  )
78
82
 
79
83
  return BaseRefreshableSession.registry[method](**kwargs)
@@ -86,7 +90,7 @@ class RefreshableSession:
86
90
  -------
87
91
  list[str]
88
92
  A list of all currently available credential refresh methods,
89
- e.g. 'sts', 'ecs', 'custom'.
93
+ e.g. 'sts', 'custom'.
90
94
  """
91
95
 
92
96
  args = list(get_args(Method))
@@ -0,0 +1,16 @@
1
+ # This Source Code Form is subject to the terms of the Mozilla Public
2
+ # License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ # file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+
5
+ __all__ = []
6
+
7
+ from . import cache, constants, internal, typing
8
+ from .cache import *
9
+ from .constants import *
10
+ from .internal import *
11
+ from .typing import *
12
+
13
+ __all__.extend(cache.__all__)
14
+ __all__.extend(constants.__all__)
15
+ __all__.extend(internal.__all__)
16
+ __all__.extend(typing.__all__)
@@ -0,0 +1,98 @@
1
+ # This Source Code Form is subject to the terms of the Mozilla Public
2
+ # License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ # file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+
5
+ """Cache primitives for memoizing boto3 client instances.
6
+
7
+ `ClientCache` provides a thread-safe mapping for cached clients and raises
8
+ `BRSCacheError` when lookups or mutations violate the expected cache contract.
9
+ """
10
+
11
+ __all__ = ["ClientCache"]
12
+
13
+ from threading import Lock
14
+ from typing import Hashable, Optional
15
+
16
+ from botocore.client import BaseClient
17
+
18
+ from ..exceptions import BRSCacheExistsError, BRSCacheNotFoundError
19
+
20
+
21
+ class ClientCache:
22
+ """A thread-safe cache for storing boto3 clients which can be used like a
23
+ dictionary."""
24
+
25
+ def __init__(self):
26
+ self._cache: dict[Hashable, BaseClient] = {}
27
+ self._lock = Lock()
28
+
29
+ def __len__(self) -> int:
30
+ with self._lock:
31
+ return len(self._cache)
32
+
33
+ def __contains__(self, hash: Hashable) -> bool:
34
+ with self._lock:
35
+ return hash in self._cache
36
+
37
+ def __iter__(self):
38
+ with self._lock:
39
+ return iter(self._cache.keys())
40
+
41
+ def __getitem__(self, hash: Hashable) -> BaseClient:
42
+ with self._lock:
43
+ try:
44
+ return self._cache[hash]
45
+ except KeyError as err:
46
+ raise BRSCacheNotFoundError(
47
+ "The client you requested has not been cached."
48
+ ) from err
49
+
50
+ def __setitem__(self, hash: Hashable, client: BaseClient) -> None:
51
+ with self._lock:
52
+ if hash in self._cache:
53
+ raise BRSCacheExistsError("Client already exists in cache.")
54
+
55
+ self._cache[hash] = client
56
+
57
+ def __delitem__(self, hash: Hashable) -> None:
58
+ with self._lock:
59
+ if hash not in self._cache:
60
+ raise BRSCacheNotFoundError("Client not found in cache.")
61
+ del self._cache[hash]
62
+
63
+ def keys(self) -> tuple[Hashable, ...]:
64
+ """Returns the keys in the cache."""
65
+
66
+ with self._lock:
67
+ return tuple(self._cache.keys())
68
+
69
+ def values(self) -> tuple[BaseClient, ...]:
70
+ """Returns the clients from the cache."""
71
+
72
+ with self._lock:
73
+ return tuple(self._cache.values())
74
+
75
+ def items(self) -> tuple[tuple[Hashable, BaseClient], ...]:
76
+ """Returns the items in the cache as (hash, BaseClient) tuples."""
77
+
78
+ with self._lock:
79
+ return tuple(self._cache.items())
80
+
81
+ def get(
82
+ self, hash: Hashable, default: Optional[BaseClient] = None
83
+ ) -> Optional[BaseClient]:
84
+ """Gets the client using the given signature, or returns None if no
85
+ default is provided.
86
+ """
87
+
88
+ with self._lock:
89
+ return self._cache.get(hash, default)
90
+
91
+ def pop(self, hash: Hashable) -> BaseClient:
92
+ """Pops and returns the client using the given signature."""
93
+
94
+ with self._lock:
95
+ if (client := self._cache.get(hash)) is None:
96
+ raise BRSCacheNotFoundError("Client not found in cache.")
97
+ del self._cache[hash]
98
+ return client
@@ -0,0 +1,16 @@
1
+ # This Source Code Form is subject to the terms of the Mozilla Public
2
+ # License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ # file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
+
5
+ __all__ = [
6
+ "ROLE_ARN_PATTERN",
7
+ "MFA_SERIAL_PATTERN",
8
+ "ROLE_SESSION_NAME_PATTERN",
9
+ ]
10
+
11
+ from re import compile
12
+
13
+ # AWS ARN validation patterns
14
+ ROLE_ARN_PATTERN = compile(r"^arn:aws[a-z-]*:iam::\d{12}:role/[\w+=,.@-]+$")
15
+ MFA_SERIAL_PATTERN = compile(r"^arn:aws[a-z-]*:iam::\d{12}:mfa/[\w+=,.@-]+$")
16
+ ROLE_SESSION_NAME_PATTERN = compile(r"^[a-zA-Z0-9+=,.@-]{2,64}$")