Skip to content

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

fix(iot): device certificates take the mode of their CA (#2190)

iot-rocketf91b9e0e90d4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c31fac5b..103ecc3b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **IoT — device certificates take the mode of their CA** — a device certificate registered under a CA in `SNI_ONLY` mode, with or without `caCertificatePem`, or created by just-in-time registration reported `certificateMode` `DEFAULT`; it now takes its CA's mode, and RegisterCertificateWithoutCA stays `SNI_ONLY`. Contributed by @iot-rocket.
- **IoT — deletes of unknown resources and `DeleteThing` `expectedVersion`** — `DeleteThing`, `DeleteThingType`, `DeleteThingGroup`, `RemoveThingFromThingGroup` with an unknown group and `DetachPolicy` with an unknown policy answered `ResourceNotFoundException`, and `DeleteThing` ignored `expectedVersion`; they now answer 200 as AWS does, and `DeleteThing` refuses a wrong `expectedVersion` with `VersionConflictException` before the attached-principal check. Contributed by @iot-rocket.
- **CloudFormation — IoT policy and thing type deletes check what still uses them** — a stack delete removed an `AWS::IoT::Policy` or `AWS::IoT::ThingType` directly, even while a certificate outside the stack had the policy attached or a thing used the type; the policy delete now removes the non-default versions and then calls `DeletePolicy`, the thing type delete deprecates the type and then calls `DeleteThingType`, so a resource still in use fails its delete with the service's message and the stack ends in `DELETE_FAILED`, as on AWS. Contributed by @iot-rocket.
- **IoT — MQTT 5 message expiry** — the broker forwarded the Message Expiry Interval unchanged and never expired a message; a subscriber now receives the seconds left (60 arrives as 59, 0 counts as 1, intervals above seven days are capped at 604800), an expired retained message is no longer replayed to new subscribers or returned by `GetRetainedMessage` and `ListRetainedMessages`, and a QoS 1 message queued for an offline persistent session is dropped once it expires (the queue also keeps the MQTT 5 properties now). Contributed by @iot-rocket.
diff --git a/ministack/services/iot.py b/ministack/services/iot.py
index 6974290f..d97822e8 100644
--- a/ministack/services/iot.py
+++ b/ministack/services/iot.py
@@ -2719,12 +2719,14 @@ def _provisioning_thing_changes(
def _certificate_record(
cert_id: str, cert_pem: str, status: str, ca_id: str | None = None
) -> dict:
- """The registry record of a device certificate, owned by the current scope."""
+ """The registry record of a device certificate, owned by the current scope; it takes its CA's mode."""
+ ca = _ca_certificates.get(ca_id) if ca_id else None
return {
"certificateId": cert_id,
"certificateArn": _cert_arn(cert_id),
"certificatePem": cert_pem, # verbatim
"status": status,
+ "certificateMode": (ca or {}).get("certificateMode", "DEFAULT"),
"creationDate": _now_epoch(),
"ownedBy": get_account_id(),
"caCertificateId": ca_id,
diff --git a/tests/test_iot.py b/tests/test_iot.py
index f0d4b6e1..df98b9ea 100644
--- a/tests/test_iot.py
+++ b/tests/test_iot.py
@@ -9766,6 +9766,41 @@ def test_iot_register_certificate_without_ca_pem_links_the_signing_ca(iot_client
_delete_certificates_and_cas(iot_client, cert_ids, ca_ids)
+def test_iot_register_certificate_takes_the_mode_of_its_ca(iot_client):
+ """A certificate registered under a CA reports the CA's mode; RegisterCertificateWithoutCA stays SNI_ONLY."""
+ modes, ca_ids, cert_ids = {}, [], []
+ try:
+ for ca_mode in ("SNI_ONLY", "DEFAULT"):
+ ca_pem, verification_pem, leaves = _generate_ca_and_leaves(
+ _registration_code(iot_client), count=3)
+ ca_ids.append(iot_client.register_ca_certificate(
+ caCertificate=ca_pem, setAsActive=True, certificateMode=ca_mode,
+ **({"verificationCertificate": verification_pem} if ca_mode == "DEFAULT" else {}),
+ )["certificateId"])
+ with_ca_pem, no_ca_pem, without_ca = leaves
+ registered = {
+ "with-ca-pem": iot_client.register_certificate(
+ certificatePem=with_ca_pem, caCertificatePem=ca_pem, status="ACTIVE"),
+ "no-ca-pem": iot_client.register_certificate(certificatePem=no_ca_pem, status="ACTIVE"),
+ "without-ca": iot_client.register_certificate_without_ca(
+ certificatePem=without_ca, status="ACTIVE"),
+ }
+ for case, resp in registered.items():
+ cert_ids.append(resp["certificateId"])
+ modes[ca_mode, case] = iot_client.describe_certificate(
+ certificateId=resp["certificateId"])["certificateDescription"]["certificateMode"]
+ assert modes == {
+ ("SNI_ONLY", "with-ca-pem"): "SNI_ONLY",
+ ("SNI_ONLY", "no-ca-pem"): "SNI_ONLY",
+ ("SNI_ONLY", "without-ca"): "SNI_ONLY",
+ ("DEFAULT", "with-ca-pem"): "DEFAULT",
+ ("DEFAULT", "no-ca-pem"): "DEFAULT",
+ ("DEFAULT", "without-ca"): "SNI_ONLY",
+ }
+ finally:
+ _delete_certificates_and_cas(iot_client, cert_ids, ca_ids)
+
+
def _assert_register_refused(iot_client, http_status, code, message, **kwargs):
with pytest.raises(ClientError) as ei:
iot_client.register_certificate(**kwargs)
diff --git a/tests/test_iot_data.py b/tests/test_iot_data.py
index 7133a1bf..e9ab8cf9 100644
--- a/tests/test_iot_data.py
+++ b/tests/test_iot_data.py
@@ -4727,7 +4727,8 @@ def test_mtls_jitr_auto_registers_an_unknown_cert_without_connack(broker, tmp_pa
def test_mtls_jitr_under_an_sni_only_ca_needs_sni(broker, tmp_path):
"""A CA in SNI_ONLY mode auto-registers a device only when its ClientHello
- carries a server name. Without one the certificate stays unknown and is
+ carries a server name, and the certificate it creates is in SNI_ONLY mode
+ as on AWS. Without one the certificate stays unknown and is
refused like any other unknown certificate (CONNACK 5)."""
from ministack.core.x509_utils import generate_ca, get_certificate_id, sign_leaf_certificate
@@ -4756,6 +4757,7 @@ def test_mtls_jitr_under_an_sni_only_ca_needs_sni(broker, tmp_path):
desc = iot.describe_certificate(certificateId=cert_id)["certificateDescription"]
assert desc["status"] == "PENDING_ACTIVATION"
assert desc["caCertificateId"] == ca_id
+ assert desc["certificateMode"] == "SNI_ONLY"
finally:
try:
iot.update_certificate(certificateId=cert_id, newStatus="INACTIVE")