diff --git a/src/cfnlint/jsonschema/_keywords.py b/src/cfnlint/jsonschema/_keywords.py index 993756c145..3ca672e1ab 100644 --- a/src/cfnlint/jsonschema/_keywords.py +++ b/src/cfnlint/jsonschema/_keywords.py @@ -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( diff --git a/src/cfnlint/rules/functions/Ref.py b/src/cfnlint/rules/functions/Ref.py index d608fa8fc0..8450a1d78f 100644 --- a/src/cfnlint/rules/functions/Ref.py +++ b/src/cfnlint/rules/functions/Ref.py @@ -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 @@ -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: diff --git a/src/cfnlint/rules/resources/CreationPolicy.py b/src/cfnlint/rules/resources/CreationPolicy.py index 303a720331..879d1503c2 100644 --- a/src/cfnlint/rules/resources/CreationPolicy.py +++ b/src/cfnlint/rules/resources/CreationPolicy.py @@ -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 @@ -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), diff --git a/src/cfnlint/rules/resources/updatepolicy/Configuration.py b/src/cfnlint/rules/resources/updatepolicy/Configuration.py index f0ed7a2c36..fd087afd64 100644 --- a/src/cfnlint/rules/resources/updatepolicy/Configuration.py +++ b/src/cfnlint/rules/resources/updatepolicy/Configuration.py @@ -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 @@ -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, diff --git a/test/fixtures/results/integration/creationpolicy_yaml.json b/test/fixtures/results/integration/creationpolicy_yaml.json new file mode 100644 index 0000000000..121c43918a --- /dev/null +++ b/test/fixtures/results/integration/creationpolicy_yaml.json @@ -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" + } + } +] diff --git a/test/fixtures/results/integration/updatepolicy_yaml.json b/test/fixtures/results/integration/updatepolicy_yaml.json new file mode 100644 index 0000000000..d718b6bfd5 --- /dev/null +++ b/test/fixtures/results/integration/updatepolicy_yaml.json @@ -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" + } + } +] diff --git a/test/fixtures/templates/integration/creationpolicy.yaml b/test/fixtures/templates/integration/creationpolicy.yaml new file mode 100644 index 0000000000..2fc9771b13 --- /dev/null +++ b/test/fixtures/templates/integration/creationpolicy.yaml @@ -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}" diff --git a/test/fixtures/templates/integration/updatepolicy.yaml b/test/fixtures/templates/integration/updatepolicy.yaml new file mode 100644 index 0000000000..5d02f46d0c --- /dev/null +++ b/test/fixtures/templates/integration/updatepolicy.yaml @@ -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 diff --git a/test/integration/test_integration_templates.py b/test/integration/test_integration_templates.py index 844aebff59..dc124295e4 100644 --- a/test/integration/test_integration_templates.py +++ b/test/integration/test_integration_templates.py @@ -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): diff --git a/test/unit/rules/functions/test_ref.py b/test/unit/rules/functions/test_ref.py index 9fe1c32738..c39a91c3d3 100644 --- a/test/unit/rules/functions/test_ref.py +++ b/test/unit/rules/functions/test_ref.py @@ -138,6 +138,56 @@ def context(cfn): ), ], ), + ( + "Pseudo parameter (string) is not an object", + {"Ref": "AWS::Region"}, + {"type": "object"}, + {}, + [ + ValidationError( + "{'Ref': 'AWS::Region'} is not of type 'object'", + ), + ], + ), + ( + "Pseudo parameter (list) does not satisfy a scalar schema", + {"Ref": "AWS::NotificationARNs"}, + {"type": "string"}, + {}, + [ + ValidationError( + "{'Ref': 'AWS::NotificationARNs'} is not of type 'string'", + ), + ], + ), + ( + "Pseudo parameter (string) satisfies a string schema", + {"Ref": "AWS::Region"}, + {"type": "string"}, + {}, + [], + ), + ( + "Pseudo parameter (list) satisfies an array schema", + {"Ref": "AWS::NotificationARNs"}, + {"type": "array"}, + {}, + [], + ), + ( + "Pseudo parameter with no schema type is not validated", + {"Ref": "AWS::Region"}, + {}, + {}, + [], + ), + ( + "Pseudo parameter (string) satisfies a string/array schema", + {"Ref": "AWS::Region"}, + {"type": ["string", "array"]}, + {}, + [], + ), ], ) def test_validate(name, instance, schema, context_evolve, expected, rule, context, cfn):