boto3-refresh-session 0.0.7__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
+ [![PyPI Download](https://img.shields.io/pypi/v/boto3-refresh-session?logo=pypis.svg)](https://pypi.org/project/boto3-refresh-session/)
27
+ [![Workflow](https://img.shields.io/github/actions/workflow/status/michaelthomasletts/boto3-refresh-session/push_pullrequest.yml?logo=github)](https://github.com/michaelthomasletts/boto3-refresh-session/actions/workflows/push_pullrequest.yml)
28
+ ![Python Version](https://img.shields.io/pypi/pyversions/boto3-refresh-session?style=pypi)
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,44 @@
1
+ # boto3-refresh-session
2
+ [![PyPI Download](https://img.shields.io/pypi/v/boto3-refresh-session?logo=pypis.svg)](https://pypi.org/project/boto3-refresh-session/)
3
+ [![Workflow](https://img.shields.io/github/actions/workflow/status/michaelthomasletts/boto3-refresh-session/push_pullrequest.yml?logo=github)](https://github.com/michaelthomasletts/boto3-refresh-session/actions/workflows/push_pullrequest.yml)
4
+ ![Python Version](https://img.shields.io/pypi/pyversions/boto3-refresh-session?style=pypi)
5
+
6
+ ## Overview
7
+
8
+ A simple Python package for refreshing boto3 sessions automatically.
9
+
10
+ ## Features
11
+ - `boto3_refresh_session.AutoRefreshableSession` method for generating an automatically refreshing `boto.Session` object.
12
+
13
+ ## Installation
14
+
15
+ To install the package using `pip`:
16
+
17
+ ```bash
18
+ $ pip install boto3-refresh-session
19
+ ```
20
+
21
+ Refer to the [boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) for configuring credentials on your machine.
22
+
23
+ ## Directory
24
+
25
+ ```
26
+ boto3_refresh_session
27
+ ├── __init__.py
28
+ └── session.py
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Here's how to initialize the `boto3.Client.S3` object:
34
+
35
+ ```python
36
+ from boto3_refresh_session import AutoRefreshableSession
37
+
38
+ session = AutoRefreshableSession(region="us-east-1", role_arn="<your-arn>", session_name="test")
39
+ s3_client = session.session.client(service_name="s3")
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ Refer to [this document](https://github.com/michaelthomasletts/boto3-refresh-session/blob/main/docs/contributing.md) for instructions.
@@ -0,0 +1,6 @@
1
+ __all__ = []
2
+
3
+ from . import session
4
+ from .session import AutoRefreshableSession
5
+
6
+ __all__.extend(["session", "AutoRefreshableSession"])
@@ -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,41 @@
1
+ [project]
2
+ name = "boto3-refresh-session"
3
+ version = "0.0.7"
4
+ description = "A simple Python package for refreshing boto3 sessions automatically."
5
+ authors = [
6
+ {name = "Mike Letts",email = "michaelthomasletts@gmail.com"}
7
+ ]
8
+ license = {text = "MIT"}
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ dependencies = ["boto3", "botocore", "attrs (>=24.3.0,<25.0.0)"]
12
+ keywords = ["boto3", "botocore", "aws"]
13
+ maintainers = [
14
+ {name="Michael Letts", email="lettsmt@gmail.com"},
15
+ ]
16
+
17
+ [project.urls]
18
+ repository = "https://github.com/michaelthomasletts/boto3-refresh-session"
19
+
20
+ [build-system]
21
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
22
+ build-backend = "poetry.core.masonry.api"
23
+
24
+ [tool.poetry.group.dev.dependencies]
25
+ pytest = "^8.3.4"
26
+ black = "^24.10.0"
27
+ isort = "^5.13.2"
28
+ flake8 = "^7.1.1"
29
+ pre-commit = "^4.0.1"
30
+
31
+ [tool.black]
32
+ line-length = 79
33
+ target-version = ["py310"]
34
+ verbose = true
35
+
36
+ [tool.isort]
37
+ line_length = 79
38
+ ensure_newline_before_comments = true
39
+ use_parentheses = true
40
+ include_trailing_comma = true
41
+ multi_line_output = 3