boto3-refresh-session 2.0.5__py3-none-any.whl → 7.1.3__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.
- boto3_refresh_session/__init__.py +16 -4
- boto3_refresh_session/exceptions.py +115 -3
- boto3_refresh_session/methods/__init__.py +14 -0
- boto3_refresh_session/methods/custom.py +58 -26
- boto3_refresh_session/methods/iot/__init__.py +11 -0
- boto3_refresh_session/methods/iot/{core.typed → core.py} +14 -18
- boto3_refresh_session/methods/iot/x509.py +614 -0
- boto3_refresh_session/methods/sts.py +174 -36
- boto3_refresh_session/session.py +48 -32
- boto3_refresh_session/utils/__init__.py +18 -0
- boto3_refresh_session/utils/cache.py +98 -0
- boto3_refresh_session/utils/config/__init__.py +10 -0
- boto3_refresh_session/utils/config/config.py +274 -0
- boto3_refresh_session/utils/constants.py +41 -0
- boto3_refresh_session/utils/internal.py +441 -0
- boto3_refresh_session/utils/typing.py +138 -0
- {boto3_refresh_session-2.0.5.dist-info → boto3_refresh_session-7.1.3.dist-info}/METADATA +99 -114
- boto3_refresh_session-7.1.3.dist-info/RECORD +21 -0
- {boto3_refresh_session-2.0.5.dist-info → boto3_refresh_session-7.1.3.dist-info}/WHEEL +1 -1
- boto3_refresh_session-7.1.3.dist-info/licenses/LICENSE +373 -0
- boto3_refresh_session-7.1.3.dist-info/licenses/NOTICE +21 -0
- boto3_refresh_session/methods/ecs.py +0 -109
- boto3_refresh_session/methods/iot/__init__.typed +0 -4
- boto3_refresh_session/methods/iot/certificate.typed +0 -54
- boto3_refresh_session/methods/iot/cognito.typed +0 -16
- boto3_refresh_session/utils.py +0 -212
- boto3_refresh_session-2.0.5.dist-info/LICENSE +0 -21
- boto3_refresh_session-2.0.5.dist-info/NOTICE +0 -12
- boto3_refresh_session-2.0.5.dist-info/RECORD +0 -17
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
"""Configurations for AWS STS AssumeRole and STS Client.
|
|
6
|
+
|
|
7
|
+
The following configuration classes do not validate most user inputs except
|
|
8
|
+
'TokenCode' in `AssumeRoleConfig` and `service_name` in `STSClientConfig`.
|
|
9
|
+
It is the user's responsibility to ensure that the provided values conform
|
|
10
|
+
to AWS and boto specifications. The purpose of these configurations is to
|
|
11
|
+
provide a structured way to manage parameters when working with AWS STS.
|
|
12
|
+
|
|
13
|
+
For additional information on AWS specifications, refer to the
|
|
14
|
+
`API Reference for AssumeRole <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html>`_.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
__all__ = ["AssumeRoleConfig", "STSClientConfig"]
|
|
18
|
+
|
|
19
|
+
from abc import ABC, abstractmethod
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
from botocore.config import Config
|
|
23
|
+
|
|
24
|
+
from ...exceptions import BRSValidationError, BRSWarning
|
|
25
|
+
from ..constants import (
|
|
26
|
+
ASSUME_ROLE_CONFIG_PARAMETERS,
|
|
27
|
+
STS_CLIENT_CONFIG_PARAMETERS,
|
|
28
|
+
)
|
|
29
|
+
from ..typing import PolicyDescriptorType, ProvidedContext, Tag
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class BaseConfig(dict, ABC):
|
|
33
|
+
"""Base configuration class."""
|
|
34
|
+
|
|
35
|
+
def __init__(self, **kwargs):
|
|
36
|
+
super().__init__()
|
|
37
|
+
self.update(kwargs)
|
|
38
|
+
|
|
39
|
+
def __setitem__(self, key: str, value: Any) -> None:
|
|
40
|
+
self._validate(key, value)
|
|
41
|
+
if value is None:
|
|
42
|
+
if key in self:
|
|
43
|
+
super().__delitem__(key)
|
|
44
|
+
return
|
|
45
|
+
super().__setitem__(key, value)
|
|
46
|
+
|
|
47
|
+
def __getattr__(self, name: str) -> Any:
|
|
48
|
+
try:
|
|
49
|
+
return self[name]
|
|
50
|
+
except KeyError:
|
|
51
|
+
try:
|
|
52
|
+
self._validate(name, None)
|
|
53
|
+
except BRSValidationError as exc:
|
|
54
|
+
raise AttributeError(
|
|
55
|
+
f"'{name}' is an unknown attribute."
|
|
56
|
+
) from exc
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
def __setattr__(self, name: str, value: Any) -> None:
|
|
60
|
+
self.__setitem__(name, value)
|
|
61
|
+
|
|
62
|
+
def update(self, *args, **kwargs) -> None:
|
|
63
|
+
for key, value in dict(*args, **kwargs).items():
|
|
64
|
+
self.__setitem__(key, value)
|
|
65
|
+
|
|
66
|
+
def setdefault(self, key: str, default: Any = None):
|
|
67
|
+
if key in self:
|
|
68
|
+
return super().setdefault(key, default)
|
|
69
|
+
self._validate(key, default)
|
|
70
|
+
return super().setdefault(key, default)
|
|
71
|
+
|
|
72
|
+
@abstractmethod
|
|
73
|
+
def _validate(self, key: str, value: Any) -> None: ...
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class AssumeRoleConfig(BaseConfig):
|
|
77
|
+
"""Configuration for AWS STS AssumeRole API.
|
|
78
|
+
|
|
79
|
+
Attributes
|
|
80
|
+
----------
|
|
81
|
+
RoleArn : str
|
|
82
|
+
The Amazon Resource Name (ARN) of the role to assume.
|
|
83
|
+
RoleSessionName : str, optional
|
|
84
|
+
An identifier for the assumed role session.
|
|
85
|
+
PolicyArns : list of PolicyDescriptorType, optional
|
|
86
|
+
The Amazon Resource Names (ARNs) of the IAM managed policies to
|
|
87
|
+
use as managed session policies.
|
|
88
|
+
Policy : str, optional
|
|
89
|
+
An IAM policy in JSON format to use as an inline session policy.
|
|
90
|
+
DurationSeconds : int, optional
|
|
91
|
+
The duration, in seconds, of the role session.
|
|
92
|
+
ExternalId : str, optional
|
|
93
|
+
A unique identifier that might be required when you assume a role
|
|
94
|
+
in another account.
|
|
95
|
+
SerialNumber : str, optional
|
|
96
|
+
The identification number of the MFA device.
|
|
97
|
+
TokenCode : str, optional
|
|
98
|
+
The value provided by the MFA device. Must be a 6-digit numeric
|
|
99
|
+
string.
|
|
100
|
+
Tags : list of Tag, optional
|
|
101
|
+
A list of session tags.
|
|
102
|
+
TransitiveTagKeys : list of str, optional
|
|
103
|
+
A list of keys for session tags that you want to pass to the role
|
|
104
|
+
session.
|
|
105
|
+
SourceIdentity : str, optional
|
|
106
|
+
A unique identifier that is passed in the AssumeRole call.
|
|
107
|
+
ProvidedContexts : list of ProvidedContext, optional
|
|
108
|
+
A list of context keys and values for the session.
|
|
109
|
+
|
|
110
|
+
Notes
|
|
111
|
+
-----
|
|
112
|
+
Values can be accessed via dot-notation (e.g., ``config.RoleArn``)
|
|
113
|
+
or dictionary-style access (e.g., ``config['RoleArn']``).
|
|
114
|
+
|
|
115
|
+
Accessing a valid but unset attribute (e.g., ``SerialNumber``) via
|
|
116
|
+
dot-notation returns ``None`` instead of raising an error. While this
|
|
117
|
+
behavior is convenient, it may surprise users accustomed to seeing
|
|
118
|
+
``AttributeError`` exceptions in similar contexts.
|
|
119
|
+
|
|
120
|
+
For additional information on AWS specifications, refer to the
|
|
121
|
+
`API Reference for AssumeRole <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html>`_.
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
def __init__(
|
|
125
|
+
self,
|
|
126
|
+
*, # enforce keyword-only arguments
|
|
127
|
+
RoleArn: str,
|
|
128
|
+
RoleSessionName: str | None = None,
|
|
129
|
+
PolicyArns: list[PolicyDescriptorType] | None = None,
|
|
130
|
+
Policy: str | None = None,
|
|
131
|
+
DurationSeconds: int | None = None,
|
|
132
|
+
ExternalId: str | None = None,
|
|
133
|
+
SerialNumber: str | None = None,
|
|
134
|
+
TokenCode: str | None = None,
|
|
135
|
+
Tags: list[Tag] | None = None,
|
|
136
|
+
TransitiveTagKeys: list[str] | None = None,
|
|
137
|
+
SourceIdentity: str | None = None,
|
|
138
|
+
ProvidedContexts: list[ProvidedContext] | None = None,
|
|
139
|
+
):
|
|
140
|
+
super().__init__(
|
|
141
|
+
RoleArn=RoleArn,
|
|
142
|
+
RoleSessionName=RoleSessionName,
|
|
143
|
+
PolicyArns=PolicyArns,
|
|
144
|
+
Policy=Policy,
|
|
145
|
+
DurationSeconds=DurationSeconds,
|
|
146
|
+
ExternalId=ExternalId,
|
|
147
|
+
SerialNumber=SerialNumber,
|
|
148
|
+
TokenCode=TokenCode,
|
|
149
|
+
Tags=Tags,
|
|
150
|
+
TransitiveTagKeys=TransitiveTagKeys,
|
|
151
|
+
SourceIdentity=SourceIdentity,
|
|
152
|
+
ProvidedContexts=ProvidedContexts,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
def _validate(self, key: str, value: Any) -> None:
|
|
156
|
+
if not isinstance(key, str):
|
|
157
|
+
raise BRSValidationError("Attribute name must be a string.")
|
|
158
|
+
|
|
159
|
+
if key not in ASSUME_ROLE_CONFIG_PARAMETERS:
|
|
160
|
+
raise BRSValidationError(
|
|
161
|
+
f"'{key}' is not a valid attribute for AssumeRoleConfig."
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
if (
|
|
165
|
+
key == "TokenCode"
|
|
166
|
+
and isinstance(value, str)
|
|
167
|
+
and (len(value) != 6 or not value.isdigit())
|
|
168
|
+
):
|
|
169
|
+
raise BRSValidationError(
|
|
170
|
+
f"'{key}' must be a 6-digit numeric string."
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class STSClientConfig(BaseConfig):
|
|
175
|
+
"""Configuration for boto3 STS Client.
|
|
176
|
+
|
|
177
|
+
Attributes
|
|
178
|
+
----------
|
|
179
|
+
service_name : str, optional
|
|
180
|
+
The name of the AWS service. Defaults to 'sts'.
|
|
181
|
+
region_name : str, optional
|
|
182
|
+
The AWS region name.
|
|
183
|
+
api_version : str, optional
|
|
184
|
+
The API version to use.
|
|
185
|
+
use_ssl : bool, optional
|
|
186
|
+
Whether to use SSL.
|
|
187
|
+
verify : bool or str, optional
|
|
188
|
+
Whether to verify SSL certificates or a path to a CA bundle.
|
|
189
|
+
endpoint_url : str, optional
|
|
190
|
+
The complete URL to use for the constructed client.
|
|
191
|
+
aws_access_key_id : str, optional
|
|
192
|
+
The AWS access key ID.
|
|
193
|
+
aws_secret_access_key : str, optional
|
|
194
|
+
The AWS secret access key.
|
|
195
|
+
aws_session_token : str, optional
|
|
196
|
+
The AWS session token.
|
|
197
|
+
config : botocore.config.Config, optional
|
|
198
|
+
Advanced client configuration options.
|
|
199
|
+
aws_account_id : str, optional
|
|
200
|
+
The AWS account ID associated with the credentials.
|
|
201
|
+
|
|
202
|
+
Notes
|
|
203
|
+
-----
|
|
204
|
+
Values can be accessed via dot-notation (e.g., ``config.RoleArn``)
|
|
205
|
+
or dictionary-style access (e.g., ``config['RoleArn']``).
|
|
206
|
+
|
|
207
|
+
Accessing a valid but unset attribute (e.g., ``SerialNumber``) via
|
|
208
|
+
dot-notation returns ``None`` instead of raising an error. While this
|
|
209
|
+
behavior is convenient, it may surprise users accustomed to seeing
|
|
210
|
+
``AttributeError`` exceptions in similar contexts.
|
|
211
|
+
|
|
212
|
+
``service_name`` is enforced to be 'sts'. If a different value is
|
|
213
|
+
provided, it will be overridden to 'sts' with a warning.
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
def __init__(
|
|
217
|
+
self,
|
|
218
|
+
*, # enforce keyword-only arguments
|
|
219
|
+
service_name: str | None = None,
|
|
220
|
+
region_name: str | None = None,
|
|
221
|
+
api_version: str | None = None,
|
|
222
|
+
use_ssl: bool | None = None,
|
|
223
|
+
verify: bool | str | None = None,
|
|
224
|
+
endpoint_url: str | None = None,
|
|
225
|
+
aws_access_key_id: str | None = None,
|
|
226
|
+
aws_secret_access_key: str | None = None,
|
|
227
|
+
aws_session_token: str | None = None,
|
|
228
|
+
config: Config | None = None,
|
|
229
|
+
aws_account_id: str | None = None,
|
|
230
|
+
):
|
|
231
|
+
super().__init__(
|
|
232
|
+
service_name=service_name,
|
|
233
|
+
region_name=region_name,
|
|
234
|
+
api_version=api_version,
|
|
235
|
+
use_ssl=use_ssl,
|
|
236
|
+
verify=verify,
|
|
237
|
+
endpoint_url=endpoint_url,
|
|
238
|
+
aws_access_key_id=aws_access_key_id,
|
|
239
|
+
aws_secret_access_key=aws_secret_access_key,
|
|
240
|
+
aws_session_token=aws_session_token,
|
|
241
|
+
config=config,
|
|
242
|
+
aws_account_id=aws_account_id,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
def __setitem__(self, key: str, value: Any) -> None:
|
|
246
|
+
"""Override to enforce 'sts' as service_name."""
|
|
247
|
+
|
|
248
|
+
if key == "service_name":
|
|
249
|
+
match value:
|
|
250
|
+
case None:
|
|
251
|
+
value = "sts"
|
|
252
|
+
case str() if value != "sts":
|
|
253
|
+
BRSWarning.warn(
|
|
254
|
+
"The 'service_name' for STSClientConfig should be "
|
|
255
|
+
"'sts'. Overriding to 'sts'."
|
|
256
|
+
)
|
|
257
|
+
value = "sts"
|
|
258
|
+
case str():
|
|
259
|
+
...
|
|
260
|
+
case _:
|
|
261
|
+
raise BRSValidationError(
|
|
262
|
+
"'service_name' must be a string."
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
super().__setitem__(key, value)
|
|
266
|
+
|
|
267
|
+
def _validate(self, key: str, value: Any) -> None:
|
|
268
|
+
if not isinstance(key, str):
|
|
269
|
+
raise BRSValidationError("Attribute name must be a string.")
|
|
270
|
+
|
|
271
|
+
if key not in STS_CLIENT_CONFIG_PARAMETERS:
|
|
272
|
+
raise BRSValidationError(
|
|
273
|
+
f"'{key}' is not a valid attribute for STSClientConfig."
|
|
274
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
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__ = ["ASSUME_ROLE_CONFIG_PARAMETERS", "STS_CLIENT_CONFIG_PARAMETERS"]
|
|
6
|
+
|
|
7
|
+
from re import compile
|
|
8
|
+
|
|
9
|
+
# THESE CONSTANTS WILL BE DEPRECATED IN A FUTURE RELEASE!!
|
|
10
|
+
ROLE_ARN_PATTERN = compile(r"^arn:aws[a-z-]*:iam::\d{12}:role/[\w+=,.@-]+$")
|
|
11
|
+
MFA_SERIAL_PATTERN = compile(r"^arn:aws[a-z-]*:iam::\d{12}:mfa/[\w+=,.@-]+$")
|
|
12
|
+
ROLE_SESSION_NAME_PATTERN = compile(r"^[a-zA-Z0-9+=,.@-]{2,64}$")
|
|
13
|
+
|
|
14
|
+
# config parameter names
|
|
15
|
+
ASSUME_ROLE_CONFIG_PARAMETERS = (
|
|
16
|
+
"RoleArn",
|
|
17
|
+
"RoleSessionName",
|
|
18
|
+
"PolicyArns",
|
|
19
|
+
"Policy",
|
|
20
|
+
"DurationSeconds",
|
|
21
|
+
"ExternalId",
|
|
22
|
+
"SerialNumber",
|
|
23
|
+
"TokenCode",
|
|
24
|
+
"Tags",
|
|
25
|
+
"TransitiveTagKeys",
|
|
26
|
+
"SourceIdentity",
|
|
27
|
+
"ProvidedContexts",
|
|
28
|
+
)
|
|
29
|
+
STS_CLIENT_CONFIG_PARAMETERS = (
|
|
30
|
+
"service_name",
|
|
31
|
+
"region_name",
|
|
32
|
+
"api_version",
|
|
33
|
+
"use_ssl",
|
|
34
|
+
"verify",
|
|
35
|
+
"endpoint_url",
|
|
36
|
+
"aws_access_key_id",
|
|
37
|
+
"aws_secret_access_key",
|
|
38
|
+
"aws_session_token",
|
|
39
|
+
"config",
|
|
40
|
+
"aws_account_id",
|
|
41
|
+
)
|