-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRolesEnum.py
33 lines (28 loc) · 1.23 KB
/
RolesEnum.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
import subprocess
import json
def run_command(username):
command = "aws iam list-roles --profile " + username
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if error:
print("Error:", error.decode())
return output.decode()
username = input("Enter your AWS profile name: ")
output = run_command(username)
print("Roles.txt is Ready!")
if output:
data = json.loads(output)
with open("Roles.txt", "w") as roles_file:
for role in data["Roles"]:
role_name = role["RoleName"]
role_id = role["RoleId"]
description = role.get("Description", "")
arn = role["Arn"]
statement = role["AssumeRolePolicyDocument"]["Statement"]
statement_str = ", ".join([f"{key}: {value}" for dic in statement for key, value in dic.items()])
roles_file.write(f"RoleName: {role_name}\n")
roles_file.write(f"RoleId: {role_id}\n")
roles_file.write(f"Description: {description}\n")
roles_file.write(f"Arn: {arn}\n")
roles_file.write(f"Statement: {statement_str}\n")
roles_file.write("\n")