Skip to content

Commit 6af0ee2

Browse files
committed
Check for unsupported element combination (#3146)
* Check for unsupported element combination * Updating tests for principal
1 parent b29eacb commit 6af0ee2

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/cfnlint/data/schemas/other/iam/policy_resource.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"NotResource"
1818
]
1919
},
20+
{
21+
"requiredXor": [
22+
"Principal",
23+
"NotPrincipal"
24+
]
25+
},
2026
{
2127
"required": [
2228
"Effect"

src/cfnlint/data/schemas/other/iam/policy_resource_ecr.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"NotAction"
1212
]
1313
},
14+
{
15+
"requiredXor": [
16+
"Principal",
17+
"NotPrincipal"
18+
]
19+
},
1420
{
1521
"required": [
1622
"Effect"

test/unit/rules/resources/iam/test_resource_policy.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def test_object_multiple_effect(self):
5050
"cloudformation:*",
5151
],
5252
"Resource": "*",
53+
"Principal": {
54+
"AWS": [
55+
"arn:aws:iam::123456789012:root",
56+
"999999999999",
57+
],
58+
"CanonicalUser": "79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be",
59+
},
5360
}
5461
],
5562
}
@@ -107,14 +114,18 @@ def test_object_statements(self):
107114
validator=validator, policy=policy, schema={}, policy_type=None
108115
)
109116
)
110-
self.assertEqual(len(errs), 2, errs)
111-
self.assertEqual(errs[0].message, "'NotAllow' is not one of ['Allow', 'Deny']")
112-
self.assertListEqual(list(errs[0].path), ["Statement", 0, "Effect"])
117+
self.assertEqual(len(errs), 3, errs)
113118
self.assertEqual(
114-
errs[1].message,
119+
errs[0].message,
120+
"Only one of ['Principal', 'NotPrincipal'] is a required property",
121+
)
122+
self.assertEqual(errs[1].message, "'NotAllow' is not one of ['Allow', 'Deny']")
123+
self.assertListEqual(list(errs[1].path), ["Statement", 0, "Effect"])
124+
self.assertEqual(
125+
errs[2].message,
115126
"{'NotValid': ['arn:${AWS::Partition}:iam::123456789012:role/object-role']} is not of type 'string'",
116127
)
117-
self.assertListEqual(list(errs[1].path), ["Statement", 0, "Resource", 1])
128+
self.assertListEqual(list(errs[2].path), ["Statement", 0, "Resource", 1])
118129

119130
def test_string_statements(self):
120131
"""Test Positive"""
@@ -146,13 +157,17 @@ def test_string_statements(self):
146157
validator=validator, policy=policy, schema={}, policy_type=None
147158
)
148159
)
149-
self.assertEqual(len(errs), 2, errs)
160+
self.assertEqual(len(errs), 3, errs)
150161
self.assertEqual(
151162
errs[0].message,
163+
"Only one of ['Principal', 'NotPrincipal'] is a required property",
164+
)
165+
self.assertEqual(
166+
errs[1].message,
152167
"{'Fn::Sub': ['arn:${AWS::Partition}:iam::123456789012/role/string-role']} is not of type 'string'",
153168
)
154-
self.assertListEqual(list(errs[0].path), ["Statement", 0, "Resource", 1])
169+
self.assertListEqual(list(errs[1].path), ["Statement", 0, "Resource", 1])
155170
self.assertEqual(
156-
errs[1].message, "'2012-10-18' is not one of ['2008-10-17', '2012-10-17']"
171+
errs[2].message, "'2012-10-18' is not one of ['2008-10-17', '2012-10-17']"
157172
)
158-
self.assertListEqual(list(errs[1].path), ["Version"])
173+
self.assertListEqual(list(errs[2].path), ["Version"])

0 commit comments

Comments
 (0)