Skip to content

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

fix(lambda): Docker-executor functions reach a USE_SSL gateway (#1918)

Jeff Heller4eae8fbd87a3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d788db94..54ce11c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Lambda — Docker-executor functions reach a `USE_SSL=1` gateway** — the gateway listener serves only HTTPS, but a container's default `AWS_ENDPOINT_URL` was `http://host.docker.internal:<port>`, and its Node shim downgraded a function's `https.request` to the gateway to plain HTTP, which the listener resets. The default endpoint now follows the gateway's scheme and those requests keep TLS. With a gateway certificate that names `host.docker.internal` (`MINISTACK_SSL_CERT`/`MINISTACK_SSL_KEY`), whose CA MiniStack already mounts into these containers, SDK calls from a function succeed. Without `USE_SSL` nothing changes.
- **RDS — `MINISTACK_RDS_PUBLIC_ENDPOINT=1` works with a Compose `hostname:`** — the #1884 fix told a containerised MiniStack from a host-run one by its `HOSTNAME` self-lookup, which a Compose `hostname:` defeats, so instances again stayed `creating`. MiniStack now checks Docker's or Podman's marker file for that, and a containerised MiniStack detects its network as with the setting off (`DOCKER_NETWORK`, then the self-lookup).
### Added
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index db5996e2..4e8b40b3 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -4208,7 +4208,7 @@ def handler(event, context):
_JS_CTX_ARN_SHIM = '''\
// MiniStack shim: hand user code the control-plane ARN in its context, and
-// reach the gateway over plain HTTP when a library insists on HTTPS.
+// reach the gateway over plain HTTP (without USE_SSL) when a library insists on HTTPS.
const path = require("path");
const fs = require("fs");
const http = require("http");
@@ -4216,7 +4216,7 @@ const https = require("https");
const REAL = process.env._MS_REAL_HANDLER || "index.handler";
const ARN = process.env._LAMBDA_FUNCTION_ARN || "";
const TASK_ROOT = process.env.LAMBDA_TASK_ROOT || "/var/task";
-// The container talks to MiniStack over http://<gateway host>:<port>, but the
+// Without USE_SSL the container talks to MiniStack over http://<gateway host>:<port>, but 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
@@ -4258,6 +4258,8 @@ try {
if (!PLAIN_HOSTS.has(host) || (rawPort && rawPort !== "443" && rawPort !== EP_PORT)) {
return origHttpsRequest.apply(https, original);
}
+ // USE_SSL: the gateway serves only HTTPS, so a request to it keeps TLS.
+ if (EP.protocol === "https:") return origHttpsRequest.apply(https, original);
opts.protocol = "http:";
opts.hostname = host;
opts.host = host + ":" + EP_PORT;
@@ -4465,8 +4467,11 @@ def _spawn_lambda_container_impl(config: dict, code_zip: bytes | None,
if not endpoint:
endpoint = _normalize_endpoint_url(env_vars.get("LOCALSTACK_HOSTNAME", ""))
if not endpoint:
+ from ministack.core import tls as _tls
+
port = os.environ.get("GATEWAY_PORT", os.environ.get("EDGE_PORT", "4566"))
- endpoint = f"http://host.docker.internal:{port}"
+ scheme = "https" if _tls.use_ssl_enabled() else "http"
+ endpoint = f"{scheme}://host.docker.internal:{port}"
else:
# Rewrite localhost/127.0.0.1 → host.docker.internal for container access
endpoint = _rewrite_host_for_container(endpoint)
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 196850be..7e218d43 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -7681,6 +7681,16 @@ def test_lambda_timeout_update_evicts_warm_container(monkeypatch, package_type,
container.remove.assert_called_once()
[email protected]("use_ssl, scheme", [("1", "https"), ("", "http")])
+def test_lambda_container_default_endpoint_follows_gateway_scheme(monkeypatch, use_ssl, scheme):
+ """With no endpoint configured, a Docker Lambda is pointed at the gateway with the scheme it serves."""
+ monkeypatch.setenv("USE_SSL", use_ssl)
+ monkeypatch.setenv("GATEWAY_PORT", "4566")
+ captured = _spawn_capture_run_kwargs(monkeypatch, endpoint="")
+
+ assert captured["environment"]["AWS_ENDPOINT_URL"] == f"{scheme}://host.docker.internal:4566"
+
+
def test_lambda_container_maps_host_docker_internal_to_host_gateway(monkeypatch):
"""A container pointed at host.docker.internal gets the name mapped.
@@ -12538,6 +12548,52 @@ def test_node_context_shim_downgrades_https_to_the_gateway(tmp_path):
server.server_close()
+def test_node_context_shim_keeps_https_to_a_tls_gateway(tmp_path):
+ """Under USE_SSL the gateway speaks only TLS: the container shim must not downgrade a request to it."""
+ import http.server
+ import subprocess
+ import threading
+
+ seen = []
+
+ class Handler(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ seen.append(self.path)
+ self.send_response(200)
+ self.end_headers()
+
+ def log_message(self, *args):
+ pass
+
+ server = http.server.HTTPServer(("127.0.0.1", 0), Handler)
+ port = server.server_address[1]
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ try:
+ code_dir = tmp_path / "task"
+ code_dir.mkdir()
+ (code_dir / "index.js").write_text(
+ "const https = require('https');\n"
+ "exports.handler = (event) => new Promise((resolve) => {\n"
+ " https.get({hostname: '127.0.0.1', port: event.port, path: '/probe'}, (res) => resolve({status: res.statusCode}))\n"
+ " .on('error', (e) => resolve({error: e.code || String(e)}));\n"
+ "});\n"
+ )
+ assert lsvc._write_context_arn_shim(str(code_dir), "nodejs20.x", "index.handler") == "_msctx_shim.handler"
+ env = {**os.environ, "LAMBDA_TASK_ROOT": str(code_dir), "_MS_REAL_HANDLER": "index.handler",
+ "AWS_ENDPOINT_URL": f"https://127.0.0.1:{port}"}
+ script = ("require(process.argv[1]).handler({port: Number(process.argv[2])}, {})"
+ ".then((r) => process.stdout.write(JSON.stringify(r)));")
+ proc = subprocess.run(["node", "-e", script, str(code_dir / "_msctx_shim.js"), str(port)],
+ env=env, capture_output=True, text=True, timeout=30)
+ assert proc.returncode == 0, proc.stderr
+ # Still TLS: the plain-HTTP listener never sees the request as HTTP.
+ assert "error" in json.loads(proc.stdout)
+ assert seen == []
+ finally:
+ server.shutdown()
+ server.server_close()
+
+
def test_extract_cache_sweep_is_reference_based(monkeypatch, tmp_path):
"""The sweep drops extracted trees whose blob no longer backs any
function, version, or layer version — and only those. References are read