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
28 changes: 4 additions & 24 deletions src/cfnlint/jsonschema/_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,16 @@ def additionalProperties(
def allOf(
validator: Validator, allOf: Any, instance: Any, schema: dict[str, Any]
) -> ValidationResult:
# allOf is conjunctive: every subschema must hold and there is no branch to
# select, so an unresolvable intrinsic function creates no ambiguity here
# (unlike anyOf/oneOf/if). Validate each subschema and yield all errors.
validator = validator.evolve(
function_filter=validator.function_filter.evolve(
add_cfn_lint_keyword=False,
),
context=validator.context.evolve(
unresolvable_function_mode=True,
),
)
has_unknown = False
known_errors = []

for index, subschema in enumerate(allOf):
errs = list(validator.descend(instance, subschema, schema_path=index))

if any(getattr(err, "unknown", False) for err in errs):
has_unknown = True
else:
known_errors.extend(errs)

# If we have unknown branches, we can't determine if allOf is satisfied
if has_unknown:
yield ValidationError(
f"Cannot determine allOf for {instance!r}",
unknown=True,
)
return

# Yield all known errors
for err in known_errors:
yield err
yield from validator.descend(instance, subschema, schema_path=index)


def anyOf(
Expand Down
30 changes: 29 additions & 1 deletion src/cfnlint/rules/functions/Ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

from typing import Any

from cfnlint.helpers import VALID_PARAMETER_TYPES, VALID_PARAMETER_TYPES_LIST
from cfnlint.helpers import (
PSEUDOPARAMS_MULTIPLE,
PSEUDOPARAMS_SINGLE,
VALID_PARAMETER_TYPES,
VALID_PARAMETER_TYPES_LIST,
)
from cfnlint.jsonschema import ValidationError, Validator
from cfnlint.rules.functions._BaseFn import BaseFn, all_types

Expand Down Expand Up @@ -68,6 +73,29 @@ def ref(
yield ValidationError(f"{instance!r} is not of type {reprs}")
return

elif value in PSEUDOPARAMS_SINGLE or value in PSEUDOPARAMS_MULTIPLE:
# Pseudo parameters resolve to a known type: the SINGLE set are
# strings, PSEUDOPARAMS_MULTIPLE (AWS::NotificationARNs) is a list.
# Validate that resolved type against the schema, the same way we
# do for named parameters.
schema_types = self.resolve_type(validator, subschema)
if not schema_types:
return
reprs = ", ".join(repr(type) for type in schema_types)
is_list = value in PSEUDOPARAMS_MULTIPLE

if all(
st not in ["string", "boolean", "integer", "number"]
for st in schema_types
):
if not is_list:
yield ValidationError(f"{instance!r} is not of type {reprs}")
return
elif all(st not in ["array"] for st in schema_types):
if is_list:
yield ValidationError(f"{instance!r} is not of type {reprs}")
return

for rule_id in self._all_refs:
rule = self.child_rules.get(rule_id)
if rule:
Expand Down
10 changes: 3 additions & 7 deletions src/cfnlint/rules/resources/CreationPolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing import Any

from cfnlint.helpers import FUNCTIONS
from cfnlint.jsonschema import Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema

Expand Down Expand Up @@ -84,13 +85,8 @@ def validate(self, validator: Validator, dP: str, instance, schema):

validator = validator.evolve(
context=validator.context.evolve(
functions=[
"Fn::Sub",
"Fn::Select",
"Fn::FindInMap",
"Fn::If",
"Ref",
],
functions=list(FUNCTIONS),
resources={},
strict_types=False,
),
schema=self._get_schema(resource_type),
Expand Down
2 changes: 1 addition & 1 deletion src/cfnlint/rules/resources/updatepolicy/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

import cfnlint.data.schemas.other.resources
import cfnlint.helpers
from cfnlint.helpers import FUNCTIONS
from cfnlint.jsonschema import Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
Expand Down Expand Up @@ -36,6 +35,7 @@ def validate(self, validator: Validator, keywords: Any, instance: Any, schema: A
validator = validator.evolve(
context=validator.context.evolve(
functions=list(FUNCTIONS),
resources={},
strict_types=False,
),
schema=self._schema,
Expand Down
115 changes: 115 additions & 0 deletions test/fixtures/results/integration/creationpolicy_yaml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
[
{
"Filename": "test/fixtures/templates/integration/creationpolicy.yaml",
"Id": "79d23d5c-b5c1-c364-58e0-b3c01c5aa274",
"Level": "Error",
"Location": {
"End": {
"ColumnNumber": 19,
"LineNumber": 31
},
"Path": [
"Resources",
"BareRefParam",
"CreationPolicy"
],
"Start": {
"ColumnNumber": 5,
"LineNumber": 31
}
},
"Message": "{'Ref': 'P'} is not of type 'object'",
"ParentId": null,
"Rule": {
"Description": "Making sure the Ref has a String value (no other functions are supported)",
"Id": "E1020",
"ShortDescription": "Ref validation of value",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html"
}
},
{
"Filename": "test/fixtures/templates/integration/creationpolicy.yaml",
"Id": "3f4508fa-3fdd-6bec-6e01-d63493f18c72",
"Level": "Error",
"Location": {
"End": {
"ColumnNumber": 19,
"LineNumber": 34
},
"Path": [
"Resources",
"BareRefResource",
"CreationPolicy",
"Ref"
],
"Start": {
"ColumnNumber": 5,
"LineNumber": 34
}
},
"Message": "'ValidObject' is not one of ['P', 'SignalCount', 'AWS::AccountId', 'AWS::NoValue', 'AWS::NotificationARNs', 'AWS::Partition', 'AWS::Region', 'AWS::StackId', 'AWS::StackName', 'AWS::URLSuffix']",
"ParentId": null,
"Rule": {
"Description": "Making sure the Ref has a String value (no other functions are supported)",
"Id": "E1020",
"ShortDescription": "Ref validation of value",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html"
}
},
{
"Filename": "test/fixtures/templates/integration/creationpolicy.yaml",
"Id": "4b94035f-269f-152b-e2a2-c63b9cc36b69",
"Level": "Error",
"Location": {
"End": {
"ColumnNumber": 19,
"LineNumber": 37
},
"Path": [
"Resources",
"BareRefPseudo",
"CreationPolicy"
],
"Start": {
"ColumnNumber": 5,
"LineNumber": 37
}
},
"Message": "{'Ref': 'AWS::Region'} is not of type 'object'",
"ParentId": null,
"Rule": {
"Description": "Making sure the Ref has a String value (no other functions are supported)",
"Id": "E1020",
"ShortDescription": "Ref validation of value",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html"
}
},
{
"Filename": "test/fixtures/templates/integration/creationpolicy.yaml",
"Id": "701e011a-40e0-f891-2327-7f29a76110d7",
"Level": "Error",
"Location": {
"End": {
"ColumnNumber": 19,
"LineNumber": 40
},
"Path": [
"Resources",
"BareSub",
"CreationPolicy"
],
"Start": {
"ColumnNumber": 5,
"LineNumber": 40
}
},
"Message": "{'Fn::Sub': '${P}'} is not of type 'object'",
"ParentId": null,
"Rule": {
"Description": "Making sure the sub function is properly configured",
"Id": "E1019",
"ShortDescription": "Sub validation of parameters",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html"
}
}
]
30 changes: 30 additions & 0 deletions test/fixtures/results/integration/updatepolicy_yaml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"Filename": "test/fixtures/templates/integration/updatepolicy.yaml",
"Id": "9c312053-fd6c-cd5f-e0a6-acc1cd7985ff",
"Level": "Error",
"Location": {
"End": {
"ColumnNumber": 17,
"LineNumber": 49
},
"Path": [
"Resources",
"BareRef",
"UpdatePolicy"
],
"Start": {
"ColumnNumber": 5,
"LineNumber": 49
}
},
"Message": "{'Ref': 'P'} is not of type 'object'",
"ParentId": null,
"Rule": {
"Description": "Making sure the Ref has a String value (no other functions are supported)",
"Id": "E1020",
"ShortDescription": "Ref validation of value",
"Source": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html"
}
}
]
40 changes: 40 additions & 0 deletions test/fixtures/templates/integration/creationpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Parameters:
P:
Type: String
SignalCount:
Type: Number
Default: 1
Conditions:
IsProd: !Equals [!Ref "AWS::Region", "us-east-1"]
Resources:
# Valid: object literal with a nested Ref
ValidObject:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy:
ResourceSignal:
Count: !Ref SignalCount
Timeout: PT15M
# Valid: Fn::If resolving to objects
ValidIf:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy: !If
- IsProd
- ResourceSignal:
Count: 1
Timeout: PT15M
- ResourceSignal:
Count: 2
Timeout: PT15M
# Invalid: a bare intrinsic cannot stand in for the object
BareRefParam:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy: !Ref P
BareRefResource:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy: !Ref ValidObject
BareRefPseudo:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy: !Ref "AWS::Region"
BareSub:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy: !Sub "${P}"
56 changes: 56 additions & 0 deletions test/fixtures/templates/integration/updatepolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Parameters:
P:
Type: String
Batch:
Type: Number
Default: 1
Conditions:
IsProd: !Equals [!Ref "AWS::Region", "us-east-1"]
Resources:
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: ami-1234567890abcdef0
InstanceType: t3.micro
# Valid: object literal with a nested Ref
ValidObject:
Type: AWS::AutoScaling::AutoScalingGroup
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: !Ref Batch
MinInstancesInService: 0
Properties:
MinSize: "0"
MaxSize: "0"
AvailabilityZones: !GetAZs ""
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
# Valid: Fn::If resolving to objects
ValidIf:
Type: AWS::AutoScaling::AutoScalingGroup
UpdatePolicy: !If
- IsProd
- AutoScalingRollingUpdate:
MaxBatchSize: 1
- AutoScalingRollingUpdate:
MaxBatchSize: 2
Properties:
MinSize: "0"
MaxSize: "0"
AvailabilityZones: !GetAZs ""
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
# Invalid: a bare intrinsic cannot stand in for the object
BareRef:
Type: AWS::AutoScaling::AutoScalingGroup
UpdatePolicy: !Ref P
Properties:
MinSize: "0"
MaxSize: "0"
AvailabilityZones: !GetAZs ""
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
14 changes: 14 additions & 0 deletions test/integration/test_integration_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ class TestQuickStartTemplates(BaseCliTestCase):
),
"exit_code": 0,
},
{
"filename": "test/fixtures/templates/integration/creationpolicy.yaml",
"results_filename": (
"test/fixtures/results/integration/creationpolicy_yaml.json"
),
"exit_code": 2,
},
{
"filename": "test/fixtures/templates/integration/updatepolicy.yaml",
"results_filename": (
"test/fixtures/results/integration/updatepolicy_yaml.json"
),
"exit_code": 2,
},
]

def test_templates(self):
Expand Down
Loading
Loading