Skip to content

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

fix(tls): generate a server certificate rustls accepts (#2080)

Antonie Popovica877b21b4372
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 935cb6c3..db6fdefa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+- **TLS — the generated certificate works with rustls** — it was a CA certificate, which rustls refuses as a server's (`CaUsedAsEndEntity`). It is now a server certificate (`CA:FALSE`, `serverAuth`), and a cached CA certificate is regenerated.
- **Lambda — preserve invocation aliases in runtime context** — `Invoke` retains the requested alias or version in `invoked_function_arn`, while `function_version` remains the resolved published version. Warm workers receive invocation identity per request. Contributed by @jayjanssen.
- **IoT — `DescribeCertificate` reports `certificateMode`** — the field was missing from the description. It is now `SNI_ONLY` for a certificate registered with `RegisterCertificateWithoutCA` and `DEFAULT` for the others, as AWS reports it. Contributed by @iot-rocket.
### Fixed
diff --git a/ministack/core/tls.py b/ministack/core/tls.py
index 51e9d5cf..8719b19c 100644
--- a/ministack/core/tls.py
+++ b/ministack/core/tls.py
@@ -119,15 +119,19 @@ def java_truststore_path(cert_path: str) -> "str | None":
def _cert_names(cert_path: str, names: "list[str]") -> bool:
- """Whether the certificate at `cert_path` carries every name in `names` as a SAN."""
+ """Whether the certificate at `cert_path` carries every name in `names` as a SAN and is no CA.
+
+ webpki (rustls) refuses a CA certificate served as the leaf (CaUsedAsEndEntity).
+ """
try:
out = subprocess.run(
- ["openssl", "x509", "-in", cert_path, "-noout", "-ext", "subjectAltName"],
+ ["openssl", "x509", "-in", cert_path, "-noout", "-ext", "subjectAltName,basicConstraints"],
capture_output=True, text=True, check=False,
)
except OSError:
return True # No openssl to check with; leave the cached cert alone.
- return all(f"DNS:{name}" in (out.stdout or "") for name in names)
+ text = out.stdout or ""
+ return all(f"DNS:{name}" in text for name in names) and "CA:TRUE" not in text
def trust_gateway_cert(env: dict) -> None:
@@ -180,6 +184,9 @@ def resolve_tls_material() -> "tuple[str, str]":
"-keyout", key_path, "-out", cert_path,
"-days", "825",
"-subj", "/CN=ministack-local/O=MiniStack",
+ "-addext", "basicConstraints=critical,CA:FALSE",
+ "-addext", "keyUsage=critical,digitalSignature,keyEncipherment",
+ "-addext", "extendedKeyUsage=serverAuth",
"-addext",
"subjectAltName=DNS:localhost,DNS:ministack,"
+ "".join(f"DNS:{host}," for host in _generated_cert_names())
diff --git a/tests/test_tls.py b/tests/test_tls.py
index cf2a71d7..bdfaa6e3 100644
--- a/tests/test_tls.py
+++ b/tests/test_tls.py
@@ -342,3 +342,47 @@ def test_java_truststore_carries_our_cert_and_the_public_roots(tmp_path, monkeyp
subjects = [entry.certificate.subject for entry in loaded.additional_certs]
assert ours.subject in subjects
assert len(subjects) > 5
+
+
+def _fresh_tls(tmp_path, monkeypatch):
+ import tempfile
+
+ monkeypatch.setenv("TMPDIR", str(tmp_path))
+ monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
+ monkeypatch.setenv("USE_SSL", "1")
+ monkeypatch.delenv("MINISTACK_SSL_CERT", raising=False)
+ monkeypatch.delenv("MINISTACK_SSL_KEY", raising=False)
+
+
+def test_generated_cert_is_a_server_certificate_not_a_ca(tmp_path, monkeypatch):
+ """webpki (rustls) refuses a CA certificate presented as the server's, CaUsedAsEndEntity."""
+ from ministack.core import tls
+
+ _fresh_tls(tmp_path, monkeypatch)
+ cert_path, _key = tls.resolve_tls_material()
+ out = subprocess.run(
+ ["openssl", "x509", "-in", cert_path, "-noout", "-ext", "basicConstraints,extendedKeyUsage"],
+ capture_output=True, text=True, check=True).stdout
+ assert "CA:FALSE" in out and "CA:TRUE" not in out
+ assert "TLS Web Server Authentication" in out
+ verify = subprocess.run(["openssl", "verify", "-CAfile", cert_path, cert_path],
+ capture_output=True, text=True)
+ assert verify.returncode == 0, verify.stdout + verify.stderr
+
+
+def test_cached_ca_certificate_is_regenerated(tmp_path, monkeypatch):
+ from ministack.core import tls
+
+ _fresh_tls(tmp_path, monkeypatch)
+ tls_dir = tmp_path / "ministack-tls"
+ tls_dir.mkdir()
+ names = "".join(f"DNS:{h}," for h in tls._generated_cert_names())
+ subprocess.run([
+ "openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes",
+ "-keyout", str(tls_dir / "server.key"), "-out", str(tls_dir / "server.crt"),
+ "-days", "1", "-subj", "/CN=old", "-addext", "basicConstraints=critical,CA:TRUE",
+ "-addext", f"subjectAltName={names}DNS:localhost",
+ ], check=True, capture_output=True)
+ cert, _ = tls.resolve_tls_material()
+ assert "CA:FALSE" in subprocess.run(["openssl", "x509", "-in", cert, "-noout", "-ext", "basicConstraints"],
+ capture_output=True, text=True, check=True).stdout