-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·70 lines (61 loc) · 2.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
from boto3.session import Session
import re
from sso import get_account_roles, get_accounts, get_oidc_token
from cloudformation import list_stacks
import csv
def main():
session = Session()
token = get_oidc_token(session)
access_token = token["accessToken"]
accounts = get_accounts(session, access_token)
with open("stacks.csv", "w+") as f:
writer = csv.DictWriter(
f,
fieldnames=[
"accountName",
"accountId",
"stackName",
"stackType",
"stackVersion",
],
)
writer.writeheader()
for account in accounts:
account_name = account["accountName"]
account_id = account["accountId"]
for role in get_account_roles(session, access_token, account_id):
role_name = role["roleName"]
if role_name == "di-support-readonly":
sso = session.client("sso")
role_creds = sso.get_role_credentials(
roleName=role["roleName"],
accountId=account_id,
accessToken=access_token,
)["roleCredentials"]
session = Session(
region_name="eu-west-2",
aws_access_key_id=role_creds["accessKeyId"],
aws_secret_access_key=role_creds["secretAccessKey"],
aws_session_token=role_creds["sessionToken"],
)
stacks = list_stacks(session)
for stack in stacks:
name = stack["StackName"]
description = stack.get("Description", "")
m = re.match(
"^(di-)?devplatform-deploy ([a-z\-]+) template version: v([\d\.]+)",
description,
)
if m:
writer.writerow(
{
"accountId": account_id,
"accountName": account_name,
"stackName": name,
"stackType": m.group(2),
"stackVersion": m.group(3),
}
)
if __name__ == "__main__":
main()