Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(cloudformation): parameter constraints, unknown AWS::CloudFormation::* types, stack id lookups (#1648)
iot-rocket178d97373cb4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex eeb8c27f..1ea92612 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,10 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **CloudFormation — parameter constraints are enforced** — `AllowedPattern`, `MinLength`, `MaxLength`, `MinValue` and `MaxValue` were ignored (only `AllowedValues` was checked), so a value a real account refuses deployed locally. They are checked before a stack exists, with CloudFormation's message (`Parameter 'P' must match pattern ^[a-z]+$`, measured) and a `ConstraintDescription` in place of the reason; a `CommaDelimitedList` is matched per member and a `List<Number>` is validated as numbers. Contributed by @iot-rocket.+- **CloudFormation — an unregistered `AWS::CloudFormation::*` type is unrecognized, not a silent no-op** — `AWS::CloudFormation::Macro`, `HookVersion`, `ModuleVersion` and any typo under that prefix deployed as a placeholder with a fabricated physical id, logged at INFO, while the README promises that unsupported types do not silently succeed. The pre-flight now treats them like every other unrecognized type (`Template format error: Unrecognized resource types`); the registered ones — `WaitCondition`, `WaitConditionHandle`, `Stack`, `CustomResource` — are unaffected. Contributed by @iot-rocket.+- **CloudFormation — the stack id addresses `GetTemplateSummary` and an UPDATE change set, and the Describe*Resource responses carry the stack name** — `GetTemplateSummary(StackName=<id>)` and `CreateChangeSet` on an existing stack looked the id up by name and answered "does not exist" while every other action accepted it; `DescribeStackResource` and `DescribeStackResources` answered a request by id with the id in `StackName`. Contributed by @iot-rocket.### Added- **CloudFormation — dynamic references resolve** — `{{resolve:ssm:...}}`, `{{resolve:ssm-secure:...}}` and `{{resolve:secretsmanager:...}}` were refused up front (before that, the literal reached the service). They now resolve at provisioning time against the in-process stores — a parameter by name and optional version, a secret by name or ARN with the optional `SecretString`, JSON key, version stage or version id segments — and `GetTemplate` keeps the literal. On update the two kinds follow what a real account does: an `ssm` reference re-resolves when the stack is updated with a changed template or parameters (an identical template is accepted and changes nothing; `UsePreviousTemplate` is still refused with "No updates are to be performed."), a `secretsmanager` reference only when the resource that carries it changes. A reference that cannot be resolved fails the resource; a reference to any other service is refused before a stack exists. Contributed by @iot-rocket.### Fixeddiff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.pyindex f1aa793d..301ffb35 100644--- a/ministack/services/cloudformation/changesets.py+++ b/ministack/services/cloudformation/changesets.py@@ -77,6 +77,7 @@ def _resolve_props_for_diff(template, params, stack_name, stack_id):def _create_change_set(params):from ministack.services.cloudformation import _change_sets, _stack_events, _stacks+ from ministack.services.cloudformation.handlers import _resolve_stackstack_name = _p(params, "StackName")cs_name = _p(params, "ChangeSetName")cs_type = _p(params, "ChangeSetType", "UPDATE")@@ -102,7 +103,11 @@ def _create_change_set(params):provided_params = _extract_members(params, "Parameters")tags = _extract_members(params, "Tags")- stack = _stacks.get(stack_name)+ stack = _resolve_stack(stack_name)+ if stack is not None and cs_type != "CREATE":+ # A change set is keyed by the stack's name; an UPDATE set addressed+ # by stack id carries on under the name.+ stack_name = stack.get("StackName", stack_name)if cs_type == "CREATE":if stack and stack.get("StackStatus") not in (diff --git a/ministack/services/cloudformation/engine.py b/ministack/services/cloudformation/engine.pyindex 320c4a37..155ebc7f 100644--- a/ministack/services/cloudformation/engine.py+++ b/ministack/services/cloudformation/engine.py@@ -267,11 +267,11 @@ def validate_template_support(template: dict, conditions: dict) -> None:if cond and not conditions.get(cond, True):continuertype = res.get("Type", "AWS::CloudFormation::CustomResource")- if (- rtype in _RESOURCE_HANDLERS- or rtype.startswith("Custom::")- or rtype.startswith("AWS::CloudFormation::")- ):+ # The registered AWS::CloudFormation::* types (WaitCondition,+ # WaitConditionHandle, Stack, CustomResource) pass like any other+ # handler; an unregistered one (Macro, HookVersion, a typo) is+ # unrecognized, not a silent placeholder.+ if rtype in _RESOURCE_HANDLERS or rtype.startswith("Custom::"):continueunrecognized.add(rtype)if unrecognized:@@ -324,6 +324,47 @@ _AWS_SPECIFIC_TYPES = {_SSM_PARAMETER_VALUE_PREFIX = "AWS::SSM::Parameter::Value<"+def _constraint_error(name: str, defn: dict, reason: str) -> ValueError:+ """The ValidationError a violated parameter constraint raises: real+ CloudFormation answers ``Parameter 'P' must match pattern ^[a-z]+$`` (measured),+ and a ``ConstraintDescription`` replaces the generic reason."""+ description = defn.get("ConstraintDescription")+ return ValueError(f"Parameter '{name}' {description or reason}")+++def _check_parameter_constraints(name: str, defn: dict, ptype: str, value: str) -> None:+ """Apply AllowedPattern, MinLength, MaxLength, MinValue and MaxValue the way+ the Parameters reference defines them: the pattern matches the whole value+ (each member of a CommaDelimitedList), the lengths apply to String types,+ the bounds to Number types (each member of a List<Number>)."""+ is_list = ptype in ("CommaDelimitedList", "List<Number>")+ members = [m.strip() for m in value.split(",")] if is_list else [value]+ pattern = defn.get("AllowedPattern")+ if pattern is not None and ptype != "Number":+ try:+ compiled = re.compile(str(pattern))+ except re.error as exc:+ raise ValueError(+ f"Parameter '{name}' has an invalid AllowedPattern: {exc}") from None+ if not all(compiled.fullmatch(m) for m in members):+ raise _constraint_error(name, defn, f"must match pattern {pattern}")+ if ptype in ("Number", "List<Number>"):+ numbers = [float(m) for m in members]+ if "MinValue" in defn and any(n < float(defn["MinValue"]) for n in numbers):+ raise _constraint_error(+ name, defn, f"must be a number not less than {defn['MinValue']}")+ if "MaxValue" in defn and any(n > float(defn["MaxValue"]) for n in numbers):+ raise _constraint_error(+ name, defn, f"must be a number not greater than {defn['MaxValue']}")+ elif not is_list:+ if "MinLength" in defn and len(value) < int(defn["MinLength"]):+ raise _constraint_error(+ name, defn, f"must contain at least {defn['MinLength']} characters")+ if "MaxLength" in defn and len(value) > int(defn["MaxLength"]):+ raise _constraint_error(+ name, defn, f"must contain at most {defn['MaxLength']} characters")++def _resolve_parameters(template: dict, provided_params: list[dict],previous_params: dict | None = None) -> dict:"""Resolve template parameters with provided values and defaults.@@ -417,11 +458,20 @@ def _resolve_parameters(template: dict, provided_params: list[dict],float(value)except ValueError:raise ValueError(f"Parameter '{name}' value '{value}' is not a valid Number")+ elif ptype == "List<Number>":+ for member in value.split(","):+ try:+ float(member.strip())+ except ValueError:+ raise ValueError(+ f"Parameter '{name}' value '{value}' is not a valid List<Number>")elif ptype == "CommaDelimitedList":# Keep as string; Fn::Select will splitpass# AWS-specific types treated as String -- no extra validation+ _check_parameter_constraints(name, defn, ptype, value)+out = {"Value": value, "NoEcho": no_echo}if ssm_name is not None:# Persisted so a later UpdateStack with UsePreviousValue re-resolvesdiff --git a/ministack/services/cloudformation/handlers.py b/ministack/services/cloudformation/handlers.pyindex a38dc61c..e14e9f8f 100644--- a/ministack/services/cloudformation/handlers.py+++ b/ministack/services/cloudformation/handlers.py@@ -333,6 +333,7 @@ def _describe_stack_resource(params):if not stack:return _error("ValidationError",f"Stack [{stack_name}] does not exist")+ stack_name = stack.get("StackName", stack_name)resources = stack.get("_resources", {})res = resources.get(logical_id)@@ -367,6 +368,7 @@ def _describe_stack_resources(params):if not stack:return _error("ValidationError",f"Stack [{stack_name}] does not exist")+ stack_name = stack.get("StackName", stack_name)resources = stack.get("_resources", {})@@ -767,8 +769,8 @@ def _get_template_summary(params):stack_name = _p(params, "StackName")if stack_name and not template_body:- stack = _stacks.get(stack_name)- if not stack:+ stack = _resolve_stack(stack_name)+ if not stack or stack.get("StackStatus") == "DELETE_COMPLETE":return _error("ValidationError",f"Stack [{stack_name}] does not exist")template_body = stack.get("_template_body", "{}")diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 2631adc8..8e4ad17d 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -1159,6 +1159,64 @@ def test_cfn_stack_with_parameters(cfn, sqs):assert any("cfn-t02-custom" in u for u in urls)+def test_cfn_parameter_constraints_are_enforced(cfn):+ """AllowedPattern, MinLength, MaxLength, MinValue and MaxValue are checked+ before a stack exists, with CloudFormation's message (measured:+ ``Parameter 'P' must match pattern ^[a-z]+$``); a ConstraintDescription+ replaces the reason; a CommaDelimitedList is checked per member."""+ uid = _uuid_mod.uuid4().hex[:8]++ def template(params):+ return json.dumps({"Parameters": params, "Resources": {"P": {+ "Type": "AWS::SSM::Parameter",+ "Properties": {"Name": f"/cfn-constraints/{uid}", "Type": "String",+ "Value": {"Ref": next(iter(params))}}}}})++ def refused(params, value, expected):+ name = f"cfn-constraints-{uid}-{_uuid_mod.uuid4().hex[:4]}"+ key = next(iter(params))+ with pytest.raises(ClientError) as exc:+ cfn.create_stack(StackName=name, TemplateBody=template(params),+ Parameters=[{"ParameterKey": key, "ParameterValue": value}])+ assert exc.value.response["Error"]["Code"] == "ValidationError"+ assert exc.value.response["Error"]["Message"] == expected+ with pytest.raises(ClientError):+ cfn.describe_stacks(StackName=name)++ refused({"P": {"Type": "String", "AllowedPattern": "^[a-z]+$", "MinLength": "3",+ "MaxLength": "3"}}, "ABC1", "Parameter 'P' must match pattern ^[a-z]+$")+ refused({"P": {"Type": "String", "AllowedPattern": "[a-z]+", "ConstraintDescription":+ "must be lowercase letters"}}, "abc1", "Parameter 'P' must be lowercase letters")+ refused({"P": {"Type": "String", "MinLength": "3"}}, "ab",+ "Parameter 'P' must contain at least 3 characters")+ refused({"P": {"Type": "String", "MaxLength": "3"}}, "abcd",+ "Parameter 'P' must contain at most 3 characters")+ refused({"N": {"Type": "Number", "MinValue": "1", "MaxValue": "10"}}, "0",+ "Parameter 'N' must be a number not less than 1")+ refused({"N": {"Type": "Number", "MinValue": "1", "MaxValue": "10"}}, "11",+ "Parameter 'N' must be a number not greater than 10")+ refused({"L": {"Type": "CommaDelimitedList", "AllowedPattern": "[a-z]+"}}, "ab, c1",+ "Parameter 'L' must match pattern [a-z]+")+ refused({"L": {"Type": "List<Number>"}}, "1,x",+ "Parameter 'L' value '1,x' is not a valid List<Number>")+ refused({"L": {"Type": "List<Number>", "MinValue": "1"}}, "1,0",+ "Parameter 'L' must be a number not less than 1")+ refused({"P": {"Type": "String", "AllowedPattern": "["}}, "abc",+ "Parameter 'P' has an invalid AllowedPattern: unterminated character set at position 0")++ # A Default that satisfies the constraints and a valid override both pass.+ name = f"cfn-constraints-ok-{uid}"+ params = {"P": {"Type": "String", "Default": "abc", "AllowedPattern": "^[a-z]+$",+ "MinLength": "1", "MaxLength": "5"}}+ cfn.create_stack(StackName=name, TemplateBody=template(params),+ Parameters=[{"ParameterKey": "P", "ParameterValue": "hello"}])+ try:+ stack = _wait_stack(cfn, name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ finally:+ _delete_cfn_test_stack(cfn, name)++def test_cfn_unnamed_dynamodb_table_survives_unrelated_update(cfn, ddb, ssm):"""A stack update must not touch an auto-named resource whose ownproperties didn't change — DynamoDB::Table has no update handler, so it@@ -2218,6 +2276,42 @@ def test_cfn_describe_stack_resources_logical_id_filter(cfn, s3, sqs):assert exc_info.value.response["Error"]["Code"] == "ValidationError"+def test_cfn_stack_id_addresses_every_read_action(cfn, sqs):+ """Every action that takes a StackName accepts the stack id (what the CDK+ sends after its first DescribeStacks), and the responses carry the stack's+ name, not the id that was passed."""+ uid = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-by-id-{uid}"+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps({"Resources": {+ "Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": f"cfn-by-id-{uid}"}}}}))+ try:+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ stack_id = stack["StackId"]++ assert cfn.describe_stacks(StackName=stack_id)["Stacks"][0]["StackName"] == stack_name+ detail = cfn.describe_stack_resource(StackName=stack_id, LogicalResourceId="Queue")+ assert detail["StackResourceDetail"]["StackName"] == stack_name+ resources = cfn.describe_stack_resources(StackName=stack_id)["StackResources"]+ assert [r["StackName"] for r in resources] == [stack_name]+ summaries = cfn.list_stack_resources(StackName=stack_id)["StackResourceSummaries"]+ assert [r["LogicalResourceId"] for r in summaries] == ["Queue"]+ assert cfn.get_template(StackName=stack_id)["TemplateBody"]+ assert cfn.describe_stack_events(StackName=stack_id)["StackEvents"]+ assert cfn.get_template_summary(StackName=stack_id)["ResourceTypes"] == ["AWS::SQS::Queue"]++ cfn.create_change_set(StackName=stack_id, ChangeSetName="by-id", ChangeSetType="UPDATE",+ TemplateBody=json.dumps({"Resources": {"Queue": {+ "Type": "AWS::SQS::Queue", "Properties": {+ "QueueName": f"cfn-by-id-{uid}",+ "VisibilityTimeout": 45}}}}))+ described = cfn.describe_change_set(ChangeSetName="by-id", StackName=stack_name)+ assert described["StackName"] == stack_name+ assert described["Status"] == "CREATE_COMPLETE", described.get("StatusReason")+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++def test_cfn_yaml_template(cfn, s3):yaml_body = """AWSTemplateFormatVersion: '2010-09-09'@@ -11559,6 +11653,32 @@ def test_cfn_unrecognized_type_behind_false_condition_is_fine(cfn):_wait_stack(cfn, name)+def test_cfn_unknown_cloudformation_type_is_rejected_like_any_other(cfn):+ """An unregistered AWS::CloudFormation::* type (a Macro, a typo) is+ 'Unrecognized resource types', not a silent no-op placeholder; the+ registered ones (WaitConditionHandle) still deploy."""+ name = "cfn-preflight-cfn-types"+ bad = json.dumps({"Resources": {+ "Handle": {"Type": "AWS::CloudFormation::WaitConditionHandle"},+ "Typo": {"Type": "AWS::CloudFormation::DoesNotExist", "Properties": {}},+ }})+ with pytest.raises(ClientError) as exc:+ cfn.create_stack(StackName=name, TemplateBody=bad)+ assert exc.value.response["Error"]["Message"] == (+ "Template format error: Unrecognized resource types: "+ "[AWS::CloudFormation::DoesNotExist]")+ with pytest.raises(ClientError):+ cfn.describe_stacks(StackName=name)++ good = json.dumps({"Resources": {+ "Handle": {"Type": "AWS::CloudFormation::WaitConditionHandle"}}})+ cfn.create_stack(StackName=name, TemplateBody=good)+ try:+ assert _wait_stack(cfn, name)["StackStatus"] == "CREATE_COMPLETE"+ finally:+ _delete_cfn_test_stack(cfn, name)++def test_cfn_validate_template_accepts_a_sam_template(cfn):"""A template that declares a Transform is exempt from the unrecognizedtype check: a macro can rewrite any resource, so real CloudFormation does