-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·89 lines (68 loc) · 2.62 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import yaml
import json
class TransformFile:
def __init__(self):
pass
def read_file(self):
file = 'serverless.yml'
with open(file, 'r') as reader:
full_file: dict = yaml.load(reader.read(), Loader=yaml.BaseLoader)
resources: dict = full_file.get('Resources')
if resources is None:
resources: dict = full_file.get('resources').get('Resources')
items_full: list = [resources.get(item).get('Type') for item in resources]
items_uniq: list = []
for item in dict.fromkeys(items_full):
item_origin: str = item.split('::')[1].lower()
item_final: str = item.split('::')[2]
resource_info = {item_origin: item_final}
items_uniq.append(resource_info)
aws: object = AmazonFile()
basic_policy: dict = aws.base_policy()
for action in items_uniq:
statement: dict = aws.create_policy(action)
basic_policy['Statement'].append(statement)
with open('policy.json', 'w+') as policy_file:
policy = json.dumps(basic_policy, indent=4)
print(policy)
policy_file.write(policy)
class AmazonFile:
def __init__(self):
pass
def create_policy(self, action: dict):
statement: dict = {}
resource = list(action.keys())[0]
dest: str = ''
#TODO: Make this better
if action[resource] == 'VPC':
dest: str = 'CreateVpc'
if action[resource] == 'InternetGateway':
dest: str = 'CreateInternetGateway'
if action[resource] == 'Subnet':
dest: str = 'CreateSubnet'
if action[resource] == 'RouteTable':
dest: str = 'CreateRouteTable'
if action[resource] == 'Route':
dest: str = 'CreateRoute'
if action[resource] == 'VPCEndpoint':
dest: str = 'CreateVpcEndpoint'
if action[resource] == 'Bucket':
dest: str = 'CreateBucket'
if action[resource] == 'BucketPolicy':
dest: str = 'PutBucketPolicy'
if action[resource] == 'Distribution':
dest: str = 'CreateDistribution'
else:
pass
statement['Effect'] = 'Allow'
statement['Resource'] = f'*'
statement['Action'] = f'{resource}:{dest}'
return statement
def base_policy(self):
policy: dict = {}
policy['Version'] = "2012-10-17"
policy['Statement'] = []
return policy
if __name__ == '__main__':
tr: object = TransformFile()
tr.read_file()