-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_statement.py
192 lines (148 loc) · 6.05 KB
/
test_statement.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import re
import pytest
from pycfmodel.model.resources.properties.statement import Statement
def statement_1():
return Statement(**{"Effect": "Allow", "Action": ["action1"], "NotAction": "action2", "Resource": ["arn"]})
def statement_2():
return Statement(**{"Effect": "Allow", "Action": "action1", "NotAction": ["action2"], "Resource": ["arn"]})
def statement_3():
return Statement(**{"Effect": "Allow", "Action": "action1", "Resource": ["arn1"], "NotResource": "arn2"})
def statement_4():
return Statement(**{"Effect": "Allow", "Action": "action2", "Resource": "arn1", "NotResource": ["arn2"]})
def statement_principal_1():
return Statement(**{"Principal": {"AWS": "arn:aws:iam::123456789012:root"}})
def statement_principal_2():
return Statement(
**{
"Principal": {
"AWS": ["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"]
}
}
)
def statement_principal_3():
return Statement(**{"Principal": {"Federated": "cognito-identity.amazonaws.com"}})
def statement_principal_4():
return Statement(**{"Principal": "arn:aws:iam::123456789012:root"})
def statement_principal_5():
return Statement(
**{"Principal": ["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"]}
)
def statement_not_principal_1():
return Statement(**{"NotPrincipal": {"AWS": "arn:aws:iam::123456789012:root"}})
def statement_not_principal_2():
return Statement(
**{
"NotPrincipal": {
"AWS": ["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"]
}
}
)
def statement_not_principal_3():
return Statement(**{"NotPrincipal": {"Federated": "cognito-identity.amazonaws.com"}})
def statement_not_principal_4():
return Statement(**{"NotPrincipal": "arn:aws:iam::123456789012:root"})
def statement_not_principal_5():
return Statement(
**{
"NotPrincipal": [
"arn:aws:iam::AWS-account-ID:user/user-name-1",
"arn:aws:iam::AWS-account-ID:user/UserName2",
]
}
)
@pytest.mark.parametrize(
"statement, expected_output",
[
(statement_1(), ["action1", "action2"]),
(statement_2(), ["action1", "action2"]),
(statement_3(), ["action1"]),
(statement_4(), ["action2"]),
],
)
def test_get_action_list(statement, expected_output):
assert statement.get_action_list() == expected_output
@pytest.mark.parametrize(
"statement, expected_output",
[
(statement_1(), ["arn"]),
(statement_2(), ["arn"]),
(statement_3(), ["arn1", "arn2"]),
(statement_4(), ["arn1", "arn2"]),
],
)
def test_get_resource_list(statement, expected_output):
assert statement.get_resource_list() == expected_output
@pytest.mark.parametrize(
"statement, expected_output",
[
(statement_principal_1(), ["arn:aws:iam::123456789012:root"]),
(
statement_principal_2(),
["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"],
),
(statement_principal_3(), ["cognito-identity.amazonaws.com"]),
(statement_principal_4(), ["arn:aws:iam::123456789012:root"]),
(
statement_principal_5(),
["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"],
),
(statement_not_principal_1(), ["arn:aws:iam::123456789012:root"]),
(
statement_not_principal_2(),
["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"],
),
(statement_not_principal_3(), ["cognito-identity.amazonaws.com"]),
(statement_not_principal_4(), ["arn:aws:iam::123456789012:root"]),
(
statement_not_principal_5(),
["arn:aws:iam::AWS-account-ID:user/user-name-1", "arn:aws:iam::AWS-account-ID:user/UserName2"],
),
],
)
def test_get_principal_list(statement, expected_output):
assert statement.get_principal_list() == expected_output
@pytest.mark.parametrize(
"statement, pattern, expected_output",
[
(statement_1(), re.compile(r"action.*"), ["action1", "action2"]),
(statement_2(), re.compile(r"action.*"), ["action1", "action2"]),
(statement_3(), re.compile(r"action2"), []),
(statement_4(), re.compile(r"a.*2"), ["action2"]),
],
)
def test_actions_with(statement, pattern, expected_output):
assert statement.actions_with(pattern) == expected_output
@pytest.mark.parametrize(
"statement, pattern, expected_output",
[
(statement_principal_1(), re.compile(r"^.*123456789012.*$"), ["arn:aws:iam::123456789012:root"]),
(statement_principal_2(), re.compile(r"^.*user-name-1$"), ["arn:aws:iam::AWS-account-ID:user/user-name-1"]),
(statement_principal_3(), re.compile(r"^.*\.amazonaws\.com$"), ["cognito-identity.amazonaws.com"]),
],
)
def test_principals_with(statement, pattern, expected_output):
assert statement.principals_with(pattern) == expected_output
@pytest.mark.parametrize(
"statement, pattern, expected_output",
[
(statement_1(), re.compile(r"^[a]+$"), []),
(statement_2(), re.compile(r"^[arn]+$"), ["arn"]),
(statement_3(), re.compile(r"^.*1$"), ["arn1"]),
(statement_4(), re.compile(r"^.*2$"), ["arn2"]),
],
)
def test_resources_with(statement, pattern, expected_output):
assert statement.resources_with(pattern) == expected_output
@pytest.mark.parametrize(
"statement, whitelist, expected_output",
[
(statement_principal_1(), ["arn:aws:iam::123456789012:root"], []),
(
statement_principal_2(),
["arn:aws:iam::AWS-account-ID:user/user-name-1"],
["arn:aws:iam::AWS-account-ID:user/UserName2"],
),
],
)
def test_non_whitelisted_principals(statement, whitelist, expected_output):
assert statement.non_whitelisted_principals(whitelist) == expected_output