diff --git a/src/cfnlint/jsonschema/_keywords_cfn.py b/src/cfnlint/jsonschema/_keywords_cfn.py index f436690d95..43366b235f 100644 --- a/src/cfnlint/jsonschema/_keywords_cfn.py +++ b/src/cfnlint/jsonschema/_keywords_cfn.py @@ -82,13 +82,15 @@ def resolve_data_reference(value: Any, root_instance: Any) -> Any: elif isinstance(current, list): current = current[int(part)] else: - return value # Can't navigate further + return _UNRESOLVED return current except (KeyError, IndexError, ValueError): - return value # Reference not found, return original + return _UNRESOLVED -def resolve_data_in_schema(schema: Any, root_instance: Any) -> Any: +def resolve_data_in_schema( + schema: Any, root_instance: Any, allow_intrinsics: bool = False +) -> Any: """ Recursively resolve all $data references in a schema. """ @@ -119,6 +121,12 @@ def resolve_data_in_schema(schema: Any, root_instance: Any) -> Any: if k in _data_keywords: resolved = resolve_data_reference(v, root_instance) if resolved is _UNRESOLVED: + result["cfnUnresolved"] = True + continue + # Intrinsics are deployment-time values unless a cfnContext + # explicitly requests literal comparison of function objects. + if not allow_intrinsics and is_function(resolved)[0] is not None: + result["cfnUnresolved"] = True continue if k in _numeric_keywords: try: @@ -135,10 +143,18 @@ def resolve_data_in_schema(schema: Any, root_instance: Any) -> Any: continue result[k] = resolved else: - result[k] = resolve_data_in_schema(v, root_instance) + literal_functions = allow_intrinsics or ( + k == "cfnContext" + and isinstance(v, dict) + and v.get("functions") == [] + ) + result[k] = resolve_data_in_schema(v, root_instance, literal_functions) return result elif isinstance(schema, list): - return [resolve_data_in_schema(item, root_instance) for item in schema] + return [ + resolve_data_in_schema(item, root_instance, allow_intrinsics) + for item in schema + ] else: return schema @@ -583,6 +599,8 @@ def _validate_gathered( for err in scenario_validator.descend( gathered, resolved_schema, schema_path="schema" ): + if err.unknown: + continue if not err.path: yield err continue @@ -636,10 +654,18 @@ def _function_unknown( return +def cfnUnresolved( + validator: Validator, value: Any, instance: Any, schema: dict[str, Any] +) -> ValidationResult: + """Preserve unknown comparisons through negation and conditional schemas.""" + yield ValidationError("Cannot resolve gathered schema value", unknown=True) + + cfn_validators: dict[str, V] = { "additionalProperties": additionalProperties, "cfnContext": cfnContext, "cfnGather": cfnGather, + "cfnUnresolved": cfnUnresolved, "dynamicValidation": dynamicValidation, "type": cfn_type, # CloudFormation intrinsic functions - default to unknown diff --git a/test/fixtures/templates/issues/cfn_gather_literal_references.yaml b/test/fixtures/templates/issues/cfn_gather_literal_references.yaml new file mode 100644 index 0000000000..8c14d2c62e --- /dev/null +++ b/test/fixtures/templates/issues/cfn_gather_literal_references.yaml @@ -0,0 +1,67 @@ +AWSTemplateFormatVersion: "2010-09-09" +Parameters: + UseFifo: + Type: String + AllowedValues: ["true", "false"] + Default: "false" +Resources: + # Literal ARN, standard -> standard + StdSourceLiteralStdArn: + Type: AWS::SQS::Queue + Properties: + RedrivePolicy: + deadLetterTargetArn: arn:aws:sqs:us-east-1:123456789012:std-dlq + maxReceiveCount: 5 + # Literal ARN, FIFO -> .fifo + FifoSourceLiteralFifoArn: + Type: AWS::SQS::Queue + Properties: + FifoQueue: true + RedrivePolicy: + deadLetterTargetArn: arn:aws:sqs:us-east-1:123456789012:fifo-dlq.fifo + maxReceiveCount: 5 + # DLQ whose FifoQueue is a parameter + DlqParamFifo: + Type: AWS::SQS::Queue + Properties: + FifoQueue: !Ref UseFifo + StdSourceToParamFifoDlq: + Type: AWS::SQS::Queue + Properties: + RedrivePolicy: + deadLetterTargetArn: !GetAtt DlqParamFifo.Arn + maxReceiveCount: 5 + # Controls: GetAtt to in-template DLQs + StdDlq: + Type: AWS::SQS::Queue + FifoDlq: + Type: AWS::SQS::Queue + Properties: + FifoQueue: true + ControlMatch: + Type: AWS::SQS::Queue + Properties: + RedrivePolicy: + deadLetterTargetArn: !GetAtt StdDlq.Arn + maxReceiveCount: 5 + ControlMismatch: + Type: AWS::SQS::Queue + Properties: + RedrivePolicy: + deadLetterTargetArn: !GetAtt FifoDlq.Arn + maxReceiveCount: 5 + # Same mechanism, two RDS rules: literal DBClusterIdentifier + Cluster: + Type: AWS::RDS::DBCluster + Properties: + Engine: aurora-mysql + StorageEncrypted: true + MasterUsername: admin + MasterUserPassword: "{{resolve:secretsmanager:dummy}}" + Instance: + Type: AWS::RDS::DBInstance + Properties: + Engine: aurora-mysql + DBInstanceClass: db.t3.medium + DBClusterIdentifier: my-cluster + StorageEncrypted: true diff --git a/test/integration/test_gather_references.py b/test/integration/test_gather_references.py new file mode 100644 index 0000000000..a726c0710b --- /dev/null +++ b/test/integration/test_gather_references.py @@ -0,0 +1,65 @@ +""" +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +SPDX-License-Identifier: MIT-0 +""" + +import json +from pathlib import Path + +import pytest + +from cfnlint import lint + + +def test_literal_and_intrinsic_remote_properties(): + template = Path( + "test/fixtures/templates/issues/cfn_gather_literal_references.yaml" + ).read_text() + + matches = [ + match + for match in lint(template, regions=["us-east-1"]) + if match.rule.id in {"E3502", "E3707", "E3709"} + ] + + assert len(matches) == 1 + assert matches[0].path[:2] == ["Resources", "ControlMismatch"] + assert matches[0].message == ( + "Source queue type 'standard' does not match destination queue type 'FIFO'" + ) + + +@pytest.mark.parametrize( + "engine,encrypted,expected", + [ + ("aurora-mysql", True, set()), + ("aurora-postgresql", True, {"E3707"}), + ("aurora-mysql", False, {"E3709"}), + ], +) +def test_known_cluster_properties(engine, encrypted, expected): + template = { + "Resources": { + "Cluster": { + "Type": "AWS::RDS::DBCluster", + "Properties": {"Engine": "aurora-mysql", "StorageEncrypted": True}, + }, + "Instance": { + "Type": "AWS::RDS::DBInstance", + "Properties": { + "Engine": engine, + "StorageEncrypted": encrypted, + "DBClusterIdentifier": {"Ref": "Cluster"}, + }, + }, + } + } + + matches = [ + match + for match in lint(json.dumps(template), regions=["us-east-1"]) + if match.rule.id in {"E3707", "E3709"} + ] + + assert {match.rule.id for match in matches} == expected + assert all(match.path[:2] == ["Resources", "Instance"] for match in matches) diff --git a/test/unit/module/jsonschema/test_cfn_gather.py b/test/unit/module/jsonschema/test_cfn_gather.py index aa41c62d19..af64ddde44 100644 --- a/test/unit/module/jsonschema/test_cfn_gather.py +++ b/test/unit/module/jsonschema/test_cfn_gather.py @@ -2,10 +2,34 @@ Test cfnGather keyword functionality """ +import pytest + from cfnlint.jsonschema import CfnTemplateValidator from cfnlint.template import Template +@pytest.mark.parametrize( + "constraint", + [ + {"not": {"const": {"$data": "/remote/value"}}}, + {"if": {"const": {"$data": "/remote/value"}}, "then": False}, + {"oneOf": [{"const": {"$data": "/remote/value"}}, {"const": "actual"}]}, + ], +) +def test_unknown_gathered_value_in_composite_schema(constraint): + schema = { + "cfnGather": { + "gather": { + "local": {"properties": {"value": "/Value"}}, + "remote": {"reference": "/Target", "properties": {"value": "/Value"}}, + }, + "schema": {"properties": {"local": {"properties": {"value": constraint}}}}, + } + } + validator = CfnTemplateValidator(schema=schema, cfn=Template("", {})) + assert list(validator.iter_errors({"Target": "external", "Value": "actual"})) == [] + + class TestCfnGather: """Test cfnGather keyword""" diff --git a/test/unit/module/jsonschema/test_keywords_cfn.py b/test/unit/module/jsonschema/test_keywords_cfn.py index edd464f21f..2b2805e5a5 100644 --- a/test/unit/module/jsonschema/test_keywords_cfn.py +++ b/test/unit/module/jsonschema/test_keywords_cfn.py @@ -517,3 +517,38 @@ def test_resolve_data_in_schema_pattern_non_string(): instance = {"target": {"value": "^s3\\.amazonaws\\.com$"}} result = resolve_data_in_schema(schema, instance) assert result["pattern"] == "^s3\\.amazonaws\\.com$" + + +@pytest.mark.parametrize( + "instance", + [{}, {"target": {}}, {"target": None}, {"target": {"value": {"Ref": "Parameter"}}}], +) +def test_unknown_data_constraint_is_deferred(instance): + from cfnlint.jsonschema._keywords_cfn import resolve_data_in_schema + + assert resolve_data_in_schema({"const": {"$data": "/target/value"}}, instance) == { + "cfnUnresolved": True + } + + +@pytest.mark.parametrize("value", [False, 0, None, "", "aurora-mysql"]) +def test_known_data_constraint_is_preserved(value): + from cfnlint.jsonschema._keywords_cfn import resolve_data_in_schema + + assert resolve_data_in_schema( + {"const": {"$data": "/target/value"}}, {"target": {"value": value}} + ) == {"const": value} + + +def test_explicit_literal_function_comparison_is_preserved(): + from cfnlint.jsonschema._keywords_cfn import resolve_data_in_schema + + schema = { + "cfnContext": { + "functions": [], + "schema": {"const": {"$data": "/target/value"}}, + } + } + assert resolve_data_in_schema(schema, {"target": {"value": {"Ref": "Vpc"}}}) == { + "cfnContext": {"functions": [], "schema": {"const": {"Ref": "Vpc"}}} + }