Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class DynamicReferenceSecretsManagerArn(CloudFormationLintRule):
tags = ["functions", "dynamic reference"]

def validate(self, validator: Validator, s: Any, instance: Any, schema: Any):
# When run as a child rule of Fn::Sub, the instance is the whole
# Sub value, so use the string element of the two-element form.
if validator.is_type(instance, "array"):
instance = instance[0]
if "Fn::Sub" == validator.context.path.path[-1]:
if not re.match(r"^(\\\")?{{resolve:secretsmanager:.*}}(\\\")?$", instance):
return
Expand Down
7 changes: 6 additions & 1 deletion src/cfnlint/rules/functions/Sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ def fn_sub(
path=validator.context.path.descend(path=key)
)
)
# Pass the whole Fn::Sub value to the child rules, not just the
# string. W1019 needs the variable map (the second element of the
# two-element form) to report unused variables.
for _, rule in self.child_rules.items():
if rule and hasattr(rule, "validate"):
for err in rule.validate(value_validator, s, value, schema):
for err in rule.validate(
value_validator, s, instance.get("Fn::Sub"), schema
):
err.path.append("Fn::Sub")
err.rule = rule
yield err
48 changes: 48 additions & 0 deletions test/unit/rules/functions/test_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from cfnlint.rules.functions.GetAz import GetAz
from cfnlint.rules.functions.Ref import Ref
from cfnlint.rules.functions.Sub import Sub
from cfnlint.rules.functions.SubParametersUsed import SubParametersUsed
from cfnlint.rules.functions.SubResolved import SubResolved
from cfnlint.rules.functions.SubUnneeded import SubUnneeded
from cfnlint.template import Template


Expand Down Expand Up @@ -541,3 +543,49 @@ def test_sam_function_version_alias_in_sub(
assert [err.instance for err in errs] == expected_attrs, (
f"Test {name!r} got {errs!r}"
)


@pytest.mark.parametrize(
"name,instance,expected",
[
(
"Unused variable in the two-element form is reported",
{"Fn::Sub": ["hello-${A}", {"A": "x", "B": "y"}]},
[("Parameter 'B' not used in 'Fn::Sub'", [1, "B", "Fn::Sub"])],
),
(
"Unused variable is reported even when no variable is used",
{"Fn::Sub": ["hello", {"B": "y"}]},
[
("Parameter 'B' not used in 'Fn::Sub'", [1, "B", "Fn::Sub"]),
(
"'Fn::Sub' isn't needed because there are no variables",
[0, "Fn::Sub"],
),
],
),
(
"All variables used reports nothing",
{"Fn::Sub": ["hello-${A}", {"A": "x"}]},
[],
),
],
)
def test_sub_child_rules_get_whole_sub_value(name, instance, expected, context, cfn):
# Regression test for W1019 going silent: fn_sub must pass the whole
# Fn::Sub value (string plus variable map) to its child rules, not
# just the string, or SubParametersUsed never sees the map.
rule = Sub()
rule.child_rules["W1019"] = SubParametersUsed()
rule.child_rules["W1020"] = SubUnneeded()
validator = CfnTemplateValidator({}).extend(
validators={
"ref": Ref().ref,
"fn_getatt": GetAtt().fn_getatt,
"fn_getazs": GetAz().fn_getazs,
}
)(context=context.evolve(strict_types=False), cfn=cfn)
errs = list(rule.fn_sub(validator, {"type": "string"}, instance, {}))
assert [(err.message, list(err.path)) for err in errs] == expected, (
f"Test {name!r} got {errs!r}"
)
Loading