From b355e0ada43b28214c49cc0572b6c47d726b40a8 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Wed, 10 Nov 2021 12:01:55 -0100 Subject: [PATCH] CloudFormation - verify shorthand Yaml-functions (#4556) --- moto/cloudformation/parsing.py | 4 +- .../test_cloudformation_stack_crud_boto3.py | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/moto/cloudformation/parsing.py b/moto/cloudformation/parsing.py index 5dbec31d8..bcd9eae30 100644 --- a/moto/cloudformation/parsing.py +++ b/moto/cloudformation/parsing.py @@ -378,12 +378,14 @@ def parse_condition(condition, resources_map, condition_map): condition_values = [] for value in list(condition.values())[0]: # Check if we are referencing another Condition - if "Condition" in value: + if isinstance(value, dict) and "Condition" in value: condition_values.append(condition_map[value["Condition"]]) else: condition_values.append(clean_json(value, resources_map)) if condition_operator == "Fn::Equals": + if condition_values[1] in [True, False]: + return str(condition_values[0]).lower() == str(condition_values[1]).lower() return condition_values[0] == condition_values[1] elif condition_operator == "Fn::Not": return not parse_condition(condition_values[0], resources_map, condition_map) diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py index 59bb012c0..ea2341ff5 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py +++ b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py @@ -113,6 +113,26 @@ Resources: Value: Name tag for tests """ +dummy_yaml_template_with_equals = """--- +AWSTemplateFormatVersion: 2010-09-09 +Description: Stack with yaml template +Conditions: + maybe: + Fn::Equals: [!Ref enabled, true] +Parameters: + enabled: + Type: String + AllowedValues: + - true + - false +Resources: + VPC1: + Type: AWS::EC2::VPC + Condition: maybe + Properties: + CidrBlock: 192.168.0.0/16 +""" + dummy_template_yaml_with_ref = """--- AWSTemplateFormatVersion: 2010-09-09 Description: Stack1 with yaml template @@ -1851,6 +1871,42 @@ def test_cloudformation_params_conditions_and_resources_are_distinct(): ] +@mock_cloudformation +@mock_ec2 +def test_cloudformation_conditions_yaml_equals(): + cf = boto3.client("cloudformation", region_name="us-east-1") + cf.create_stack( + StackName="teststack2", + TemplateBody=dummy_yaml_template_with_equals, + Parameters=[{"ParameterKey": "enabled", "ParameterValue": "true"}], + ) + resources = cf.list_stack_resources(StackName="teststack2")[ + "StackResourceSummaries" + ] + assert [ + resource for resource in resources if resource["LogicalResourceId"] == "VPC1" + ] + + +@mock_cloudformation +@mock_ec2 +def test_cloudformation_conditions_yaml_equals_shortform(): + _template = dummy_yaml_template_with_equals + _template = _template.replace("Fn::Equals:", "!Equals") + cf = boto3.client("cloudformation", region_name="us-east-1") + cf.create_stack( + StackName="teststack2", + TemplateBody=_template, + Parameters=[{"ParameterKey": "enabled", "ParameterValue": "true"}], + ) + resources = cf.list_stack_resources(StackName="teststack2")[ + "StackResourceSummaries" + ] + assert [ + resource for resource in resources if resource["LogicalResourceId"] == "VPC1" + ] + + @mock_cloudformation def test_stack_tags(): tags = [{"Key": "foo", "Value": "bar"}, {"Key": "baz", "Value": "bleh"}]