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
feat(cloudformation): report capabilities, refuse unknown values, check nested stacks under AUTH=true (#1700)
iot-rocketbc3f41e2c417
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cf5865db..b7772b8a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,10 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Added+- **CloudFormation — `ValidateTemplate` reports the capabilities and transforms it found** — it answered `Description` and `Parameters` only, so a caller checking a template before a deploy could not see that it needs `CAPABILITY_IAM` or that it declares a transform. It now returns `Capabilities`, `CapabilitiesReason` and `DeclaredTransforms` as the API documents, using the same rule `GetTemplateSummary` already applied. Contributed by @iot-rocket.+- **CloudFormation — a stack and a change set report their capabilities** — `DescribeStacks` and `DescribeChangeSet` left the `Capabilities` member out, so a client could not see what a deploy had acknowledged. Both report them now: a stack carries what its last `CreateStack` or `UpdateStack` acknowledged, an executed change set makes its own set the stack's, and a change set reports what it was created with. Contributed by @iot-rocket.+### Fixed- **S3 — the `s3:TestEvent` no longer reaches Lambda targets** — `PutBucketNotificationConfiguration` fanned the test event out to every destination, Lambda included, but AWS sends it to SQS queues and SNS topics only; a Lambda destination is verified through its function permissions, not by invoking it. The payload carries no `Records` array, so each S3-triggered function raised on it, and because delivery goes through the async path the event was retried to `MaximumRetryAttempts` (default 2) and then routed to the function's DLQ or `OnFailure` destination, where a synthetic event AWS never sends looked like a genuinely dropped message. Any CDK stack with an S3-triggered Lambda hit this through `BucketNotificationsHandler`. Queue and topic destinations still receive the test event, and real object events still invoke the function. Contributed by @ppettitau.### Changed@@ -17,6 +21,11 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **Transcribe — `StartTranscriptionJob` returns a job already `IN_PROGRESS`** — the start response reported `QUEUED` with no `StartTime`, and the job reached `IN_PROGRESS` only from the background worker, after the response had been sent. A caller that reads the status off the start response and then waits for the terminal `Transcribe Job State Change` event, rather than polling, therefore never saw an in-progress state at all: the job went straight from queued to complete. Jobs now start `IN_PROGRESS` with `StartTime` set, matching the response the AWS CLI walkthrough documents, and `TRANSCRIBE_JOB_RUN_SECONDS` covers the whole run rather than half of it. `QUEUED`, which AWS reaches only for a request that opted into job queueing through `JobExecutionSettings.AllowDeferredExecution` while the account is at its concurrent job limit, stays reachable through the new `TRANSCRIBE_JOB_QUEUE_SECONDS` (default `0`); both knobs are settable at runtime through `/_ministack/config`. Contributed by @ppettitau.+- **CloudFormation — a nested stack's IAM resources need the parent's capabilities** — the check under `AUTH=true` read the parent template only, so a parent deployed without `--capabilities` provisioned a child full of IAM roles through `AWS::CloudFormation::Stack`. The child template is now checked against the set the parent acknowledged when it is read, and a child's child reads the same set, as AWS asks for the capabilities on the parent for every level of nesting; a missing one fails the nested-stack resource with the message of `InsufficientCapabilitiesException` (`Requires capabilities : [CAPABILITY_IAM]`) and the parent rolls back unless rollback is disabled. Under `AUTH=true` the child's record carries the parent's set, so `DescribeStacks` reports it there too; without `AUTH` nothing is recorded and nothing changes. Contributed by @iot-rocket.++- **CloudFormation — the SAM transform reads the transforms the response reports** — `ValidateTemplate` normalises the `Transform` section in every form it takes, but the deploy path had its own normalisation that left the macro object (`Transform: {Name: AWS::Serverless-2016-10-31}`) in the list, so a template written that way was reported as declaring the SAM transform and then deployed untransformed: its `AWS::Serverless::Function` resources stayed as they were and the stack failed on an unsupported type. Both read one function now, so the response and the deploy agree about what a template declares. Contributed by @iot-rocket.++- **CloudFormation — an unknown `Capabilities` value is refused** — `CreateStack`, `UpdateStack` and `CreateChangeSet` accepted any string, so a typo or a capability of another tool (`CAPABILITY_RESOURCE_POLICY`, which the CloudFormation API does not define) was kept without effect and the deploy behaved as if it had been acknowledged. The member is now checked against the three documented values with the API's parameter validation message, joined with the other request-level problems. Contributed by @iot-rocket.### Fixed- **CloudFormation — the template and stack quotas are enforced** — a template of any size, with any number of resources, parameters, outputs or mappings, and a stack name of any shape were accepted, so a template that a real account refuses deployed. `CreateStack`, `UpdateStack`, `CreateChangeSet`, `ValidateTemplate` and `GetTemplateSummary` now refuse a `TemplateBody` over 51,200 bytes and a template behind `TemplateURL` over 1,000,000 bytes; the engine refuses more than 500 resources, 200 parameters, 200 outputs, 200 mappings or 200 attributes in a mapping, a name over 255 characters, a description over 1,024 bytes and a parameter default or value over 4,096 bytes; `CreateStack` and a `CREATE` change set refuse a stack name that is not alphanumeric-and-hyphens starting with a letter or longer than 128 characters, joined with an oversized body into the one message the API's parameter validation gives. All of it before a stack record exists, with the messages of the API's parameter validation and the template format errors. `ValidateTemplate` also takes `TemplateURL` now. Contributed by @iot-rocket.- **Lambda — a Node custom resource signals its stack in the docker executor** — the response submitters the CDK bundles into its custom-resource handlers (`nodejs-entrypoint`, the provider framework, `AwsCustomResource`) build the `ResponseURL` PUT from the URL's hostname and path only and hand it to `https.request`, so under `LAMBDA_EXECUTOR=docker` the callback went out over TLS to port 443, nothing answered, and an `AwsCustomResource`-backed resource ran its API call and then hung its stack until `ServiceTimeout` (the local executor already downgraded https for the gateway). The shim the docker executor injects now turns https to the gateway hosts (the endpoint host, `host.docker.internal`, `localhost` and `MINISTACK_HOST`) on the https default port or the gateway port into plain http on the gateway port; other hosts, and those hosts on other ports, keep TLS. The local executor's own list gains `MINISTACK_HOST` as well. Contributed by @iot-rocket.diff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.pyindex 66eae044..bcb826f1 100644--- a/ministack/services/cloudformation/changesets.py+++ b/ministack/services/cloudformation/changesets.py@@ -21,6 +21,7 @@ from .helpers import (_error,_esc,_extract_members,+ _extract_string_members,_p,_page,_request_problems,@@ -266,6 +267,7 @@ def _create_change_set(params):for k, v in param_values.items()],"Tags": tags,+ "Capabilities": _extract_string_members(params, "Capabilities"),"_tags_given": tags_given,"_template": template,"_template_body": template_body,@@ -352,6 +354,9 @@ def _describe_change_set(params):f"<CreationTime>{cs['CreationTime']}</CreationTime>"f"<Description>{_esc(cs.get('Description', ''))}</Description>"f"<ChangeSetType>{cs.get('ChangeSetType', '')}</ChangeSetType>"+ "<Capabilities>"+ + "".join(f"<member>{_esc(c)}</member>" for c in cs.get("Capabilities", []))+ + "</Capabilities>"f"<Parameters>{params_xml}</Parameters>"f"<Changes>{changes_xml}</Changes>"f"<Tags>{tags_xml}</Tags>"@@ -427,6 +432,9 @@ def _execute_change_set(params):stack["StackStatus"] = f"{status_prefix}_IN_PROGRESS"stack["LastUpdatedTime"] = now_iso()stack["_template_body"] = template_body+ # The stack reports what the operation acknowledged, and for an executed+ # change set that is what the change set was created with.+ stack["Capabilities"] = list(cs.get("Capabilities", []))if tags or cs.get("_tags_given"):stack["Tags"] = tagsstack["Parameters"] = [diff --git a/ministack/services/cloudformation/engine.py b/ministack/services/cloudformation/engine.pyindex 81ae9d0b..271b1186 100644--- a/ministack/services/cloudformation/engine.py+++ b/ministack/services/cloudformation/engine.py@@ -1557,6 +1557,25 @@ logging.getLogger("samtranslator.feature_toggle.feature_toggle").setLevel(loggin_NO_IAM_POLICY_LOADER = SimpleNamespace(load=lambda: {})+def declared_transforms(template: dict) -> list[str]:+ """The transform names a template declares at the top level, in every+ form the section takes: a string, the macro object with a ``Name`` (the+ shape the AWS::Include reference documents at the top level), or a list+ of either. Anything else is dropped. ValidateTemplate reports this list+ and the SAM transform is looked up in it, so the two cannot disagree+ about what a template declares.+ """+ declared = template.get("Transform")+ entries = declared if isinstance(declared, list) else [declared]+ names = []+ for entry in entries:+ if isinstance(entry, str):+ names.append(entry)+ elif isinstance(entry, dict) and isinstance(entry.get("Name"), str):+ names.append(entry["Name"])+ return names++def _apply_sam_transform_if_applicable(template: dict) -> dict:"""The transforms a stack operation applies before validation: everyembedded ``AWS::Include`` first, then the SAM transform when the template@@ -1564,9 +1583,7 @@ def _apply_sam_transform_if_applicable(template: dict) -> dict:template = _apply_include_transform(template)if isinstance(template, _IncludeFailed):return template- declared = template.get("Transform")- transforms = declared if isinstance(declared, list) else [declared]- if _SAM_TRANSFORM not in transforms:+ if _SAM_TRANSFORM not in declared_transforms(template):return templatetry:diff --git a/ministack/services/cloudformation/handlers.py b/ministack/services/cloudformation/handlers.pyindex 69ff8cf5..a4cb2260 100644--- a/ministack/services/cloudformation/handlers.py+++ b/ministack/services/cloudformation/handlers.py@@ -26,6 +26,7 @@ from .engine import (_parse_template,_resolve_parameters,_resolve_refs,+ declared_transforms,validate_template_support,)from .helpers import (@@ -159,6 +160,24 @@ def _required_capabilities(template, strict=False):return capabilities, reason_types+def _capabilities_xml(template):+ """The ``Capabilities`` / ``CapabilitiesReason`` pair that+ GetTemplateSummary and ValidateTemplate both report, or an empty string+ when the template needs none (AWS omits both elements then)."""+ capabilities, reason_types = _required_capabilities(template)+ if not capabilities:+ return ""+ caps_xml = "".join(f"<member>{c}</member>" for c in capabilities)+ # AWS'es behavior here is very inconsistent with their docs. AWS doesn't necessarily return+ # all of the types it should every time. We're doing the best we can here.+ reason = (+ "The following resource(s) require capabilities: [" + ", ".join(reason_types) + "]"+ if reason_types else ""+ )+ return (f"<Capabilities>{caps_xml}</Capabilities>"+ f"<CapabilitiesReason>{_esc(reason)}</CapabilitiesReason>")++def _check_capabilities(sent, template, params, macros=True):"""Refuse a template whose required capabilities the request does notacknowledge, the way CreateStack does: HTTP 400@@ -181,20 +200,39 @@ def _check_capabilities(sent, template, params, macros=True):if not AUTH:return Nonegiven = set(_extract_string_members(params, "Capabilities"))- required = [cap for cap in _required_capabilities(template, strict=True)[0]- if cap != "CAPABILITY_AUTO_EXPAND"]+ required = _required_iam_capabilities(template)if macros and _uses_macro(sent):required.append("CAPABILITY_AUTO_EXPAND")+ missing = _missing_capabilities(given, required)+ if not missing:+ return None+ return _error("InsufficientCapabilitiesException", _insufficient_capabilities_message(missing))+++def _required_iam_capabilities(template):+ """The capabilities a template's IAM resources demand, which is the rule+ both the request-level check and the nested-stack check enforce.+ CAPABILITY_AUTO_EXPAND is dropped here: a transform is the caller's own+ declaration, judged per call site, not a property of the IAM resources.+ """+ return [cap for cap in _required_capabilities(template, strict=True)[0]+ if cap != "CAPABILITY_AUTO_EXPAND"]+++def _missing_capabilities(given, required):+ """The capabilities of ``required`` that ``given`` does not acknowledge;+ ``CAPABILITY_IAM`` is satisfied by ``CAPABILITY_NAMED_IAM`` as well."""missing = []for cap in required:if cap == "CAPABILITY_IAM" and "CAPABILITY_NAMED_IAM" in given:continueif cap not in given:missing.append(cap)- if not missing:- return None- return _error("InsufficientCapabilitiesException",- "Requires capabilities : [" + ", ".join(missing) + "]")+ return missing+++def _insufficient_capabilities_message(missing):+ return "Requires capabilities : [" + ", ".join(missing) + "]"# --- CreateStack ---@@ -285,6 +323,7 @@ def _create_stack(params):for k, v in param_values.items()],"Tags": tags,+ "Capabilities": _extract_string_members(params, "Capabilities"),"Outputs": [],"DisableRollback": disable_rollback,"EnableTerminationProtection": termination_protection,@@ -409,6 +448,9 @@ def _describe_stacks(params):"</member>")+ caps_xml = "".join(+ f"<member>{_esc(c)}</member>" for c in s.get("Capabilities", []))+members += ("<member>"f"<StackName>{_esc(s['StackName'])}</StackName>"@@ -422,6 +464,7 @@ def _describe_stacks(params):"<EnableTerminationProtection>"f"{str(s.get('EnableTerminationProtection', False)).lower()}""</EnableTerminationProtection>"+ f"<Capabilities>{caps_xml}</Capabilities>"f"<Parameters>{params_xml}</Parameters>"f"<Outputs>{outputs_xml}</Outputs>"f"<Tags>{tags_xml}</Tags>"@@ -932,6 +975,9 @@ def _update_stack(params):stack["StackStatus"] = "UPDATE_IN_PROGRESS"stack["LastUpdatedTime"] = now_iso()stack["_template_body"] = template_body+ # The capabilities a stack reports are the ones its last operation+ # acknowledged, so an update replaces them rather than adding to them.+ stack["Capabilities"] = _extract_string_members(params, "Capabilities")if tags or tags_given:stack["Tags"] = tagsstack["Parameters"] = [@@ -1004,10 +1050,17 @@ def _validate_template(params):"</member>")+ transforms_xml = "".join(+ f"<member>{_esc(t)}</member>" for t in declared_transforms(template))+ declared_block = (f"<DeclaredTransforms>{transforms_xml}</DeclaredTransforms>"+ if transforms_xml else "")+return _xml(200, "ValidateTemplateResponse",f"<ValidateTemplateResult>"f"<Description>{_esc(description)}</Description>"f"<Parameters>{params_xml}</Parameters>"+ f"{_capabilities_xml(template)}"+ f"{declared_block}"f"</ValidateTemplateResult>")@@ -1081,21 +1134,7 @@ def _get_template_summary(params):"</member>")- capabilities, caps_reason_types = _required_capabilities(template)-- caps_xml = "".join(f"<member>{c}</member>" for c in capabilities)- # AWS'es behavior here is very inconsistent with their docs. AWS doesn't necessarily return- # all of the types it should every time. We're doing the best we can here.- caps_reason = (- "The following resource(s) require capabilities: [" + ", ".join(caps_reason_types) + "]"- if caps_reason_types else ""- )-- caps_block = (- f"<Capabilities>{caps_xml}</Capabilities>"- f"<CapabilitiesReason>{_esc(caps_reason)}</CapabilitiesReason>"- if capabilities else ""- )+ caps_block = _capabilities_xml(template)return _xml(200, "GetTemplateSummaryResponse",f"<GetTemplateSummaryResult>"diff --git a/ministack/services/cloudformation/helpers.py b/ministack/services/cloudformation/helpers.pyindex 6254a644..c4ab6a3d 100644--- a/ministack/services/cloudformation/helpers.py+++ b/ministack/services/cloudformation/helpers.py@@ -128,6 +128,24 @@ def stack_name_problems(stack_name: str) -> list[str]:return problems+CAPABILITY_VALUES = ("CAPABILITY_IAM", "CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND")+++def capabilities_problems(values: list[str]) -> list[str]:+ """The enum constraint on ``Capabilities``. CreateStack, UpdateStack and+ CreateChangeSet document exactly three values, so anything else is a+ parameter validation error rather than a capability the stack quietly+ keeps. One sentence for the whole member, in the shape the API uses for a+ list of enums (unmeasured wording; the enum itself is the API reference's+ Valid Values)."""+ unknown = [v for v in values if v not in CAPABILITY_VALUES]+ if not unknown:+ return []+ return [f"Value '[{', '.join(values)}]' at 'capabilities' failed to satisfy "+ f"constraint: Member must satisfy enum value set: "+ f"[{', '.join(CAPABILITY_VALUES)}]"]++def _template_body_problem(body: str) -> str | None:"""The request-level constraint on ``TemplateBody`` (the quota page:"Template body size in a request", 51,200 bytes), as one sentence of the@@ -143,14 +161,16 @@ def _template_body_problem(body: str) -> str | None:def _request_problems(params, stack_name: str = ""):"""The request-level constraints of a call that carries a template: the- stack name where the call names one, and the size of an inline- ``TemplateBody``. The API reports every violation of a request in one- message, so both are collected before it answers. Returns the error- response, or None when the request passes."""+ stack name where the call names one, the size of an inline+ ``TemplateBody`` and the ``Capabilities`` enum. The API reports every+ violation of a request in one message, so all of them are collected+ before it answers. Returns the error response, or None when the request+ passes."""problems = stack_name_problems(stack_name) if stack_name else []body_problem = _template_body_problem(_p(params, "TemplateBody"))if body_problem:problems.append(body_problem)+ problems.extend(capabilities_problems(_extract_string_members(params, "Capabilities")))if problems:return _error("ValidationError", validation_error_message(problems))return Nonediff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex 374304a5..4ccf3687 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -3220,6 +3220,46 @@ def _cfn_noop_delete(physical_id, props):# --- CloudFormation Nested Stack (AWS::CloudFormation::Stack) ---+def _check_nested_stack_capabilities(parent_stack_name, template):+ """Refuse a nested stack's template whose IAM resources the parent did+ not acknowledge. AWS asks for the capabilities on the parent ("For nested+ stacks that contain IAM resources, you must acknowledge IAM capabilities",+ using-cfn-nested-stacks), so the set the parent stored covers the child+ and, through the child's own record, every level below it. Like the+ parent's check this runs under AUTH=true only, and it reads the IAM+ rule alone: whether a child template's own Transform needs+ CAPABILITY_AUTO_EXPAND on the parent is not modelled here.+ """+ from ministack.app import AUTH+ if not AUTH:+ return+ from ministack.services.cloudformation.handlers import (+ _insufficient_capabilities_message,+ _missing_capabilities,+ _required_iam_capabilities,+ )+ missing = _missing_capabilities(set(_parent_capabilities(parent_stack_name)),+ _required_iam_capabilities(template))+ if missing:+ raise ValueError(_insufficient_capabilities_message(missing))+++def _parent_capabilities(parent_stack_name):+ """The capabilities the parent stack acknowledged on its last operation."""+ from ministack.services.cloudformation import _stacks+ return list((_stacks.get(parent_stack_name) or {}).get("Capabilities", []))+++def _inherited_capabilities(parent_stack_name):+ """What a child stack's record carries, which is the parent's set under+ AUTH=true and nothing without it. The set exists to be read by the check+ on the level below, so recording it where no check runs would only change+ what DescribeStacks reports on a child.+ """+ from ministack.app import AUTH+ return _parent_capabilities(parent_stack_name) if AUTH else []++def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,previous_physical_id=None, previous_props=None):"""Provision an `AWS::CloudFormation::Stack` nested-stack resource.@@ -3264,6 +3304,7 @@ def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,raise ValueError(f"Nested-stack template empty at {template_url}")template = _parse_template(template_body)+ _check_nested_stack_capabilities(parent_stack_name, template)raw_param_props = props.get("Parameters") or {}if isinstance(raw_param_props, dict):@@ -3319,6 +3360,11 @@ def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,"_parent_stack_name": parent_stack_name,"RootId": _cr_stack_id(parent_stack_name),"ParentId": _cr_stack_id(parent_stack_name),+ # The parent's acknowledgement covers every level of nesting, so a+ # child of this child reads the same set. Only the check needs it, so+ # it is recorded only where the check runs: without AUTH a child's+ # DescribeStacks reports what it reported before, nothing.+ "Capabilities": _inherited_capabilities(parent_stack_name),}_stacks[child_name] = child_stack_stack_events.setdefault(child_stack_id, [])diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex d1236ff8..16f33e41 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -2350,6 +2350,117 @@ def test_cfn_change_set_lifecycle(cfn):stack = _wait_stack(cfn, "cfn-t08")assert stack["StackStatus"] == "CREATE_COMPLETE"+def test_cfn_stack_and_change_set_report_their_capabilities(cfn):+ """DescribeStacks and DescribeChangeSet report the capabilities the+ operation acknowledged; an update replaces them, and an update that sends+ none leaves the member empty."""+ uid = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-caps-rep-{uid}"+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Q": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": f"{stack_name}-q"}},+ },+ }+ try:+ cfn.create_change_set(+ StackName=stack_name,+ ChangeSetName="cs1",+ TemplateBody=json.dumps(template),+ ChangeSetType="CREATE",+ Capabilities=["CAPABILITY_IAM"],+ )+ time.sleep(1)+ cs = cfn.describe_change_set(StackName=stack_name, ChangeSetName="cs1")+ assert cs["Capabilities"] == ["CAPABILITY_IAM"]++ cfn.execute_change_set(StackName=stack_name, ChangeSetName="cs1")+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE"+ assert stack["Capabilities"] == ["CAPABILITY_IAM"]++ updated = json.loads(json.dumps(template))+ updated["Resources"]["Q"]["Properties"]["DelaySeconds"] = 5+ cfn.update_stack(+ StackName=stack_name,+ TemplateBody=json.dumps(updated),+ Capabilities=["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"],+ )+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE"+ assert stack["Capabilities"] == ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]++ # The member is what the last operation acknowledged: an update that+ # sends none blanks it.+ updated["Resources"]["Q"]["Properties"]["DelaySeconds"] = 6+ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(updated))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE"+ assert stack.get("Capabilities", []) == []+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_unknown_capability_is_refused(cfn):+ """Capabilities is an enum of three values, so a fourth is a parameter+ validation error rather than something the stack silently keeps, on+ CreateStack, CreateChangeSet and UpdateStack alike."""+ uid = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-caps-enum-{uid}"+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Q": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": f"{stack_name}-q"}},+ },+ }+ bad = ["CAPABILITY_IAM", "CAPABILITY_RESOURCE_POLICY"]+ try:+ with pytest.raises(ClientError) as exc:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template),+ Capabilities=bad)+ message = exc.value.response["Error"]["Message"]+ assert "1 validation error detected" in message+ assert "'capabilities'" in message+ assert "CAPABILITY_AUTO_EXPAND" in message+ with pytest.raises(ClientError):+ cfn.describe_stacks(StackName=stack_name)+ with pytest.raises(ClientError) as exc:+ cfn.create_change_set(StackName=stack_name, ChangeSetName="cs",+ TemplateBody=json.dumps(template), ChangeSetType="CREATE",+ Capabilities=bad)+ assert "'capabilities'" in exc.value.response["Error"]["Message"]++ # The capability problem joins the other request-level problems into+ # one message, which is what _request_problems is for: a stack name+ # over the length limit and a bad capability arrive together, and the+ # count is the plural the message builder produces.+ with pytest.raises(ClientError) as exc:+ cfn.create_stack(StackName="c" * 129, TemplateBody=json.dumps(template),+ Capabilities=bad)+ message = exc.value.response["Error"]["Message"]+ assert "2 validation errors detected" in message+ assert "'capabilities'" in message+ assert "'stackName'" in message+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++ try:+ # The three documented values are accepted.+ cfn.create_stack(+ StackName=stack_name,+ TemplateBody=json.dumps(template),+ Capabilities=["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"],+ )+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE"+ with pytest.raises(ClientError) as exc:+ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(template),+ Capabilities=bad)+ assert "'capabilities'" in exc.value.response["Error"]["Message"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++def test_cfn_change_set_create_emits_review_event(cfn):template = {"AWSTemplateFormatVersion": "2010-09-09",@@ -2438,6 +2549,80 @@ def test_cfn_validate_template(cfn):with pytest.raises(ClientError):cfn.validate_template(TemplateBody=json.dumps(invalid_template))++def test_cfn_declared_transforms_drive_the_sam_transform(monkeypatch):+ """DeclaredTransforms and the deploy path read one function, so a+ template that ValidateTemplate reports as declaring the SAM transform is+ a template the stack operation actually transforms. The macro object+ (Transform: {Name: ...}) is the form that used to be reported and not+ applied."""+ from ministack.services.cloudformation.engine import (+ _apply_sam_transform_if_applicable,+ declared_transforms,+ )++ sam = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"F": {"Type": "AWS::Serverless::Function", "Properties": {+ "Runtime": "python3.12", "Handler": "index.handler", "CodeUri": "s3://b/k"}}},+ }+ for declared in ("AWS::Serverless-2016-10-31",+ {"Name": "AWS::Serverless-2016-10-31"},+ ["AWS::Serverless-2016-10-31"]):+ template = json.loads(json.dumps(dict(sam, Transform=declared)))+ assert declared_transforms(template) == ["AWS::Serverless-2016-10-31"]+ out = _apply_sam_transform_if_applicable(template)+ types = {res.get("Type") for res in out["Resources"].values()}+ assert "AWS::Serverless::Function" not in types+ assert "AWS::Lambda::Function" in types+ # Entries the section cannot name are dropped rather than reported.+ assert declared_transforms({"Transform": [{"Parameters": {}}, 5]}) == []+ assert declared_transforms({}) == []+++def test_cfn_validate_template_reports_capabilities_and_transforms(cfn):+ """ValidateTemplate answers Capabilities, CapabilitiesReason and+ DeclaredTransforms, the three response elements the API documents next to+ Description and Parameters."""+ plain = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"B": {"Type": "AWS::S3::Bucket"}},+ }+ result = cfn.validate_template(TemplateBody=json.dumps(plain))+ assert result.get("Capabilities", []) == []+ assert result.get("DeclaredTransforms", []) == []++ iam = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "R": {+ "Type": "AWS::IAM::Role",+ "Properties": {+ "RoleName": "vt-caps-role",+ "AssumeRolePolicyDocument": {"Version": "2012-10-17", "Statement": []},+ },+ },+ },+ }+ result = cfn.validate_template(TemplateBody=json.dumps(iam))+ assert result["Capabilities"] == ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]+ assert "AWS::IAM::Role" in result["CapabilitiesReason"]+ assert result["DeclaredTransforms"] == ["AWS::Serverless-2016-10-31"]++ # The list form and the macro object, the shape the AWS::Include reference+ # documents at the top level, report their names too.+ listed = dict(plain, Transform=["AWS::LanguageExtensions",+ {"Name": "AWS::Include",+ "Parameters": {"Location": "s3://bucket/snippet.yaml"}}])+ result = cfn.validate_template(TemplateBody=json.dumps(listed))+ assert result["DeclaredTransforms"] == ["AWS::LanguageExtensions", "AWS::Include"]+ macro = dict(plain, Transform={"Name": "AWS::Include",+ "Parameters": {"Location": "s3://bucket/snippet.yaml"}})+ result = cfn.validate_template(TemplateBody=json.dumps(macro))+ assert result["DeclaredTransforms"] == ["AWS::Include"]++def test_cfn_get_template_summary(cfn):# Basic template: parameters and resource types surfaced, no capabilitiesbasic = {@@ -2718,6 +2903,164 @@ def test_cfn_capabilities_check_under_auth(monkeypatch):assert check(_CAPS_NULL_PROPS_ROLE_TEMPLATE) == refused+def _deploy_nested_child(monkeypatch, parent_name, capabilities, templates, root_url):+ """Deploy an AWS::CloudFormation::Stack child of ``parent_name`` in+ process: ``templates`` maps a TemplateURL to its body, ``capabilities`` is+ what the parent acknowledged. Returns the child stack name."""+ from ministack.services.cloudformation import _stacks+ from ministack.services.cloudformation import helpers as _helpers+ from ministack.services.cloudformation.provisioners import _cfn_nested_stack_deploy++ monkeypatch.setattr(+ _helpers, "_resolve_template",+ lambda params: (json.dumps(templates[params["TemplateURL"][0]]), None))+ _stacks[parent_name] = {+ "StackName": parent_name,+ "StackId": f"arn:aws:cloudformation:us-east-1:000000000000:stack/{parent_name}/parent",+ "StackStatus": "CREATE_IN_PROGRESS",+ "Capabilities": list(capabilities),+ "_resources": {},+ }+ child_id, _attrs = _cfn_nested_stack_deploy("Child", {"TemplateURL": root_url}, parent_name)+ return child_id.split("/")[1]+++def _forget_nested_test_stacks(prefix):+ from ministack.services.cloudformation import _stack_events, _stacks+ for name in [n for n in list(_stacks) if n.startswith(prefix)]:+ stack = _stacks.pop(name, None) or {}+ _stack_events.pop(stack.get("StackId"), None)+++def test_cfn_nested_stack_capabilities_follow_the_parent_under_auth(monkeypatch):+ """A nested stack's IAM resources are covered by the capabilities the+ parent acknowledged (using-cfn-nested-stacks: "For nested stacks that+ contain IAM resources, you must acknowledge IAM capabilities"), so a+ child with an unnamed role is refused when the parent sent none and+ provisioned when it sent CAPABILITY_IAM; a custom-named role needs+ CAPABILITY_NAMED_IAM."""+ import ministack.app as app_mod+ from ministack.services.cloudformation import _stacks++ monkeypatch.setattr(app_mod, "AUTH", True)+ uid = _uuid_mod.uuid4().hex[:8]+ parent = f"cfn-nested-caps-{uid}"+ url = "http://localhost:4566/tpl/child.json"+ templates = {url: _CAPS_ROLE_TEMPLATE}+ try:+ with pytest.raises(ValueError, match=r"Requires capabilities : \[CAPABILITY_IAM\]"):+ _deploy_nested_child(monkeypatch, parent, [], templates, url)+ # The check runs before the child record exists, so nothing is left.+ assert not [n for n in _stacks if n.startswith(f"{parent}-Child")]++ child = _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_IAM"], templates, url)+ assert _stacks[child]["StackStatus"] == "CREATE_COMPLETE"+ assert _stacks[child]["Capabilities"] == ["CAPABILITY_IAM"]++ named = {url: json.loads(json.dumps(_CAPS_NAMED_ROLE_TEMPLATE))}+ # A unique name, so a rerun does not meet the role of a previous run.+ named[url]["Resources"]["Role"]["Properties"]["RoleName"] = f"cfn-nested-caps-{uid}-role"+ with pytest.raises(ValueError, match=r"Requires capabilities : \[CAPABILITY_NAMED_IAM\]"):+ _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_IAM"], named, url)+ child = _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_NAMED_IAM"], named, url)+ assert _stacks[child]["StackStatus"] == "CREATE_COMPLETE"+ finally:+ _forget_nested_test_stacks(parent)+++def test_cfn_nested_stack_capabilities_reach_the_second_level_under_auth(monkeypatch):+ """A child's child reads the root's acknowledgement: the inner stack with+ an IAM role is refused when the root sent nothing and deploys when it+ sent CAPABILITY_IAM."""+ import ministack.app as app_mod+ from ministack.services.cloudformation import _stacks++ monkeypatch.setattr(app_mod, "AUTH", True)+ uid = _uuid_mod.uuid4().hex[:8]+ parent = f"cfn-nested-caps2-{uid}"+ middle_url = "http://localhost:4566/tpl/middle.json"+ inner_url = "http://localhost:4566/tpl/inner.json"+ templates = {+ inner_url: _CAPS_ROLE_TEMPLATE,+ middle_url: {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"Inner": {"Type": "AWS::CloudFormation::Stack",+ "Properties": {"TemplateURL": inner_url}}},+ },+ }+ try:+ with pytest.raises(ValueError, match=r"Requires capabilities : \[CAPABILITY_IAM\]"):+ _deploy_nested_child(monkeypatch, parent, [], templates, middle_url)+ child = _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_IAM"], templates, middle_url)+ assert _stacks[child]["StackStatus"] == "CREATE_COMPLETE"+ inner = [n for n in _stacks if n.startswith(f"{child}-Inner")]+ assert [_stacks[n]["StackStatus"] for n in inner] == ["CREATE_COMPLETE"]+ finally:+ _forget_nested_test_stacks(parent)+++def test_cfn_nested_stack_capabilities_not_checked_without_auth(monkeypatch):+ import ministack.app as app_mod+ from ministack.services.cloudformation import _stacks++ monkeypatch.setattr(app_mod, "AUTH", False)+ uid = _uuid_mod.uuid4().hex[:8]+ parent = f"cfn-nested-noauth-{uid}"+ url = "http://localhost:4566/tpl/child.json"+ try:+ child = _deploy_nested_child(monkeypatch, parent, [], {url: _CAPS_ROLE_TEMPLATE}, url)+ assert _stacks[child]["StackStatus"] == "CREATE_COMPLETE"+ # The set exists to be read by the check on the level below, so+ # without AUTH the child's record carries none and DescribeStacks+ # reports on it what it reported before: nothing changes.+ _stacks[parent]["Capabilities"] = ["CAPABILITY_IAM"]+ child = _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_IAM"],+ {url: _CAPS_ROLE_TEMPLATE}, url)+ assert _stacks[child].get("Capabilities", []) == []+ finally:+ _forget_nested_test_stacks(parent)+++def test_cfn_nested_stack_capabilities_are_rechecked_on_an_update(monkeypatch):+ """A parent update that re-deploys the nested resource re-checks the+ child, so a parent that drops Capabilities fails it: a stack reports what+ its last operation acknowledged, and the child's own resources follow the+ same rule the parent's do under AUTH=true."""+ import ministack.app as app_mod+ from ministack.services.cloudformation import _stacks+ from ministack.services.cloudformation.provisioners import _cfn_nested_stack_deploy++ monkeypatch.setattr(app_mod, "AUTH", True)+ uid = _uuid_mod.uuid4().hex[:8]+ parent = f"cfn-nested-caps-upd-{uid}"+ url = "http://localhost:4566/tpl/child.json"+ plain = {"AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"Q": {"Type": "AWS::SQS::Queue",+ "Properties": {"QueueName": f"{parent}-q"}}}}+ templates = {url: plain}+ try:+ child = _deploy_nested_child(monkeypatch, parent, ["CAPABILITY_IAM"], templates, url)+ assert _stacks[child]["StackStatus"] == "CREATE_COMPLETE"++ # The child gains an IAM role and the parent still acknowledges: fine.+ templates[url] = _CAPS_ROLE_TEMPLATE+ _cfn_nested_stack_deploy("Child", {"TemplateURL": url}, parent,+ previous_physical_id=child,+ previous_props={"TemplateURL": url})+ assert _stacks[child]["StackStatus"] == "UPDATE_COMPLETE"++ # The same update with the acknowledgement dropped is refused: a+ # stack reports what its last operation acknowledged, so a parent+ # update that sends no Capabilities covers nothing.+ _stacks[parent]["Capabilities"] = []+ with pytest.raises(ValueError, match=r"Requires capabilities : \[CAPABILITY_IAM\]"):+ _cfn_nested_stack_deploy("Child", {"TemplateURL": url}, parent,+ previous_physical_id=child,+ previous_props={"TemplateURL": url})+ finally:+ _forget_nested_test_stacks(parent)++def test_cfn_create_stack_refuses_missing_capabilities_under_auth(monkeypatch):import ministack.app as app_modfrom ministack.services.cloudformation import _stack_events, _stacks