-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
109 lines (94 loc) · 3.37 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import json
import unittest
def verify_input_json(json_file):
try:
data = json.loads(json_file)
if "PolicyDocument" not in data or "Statement" not in data["PolicyDocument"]:
print("Missing components in the Json file.")
return True
for statement in data["PolicyDocument"]["Statement"]:
if not isinstance(statement, dict):
print("Statement should be a dictionary.")
return True
if "Resource" in statement:
if statement["Resource"] == "*":
return False
return True
except FileNotFoundError:
print("Given file couldn't be found.")
return False
except json.JSONDecodeError:
print("Invalid JSON format.")
return False
class test_verify_input_json(unittest.TestCase):
def test_valid_json(self):
json_data = '''
{
"PolicyDocument": {
"Statement": [
{
"Resource": "arn:aws:s3:::example-bucket",
"Effect": "Allow",
"Action": "s3:GetObject"
}
]
}
}
'''
self.assertTrue(verify_input_json(json_data))
def test_missing_policy_document_key(self):
json_data = '{"Statement": []}'
self.assertTrue(verify_input_json(json_data))
def test_invalid_policy_document_value(self):
json_data = '{"PolicyDocument": "not_a_dict", "Statement": []}'
self.assertTrue(verify_input_json(json_data))
def test_missing_statement_key(self):
json_data = '{"PolicyDocument": {}}'
self.assertTrue(verify_input_json(json_data))
def test_invalid_statement_value(self):
json_data = '{"PolicyDocument": {"Statement": "not_a_list"}}'
self.assertTrue(verify_input_json(json_data))
def test_invalid_resource_value(self):
json_data = '''
{
"PolicyDocument": {
"Statement": [
{
"Resource": 123
}
]
}
}
'''
self.assertTrue(verify_input_json(json_data))
def test_correct_resource_value(self):
json_data = '''
{
"PolicyName": "root",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Sid": "IamListAccess",
"Effect": "Allow",
"Action": [
"iam:ListRoles",
"iam:ListUsers"
],
"Resource": "*"
}]
}
}
'''
self.assertFalse(verify_input_json(json_data))
def main():
if len(sys.argv) == 2 and sys.argv[1] == "--run_unit_tests":
unittest.main(argv=sys.argv[:1])
elif len(sys.argv) == 2:
input_file = sys.argv[1]
with open(input_file, 'r') as file:
input_file = file.read()
verification = verify_input_json(input_file)
print("Result of verification of given Json file:", verification)
if __name__ == '__main__':
main()