boto3-refresh-session 0.0.22__tar.gz → 0.0.24__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: boto3-refresh-session
3
- Version: 0.0.22
3
+ Version: 0.0.24
4
4
  Summary: A simple Python package for refreshing boto3 sessions automatically.
5
5
  Home-page: https://github.com/michaelthomasletts/boto3-refresh-session
6
6
  License: MIT
@@ -21,9 +21,12 @@ __all__ = ["AutoRefreshableSession"]
21
21
  from typing import Type
22
22
 
23
23
  from attrs import define, field
24
- from attrs.validators import instance_of, le, optional
24
+ from attrs.validators import ge, instance_of, optional
25
25
  from boto3 import Session
26
- from botocore.credentials import RefreshableCredentials
26
+ from botocore.credentials import (
27
+ DeferredRefreshableCredentials,
28
+ RefreshableCredentials,
29
+ )
27
30
  from botocore.session import get_session
28
31
 
29
32
 
@@ -43,8 +46,13 @@ class AutoRefreshableSession:
43
46
  AWS role ARN.
44
47
  session_name : str
45
48
  Name for session.
49
+ defer_refresh : bool, optional
50
+ If ``True`` then temporary credentials are not automatically refreshed until
51
+ they are explicitly needed. If ``False`` then temporary credentials refresh
52
+ immediately upon expiration. Default is ``True``.
46
53
  ttl : int, optional
47
- Number of seconds until temporary credentials expire, default 900.
54
+ Number of seconds until temporary credentials expire. Must be greater than or
55
+ equal to 900 seconds. Default is 900.
48
56
  session_kwargs : dict, optional
49
57
  Optional keyword arguments for :class:`boto3.session.Session`.
50
58
  client_kwargs : dict, optional
@@ -61,6 +69,10 @@ class AutoRefreshableSession:
61
69
  Check the :ref:`authorization documentation <authorization>` for additional
62
70
  information concerning how to authorize access to AWS.
63
71
 
72
+ The default ``defer_refresh`` parameter value results in temporary credentials not
73
+ being refreshed until they are explicitly requested; that is more efficient than
74
+ refreshing expired temporary credentials automatically after they expire.
75
+
64
76
  Examples
65
77
  --------
66
78
  Here's how to initialize this object:
@@ -76,8 +88,9 @@ class AutoRefreshableSession:
76
88
  region: str = field(validator=instance_of(str))
77
89
  role_arn: str = field(validator=instance_of(str))
78
90
  session_name: str = field(validator=instance_of(str))
91
+ defer_refresh: bool = field(default=True, validator=instance_of(bool))
79
92
  ttl: int = field(
80
- default=900, validator=optional([instance_of(int), le(900)])
93
+ default=900, validator=optional([instance_of(int), ge(900)])
81
94
  )
82
95
  session_kwargs: dict = field(
83
96
  default={}, validator=optional(instance_of(dict))
@@ -88,13 +101,19 @@ class AutoRefreshableSession:
88
101
  session: Type[Session] = field(init=False)
89
102
 
90
103
  def __attrs_post_init__(self):
91
- __credentials = RefreshableCredentials.create_from_metadata(
92
- metadata=self._get_credentials(),
93
- refresh_using=self._get_credentials,
94
- method="sts-assume-role",
95
- )
96
104
  __session = get_session()
97
- # https://github.com/boto/botocore/blob/f8a1dd0820b548a5e8dc05420b28b6f1c6e21154/botocore/session.py#L143
105
+
106
+ if not self.defer_refresh:
107
+ __credentials = RefreshableCredentials.create_from_metadata(
108
+ metadata=self._get_credentials(),
109
+ refresh_using=self._get_credentials,
110
+ method="sts-assume-role",
111
+ )
112
+ else:
113
+ __credentials = DeferredRefreshableCredentials(
114
+ refresh_using=self._get_credentials, method="sts-assume-role"
115
+ )
116
+
98
117
  __session._credentials = __credentials
99
118
  self.session = Session(botocore_session=__session)
100
119
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "boto3-refresh-session"
3
- version = "0.0.22"
3
+ version = "0.0.24"
4
4
  description = "A simple Python package for refreshing boto3 sessions automatically."
5
5
  authors = [
6
6
  {name = "Mike Letts",email = "michaelthomasletts@gmail.com"}
@@ -42,4 +42,10 @@ line_length = 79
42
42
  ensure_newline_before_comments = true
43
43
  use_parentheses = true
44
44
  include_trailing_comma = true
45
- multi_line_output = 3
45
+ multi_line_output = 3
46
+
47
+ [tool.pytest.ini_options]
48
+ log_cli = true
49
+ log_cli_level = "INFO"
50
+ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
51
+ log_cli_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"