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
14 changes: 11 additions & 3 deletions src/cfnlint/rules/outputs/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing import Any

from cfnlint.context.conditions.exceptions import Unsatisfiable
from cfnlint.helpers import FUNCTIONS
from cfnlint.jsonschema import Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema
Expand Down Expand Up @@ -38,12 +39,19 @@ def validate(self, validator: Validator, _: Any, instance: Any, schema: Any):
if condition:
conditions = {condition: True}

try:
resolved = validator.context.conditions.evolve(conditions)
except Unsatisfiable:
# The output's own condition can never be true (for example
# `X and not X` over two identical conditions). Validate without
# pinning the condition instead of surfacing an E6001 exception,
# consistent with a statically false condition used directly.
resolved = validator.context.conditions

validator = validator.evolve(
context=validator.context.evolve(
functions=list(FUNCTIONS),
conditions=validator.context.conditions.evolve(
conditions,
),
conditions=resolved,
strict_types=False,
)
)
Expand Down
14 changes: 11 additions & 3 deletions src/cfnlint/rules/resources/properties/Properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections import deque
from typing import Any

from cfnlint.context.conditions.exceptions import Unsatisfiable
from cfnlint.helpers import FUNCTIONS, is_function
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema
Expand Down Expand Up @@ -71,13 +72,20 @@ def validate(
if validator.is_type(instance.get("Condition"), "string"):
resolved_conditions = {instance.get("Condition"): True}

try:
conditions = validator.context.conditions.evolve(resolved_conditions)
except Unsatisfiable:
# The resource's own condition can never be true (for example
# `X and not X` over two identical conditions). Validate without
# pinning the condition instead of surfacing an E3001 exception,
# consistent with a statically false condition used directly.
conditions = validator.context.conditions

validator = validator.evolve(
context=validator.context.evolve(
functions=list(FUNCTIONS),
strict_types=False,
conditions=validator.context.conditions.evolve(
resolved_conditions,
),
conditions=conditions,
),
function_filter=validator.function_filter.evolve(
add_cfn_lint_keyword=True,
Expand Down
42 changes: 41 additions & 1 deletion test/unit/rules/outputs/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@


@pytest.fixture
def template():
def template(request):
if hasattr(request, "param"):
return request.param
return {
"Parameters": {
"additionalVpcCidr": {
Expand Down Expand Up @@ -212,3 +214,41 @@ def test_output_value(input, expected, validator):
rule = Value()
results = list(rule.validate(validator, {}, input, {}))
assert results == expected, f"Expected {expected} results, got {results}"


@pytest.mark.parametrize(
"template",
[
{
"Conditions": {
"IsStageA": {"Fn::Equals": ["beta", "beta"]},
"IsStageB": {"Fn::Equals": ["beta", "beta"]},
"AAndNotB": {
"Fn::And": [
{"Condition": "IsStageA"},
{"Fn::Not": [{"Condition": "IsStageB"}]},
]
},
},
}
],
indirect=True,
)
def test_output_value_with_unsatisfiable_condition(validator):
# Regression test for https://github.com/aws-cloudformation/cfn-lint/issues/4785
# An output whose own condition can never be true (here `X and not X`
# over two identical conditions) must not raise Unsatisfiable (previously
# surfaced as an E6001 exception); its value is still validated.
rule = Value()
instance = {
"Condition": "AAndNotB",
"Value": {"foo": "bar"},
}
results = list(rule.validate(validator, {}, instance, {}))
messages = [e.message for e in results]
assert any(
"{'foo': 'bar'} is not of type 'string'" in message for message in messages
), f"value finding missing on gated output: {messages!r}"
assert not any("raised while validating" in message for message in messages), (
f"crash surfaced: {messages!r}"
)
61 changes: 61 additions & 0 deletions test/unit/rules/resources/properties/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,64 @@ def test_validate(name, instance, patches, expected, rule, validator):
)
else:
schema_manager.get_resource_schemas_by_regions.assert_not_called()


@pytest.mark.parametrize(
"template",
[
{
"AWSTemplateFormatVersion": "2010-09-09",
"Conditions": {
"IsFeatureOn": {"Fn::Equals": ["false", "true"]},
"IsFeatureEnabled": {"Fn::Equals": ["false", "true"]},
"OnAndNotEnabled": {
"Fn::And": [
{"Condition": "IsFeatureOn"},
{"Fn::Not": [{"Condition": "IsFeatureEnabled"}]},
]
},
},
}
],
indirect=True,
)
def test_validate_with_unsatisfiable_condition(rule, validator):
# Regression test for https://github.com/aws-cloudformation/cfn-lint/issues/4785
# A resource whose own condition can never be true (here `X and not X`
# over two identical conditions) must not raise Unsatisfiable (previously
# surfaced as an E3001 exception); its property errors are reported the
# same way as on an ungated resource.
instance = {
"Type": "MyType",
"Condition": "OnAndNotEnabled",
"Properties": {"BogusProperty": 1},
}
schema_manager = MagicMock()
schema_manager.get_resource_schemas_by_regions.return_value = [
(
["us-east-1"],
Schema(
{
"typeName": "MyType",
"properties": {},
"additionalProperties": False,
}
),
)
]

with patch(
"cfnlint.rules.resources.properties.Properties.PROVIDER_SCHEMA_MANAGER",
schema_manager,
):
errs = list(rule.validate(validator, {}, instance, {}))

messages = [e.message for e in errs]
assert any(
"Additional properties are not allowed ('BogusProperty' was unexpected)"
in message
for message in messages
), f"property finding missing on gated resource: {messages!r}"
assert not any("raised while validating" in message for message in messages), (
f"crash surfaced: {messages!r}"
)
Loading