Skip to content

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

fix(iot): trust every registered certificate on the mTLS listener (#1927)

iot-rocket47387e68f84b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3044856a..e19049d1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,7 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
- **ECS — forced rolling deployments replace tasks and refresh images** — `UpdateService(forceNewDeployment=True)` replaces tasks even when the task definition is unchanged or omitted. Rolling deployments preserve scheduling limits, wait for replacement tasks to stabilize, and retain circuit-breaker rollback; zero-sized deployments drain old tasks and complete. Docker-backed services pin image digests per deployment, honor `versionConsistency: disabled`, and use Secrets Manager credentials for private registries. Tasks report resolved image digests and Fargate platform fields. Cached images remain usable on both launch types, including MiniStack ECR images, preserving MiniStack’s intentional offline fallback.
-
+- **IoT — mTLS trusts every registered device certificate** — the listener no longer fails the TLS handshake for an ACTIVE certificate whose CA was deactivated or deleted before its first connect, or that was registered without a CA; as on AWS, only the certificate's own status refuses it.
### Added
- **Athena — databases, DDL, and Trino-style table references** — `ListDatabases` and `GetDatabase` read the Glue Data Catalog; `CREATE EXTERNAL TABLE` and `DROP TABLE` apply to it, creating the Glue table Athena would (formats, SerDe, TBLPROPERTIES); `CREATE TABLE` without `EXTERNAL` is rejected as Athena rejects it, and Iceberg tables are not supported; a query may name a table as `"awsdatacatalog"."db"."t"` or `db.t`.
@@ -34,6 +34,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+
- **SES v2 — `ListEmailIdentities` and `ListConfigurationSets` answer the routes newer SDKs use** — botocore 1.43.106 sends them as `POST /v2/email/list-identities` and `POST /v2/email/list-configuration-sets` with `NextToken`, `PageSize` and `Filter` in the body; those paths answered `NotFoundException`. Both forms page, and the `Filter` keys are applied.
- **Kinesis — `ApproximateArrivalTimestamp` keeps milliseconds** — it was truncated to whole seconds, so an `AT_TIMESTAMP` iterator from an SDK that sends fractional seconds skipped records written earlier in the same second.
- **CloudFormation — an empty `Capabilities` list is accepted** — botocore sends it as a bare `Capabilities=`, which was read as one empty value and refused, so `aws cloudformation deploy` without `--capabilities` and `Capabilities=[]` from an SDK failed with a `ValidationError` since 1.5.11.
diff --git a/ministack/services/iot.py b/ministack/services/iot.py
index dced825d..c6bf5528 100644
--- a/ministack/services/iot.py
+++ b/ministack/services/iot.py
@@ -7823,9 +7823,10 @@ async def handle_websocket(
# a handshake that sends no CONNECT registers nothing.
#
# The TLS layer adds one constraint of its own: a presented chain is verified,
-# so a certificate from a CA the listener does not trust fails the handshake
-# before any of the above. The trusted set is the Local CA plus every ACTIVE
-# CA in the registry.
+# so a certificate the listener does not trust fails the handshake before any
+# of the above. The trusted set is the Local CA plus every registered CA and
+# device certificate, whatever its status: as on AWS, a registered device
+# certificate keeps connecting after its CA is deactivated or deleted.
_MTLS_DEFAULT_PORT = 8883
_MTLS_READ_CHUNK = 65536
@@ -7969,31 +7970,16 @@ def _mtls_ensure_server_cert() -> tuple[str, str]:
return cert_pem, key_pem
-def _mtls_registered_ca_pems() -> list[str]:
- """ACTIVE CA certificates from the CA registry, across every scope.
-
- The store is scoped by account and region; a single listener serves every
- tenant, so it is read across all scopes. Only ACTIVE CAs qualify:
- registering a CA without activating it is how AWS says "not yet", and
- honouring that keeps ``UpdateCACertificate`` meaningful for connections
- opened after it.
- """
- pems = []
- for record in list(_ca_certificates._data.values()):
- if not isinstance(record, dict) or record.get("status") != "ACTIVE":
- continue
- pem = record.get("certificatePem")
- if isinstance(pem, str) and pem.strip():
- pems.append(pem)
- return pems
-
-
-def _mtls_trust_anchors() -> list[str]:
- """CAs whose client certificates verify at the TLS layer: the Local CA plus
- every ACTIVE registered CA. Presenting a certificate is optional, so this
- set only decides which presented chains survive the handshake; who a
- session belongs to is decided afterwards, in ``_mtls_serve_conn``."""
- return [get_ca_cert_pem(), *_mtls_registered_ca_pems()]
+def _mtls_trust_anchors() -> list[tuple[str, str]]:
+ """(id, PEM) of the Local CA and every registered CA and device certificate, across every scope and status."""
+ local = get_ca_cert_pem()
+ anchors = [(hashlib.sha256(local.encode("utf-8")).hexdigest(), local)]
+ for store in (_ca_certificates, _certificates):
+ for record in list(store._data.values()):
+ pem = record.get("certificatePem") if isinstance(record, dict) else None
+ if isinstance(pem, str) and pem.strip():
+ anchors.append((record.get("certificateId") or hashlib.sha256(pem.encode("utf-8")).hexdigest(), pem))
+ return anchors
def _mtls_refresh_trust_anchors(ctx: ssl.SSLContext) -> None:
@@ -8001,17 +7987,15 @@ def _mtls_refresh_trust_anchors(ctx: ssl.SSLContext) -> None:
Anchors are loaded one PEM at a time so that a single unparseable
registered certificate costs only itself, and each is remembered — by
- digest, including the ones that failed — so a handshake never repeats the
- work or the warning. ``load_verify_locations`` is additive and a live
- context cannot forget an anchor, so a CA deactivated after loading stays
- trusted at the TLS layer until ``reset()`` rebinds; the session-attribution
- lookup still runs per connection.
+ certificate id, including the ones that failed — so a handshake never
+ repeats the work or the warning. ``load_verify_locations`` is additive, so an anchor
+ stays loaded after its record is gone; whether a presented certificate is
+ served is decided per connection by the registry lookup.
"""
- for pem in _mtls_trust_anchors():
- digest = hashlib.sha256(pem.encode("utf-8")).hexdigest()
- if digest in _mtls_loaded_anchors:
+ for anchor_id, pem in _mtls_trust_anchors():
+ if anchor_id in _mtls_loaded_anchors:
continue
- _mtls_loaded_anchors.add(digest)
+ _mtls_loaded_anchors.add(anchor_id)
try:
ctx.load_verify_locations(cadata=pem)
except Exception as e:
@@ -8021,9 +8005,9 @@ def _mtls_refresh_trust_anchors(ctx: ssl.SSLContext) -> None:
def _mtls_on_client_hello(
ssl_object: ssl.SSLObject, server_name: str | None, ctx: ssl.SSLContext
) -> None:
- """Pull newly registered CAs in mid-handshake, before the peer is verified.
+ """Pull newly registered certificates in mid-handshake, before the peer is verified.
- A CA registered while the listener is bound has to be trusted without a
+ A certificate registered while the listener is bound has to be trusted without a
restart, and ``sni_callback`` is the only hook that runs late enough to see
the current registry yet early enough to matter. It runs on every
handshake, including those carrying no server name — which is what a
@@ -8047,6 +8031,8 @@ def _mtls_build_ssl_context() -> ssl.SSLContext:
# signal; absence of one falls back the same way an unsigned WS upgrade
# does.
ctx.verify_mode = ssl.CERT_OPTIONAL
+ # A registered device certificate is an anchor of its own, whatever signed it.
+ ctx.verify_flags |= ssl.VERIFY_X509_PARTIAL_CHAIN
cert_pem, key_pem = _mtls_ensure_server_cert()
# load_cert_chain only takes paths; a combined cert+key file is the least
# material to leave on disk, and it is unlinked before the handshake.
diff --git a/tests/conftest.py b/tests/conftest.py
index 8c56a354..f8042d8b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -318,6 +318,7 @@ _SERIAL_TESTS = {
"tests/test_iot_data.py::test_mtls_mqtt5_refusal_is_v5_connack",
"tests/test_iot_data.py::test_mtls_inactive_cert_refused",
"tests/test_iot_data.py::test_mtls_ambiguous_cert_is_refused",
+ "tests/test_iot_data.py::test_mtls_registered_cert_connects_whatever_its_ca",
"tests/test_iot_data.py::test_mtls_registered_ca_chain_connects",
"tests/test_iot_data.py::test_mtls_jitr_auto_registers_an_unknown_cert_without_connack",
"tests/test_iot_data.py::test_mtls_account_scoped_delivery",
diff --git a/tests/test_iot_data.py b/tests/test_iot_data.py
index eec6c0a3..91affb70 100644
--- a/tests/test_iot_data.py
+++ b/tests/test_iot_data.py
@@ -3531,13 +3531,60 @@ def test_mtls_ambiguous_cert_is_refused(broker, tmp_path):
peer.close()
+def test_mtls_registered_cert_connects_whatever_its_ca(broker, tmp_path):
+ """An ACTIVE device certificate connects after its CA is deactivated or
+ deleted, and when it was registered without a CA, as on AWS; only its own
+ status refuses it. The CAs change before the first handshake, so none of
+ them was ever loaded while ACTIVE."""
+ from ministack.core.x509_utils import generate_ca, sign_leaf_certificate
+
+ iot = broker.client("iot")
+ devices = {}
+ for case in ("inactive-ca", "deleted-ca", "without-ca"):
+ if case == "without-ca":
+ ca_pem, ca_key = generate_ca(common_name=_unique(case))
+ leaf_pem, leaf_key, _public = sign_leaf_certificate(ca_pem, ca_key, common_name=case)
+ cert_id = iot.register_certificate_without_ca(certificatePem=leaf_pem, status="ACTIVE")[
+ "certificateId"
+ ]
+ else:
+ ca_pem, ca_key, verification_pem = iot_test_ca(
+ iot.get_registration_code()["registrationCode"], _unique(case)
+ )
+ leaf_pem, leaf_key, _public = sign_leaf_certificate(ca_pem, ca_key, common_name=case)
+ ca_id = iot.register_ca_certificate(
+ caCertificate=ca_pem, verificationCertificate=verification_pem, setAsActive=True
+ )["certificateId"]
+ cert_id = iot.register_certificate(
+ certificatePem=leaf_pem, caCertificatePem=ca_pem, setAsActive=True
+ )["certificateId"]
+ iot.update_ca_certificate(certificateId=ca_id, newStatus="INACTIVE")
+ if case == "deleted-ca":
+ iot.delete_ca_certificate(certificateId=ca_id)
+ devices[case] = cert_id, leaf_pem, leaf_key
+
+ for case, (_cert_id, leaf_pem, leaf_key) in devices.items():
+ peer = _Peer(_mtls_connect(broker, leaf_pem, leaf_key, tmp_path))
+ try:
+ _assert_connack(peer.connect(_unique(case)))
+ finally:
+ peer.close()
+
+ cert_id, leaf_pem, leaf_key = devices["inactive-ca"]
+ iot.update_certificate(certificateId=cert_id, newStatus="INACTIVE")
+ peer = _Peer(_mtls_connect(broker, leaf_pem, leaf_key, tmp_path))
+ try:
+ _assert_connack(peer.connect(_unique("inactive-cert")), return_code=5)
+ finally:
+ peer.close()
+
+
def test_mtls_registered_ca_chain_connects(broker, tmp_path):
"""A leaf signed by a CA registered through the API connects.
- The listener reads its trust anchors out of the IoT CA registry on every
- handshake, so this pins both properties that follow from that: a CA
- registered long after the listener bound is trusted without a restart, and
- only an ACTIVE one is.
+ The listener reads its trust anchors out of the IoT registry on every
+ handshake, so a CA registered long after the listener bound is trusted
+ without a restart.
"""
from ministack.core.x509_utils import sign_leaf_certificate
@@ -3545,9 +3592,9 @@ def test_mtls_registered_ca_chain_connects(broker, tmp_path):
ca_pem, ca_key, verification_pem = iot_test_ca(
iot.get_registration_code()["registrationCode"], "Registered Device CA"
)
- ca_id = iot.register_ca_certificate(
- caCertificate=ca_pem, verificationCertificate=verification_pem, setAsActive=False
- )["certificateId"]
+ iot.register_ca_certificate(
+ caCertificate=ca_pem, verificationCertificate=verification_pem, setAsActive=True
+ )
leaf_pem, leaf_key, _public = sign_leaf_certificate(
ca_pem, ca_key, common_name="registered-ca-device"
)
@@ -3555,15 +3602,6 @@ def test_mtls_registered_ca_chain_connects(broker, tmp_path):
certificatePem=leaf_pem, caCertificatePem=ca_pem, setAsActive=True
)
- # The leaf itself is registered ACTIVE, so its CA's status is the only thing
- # left that can refuse it — and an untrusted anchor is refused by TLS,
- # below MQTT.
- assert _refused_below_mqtt(
- broker, leaf_pem, leaf_key, tmp_path, _unique("too-early")
- ), "a leaf signed by an INACTIVE CA reached the broker"
-
- iot.update_ca_certificate(certificateId=ca_id, newStatus="ACTIVE")
-
peer = _Peer(_mtls_connect(broker, leaf_pem, leaf_key, tmp_path))
try:
_assert_connack(peer.connect(_unique("registered-ca-device")))