-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam_simulate_action.py
More file actions
77 lines (59 loc) · 1.75 KB
/
iam_simulate_action.py
File metadata and controls
77 lines (59 loc) · 1.75 KB
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
71
72
73
74
75
76
77
#
# Find which principals can run a specific API method:
#
# python iam_simulate_action.py lambda:GetFunction
#
import os
import sys
import json
import itertools
import boto3
from utils.session import get_session
from utils.regions import get_all_regions
from utils.json_encoder import json_encoder
from utils.json_writer import json_writer
from utils.json_printer import json_printer
def get_users(client):
"""
:return: ARN for all IAM users
"""
return [u['Arn'] for u in client.list_users(MaxItems=1000)['Users']]
def get_groups(client):
"""
:return: ARN for all IAM groups
"""
return [g['Arn'] for g in client.list_groups(MaxItems=1000)['Groups']]
def get_roles(client):
"""
:return: ARN for all IAM roles
"""
return [r['Arn'] for r in client.list_roles(MaxItems=1000)['Roles']]
def main():
session = get_session()
actions = [
'sts:AssumeRole'
]
iam_client = session.client('iam')
all_principals = itertools.chain(
get_users(iam_client),
get_groups(iam_client),
get_roles(iam_client),
)
allowed_principals = []
for principal in all_principals:
evaluation_result = iam_client.simulate_principal_policy(
PolicySourceArn=principal,
ActionNames=actions
)
if evaluation_result['EvaluationResults'][0]['EvalDecision'] == 'allowed':
allowed_principals.append(principal)
sys.stdout.write('A')
sys.stdout.flush()
else:
sys.stdout.write('.')
sys.stdout.flush()
print('\n')
print('These principals are allowed to %s:' % actions)
print('\n'.join([' - %s' % ap for ap in allowed_principals]))
if __name__ == '__main__':
main()