Skip to content

Commit 86037ae

Browse files
get IAM_USERNAME from AWS credentials if env var IAM_USERNAME is not set
1 parent 65c01ba commit 86037ae

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
- Description: Session Token for the current AWS session. Only required if you assume a role first.
1515

1616
#### IAM_USERNAME
17-
- Required: ***True***
18-
- Description: Name of IAM user being rotated
17+
- Required: ***False***
18+
- Description: Name of IAM user being rotated, if not set the username which is used in the AWS credentials is used
1919

2020
#### PERSONAL_ACCESS_TOKEN
2121
- Required: ***True***

rotate_keys.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323

2424
def main_function():
25-
iam_username = os.environ['IAM_USERNAME']
25+
iam_username = os.environ['IAM_USERNAME'] if 'IAM_USERNAME' in os.environ else who_am_i()
2626
github_token = os.environ['PERSONAL_ACCESS_TOKEN']
2727
owner_repository = os.environ['OWNER_REPOSITORY']
2828

@@ -59,6 +59,19 @@ def main_function():
5959

6060
sys.exit(0)
6161

62+
def who_am_i():
63+
# ask the aws backend for myself with a boto3 sts client
64+
sts = boto3.client(
65+
'sts',
66+
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID'],
67+
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY'],
68+
aws_session_token = os.environ['AWS_SESSION_TOKEN'] if 'AWS_SESSION_TOKEN' in os.environ else None
69+
)
70+
71+
user = sts.get_caller_identity()
72+
# return last element of splitted list to get username
73+
return user['Arn'].split("/")[-1]
74+
6275
def create_new_keys(iam_username):
6376
# create the keys
6477
create_ret = iam.create_access_key(

0 commit comments

Comments
 (0)