diff --git a/src/cfnlint/rules/functions/DynamicReferenceSecretsManagerArn.py b/src/cfnlint/rules/functions/DynamicReferenceSecretsManagerArn.py index cce5e692d..3297d07e2 100644 --- a/src/cfnlint/rules/functions/DynamicReferenceSecretsManagerArn.py +++ b/src/cfnlint/rules/functions/DynamicReferenceSecretsManagerArn.py @@ -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 diff --git a/src/cfnlint/rules/functions/Sub.py b/src/cfnlint/rules/functions/Sub.py index 924bcd028..6ebfe42a6 100644 --- a/src/cfnlint/rules/functions/Sub.py +++ b/src/cfnlint/rules/functions/Sub.py @@ -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 diff --git a/test/unit/rules/functions/test_sub.py b/test/unit/rules/functions/test_sub.py index c3821b778..c550ce498 100644 --- a/test/unit/rules/functions/test_sub.py +++ b/test/unit/rules/functions/test_sub.py @@ -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 @@ -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}" + )