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(iot): acknowledge MQTT 5 QoS 1 publishes with Success (#2187)
iot-rocketd96d1ef7b8ab
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 397ed3e9..171e7885 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]### Fixed+- **IoT Core — MQTT 5 PUBACK reports Success without subscribers** — a QoS 1 PUBLISH that no subscriber matched (including a topic only a rule reads, Basic Ingest and a retained publish) was acknowledged with reason code `0x10` No matching subscribers; it now gets `0x00` Success, as AWS IoT answers over both MQTT over TLS and MQTT over WebSocket. Contributed by @iot-rocket.- **IoT — HTTP `Publish` keeps the MQTT 5 properties and a literal `%` in the topic** — `Publish` ignored `userProperties`, `payloadFormatIndicator`, `contentType`, `responseTopic`, `correlationData` and `messageExpiry`, so MQTT 5 subscribers got none of them and `GetRetainedMessage` returned no `userProperties`; they now reach subscribers and the retained message, and invalid values are refused with AWS's `InvalidRequestException` messages. `Publish`, `GetRetainedMessage` and the `AUTH=true` topic ARN decoded the topic a second time, so `p%41q` became `pAq`; the topic is now kept as sent. `/_ministack/reset` kept messages retained over MQTT when no iot-data call had loaded that module; it now clears them. Contributed by @iot-rocket.- **CloudFormation — AppSync data sources and resolvers keep their configuration** — `AWS::AppSync::DataSource` dropped `Description`, `HttpConfig` and `EventBridgeConfig` and stored `DynamoDBConfig` and `LambdaConfig` in the template's PascalCase, and `AWS::AppSync::Resolver` dropped `Kind`, `Runtime`, `Code`, `PipelineConfig` and `MaxBatchSize`, so a DynamoDB resolver from a stack answered `null` and an `APPSYNC_JS` resolver never ran; both now go through `CreateDataSource` and `CreateResolver` and read back as on AWS, and a new data source `Name` or resolver `TypeName` or `FieldName` is reported as a replacement. `GetDataSource` and `GetResolver` leave out a description, service role or mapping template that was never set, a DynamoDB data source reports `useCallerCredentials` and `versioned`, an EventBridge one keeps `eventBridgeConfig`, and a resolver reports `maxBatchSize`. Contributed by @iot-rocket.- **CloudFormation — `AWS::AutoScaling::LifecycleHook` updates** — a stack update ran the create again, so a member dropped from the template went back to its default and a hook with a generated name moved to another group stayed on the old group too; the hook now updates in place and keeps a dropped member, and an `AutoScalingGroupName` or `LifecycleHookName` change replaces it. `NotificationMetadata` is stored and returned by `DescribeLifecycleHooks`, and a group deleted by its stack takes its lifecycle hooks along. Contributed by @iot-rocket.diff --git a/ministack/services/iot.py b/ministack/services/iot.pyindex 1a4a64c9..550257b3 100644--- a/ministack/services/iot.py+++ b/ministack/services/iot.py@@ -9629,14 +9629,11 @@ async def broker_publish(properties: bytes = b"",client_id: str | None = None,principal: str | None = None,-) -> int:+) -> None:"""Deliver a message to every matching subscriber.``properties`` is an encoded MQTT 5 property block forwarded verbatim to- v5 subscribers (empty for a 3.1.1 publisher or an HTTP publish). Returns- how many recipients the message reached — subscribers delivered to plus- offline persistent sessions it was queued for — which is what an MQTT 5- PUBACK reports as Success versus No matching subscribers.+ v5 subscribers (empty for a 3.1.1 publisher or an HTTP publish).``client_id`` names the publishing MQTT client, which is what a No Localsubscription is defined against (§3.8.3.1): that subscriber does not want@@ -9647,7 +9644,6 @@ async def broker_publish(``principal`` is who published the message, what the rule SQL``principal()`` returns; rules also see a fresh trace id per message."""- delivered = 0# Basic Ingest: a publish to `$aws/rules/<ruleName>` is delivered straight# to that rule's actions and bypasses pub/sub delivery entirely.if topic.startswith(_BASIC_INGEST_PREFIX):@@ -9664,7 +9660,7 @@ async def broker_publish(remainder[1] if len(remainder) > 1 else "",client_id,)- return delivered+ returnscoped = _scoped_topic(account_id, region, topic)@@ -9700,7 +9696,6 @@ async def broker_publish(payload,effective_qos,)- delivered += 1except Exception:_broker_logger.exception("IoT broker: subscriber %s delivery failed", sub.subscription_id)finally:@@ -9722,7 +9717,6 @@ async def broker_publish(ps.queued_messages.append((topic, payload, qos))if len(ps.queued_messages) > _MAX_QUEUED_MESSAGES:ps.queued_messages = ps.queued_messages[-_MAX_QUEUED_MESSAGES:]- delivered += 1breakwith _rule_message_scope(principal):@@ -9760,8 +9754,6 @@ async def broker_publish("IoT jobs bridge failed for %r", topic, exc_info=True)- return delivered-async def broker_subscribe(account_id: str,@@ -9991,7 +9983,6 @@ CONNACK_311_NOT_AUTHORIZED = 0x05# MQTT 5 reason codes, only the ones this broker produces (§2.4). Success and# Normal disconnection share the value 0x00.RC5_SUCCESS = 0x00-RC5_NO_MATCHING_SUBSCRIBERS = 0x10RC5_NO_SUBSCRIPTION_EXISTED = 0x11RC5_MALFORMED_PACKET = 0x81RC5_NOT_AUTHORIZED = 0x87@@ -10935,7 +10926,7 @@ class _WSSession:return Truereturn await self._close_unauthorized()payload = body[off:]- delivered = await broker_publish(+ await broker_publish(self.account_id,self.region,topic,@@ -10947,9 +10938,9 @@ class _WSSession:principal=self._principal,)if qos == 1 and packet_id is not None:+ # Success also when nobody subscribes, as AWS IoT answers.if self.protocol_version == MQTT_5:- reason = RC5_SUCCESS if delivered else RC5_NO_MATCHING_SUBSCRIBERS- await self.send_bytes(_make_puback(packet_id, reason_code=reason))+ await self.send_bytes(_make_puback(packet_id, reason_code=RC5_SUCCESS))else:await self.send_bytes(_make_puback(packet_id))fleet = _parse_fleet_provisioning_topic(topic)diff --git a/tests/test_iot_data.py b/tests/test_iot_data.pyindex e24b8665..dbecd2cb 100644--- a/tests/test_iot_data.py+++ b/tests/test_iot_data.py@@ -2967,7 +2967,7 @@ def test_mqtt5_publisher_reaches_mqtt311_subscriber():def test_mqtt5_qos1_puback_carries_reason_code():- """QoS 1 PUBACK reports Success with a subscriber, No matching subscribers without."""+ """QoS 1 PUBACK reports Success with a subscriber and without one."""topic = _unique("v5/qos1")async def scenario():@@ -2990,7 +2990,7 @@ def test_mqtt5_qos1_puback_carries_reason_code():return lonely, matchedlonely, matched = _run(scenario())- assert lonely == b"\x00\x07\x10\x00", "packet id 7, No matching subscribers, no properties"+ assert lonely == b"\x00\x07\x00\x00", "packet id 7, Success, no properties"assert matched == b"\x00\x08\x00\x00", "packet id 8, Success, no properties"@@ -3282,9 +3282,7 @@ def test_mqtt5_no_local_withholds_a_publisher_its_own_message():(pkt_type, _flags, puback), delivered = _run(scenario())assert pkt_type == PKT_PUBACK, "the publisher's own message must not come back"- assert puback == b"\x00\x07\x10\x00", (- "No matching subscribers: the only subscription was the publisher's own"- )+ assert puback == b"\x00\x07\x00\x00", "packet id 7, Success, no properties"assert _parse_publish(delivered, MQTT_5)[1] == b"theirs"