Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cloudformation): https callback URLs when the gateway serves TLS (#2290)

iot-rocket3e81b66b42ad
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd2269f5..9f5fa943 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **CloudFormation — custom resource `ResponseURL` and `WaitConditionHandle` URL under `USE_SSL=1`** — both URLs were always `http://`, which the TLS-only gateway drops without a response, so a Python handler that PUTs its `ResponseURL` with `urllib` (CDK's bucket notifications handler does) never signalled and the stack waited out `ServiceTimeout`, and a signal to the handle URL never arrived; both are now `https://` when `USE_SSL` is on, and the Lambdas MiniStack runs also get `SSL_CERT_FILE` set to the gateway's CA bundle, which Python's `ssl` reads where it ignores `REQUESTS_CA_BUNDLE`. Contributed by @iot-rocket.
### Added
- **S3 Tables, Glue — Iceberg REST calls are authorized under `AUTH=true`** — the Iceberg REST catalog at `/iceberg` served S3 Tables and Glue catalog calls without checking their credentials; each call is now authorized for the catalog that serves it, whatever service it is signed for: on a table bucket as the `s3tables` actions AWS lists for its operation (`loadTable` needs `GetTableMetadataLocation`, then `GetTableData`) on the bucket or table (`table/*` for one that does not exist), on the Glue catalog as its `glue` action on the catalog, then the database, then the table, and `transactions/commit` as `updateTable` on each table it changes. A denial answers 403 in the Iceberg error shape naming the action and resource ("Access Denied" for a later S3 Tables action, no body for `HEAD`), and a `{prefix}` that is neither a table bucket ARN nor `<account>:s3tablescatalog/<bucket>` answers 400 `bad_request`, as AWS does. Contributed by @iot-rocket.
- **RDS Data, S3 Control — calls are authorized under `AUTH=true`** — RDS Data calls and S3 Control tagging calls were served whatever the caller's policies allowed; each RDS Data call is now authorized as `rds-data:<operation>` on the cluster its `resourceArn` names, and `ListTagsForResource`, `TagResource` and `UntagResource` as `s3:<operation>` on the bucket, with a 403 denial (`AccessDeniedException` from RDS Data, `AccessDenied` from S3 Control) naming the action and resource, as on AWS. Contributed by @iot-rocket.
diff --git a/ministack/core/tls.py b/ministack/core/tls.py
index 8719b19c..061e398b 100644
--- a/ministack/core/tls.py
+++ b/ministack/core/tls.py
@@ -145,7 +145,7 @@ def trust_gateway_cert(env: dict) -> None:
env.setdefault("NODE_EXTRA_CA_CERTS", cert_path)
bundle = ca_bundle_path(cert_path)
if bundle:
- for var in ("AWS_CA_BUNDLE", "REQUESTS_CA_BUNDLE"):
+ for var in ("AWS_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "SSL_CERT_FILE"):
env.setdefault(var, bundle)
diff --git a/ministack/services/cloudformation/custom_resource.py b/ministack/services/cloudformation/custom_resource.py
index d27ef6bb..f4d8193a 100644
--- a/ministack/services/cloudformation/custom_resource.py
+++ b/ministack/services/cloudformation/custom_resource.py
@@ -7,6 +7,8 @@ import logging
import os
import threading
+from ministack.core.tls import use_ssl_enabled
+
logger = logging.getLogger("cloudformation")
_HOST = os.environ.get("MINISTACK_HOST", "localhost")
@@ -41,8 +43,14 @@ def reset():
_pending.clear()
+def _gateway_url() -> str:
+ """The gateway's base URL, in the scheme it serves."""
+ scheme = "https" if use_ssl_enabled() else "http"
+ return f"{scheme}://{_HOST}:{_PORT}"
+
+
def _response_url(token: str) -> str:
- return f"http://{_HOST}:{_PORT}/_ministack/cfn-response/{token}"
+ return f"{_gateway_url()}/_ministack/cfn-response/{token}"
def _normalise_resource_properties(value):
diff --git a/ministack/services/cloudformation/wait_conditions.py b/ministack/services/cloudformation/wait_conditions.py
index c84fea69..74d39e10 100644
--- a/ministack/services/cloudformation/wait_conditions.py
+++ b/ministack/services/cloudformation/wait_conditions.py
@@ -20,7 +20,7 @@ from urllib.parse import urlparse
from ministack.core.responses import new_uuid
-from .custom_resource import _HOST, _PORT
+from .custom_resource import _gateway_url
logger = logging.getLogger("cloudformation")
@@ -95,7 +95,7 @@ def validate_resource_signal_timeout(value, logical_id: str) -> int:
def handle_url(token: str) -> str:
- return f"http://{_HOST}:{_PORT}{SIGNAL_PATH}{token}"
+ return f"{_gateway_url()}{SIGNAL_PATH}{token}"
def _token_of(url) -> str | None:
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 2766eb1d..c28cb419 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -4244,7 +4244,7 @@ def _wire_cognito_issuer_host(run_kwargs, container_env, mounts, runtime="", cop
extra_hosts.setdefault(host, "host-gateway")
if not os.path.exists(cert_path):
return
- # NODE_EXTRA_CA_CERTS adds to node's roots; the other two replace the store.
+ # NODE_EXTRA_CA_CERTS adds to node's roots; the others replace the store.
bundle = _tls.ca_bundle_path(cert_path)
def trust(target, source):
@@ -4257,7 +4257,7 @@ def _wire_cognito_issuer_host(run_kwargs, container_env, mounts, runtime="", cop
container_env.setdefault("NODE_EXTRA_CA_CERTS", _CONTAINER_CA_PATH)
if bundle:
trust(_CONTAINER_BUNDLE_PATH, bundle)
- for var in ("AWS_CA_BUNDLE", "REQUESTS_CA_BUNDLE"):
+ for var in ("AWS_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "SSL_CERT_FILE"):
container_env.setdefault(var, _CONTAINER_BUNDLE_PATH)
# A JVM reads none of the above, and announces JAVA_TOOL_OPTIONS on stderr.
if runtime.startswith("java"):
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 9dcd25c2..8f489b62 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -6239,6 +6239,18 @@ def test_cfn_wait_condition_validators(validator, value, message):
assert wc.validate_resource_signal_timeout("PT1H30M15S", "Wait") == 5415
+def test_cfn_wait_condition_handle_url_names_the_scheme_the_gateway_serves(monkeypatch):
+ """Under USE_SSL the gateway answers TLS only, so the handle URL is https."""
+ from ministack.services.cloudformation import custom_resource
+ from ministack.services.cloudformation import wait_conditions as wc
+
+ monkeypatch.delenv("USE_SSL", raising=False)
+ assert wc.handle_url("tok").startswith("http://")
+ monkeypatch.setenv("USE_SSL", "1")
+ assert wc.handle_url("tok") == (
+ f"https://{custom_resource._HOST}:{custom_resource._PORT}/_ministack/cfn-signal/tok")
+
+
@pytest.mark.parametrize(
"policy",
[
@@ -18916,6 +18928,17 @@ def test_cfn_response_endpoint_accepts_put(cfn):
assert resp.status == 200
+def test_cfn_response_url_names_the_scheme_the_gateway_serves(monkeypatch):
+ """Under USE_SSL the gateway answers TLS only, so the ResponseURL is https."""
+ from ministack.services.cloudformation import custom_resource
+
+ monkeypatch.delenv("USE_SSL", raising=False)
+ assert custom_resource._response_url("tok").startswith("http://")
+ monkeypatch.setenv("USE_SSL", "1")
+ assert custom_resource._response_url("tok") == (
+ f"https://{custom_resource._HOST}:{custom_resource._PORT}/_ministack/cfn-response/tok")
+
+
# -- Create lifecycle -------------------------------------------------------
_CR_HANDLER_SUCCESS = """\
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 8852349c..1d5c52ef 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -14103,6 +14103,15 @@ def test_cognito_issuer_host_resolves_to_the_gateway_under_use_ssl(monkeypatch,
(lsvc._CONTAINER_CA_PATH, True), (lsvc._CONTAINER_BUNDLE_PATH, True)]
+def test_lambda_container_python_ssl_trusts_the_gateway_under_use_ssl(monkeypatch, tmp_path):
+ """urllib reads SSL_CERT_FILE, not the *_CA_BUNDLE vars, e.g. for a custom-resource ResponseURL PUT."""
+ from ministack.services import lambda_svc as lsvc
+
+ _rk, container_env, _mounts = _wire(
+ monkeypatch, USE_SSL="1", TMPDIR=str(tmp_path), MINISTACK_REGION="us-east-1")
+ assert container_env["SSL_CERT_FILE"] == lsvc._CONTAINER_BUNDLE_PATH
+
+
def test_lambda_trust_files_are_copied_not_mounted_when_ministack_runs_in_docker(monkeypatch, tmp_path):
"""In a container the trust files are docker cp'd, not bind-mounted; a symlink is followed."""
import tarfile
diff --git a/tests/test_tls.py b/tests/test_tls.py
index bdfaa6e3..93e3f4fa 100644
--- a/tests/test_tls.py
+++ b/tests/test_tls.py
@@ -280,6 +280,58 @@ def test_iot_data_client_reaches_the_described_endpoint_over_https(tmp_path, mon
_terminate(proc)
+_CFN_RESPONDER = """\
+import json, urllib.request
+
+def handler(event, context):
+ body = json.dumps({"Status": "SUCCESS", "PhysicalResourceId": "tls", "StackId": event["StackId"],
+ "RequestId": event["RequestId"], "LogicalResourceId": event["LogicalResourceId"],
+ "Data": {"Scheme": event["ResponseURL"].split(":", 1)[0]}}).encode()
+ urllib.request.urlopen(urllib.request.Request(event["ResponseURL"], data=body, method="PUT"), timeout=10)
+"""
+
+
+def test_python_custom_resource_answers_its_response_url_over_https(tmp_path):
+ """A Python handler PUTs its ResponseURL with urllib, as CDK's bucket notifications handler does."""
+ import io
+ import json
+ import zipfile
+
+ import boto3
+
+ port = _free_port()
+ proc = _spawn({"USE_SSL": "1", "TMPDIR": str(tmp_path), "MINISTACK_HOST": "localhost",
+ "GATEWAY_PORT": str(port), "MINISTACK_SSL_CERT": "", "MINISTACK_SSL_KEY": ""}, port)
+ try:
+ _wait_health(f"https://127.0.0.1:{port}/_ministack/health", ctx=_ctx_no_verify())
+ kw = {"endpoint_url": f"https://localhost:{port}", "region_name": "us-east-1",
+ "aws_access_key_id": "test", "aws_secret_access_key": "test",
+ "verify": str(tmp_path / "ministack-tls" / "server.crt")}
+ code = io.BytesIO()
+ with zipfile.ZipFile(code, "w") as zf:
+ zf.writestr("index.py", _CFN_RESPONDER)
+ role = boto3.client("iam", **kw).create_role(RoleName="tls-responder", AssumeRolePolicyDocument=json.dumps({
+ "Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "sts:AssumeRole",
+ "Principal": {"Service": "lambda.amazonaws.com"}}]}))["Role"]["Arn"]
+ arn = boto3.client("lambda", **kw).create_function(
+ FunctionName="tls-responder", Runtime="python3.12", Role=role,
+ Handler="index.handler", Code={"ZipFile": code.getvalue()})["FunctionArn"]
+ cfn = boto3.client("cloudformation", **kw)
+ cfn.create_stack(StackName="tls-cr", TemplateBody=json.dumps({
+ "Resources": {"CR": {"Type": "Custom::Tls", "Properties": {"ServiceToken": arn, "ServiceTimeout": "20"}}},
+ "Outputs": {"Scheme": {"Value": {"Fn::GetAtt": ["CR", "Scheme"]}}},
+ }))
+ deadline = time.time() + 60
+ while (stack := cfn.describe_stacks(StackName="tls-cr")["Stacks"][0])["StackStatus"].endswith("_IN_PROGRESS"):
+ assert time.time() < deadline, stack
+ time.sleep(1)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ assert stack["Outputs"][0]["OutputValue"] == "https"
+ finally:
+ _terminate(proc)
+
+
def test_byo_certificate_is_never_regenerated(tmp_path, monkeypatch):
"""MINISTACK_SSL_CERT is the operator's, so the issuer host is their
business and we must hand it back untouched."""