Skip to content

Commit 8751c1a

Browse files
stephenbawksrubenfonseca
authored andcommitted
adding sigv4
1 parent d2ebd0d commit 8751c1a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: aws_lambda_powertools/utilities/iam/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Advanced feature flags utility"""
2+
from .auth import SigV4AuthFactory
3+
4+
__all__ = [
5+
"SigV4AuthFactory",
6+
]

Diff for: aws_lambda_powertools/utilities/iam/auth.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
from typing import Optional
3+
from botocore.auth import SigV4Auth
4+
from botocore.credentials import Credentials
5+
from boto3 import Session
6+
7+
8+
class SigV4AuthFactory:
9+
"""
10+
SigV4 authentication utility
11+
12+
Args:
13+
region (str): AWS region
14+
service (str): AWS service
15+
access_key (str, optional): AWS access key
16+
secret_key (str, optional): AWS secret key
17+
token (str, optional): AWS session token
18+
19+
Returns:
20+
SigV4Auth: SigV4Auth instance
21+
22+
Examples
23+
--------
24+
**Using default credentials**
25+
>>> from aws_lambda_powertools.utilities.iam import SigV4AuthFactory
26+
>>> auth = SigV4AuthFactory(region="us-east-2", service="vpc-lattice-svcs")
27+
28+
29+
30+
"""
31+
def __init__(
32+
self,
33+
region: str,
34+
service: str,
35+
access_key: Optional[str],
36+
secret_key: Optional[str],
37+
token: Optional[str],
38+
):
39+
self._region = region
40+
self._service = service
41+
42+
if access_key and secret_key or token:
43+
self._access_key = access_key
44+
self._secret_key = secret_key
45+
self._credentials = Credentials(access_key=self._access_key, secret_key=self._secret_key, token=token)
46+
47+
else:
48+
self._credentials = Session().get_credentials()
49+
50+
def __call__(self):
51+
return SigV4Auth(credentials=self._credentials, service=self._service, region=self._region)

0 commit comments

Comments
 (0)