boto3-refresh-session 0.0.7__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.
@@ -0,0 +1,117 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
__all__ = ["AutoRefreshableSession"]
|
4
|
+
|
5
|
+
from typing import Type
|
6
|
+
|
7
|
+
from attrs import define, field
|
8
|
+
from attrs.validators import instance_of, le, optional
|
9
|
+
from boto3 import Session
|
10
|
+
from botocore.credentials import RefreshableCredentials
|
11
|
+
from botocore.session import get_session
|
12
|
+
|
13
|
+
|
14
|
+
@define
|
15
|
+
class AutoRefreshableSession:
|
16
|
+
"""Returns a boto3 Session object which refreshes automatically, no extra
|
17
|
+
steps required.
|
18
|
+
|
19
|
+
This object is useful for long-running processes where temporary credentials
|
20
|
+
may expire between iterations.
|
21
|
+
|
22
|
+
To use this class, you must have `~/.aws/config` or `~/.aws/credentials`
|
23
|
+
on your machine.
|
24
|
+
|
25
|
+
Attributes
|
26
|
+
----------
|
27
|
+
region : str
|
28
|
+
AWS region name.
|
29
|
+
role_arn : str
|
30
|
+
AWS role ARN.
|
31
|
+
session_name : str
|
32
|
+
Name for session.
|
33
|
+
ttl : int, optional
|
34
|
+
Number of seconds until temporary credentials expire, default 900.
|
35
|
+
session_kwargs : dict, optional
|
36
|
+
Optional keyword arguments for `boto3.Session`.
|
37
|
+
client_kwargs : dict, optional
|
38
|
+
Optional keyword arguments for `boto3.Session.client`.
|
39
|
+
|
40
|
+
Other Attributes
|
41
|
+
----------------
|
42
|
+
session
|
43
|
+
Returns a boto3 Session object with credentials which refresh
|
44
|
+
automatically.
|
45
|
+
|
46
|
+
Notes
|
47
|
+
-----
|
48
|
+
boto3 employs a variety of methods (in order) to identify credentials:
|
49
|
+
|
50
|
+
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
|
51
|
+
|
52
|
+
This class assumes that `~/.aws` exists with `/config` or `/credentials`!
|
53
|
+
|
54
|
+
Examples
|
55
|
+
--------
|
56
|
+
Here's how to initialize the `boto3.Client.S3` object:
|
57
|
+
|
58
|
+
>>> from boto3_refresh_session import AutoRefreshableSession
|
59
|
+
>>> session = AutoRefreshableSession(
|
60
|
+
>>> region="us-east-1",
|
61
|
+
>>> role_arn="<your-arn>",
|
62
|
+
>>> session_name="test",
|
63
|
+
>>> )
|
64
|
+
>>> s3_client = session.session.client(service_name="s3")
|
65
|
+
"""
|
66
|
+
|
67
|
+
region: str = field(validator=instance_of(str))
|
68
|
+
role_arn: str = field(validator=instance_of(str))
|
69
|
+
session_name: str = field(validator=instance_of(str))
|
70
|
+
ttl: int = field(
|
71
|
+
default=900, validator=optional([instance_of(int), le(900)])
|
72
|
+
)
|
73
|
+
session_kwargs: dict = field(
|
74
|
+
default={}, validator=optional(instance_of(dict))
|
75
|
+
)
|
76
|
+
client_kwargs: dict = field(
|
77
|
+
default={}, validator=optional(instance_of(dict))
|
78
|
+
)
|
79
|
+
session: Type[Session] = field(init=False)
|
80
|
+
|
81
|
+
def __attrs_post_init__(self):
|
82
|
+
__credentials = RefreshableCredentials.create_from_metadata(
|
83
|
+
metadata=self._get_credentials(),
|
84
|
+
refresh_using=self._get_credentials,
|
85
|
+
method="sts-assume-role",
|
86
|
+
)
|
87
|
+
__session = get_session()
|
88
|
+
# https://github.com/boto/botocore/blob/f8a1dd0820b548a5e8dc05420b28b6f1c6e21154/botocore/session.py#L143
|
89
|
+
__session._credentials = __credentials
|
90
|
+
self.session = Session(botocore_session=__session)
|
91
|
+
|
92
|
+
def _get_credentials(self) -> dict:
|
93
|
+
"""Returns temporary credentials via AWS STS.
|
94
|
+
|
95
|
+
Returns
|
96
|
+
-------
|
97
|
+
dict
|
98
|
+
AWS temporary credentials.
|
99
|
+
"""
|
100
|
+
|
101
|
+
__session = Session(region_name=self.region, **self.session_kwargs)
|
102
|
+
__client = __session.client(
|
103
|
+
service_name="sts", region_name=self.region, **self.client_kwargs
|
104
|
+
)
|
105
|
+
__temporary_credentials = __client.assume_role(
|
106
|
+
RoleArn=self.role_arn,
|
107
|
+
RoleSessionName=self.session_name,
|
108
|
+
DurationSeconds=self.ttl,
|
109
|
+
)["Credentials"]
|
110
|
+
return {
|
111
|
+
"access_key": __temporary_credentials.get("AccessKeyId"),
|
112
|
+
"secret_key": __temporary_credentials.get("SecretAccessKey"),
|
113
|
+
"token": __temporary_credentials.get("SessionToken"),
|
114
|
+
"expiry_time": __temporary_credentials.get(
|
115
|
+
"Expiration"
|
116
|
+
).isoformat(),
|
117
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Mike Letts
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,68 @@
|
|
1
|
+
Metadata-Version: 2.3
|
2
|
+
Name: boto3-refresh-session
|
3
|
+
Version: 0.0.7
|
4
|
+
Summary: A simple Python package for refreshing boto3 sessions automatically.
|
5
|
+
Home-page: https://github.com/michaelthomasletts/boto3-refresh-session
|
6
|
+
License: MIT
|
7
|
+
Keywords: boto3,botocore,aws
|
8
|
+
Author: Mike Letts
|
9
|
+
Author-email: michaelthomasletts@gmail.com
|
10
|
+
Maintainer: Michael Letts
|
11
|
+
Maintainer-email: lettsmt@gmail.com
|
12
|
+
Requires-Python: >=3.10
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
19
|
+
Requires-Dist: attrs (>=24.3.0,<25.0.0)
|
20
|
+
Requires-Dist: boto3
|
21
|
+
Requires-Dist: botocore
|
22
|
+
Project-URL: Repository, https://github.com/michaelthomasletts/boto3-refresh-session
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
|
25
|
+
# boto3-refresh-session
|
26
|
+
[](https://pypi.org/project/boto3-refresh-session/)
|
27
|
+
[](https://github.com/michaelthomasletts/boto3-refresh-session/actions/workflows/push_pullrequest.yml)
|
28
|
+

|
29
|
+
|
30
|
+
## Overview
|
31
|
+
|
32
|
+
A simple Python package for refreshing boto3 sessions automatically.
|
33
|
+
|
34
|
+
## Features
|
35
|
+
- `boto3_refresh_session.AutoRefreshableSession` method for generating an automatically refreshing `boto.Session` object.
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
To install the package using `pip`:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
$ pip install boto3-refresh-session
|
43
|
+
```
|
44
|
+
|
45
|
+
Refer to the [boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) for configuring credentials on your machine.
|
46
|
+
|
47
|
+
## Directory
|
48
|
+
|
49
|
+
```
|
50
|
+
boto3_refresh_session
|
51
|
+
├── __init__.py
|
52
|
+
└── session.py
|
53
|
+
```
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
Here's how to initialize the `boto3.Client.S3` object:
|
58
|
+
|
59
|
+
```python
|
60
|
+
from boto3_refresh_session import AutoRefreshableSession
|
61
|
+
|
62
|
+
session = AutoRefreshableSession(region="us-east-1", role_arn="<your-arn>", session_name="test")
|
63
|
+
s3_client = session.session.client(service_name="s3")
|
64
|
+
```
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Refer to [this document](https://github.com/michaelthomasletts/boto3-refresh-session/blob/main/docs/contributing.md) for instructions.
|
@@ -0,0 +1,6 @@
|
|
1
|
+
boto3_refresh_session/__init__.py,sha256=OMY8el4qROyEvo0vr1Kv8rtFU7g3xnjHuBss54XRTEA,135
|
2
|
+
boto3_refresh_session/session.py,sha256=D-qiFGdy52zzkVnI94Z5hjk3kmb-UkR2fCM77caC3sg,3840
|
3
|
+
boto3_refresh_session-0.0.7.dist-info/LICENSE,sha256=I3ZYTXAjbIly6bm6J-TvFTuuHwTKws4h89QaY5c5HiY,1067
|
4
|
+
boto3_refresh_session-0.0.7.dist-info/METADATA,sha256=tLeH1z-kKStUq9m8GWHdT8OJ0S2vnvGq0kv0niB8WW8,2425
|
5
|
+
boto3_refresh_session-0.0.7.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
|
6
|
+
boto3_refresh_session-0.0.7.dist-info/RECORD,,
|