Skip to content

Commit 93553aa

Browse files
committed
Improve AWS STS module.
1 parent 1fc775f commit 93553aa

File tree

4 files changed

+104
-11
lines changed

4 files changed

+104
-11
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ wr.db.to_sql(df, engine, schema="test", name="my_table")
101101
- [Amazon S3](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#amazon-s3)
102102
- [AWS Glue Catalog](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#aws-glue-catalog)
103103
- [Amazon Athena](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#amazon-athena)
104-
- [Databases (Redshift, PostgreSQL, MySQL)](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#databases-redshift-postgresql-mysql)
105-
- [EMR Cluster](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#emr-cluster)
106-
- [CloudWatch Logs](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#cloudwatch-logs)
107-
- [QuickSight](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#quicksight)
104+
- [Databases (Amazon Redshift, PostgreSQL, MySQL)](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#databases-amazon-redshift-postgresql-mysql)
105+
- [Amazon EMR](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#amazon-emr)
106+
- [Amazon CloudWatch Logs](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#amazon-cloudwatch-logs)
107+
- [Amazon QuickSight](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#amazon-quicksight)
108+
- [AWS STS](https://aws-data-wrangler.readthedocs.io/en/latest/api.html#aws-sts)
108109
- [**License**](https://github.com/awslabs/aws-data-wrangler/blob/master/LICENSE)
109110
- [**Contributing**](https://github.com/awslabs/aws-data-wrangler/blob/master/CONTRIBUTING.md)
110111
- [**Legacy Docs** (pre-1.0.0)](https://aws-data-wrangler.readthedocs.io/en/legacy/)

awswrangler/sts.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""STS module."""
2+
3+
import logging
4+
from typing import Optional
5+
6+
import boto3 # type: ignore
7+
8+
from awswrangler import _utils
9+
10+
_logger: logging.Logger = logging.getLogger(__name__)
11+
12+
13+
def get_account_id(boto3_session: Optional[boto3.Session] = None) -> str:
14+
"""Get Account ID.
15+
16+
Parameters
17+
----------
18+
boto3_session : boto3.Session(), optional
19+
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
20+
21+
Returns
22+
-------
23+
str
24+
Account ID.
25+
26+
Examples
27+
--------
28+
>>> import awswrangler as wr
29+
>>> account_id = wr.sts.get_account_id()
30+
31+
"""
32+
session: boto3.Session = _utils.ensure_session(session=boto3_session)
33+
return _utils.client(service_name="sts", session=session).get_caller_identity().get("Account")
34+
35+
36+
def get_current_identity_arn(boto3_session: Optional[boto3.Session] = None) -> str:
37+
"""Get current user/role ARN.
38+
39+
Parameters
40+
----------
41+
boto3_session : boto3.Session(), optional
42+
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
43+
44+
Returns
45+
-------
46+
str
47+
User/role ARN.
48+
49+
Examples
50+
--------
51+
>>> import awswrangler as wr
52+
>>> arn = wr.sts.get_current_identity_arn()
53+
54+
"""
55+
session: boto3.Session = _utils.ensure_session(session=boto3_session)
56+
return _utils.client(service_name="sts", session=session).get_caller_identity().get("Arn")
57+
58+
59+
def get_current_identity_name(boto3_session: Optional[boto3.Session] = None) -> str:
60+
"""Get current user/role name.
61+
62+
Parameters
63+
----------
64+
boto3_session : boto3.Session(), optional
65+
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
66+
67+
Returns
68+
-------
69+
str
70+
User/role name.
71+
72+
Examples
73+
--------
74+
>>> import awswrangler as wr
75+
>>> name = wr.sts.get_current_identity_name()
76+
77+
"""
78+
arn: str = get_current_identity_arn(boto3_session=boto3_session)
79+
name: str = arn.rpartition("/")[-1]
80+
return name

docs/source/api.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ Amazon Athena
8787
stop_query_execution
8888
wait_query
8989

90-
Databases (Redshift, PostgreSQL, MySQL)
91-
---------------------------------------
90+
Databases (Amazon Redshift, PostgreSQL, MySQL)
91+
----------------------------------------------
9292

9393
.. currentmodule:: awswrangler.db
9494

@@ -106,8 +106,8 @@ Databases (Redshift, PostgreSQL, MySQL)
106106
unload_redshift_to_files
107107
write_redshift_copy_manifest
108108

109-
EMR
110-
---
109+
Amazon EMR
110+
----------
111111

112112
.. currentmodule:: awswrangler.emr
113113

@@ -125,8 +125,8 @@ EMR
125125
submit_steps
126126
terminate_cluster
127127

128-
CloudWatch Logs
129-
---------------
128+
Amazon CloudWatch Logs
129+
----------------------
130130

131131
.. currentmodule:: awswrangler.cloudwatch
132132

@@ -184,3 +184,15 @@ Amazon QuickSight
184184
list_templates
185185
list_users
186186
list_user_groups
187+
188+
AWS STS
189+
-----------------
190+
191+
.. currentmodule:: awswrangler.sts
192+
193+
.. autosummary::
194+
:toctree: stubs
195+
196+
get_account_id
197+
get_current_identity_arn
198+
get_current_identity_name

tests/test_moto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from unittest import mock
12
from unittest.mock import ANY
23

34
import boto3
45
import botocore
5-
from unittest import mock
66
import moto
77
import pandas as pd
88
import pytest

0 commit comments

Comments
 (0)