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 RDS TLS certificates for long hostnames (#1787)
Jay Janssena66737789d43
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fd293ef8..1892ae9d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **RDS — PostgreSQL TLS with long endpoint names** — use a short certificate common name while retaining complete DNS and IP subject alternative names, so endpoints longer than 64 bytes no longer fail certificate generation.- **API Gateway — an OpenAPI body's `securityDefinitions` reach the methods** — the import discarded security outright, so a SAM API declaring a Cognito authorizer created none and every method imported as `authorizationType: NONE`, serving anonymous callers where AWS answers 401. Each scheme carrying `x-amazon-apigateway-authorizer` now becomes an authorizer, and an operation's `security` — or the document's — sets the method's `authorizationType`, `authorizerId` and `authorizationScopes`, so the enforcement added in 1.5.10 engages for body-defined APIs.- **CloudFormation — `AWS::ApiGateway::Stage` method settings reach the stage as a map** — the template's `MethodSettings` list was stored verbatim, so the throttling lookup added in 1.5.14 raised on it and every request to a CloudFormation- or SAM-deployed API answered 500. The list is now keyed `"<resourcePath>/<httpMethod>"`, `"*/*"` for the stage-wide entry, over the account-level defaults AWS reports from `GetStage`.diff --git a/ministack/services/rds.py b/ministack/services/rds.pyindex 727a5cfa..e1ca52b5 100644--- a/ministack/services/rds.py+++ b/ministack/services/rds.py@@ -1093,7 +1093,9 @@ def _pg_server_material(names, ips) -> tuple[str, str]:ca_cert, ca_key = _ensure_pg_ca()cert_pem, key_pem, _public = sign_leaf_certificate(ca_cert, ca_key,- common_name=(names[0] if names else "localhost"),+ # X.509 limits CN to 64 bytes; valid RDS DNS endpoints can be longer.+ # Connection identities belong in SANs, preserved in full below.+ common_name="Ministack RDS Server",san_dns=names, san_ips=ips,)return cert_pem, key_pemdiff --git a/tests/test_rds_tls_material.py b/tests/test_rds_tls_material.pynew file mode 100644index 00000000..9fb57d26--- /dev/null+++ b/tests/test_rds_tls_material.py@@ -0,0 +1,88 @@+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).+"""In-process certificate tests; no Docker or running MiniStack is needed.++Run with ``pytest --noconftest tests/test_rds_tls_material.py`` to avoid the+integration suite's server-reset fixture.+"""++import ipaddress+import ssl++import pytest++pytest.importorskip("cryptography")++from cryptography import x509 # noqa: E402+from cryptography.x509.oid import NameOID # noqa: E402++from ministack.services import rds # noqa: E402+++def _verify(ca_pem, cert_pem, key_pem, hostname, tmp_path):+ """Perform a real verifying TLS handshake entirely through memory BIOs."""+ chain = tmp_path / "server.pem"+ chain.write_text(cert_pem + key_pem)+ server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)+ server_context.load_cert_chain(chain)+ client_context = ssl.create_default_context(cadata=ca_pem)+ client_context.verify_flags |= ssl.VERIFY_X509_STRICT+ client_context.hostname_checks_common_name = False+ client_in, client_out = ssl.MemoryBIO(), ssl.MemoryBIO()+ server_in, server_out = ssl.MemoryBIO(), ssl.MemoryBIO()+ client = client_context.wrap_bio(client_in, client_out, server_hostname=hostname)+ server = server_context.wrap_bio(server_in, server_out, server_side=True)+ done = set()+ for _ in range(10):+ for endpoint in (client, server):+ if endpoint not in done:+ try:+ endpoint.do_handshake()+ done.add(endpoint)+ except ssl.SSLWantReadError:+ pass+ server_in.write(client_out.read())+ client_in.write(server_out.read())+ if len(done) == 2:+ return+ pytest.fail("TLS handshake did not complete")+++def ca_pem(monkeypatch):+ # Leave module-level CA state exactly as it was before this unit test.+ monkeypatch.setattr(rds, "_pg_ca", None)+ return rds.pg_ca_cert_pem()++[email protected]("hostname", [+ "db.example.test",+ "db-" + "a" * 37 + ".cluster-abcdefghijkl.us-east-1.rds.amazonaws.com",+])+def test_pg_certificate_preserves_and_verifies_all_sans(hostname, ca_pem, tmp_path):+ names = [hostname, "reader." + hostname, "localhost"]+ ips = ["127.0.0.1", "::1"]+ cert_pem, key_pem = rds._pg_server_material(names, ips)+ cert = x509.load_pem_x509_certificate(cert_pem.encode())+ cn = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value+ assert len(cn.encode("utf-8")) <= 64+ sans = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value+ assert sans.get_values_for_type(x509.DNSName) == names+ assert sans.get_values_for_type(x509.IPAddress) == [ipaddress.ip_address(ip) for ip in ips]+ for name in names + ips:+ _verify(ca_pem, cert_pem, key_pem, name, tmp_path)+ with pytest.raises(ssl.SSLCertVerificationError):+ _verify(ca_pem, cert_pem, key_pem, "unlisted.example.test", tmp_path)+++def test_pg_certificate_without_dns_names_verifies_ip(ca_pem, tmp_path):+ cert_pem, key_pem = rds._pg_server_material([], ["127.0.0.1"])+ _verify(ca_pem, cert_pem, key_pem, "127.0.0.1", tmp_path)+++def test_pg_certificate_crypto_failure_propagates(monkeypatch, ca_pem):+ from ministack.core import x509_utils++ monkeypatch.setattr(x509_utils, "HAS_CRYPTO", False)+ with pytest.raises(RuntimeError, match="requires the `cryptography` package"):+ rds._pg_server_material(["localhost"], [])