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(elbv2): keep SslPolicy and the default certificate on HTTPS/TLS listeners (#2305)
Kacper Harabasz3fc524ec6ba9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 82692a83..340e4a87 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed- **Route 53 — private hosted zones keep their VPC** — `CreateHostedZone` with a `VPC` made a public zone and dropped the VPC, so Terraform's `aws_route53_zone` with a `vpc` block planned to add it again on every run; the zone is now private, the response carries the `VPC` instead of a delegation set, also on a `CallerReference` replay, and `GetHostedZone` returns its `VPCs`. Contributed by @kharabasz.+- **ELBv2 — listener `SslPolicy` and certificate** — `CreateListener` and `ModifyListener` dropped `SslPolicy` and `Certificates`, so `DescribeListeners` returned neither and Terraform re-planned `ssl_policy` on every HTTPS listener; HTTPS and TLS listeners now keep and return both, the policy defaults to `ELBSecurityPolicy-2016-08`, a listener moved to HTTP or TCP drops them, and more than one default certificate answers `ValidationError`. Contributed by @kharabasz.+## [1.5.30] — 2026-10-20### Addeddiff --git a/ministack/services/alb.py b/ministack/services/alb.pyindex 6d57236a..196d2a91 100644--- a/ministack/services/alb.py+++ b/ministack/services/alb.py@@ -521,19 +521,68 @@ def _action_xml(a):return f"<member>{inner}</member>"+# Security policy for HTTPS/TLS listeners created without one: the AWS docs+# name ELBSecurityPolicy-2016-08 as the default for listeners created through+# the API, CLI or CloudFormation (the console uses a newer one).+_DEFAULT_SSL_POLICY = "ELBSecurityPolicy-2016-08"+_SECURE_PROTOCOLS = ("HTTPS", "TLS")++def _listener_xml(l):acts = "".join(_action_xml(a) for a in l.get("DefaultActions", []))+ certs = "".join(+ f"<member><CertificateArn>{_esc(arn)}</CertificateArn></member>"+ for arn in l.get("Certificates", [])+ )+ certs_xml = f"<Certificates>{certs}</Certificates>" if certs else ""+ policy_xml = f"<SslPolicy>{_esc(l['SslPolicy'])}</SslPolicy>" if l.get("SslPolicy") else ""+ # Member order follows the botocore Listener shape.return (f"<member>"f"<ListenerArn>{l['ListenerArn']}</ListenerArn>"f"<LoadBalancerArn>{l['LoadBalancerArn']}</LoadBalancerArn>"f"<Port>{l.get('Port',80)}</Port>"f"<Protocol>{l.get('Protocol','HTTP')}</Protocol>"+ f"{certs_xml}{policy_xml}"f"<DefaultActions>{acts}</DefaultActions>"f"</member>")+def _parse_certificate_arns(params, prefix="Certificates"):+ arns, i = [], 1+ while _p(params, f"{prefix}.member.{i}.CertificateArn"):+ arns.append(_p(params, f"{prefix}.member.{i}.CertificateArn"))+ i += 1+ return arns+++def _default_certificate(params):+ """The listener's default certificate from ``Certificates``, which takes+ exactly one entry. Returns ``(arns, error)``."""+ certs = _parse_certificate_arns(params)+ if len(certs) > 1:+ return None, _error(+ "ValidationError",+ "Certificates must contain exactly one default certificate.")+ return certs, None+++def _apply_tls_settings(listener, certs, policy):+ """Store ``SslPolicy`` and the default certificate on an HTTPS/TLS+ listener, defaulting the policy when none is set. Any other protocol keeps+ no TLS settings, so a listener moved to HTTP/TCP loses them."""+ if listener.get("Protocol") not in _SECURE_PROTOCOLS:+ listener.pop("SslPolicy", None)+ listener.pop("Certificates", None)+ return+ if certs:+ listener["Certificates"] = certs+ if policy:+ listener["SslPolicy"] = policy+ listener.setdefault("SslPolicy", _DEFAULT_SSL_POLICY)++def _condition_xml(c):"""Render a condition the way AWS does: the flat Values list *and*, for thefields that have one, the typed config carrying the same values.@@ -807,6 +856,9 @@ def _create_listener(params):lb_arn = _p(params, "LoadBalancerArn")if lb_arn not in _lbs:return _error("LoadBalancerNotFound", f"Load balancer '{lb_arn}' not found.")+ certs, err = _default_certificate(params)+ if err:+ return errlid = _short_id()lb = _lbs[lb_arn]lb_name = lb["LoadBalancerName"]@@ -825,6 +877,7 @@ def _create_listener(params):"Protocol": _p(params, "Protocol", "HTTP"),"DefaultActions": actions,}+ _apply_tls_settings(listener, certs, _p(params, "SslPolicy"))_listeners[l_arn] = listener_listener_attrs[l_arn] = [{"Key": "routing.http.response.server.enabled", "Value": "true"},@@ -859,12 +912,16 @@ def _modify_listener(params):listener = _listeners.get(arn)if not listener:return _error("ListenerNotFound", f"Listener '{arn}' not found.")+ certs, err = _default_certificate(params)+ if err:+ return errport = _p(params, "Port")if port:listener["Port"] = int(port)protocol = _p(params, "Protocol")if protocol:listener["Protocol"] = protocol+ _apply_tls_settings(listener, certs, _p(params, "SslPolicy"))actions = _parse_actions(params, "DefaultActions")if actions:listener["DefaultActions"] = actionsdiff --git a/tests/test_alb.py b/tests/test_alb.pyindex 2d4f6580..dbdd3dc5 100644--- a/tests/test_alb.py+++ b/tests/test_alb.py@@ -1930,3 +1930,143 @@ def test_alb_http_header_condition_round_trips_and_matches(elbv2):finally:elbv2.delete_listener(ListenerArn=l_arn)elbv2.delete_load_balancer(LoadBalancerArn=lb_arn)++++_TLS_CERT = "arn:aws:acm:us-east-1:000000000000:certificate/tls-default"+_TLS_CERT_2 = "arn:aws:acm:us-east-1:000000000000:certificate/tls-second"+_TLS_ACTION = [{"Type": "fixed-response",+ "FixedResponseConfig": {"StatusCode": "404", "ContentType": "text/plain"}}]+++def tls_lb(elbv2):+ """A load balancer whose listeners and itself are removed after the test."""+ created = []++ def make(lb_type="application"):+ name = f"tls-{_uuid_mod.uuid4().hex[:12]}"+ lb = elbv2.create_load_balancer(Name=name, Type=lb_type)["LoadBalancers"][0]+ created.append(lb["LoadBalancerArn"])+ return lb["LoadBalancerArn"]++ yield make+ for lb_arn in created:+ for listener in elbv2.describe_listeners(LoadBalancerArn=lb_arn)["Listeners"]:+ elbv2.delete_listener(ListenerArn=listener["ListenerArn"])+ elbv2.delete_load_balancer(LoadBalancerArn=lb_arn)+++def test_elbv2_https_listener_returns_ssl_policy_and_certificate(elbv2, tls_lb):+ lb_arn = tls_lb()+ created = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ SslPolicy="ELBSecurityPolicy-TLS13-1-2-2021-06",+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]+ assert created["SslPolicy"] == "ELBSecurityPolicy-TLS13-1-2-2021-06"+ assert created["Certificates"] == [{"CertificateArn": _TLS_CERT}]++ described = elbv2.describe_listeners(ListenerArns=[created["ListenerArn"]])["Listeners"][0]+ assert described["SslPolicy"] == "ELBSecurityPolicy-TLS13-1-2-2021-06"+ assert described["Certificates"] == [{"CertificateArn": _TLS_CERT}]+++def test_elbv2_https_listener_defaults_ssl_policy(elbv2, tls_lb):+ lb_arn = tls_lb()+ created = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]+ assert created["SslPolicy"] == "ELBSecurityPolicy-2016-08"+ described = elbv2.describe_listeners(ListenerArns=[created["ListenerArn"]])["Listeners"][0]+ assert described["SslPolicy"] == "ELBSecurityPolicy-2016-08"+++def test_elbv2_tls_listener_on_network_lb_keeps_tls_settings(elbv2, tls_lb):+ lb_arn = tls_lb("network")+ created = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="TLS", Port=443,+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]+ described = elbv2.describe_listeners(ListenerArns=[created["ListenerArn"]])["Listeners"][0]+ assert described["SslPolicy"] == "ELBSecurityPolicy-2016-08"+ assert described["Certificates"] == [{"CertificateArn": _TLS_CERT}]+++def test_elbv2_modify_listener_updates_ssl_policy_and_certificate(elbv2, tls_lb):+ lb_arn = tls_lb()+ l_arn = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]["ListenerArn"]++ modified = elbv2.modify_listener(+ ListenerArn=l_arn, SslPolicy="ELBSecurityPolicy-TLS13-1-2-2021-06",+ Certificates=[{"CertificateArn": _TLS_CERT_2}],+ )["Listeners"][0]+ assert modified["SslPolicy"] == "ELBSecurityPolicy-TLS13-1-2-2021-06"+ assert modified["Certificates"] == [{"CertificateArn": _TLS_CERT_2}]++ # A modify that names neither keeps both.+ elbv2.modify_listener(ListenerArn=l_arn, Port=8443)+ described = elbv2.describe_listeners(ListenerArns=[l_arn])["Listeners"][0]+ assert described["SslPolicy"] == "ELBSecurityPolicy-TLS13-1-2-2021-06"+ assert described["Certificates"] == [{"CertificateArn": _TLS_CERT_2}]+++def test_elbv2_modify_listener_to_http_drops_tls_settings(elbv2, tls_lb):+ lb_arn = tls_lb()+ l_arn = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ SslPolicy="ELBSecurityPolicy-TLS13-1-2-2021-06",+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]["ListenerArn"]++ elbv2.modify_listener(ListenerArn=l_arn, Protocol="HTTP", Port=80)+ described = elbv2.describe_listeners(ListenerArns=[l_arn])["Listeners"][0]+ assert described["Protocol"] == "HTTP"+ assert "SslPolicy" not in described+ assert "Certificates" not in described++ # Back to HTTPS: nothing stale returns, the policy is the default again.+ elbv2.modify_listener(ListenerArn=l_arn, Protocol="HTTPS", Port=443,+ Certificates=[{"CertificateArn": _TLS_CERT_2}])+ described = elbv2.describe_listeners(ListenerArns=[l_arn])["Listeners"][0]+ assert described["SslPolicy"] == "ELBSecurityPolicy-2016-08"+ assert described["Certificates"] == [{"CertificateArn": _TLS_CERT_2}]+++def test_elbv2_http_listener_keeps_no_tls_settings(elbv2, tls_lb):+ lb_arn = tls_lb()+ created = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTP", Port=80,+ SslPolicy="ELBSecurityPolicy-TLS13-1-2-2021-06",+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]+ assert "SslPolicy" not in created+ assert "Certificates" not in created+ described = elbv2.describe_listeners(ListenerArns=[created["ListenerArn"]])["Listeners"][0]+ assert "SslPolicy" not in described+ assert "Certificates" not in described+++def test_elbv2_listener_rejects_more_than_one_default_certificate(elbv2, tls_lb):+ lb_arn = tls_lb()+ two = [{"CertificateArn": _TLS_CERT}, {"CertificateArn": _TLS_CERT_2}]+ with pytest.raises(ClientError) as exc:+ elbv2.create_listener(LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ Certificates=two, DefaultActions=_TLS_ACTION)+ assert exc.value.response["Error"]["Code"] == "ValidationError"+ assert elbv2.describe_listeners(LoadBalancerArn=lb_arn)["Listeners"] == []++ l_arn = elbv2.create_listener(+ LoadBalancerArn=lb_arn, Protocol="HTTPS", Port=443,+ Certificates=[{"CertificateArn": _TLS_CERT}], DefaultActions=_TLS_ACTION,+ )["Listeners"][0]["ListenerArn"]+ with pytest.raises(ClientError) as exc:+ elbv2.modify_listener(ListenerArn=l_arn, Port=8443, Certificates=two)+ assert exc.value.response["Error"]["Code"] == "ValidationError"+ described = elbv2.describe_listeners(ListenerArns=[l_arn])["Listeners"][0]+ assert described["Port"] == 443+ assert described["Certificates"] == [{"CertificateArn": _TLS_CERT}]