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
36 changes: 31 additions & 5 deletions src/cfnlint/jsonschema/_keywords_cfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions test/fixtures/templates/issues/cfn_gather_literal_references.yaml
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions test/integration/test_gather_references.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions test/unit/module/jsonschema/test_cfn_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
35 changes: 35 additions & 0 deletions test/unit/module/jsonschema/test_keywords_cfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}}
}
Loading