3
3
SPDX-License-Identifier: MIT-0
4
4
"""
5
5
6
+ from collections import deque
6
7
from unittest import TestCase
7
8
8
- from cfnlint .context import Context
9
+ from cfnlint .context import Context , Path
9
10
from cfnlint .helpers import FUNCTIONS
10
- from cfnlint .jsonschema import CfnTemplateValidator
11
+ from cfnlint .jsonschema import CfnTemplateValidator , ValidationError
11
12
from cfnlint .rules .resources .iam .ResourcePolicy import ResourcePolicy
12
13
13
14
@@ -20,7 +21,13 @@ def setUp(self):
20
21
21
22
def test_object_basic (self ):
22
23
"""Test Positive"""
23
- validator = CfnTemplateValidator ()
24
+ validator = CfnTemplateValidator ({}).evolve (
25
+ context = Context (
26
+ path = Path (
27
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
28
+ )
29
+ )
30
+ )
24
31
25
32
policy = {"Version" : "2012-10-18" }
26
33
@@ -30,15 +37,21 @@ def test_object_basic(self):
30
37
)
31
38
)
32
39
self .assertEqual (len (errs ), 2 , errs )
33
- self .assertEqual (errs [0 ].message , "'Statement' is a required property" )
34
- self .assertListEqual (list (errs [0 ].path ), [])
35
40
self .assertEqual (
36
- errs [1 ].message , "'2012-10-18' is not one of ['2008-10-17', '2012-10-17']"
41
+ errs [0 ].message , "'2012-10-18' is not one of ['2008-10-17', '2012-10-17']"
37
42
)
38
- self .assertListEqual (list (errs [1 ].path ), ["Version" ])
43
+ self .assertListEqual (list (errs [0 ].path ), ["Version" ])
44
+ self .assertEqual (errs [1 ].message , "'Statement' is a required property" )
45
+ self .assertListEqual (list (errs [1 ].path ), [])
39
46
40
47
def test_object_multiple_effect (self ):
41
- validator = CfnTemplateValidator ()
48
+ validator = CfnTemplateValidator ({}).evolve (
49
+ context = Context (
50
+ path = Path (
51
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
52
+ )
53
+ )
54
+ )
42
55
43
56
policy = {
44
57
"Version" : "2012-10-17" ,
@@ -91,7 +104,12 @@ def test_object_multiple_effect(self):
91
104
92
105
def test_object_statements (self ):
93
106
validator = CfnTemplateValidator ({}).evolve (
94
- context = Context (functions = FUNCTIONS )
107
+ context = Context (
108
+ functions = FUNCTIONS ,
109
+ path = Path (
110
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
111
+ ),
112
+ )
95
113
)
96
114
97
115
policy = {
@@ -138,7 +156,13 @@ def test_object_statements(self):
138
156
139
157
def test_string_statements (self ):
140
158
"""Test Positive"""
141
- validator = CfnTemplateValidator ()
159
+ validator = CfnTemplateValidator ({}).evolve (
160
+ context = Context (
161
+ path = Path (
162
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
163
+ )
164
+ )
165
+ )
142
166
143
167
# ruff: noqa: E501
144
168
policy = """
@@ -183,7 +207,12 @@ def test_string_statements(self):
183
207
184
208
def test_principal_wildcard (self ):
185
209
validator = CfnTemplateValidator ({}).evolve (
186
- context = Context (functions = FUNCTIONS )
210
+ context = Context (
211
+ functions = FUNCTIONS ,
212
+ path = Path (
213
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
214
+ ),
215
+ )
187
216
)
188
217
189
218
policy = {
@@ -227,13 +256,59 @@ def test_principal_wildcard(self):
227
256
228
257
def test_assumed_role (self ):
229
258
validator = CfnTemplateValidator ({}).evolve (
230
- context = Context (functions = FUNCTIONS )
259
+ context = Context (
260
+ functions = FUNCTIONS ,
261
+ path = Path (
262
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
263
+ ),
264
+ )
265
+ )
266
+
267
+ policy = {
268
+ "Version" : "2012-10-17" ,
269
+ "Statement" : [
270
+ {
271
+ "Effect" : "Allow" ,
272
+ "Action" : "*" ,
273
+ "Resource" : "arn:aws:s3:::bucket" ,
274
+ "Principal" : {
275
+ "AWS" : "arn:aws:sts::123456789012:assumed-role/rolename/rolesessionname"
276
+ },
277
+ },
278
+ ],
279
+ }
280
+
281
+ errs = list (
282
+ self .rule .validate (
283
+ validator = validator , policy = policy , schema = {}, policy_type = None
284
+ )
285
+ )
286
+ self .assertListEqual (errs , [])
287
+
288
+ def test_duplicate_sid (self ):
289
+ validator = CfnTemplateValidator ({}).evolve (
290
+ context = Context (
291
+ functions = FUNCTIONS ,
292
+ path = Path (
293
+ cfn_path = deque (["Resources" , "AWS::S3::BucketPolicy" , "Properties" ])
294
+ ),
295
+ )
231
296
)
232
297
233
298
policy = {
234
299
"Version" : "2012-10-17" ,
235
300
"Statement" : [
236
301
{
302
+ "Sid" : "A" ,
303
+ "Effect" : "Allow" ,
304
+ "Action" : "*" ,
305
+ "Resource" : "arn:aws:s3:::bucket" ,
306
+ "Principal" : {
307
+ "AWS" : "arn:aws:sts::123456789012:assumed-role/rolename/rolesessionname"
308
+ },
309
+ },
310
+ {
311
+ "Sid" : "A" ,
237
312
"Effect" : "Allow" ,
238
313
"Action" : "*" ,
239
314
"Resource" : "arn:aws:s3:::bucket" ,
@@ -250,3 +325,30 @@ def test_assumed_role(self):
250
325
)
251
326
)
252
327
self .assertListEqual (errs , [])
328
+
329
+ # Fail on SNS topic
330
+ validator = CfnTemplateValidator ({}).evolve (
331
+ context = Context (
332
+ functions = FUNCTIONS ,
333
+ path = Path (
334
+ cfn_path = deque (["Resources" , "AWS::SNS::TopicPolicy" , "Properties" ])
335
+ ),
336
+ )
337
+ )
338
+ errs = list (
339
+ self .rule .validate (
340
+ validator = validator , policy = policy , schema = {}, policy_type = None
341
+ )
342
+ )
343
+ self .assertListEqual (
344
+ errs ,
345
+ [
346
+ ValidationError (
347
+ "array items are not unique for keys ['Sid']" ,
348
+ validator = "uniqueKeys" ,
349
+ schema_path = deque (["properties" , "Statement" , "uniqueKeys" ]),
350
+ path = deque (["Statement" ]),
351
+ rule = ResourcePolicy (),
352
+ )
353
+ ],
354
+ )
0 commit comments