This repository has been archived by the owner on Mar 19, 2022. It is now read-only.
/
get_actions.py
63 lines (43 loc) · 1.5 KB
/
get_actions.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
import json
import sys
from urllib.request import urlopen
# Constants
_AWS_POLICY_URL = 'https://awspolicygen.s3.amazonaws.com/js/policies.js'
_SPLIT_STR = 'app.PolicyEditorConfig='
def get_policy_js():
response = urlopen(_AWS_POLICY_URL)
return response.read().decode()
def json_to_actions(service_map):
service_actions = []
for service in service_map:
for action in service_map[service]["Actions"]:
service_actions.append(service_map[service]["StringPrefix"] + ":" + action)
return service_actions
def generate_action_list(sort=False, write_to_file=False):
policy_js = get_policy_js()
action_list = []
try:
full_json = json.loads(policy_js.split(_SPLIT_STR)[1])
action_list = json_to_actions(full_json["serviceMap"])
except (ValueError, KeyError, IndexError) as e:
print("ERR: Unexpected response, check " + _AWS_POLICY_URL, file=sys.stderr)
exit(e)
if sort:
action_list.sort()
# Write list to file
if write_to_file:
with open('aws_action_list.txt', 'w') as f:
for action in action_list:
f.write(action + "\n")
return action_list
def pull_action_list(sort=False, file='aws_action_list.txt'):
with open(file, 'r') as f:
action_list = f.readlines()
action_list = [line.strip() for line in action_list]
if sort:
action_list.sort()
return action_list
def main():
generate_action_list(True, True)
if __name__ == '__main__':
main()