Skip to content

Commit 2153ec2

Browse files
authored
Update W3663 to skip validation when Sub (#3548)
* Update W3663 to skip validation when Sub * Make sure to return rule E3673
1 parent e17d66e commit 2153ec2

File tree

4 files changed

+31
-51
lines changed

4 files changed

+31
-51
lines changed

src/cfnlint/rules/resources/ectwo/InstanceImageId.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,20 @@ def validate(
7070
launch_templates = list(
7171
self._get_related_launch_template(instance_image_id_validator, instance)
7272
)
73+
path: deque[str | int] = deque([])
74+
if "ImageId" != instance_image_id_validator.context.path.path[-1]:
75+
path = deque(
76+
list(instance_image_id_validator.context.path.path)[
77+
len(validator.context.path.path) :
78+
]
79+
)
7380

7481
if not launch_templates:
7582
yield ValidationError(
7683
"'ImageId' is a required property",
77-
path_override=instance_image_id_validator.context.path.path,
84+
validator="required",
85+
path=path,
86+
rule=self,
7887
)
7988
continue
8089

@@ -123,5 +132,7 @@ def validate(
123132
if launch_template_image_id is None and instance_image_id is None:
124133
yield ValidationError(
125134
"'ImageId' is a required property",
126-
path_override=instance_image_id_validator.context.path.path,
135+
validator="required",
136+
path=path,
137+
rule=self,
127138
)

src/cfnlint/rules/resources/lmbd/PermissionSourceAccount.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ def __init__(self):
3232
keywords=["Resources/AWS::Lambda::Permission/Properties"],
3333
)
3434

35-
def _validate_sub_has_account_id(self, validator: Validator, value: Any) -> bool:
36-
value = ensure_list(value)
37-
38-
if isinstance(value[0], str):
39-
if re.search(r":(\d{12}|\${AWS::AccountId}):", value[0]):
40-
return True
41-
42-
return False
43-
return True
44-
4535
def _validate_is_gettatt_to_bucket(self, validator: Validator, value: Any) -> bool:
4636
value = ensure_list(value)[0].split(".")[0]
4737

@@ -81,10 +71,7 @@ def validate(
8171

8272
fn_k, fn_v = is_function(source_arn)
8373
if fn_k is not None:
84-
if fn_k == "Fn::Sub":
85-
if self._validate_sub_has_account_id(scenario_validator, fn_v):
86-
continue
87-
elif fn_k == "Fn::GetAtt":
74+
if fn_k == "Fn::GetAtt":
8875
if not self._validate_is_gettatt_to_bucket(
8976
scenario_validator, fn_v
9077
):
@@ -96,4 +83,5 @@ def validate(
9683
yield ValidationError(
9784
"'SourceAccount' is a required property",
9885
validator="required",
86+
rule=self,
9987
)

test/unit/rules/resources/ec2/test_instance_image_id.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ def rule():
184184
[
185185
ValidationError(
186186
"'ImageId' is a required property",
187-
path_override=deque(
188-
["Resources", "Instance", "Properties", "ImageId"]
189-
),
187+
validator="required",
188+
rule=InstanceImageId(),
189+
path=deque([]),
190190
)
191191
],
192192
),
@@ -221,9 +221,9 @@ def rule():
221221
[
222222
ValidationError(
223223
"'ImageId' is a required property",
224-
path_override=deque(
225-
["Resources", "Instance", "Properties", "ImageId"]
226-
),
224+
validator="required",
225+
rule=InstanceImageId(),
226+
path=deque([]),
227227
)
228228
],
229229
),
@@ -268,17 +268,9 @@ def rule():
268268
[
269269
ValidationError(
270270
"'ImageId' is a required property",
271-
path_override=deque(
272-
[
273-
"Resources",
274-
"Instance",
275-
"Properties",
276-
"ImageId",
277-
"Fn::If",
278-
2,
279-
"Ref",
280-
]
281-
),
271+
validator="required",
272+
rule=InstanceImageId(),
273+
path=deque(["ImageId", "Fn::If", 2, "Ref"]),
282274
)
283275
],
284276
),
@@ -288,6 +280,10 @@ def rule():
288280
def test_validate(name, instance, expected, rule, validator):
289281
errs = list(rule.validate(validator, "", instance, {}))
290282

283+
for err in errs:
284+
print(err.path)
285+
print(err.validator)
286+
print(err.rule)
291287
assert (
292288
errs == expected
293289
), f"Expected test {name!r} to have {expected!r} but got {errs!r}"

test/unit/rules/resources/lmbd/test_permission_source_account.py

+3-18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def template():
6161
ValidationError(
6262
"'SourceAccount' is a required property",
6363
validator="required",
64+
rule=PermissionSourceAccount(),
6465
)
6566
],
6667
),
@@ -85,24 +86,6 @@ def template():
8586
{
8687
"SourceArn": {"Fn::Sub": "arn:${AWS::Partition}:s3:::bucket"},
8788
},
88-
[
89-
ValidationError(
90-
"'SourceAccount' is a required property",
91-
validator="required",
92-
)
93-
],
94-
),
95-
(
96-
{
97-
"SourceArn": {"Fn::Sub": [[], {}]},
98-
},
99-
[],
100-
),
101-
(
102-
{
103-
"SourceArn": {"Fn::Sub": "arn:${AWS::Partition}:s3:::bucket"},
104-
"SourceAccount": {"Ref": "AWS::AccountId"},
105-
},
10689
[],
10790
),
10891
(
@@ -120,6 +103,7 @@ def template():
120103
ValidationError(
121104
"'SourceAccount' is a required property",
122105
validator="required",
106+
rule=PermissionSourceAccount(),
123107
)
124108
],
125109
),
@@ -182,6 +166,7 @@ def template():
182166
ValidationError(
183167
"'SourceAccount' is a required property",
184168
validator="required",
169+
rule=PermissionSourceAccount(),
185170
)
186171
],
187172
),

0 commit comments

Comments
 (0)