mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iot): forward MQTT 5 user properties to subscribers (#7798)
The broker fanned a live publish out without its MQTT 5 properties, so an MQTT 5 subscriber received none of the user properties the publisher sent, over 1883, 8883 and /mqtt alike. AWS IoT forwards them unchanged and in the order sent, a repeated name included. The broker now copies the publish's user properties onto each live copy; Netty encodes them only for an MQTT 5 subscriber, so an MQTT 3.1.1 subscriber gets the message without them.
This commit is contained in:
1 parent
c2d7456dda
commit
64b0977c45
4 files changed
+59
-15
No files matched your search
@@ -184,6 +184,7 @@ Broker scope:
|
||||
- Support MQTT v3 and MQTT 5 CONNECT handling used by local compatibility tests.
|
||||
- Support QoS 0 and QoS 1 publish/subscribe behavior for the local AWS IoT slice. A publish, over MQTT or through IoT Data `Publish`, reaches each matching subscriber at the lower of its own and the subscription's QoS, with RETAIN 0, as on AWS. A retained message sent on subscribe carries RETAIN 1 at the lower of its stored and the subscription's QoS.
|
||||
- Accept PUBLISH payloads up to 128 KB on 1883, 8883 and `/mqtt`, the AWS IoT Core quota (packets up to 146 KB including the variable header). A larger publish disconnects the client without acknowledgement, delivery or rule evaluation, as on AWS.
|
||||
- Forward an MQTT 5 publish's user properties to each MQTT 5 subscriber unchanged and in the order sent, a repeated name included, as AWS does. An MQTT 3.1.1 subscriber gets the message without them. The publish's other MQTT 5 properties, and those of a retained message, are not forwarded.
|
||||
- Serve MQTT over TLS on 8883 next to plaintext 1883 when TLS is enabled, and verify the device certificate and its `iot:Connect` permission there.
|
||||
- Keep the plaintext listener permissive except for a `CONNECT` that names a [custom authorizer](#authorizers); topic-level authorization (`Publish`, `Subscribe`, `Receive`) is follow-up scope.
|
||||
- Keep MQTT broker logging minimal.
|
||||
|
||||
@@ -379,7 +379,7 @@ public class IotMqttBrokerService implements Resettable {
|
||||
}
|
||||
|
||||
void publish(String topic, byte[] payload, int qos) {
|
||||
fanOut(topic, payload, qos);
|
||||
fanOut(topic, payload, qos, MqttProperties.NO_PROPERTIES);
|
||||
}
|
||||
|
||||
boolean disconnectClient(String clientId, boolean cleanSession) {
|
||||
@@ -735,7 +735,14 @@ public class IotMqttBrokerService implements Resettable {
|
||||
|
||||
iotService.get().publish(topic, payload, message.isRetain(), message.qosLevel().value(), null,
|
||||
session.clientId(), rules -> evaluateRulesOnWorker(topic, rules));
|
||||
fanOut(topic, payload, message.qosLevel().value());
|
||||
fanOut(topic, payload, message.qosLevel().value(), userProperties(message.properties()));
|
||||
}
|
||||
|
||||
/** A publish's user properties in the order sent, which AWS IoT forwards unchanged to an MQTT 5 subscriber. */
|
||||
private static MqttProperties userProperties(MqttProperties received) {
|
||||
MqttProperties forwarded = new MqttProperties();
|
||||
received.getProperties(MqttProperties.MqttPropertyType.USER_PROPERTY.value()).forEach(forwarded::add);
|
||||
return forwarded;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -795,9 +802,10 @@ public class IotMqttBrokerService implements Resettable {
|
||||
|
||||
/**
|
||||
* Delivers a live copy, RETAIN 0 as on AWS, to each subscriber with a matching subscription, at the
|
||||
* lower of {@code qos} and the highest QoS among its matching subscriptions.
|
||||
* lower of {@code qos} and the highest QoS among its matching subscriptions. Netty encodes
|
||||
* {@code properties} only for an MQTT 5 subscriber.
|
||||
*/
|
||||
private void fanOut(String topic, byte[] payload, int qos) {
|
||||
private void fanOut(String topic, byte[] payload, int qos, MqttProperties properties) {
|
||||
byte[] safePayload = payload == null ? new byte[0] : payload.clone();
|
||||
for (ClientSession session : sessionsByClient.values()) {
|
||||
Map<String, Subscription> subscriptions = subscriptionsByClient.get(session.clientId());
|
||||
@@ -807,7 +815,8 @@ public class IotMqttBrokerService implements Resettable {
|
||||
}
|
||||
MqttQoS delivered = MqttQoS.valueOf(Math.min(qos, granted));
|
||||
try {
|
||||
session.endpoint().publish(topic, Buffer.buffer(safePayload), delivered, false, false, session.packetId(delivered));
|
||||
session.endpoint().publish(topic, Buffer.buffer(safePayload), delivered, false, false, session.packetId(delivered),
|
||||
properties);
|
||||
} catch (IllegalStateException e) {
|
||||
LOG.debugv("IoT MQTT client {0} closed before the publish on {1} reached it", session.clientId(), topic);
|
||||
}
|
||||
@@ -901,7 +910,7 @@ public class IotMqttBrokerService implements Resettable {
|
||||
vertx.runOnContext(ignored -> {
|
||||
iotService.get().publish(topic, payload, false, 0, null, session.clientId(),
|
||||
rules -> evaluateRulesOnWorker(topic, rules, admitted));
|
||||
fanOut(topic, payload, 0);
|
||||
fanOut(topic, payload, 0, MqttProperties.NO_PROPERTIES);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import io.floci.aws.services.sns.SnsService;
|
||||
import io.floci.aws.services.sqs.SqsService;
|
||||
import io.floci.aws.testing.ThreadStates;
|
||||
import io.floci.aws.testutil.FreePorts;
|
||||
import io.netty.handler.codec.mqtt.MqttProperties;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.quarkus.tls.CertificateUpdatedEvent;
|
||||
import io.quarkus.tls.TlsConfiguration;
|
||||
@@ -496,7 +497,7 @@ class IotMqttBrokerServiceTest {
|
||||
subscribeHandler.set(invocation.getArgument(0));
|
||||
return closing;
|
||||
});
|
||||
when(closing.publish(anyString(), any(), any(), anyBoolean(), anyBoolean(), anyInt()))
|
||||
when(closing.publish(anyString(), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class)))
|
||||
.thenThrow(new IllegalStateException("the connection closed"));
|
||||
MqttTopicSubscription requested = mock(MqttTopicSubscription.class);
|
||||
when(requested.topicName()).thenReturn("devices/+/metrics");
|
||||
@@ -514,7 +515,7 @@ class IotMqttBrokerServiceTest {
|
||||
|
||||
broker.publish("devices/d1/metrics", "{\"t\":1}".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
verify(closing).publish(eq("devices/d1/metrics"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
|
||||
verify(closing).publish(eq("devices/d1/metrics"), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class));
|
||||
assertEquals("{\"t\":1}", fannedOut.poll(10, TimeUnit.SECONDS), "the other subscriber got its copy");
|
||||
} finally {
|
||||
disconnect(subscriber);
|
||||
@@ -1123,7 +1124,7 @@ class IotMqttBrokerServiceTest {
|
||||
});
|
||||
broker.handleEndpoint(departing, false);
|
||||
verify(subscriber, timeout(10_000))
|
||||
.publish(eq("$aws/events/presence/connected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
|
||||
.publish(eq("$aws/events/presence/connected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class));
|
||||
|
||||
ExecutorService departingEventLoop = Executors.newSingleThreadExecutor();
|
||||
subscriberMonitor.lock();
|
||||
@@ -1137,7 +1138,7 @@ class IotMqttBrokerServiceTest {
|
||||
departingEventLoop.shutdown();
|
||||
}
|
||||
verify(subscriber, timeout(10_000))
|
||||
.publish(eq("$aws/events/presence/disconnected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
|
||||
.publish(eq("$aws/events/presence/disconnected/departing"), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class));
|
||||
}
|
||||
|
||||
/** A PUBLISH, SUBSCRIBE, UNSUBSCRIBE or PUBACK counts as traffic for the keep-alive, as a PINGREQ does. */
|
||||
@@ -1156,6 +1157,7 @@ class IotMqttBrokerServiceTest {
|
||||
when(message.payload()).thenReturn(Buffer.buffer("{}"));
|
||||
when(message.qosLevel()).thenReturn(MqttQoS.AT_MOST_ONCE);
|
||||
when(message.topicName()).thenReturn("keepalive/publishing");
|
||||
when(message.properties()).thenReturn(MqttProperties.NO_PROPERTIES);
|
||||
publish.getValue().handle(message);
|
||||
ArgumentCaptor<Handler<MqttSubscribeMessage>> subscribe = handlerCaptor();
|
||||
verify(subscribing).subscribeHandler(subscribe.capture());
|
||||
@@ -1261,8 +1263,8 @@ class IotMqttBrokerServiceTest {
|
||||
broker.publish("stale/topic", "{}".getBytes(StandardCharsets.UTF_8));
|
||||
broker.publish("kept/topic", "{}".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
verify(current, timeout(10_000)).publish(eq("kept/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
|
||||
verify(current, never()).publish(eq("stale/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt());
|
||||
verify(current, timeout(10_000)).publish(eq("kept/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class));
|
||||
verify(current, never()).publish(eq("stale/topic"), any(), any(), anyBoolean(), anyBoolean(), anyInt(), any(MqttProperties.class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -332,6 +332,37 @@ class IotMqttEnabledIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS IoT forwards an MQTT 5 publish's user properties to an MQTT 5 subscriber unchanged, a repeated name
|
||||
* included, in the order they were sent; an MQTT 3.1.1 subscriber gets the message without them.
|
||||
*/
|
||||
@Test
|
||||
void mqtt5UserPropertiesReachTheSubscriberUnchangedAndInOrder() throws Exception {
|
||||
String topic = "phase6/mqtt5-user-properties/" + System.nanoTime();
|
||||
byte[] payload = "segment".getBytes(StandardCharsets.UTF_8);
|
||||
List<UserProperty> sent = List.of(
|
||||
new UserProperty("seg", "1"),
|
||||
new UserProperty("total", "3"),
|
||||
new UserProperty("cid", "c-" + System.nanoTime()),
|
||||
new UserProperty("seg", "2"));
|
||||
MqttProperties properties = new MqttProperties();
|
||||
properties.setUserProperties(sent);
|
||||
|
||||
try (Mqtt5TestClient subscriber = connectMqtt5("mqtt5-user-properties-sub");
|
||||
MqttTestClient mqtt3Subscriber = connectMqtt("mqtt5-user-properties-sub-v3")) {
|
||||
subscriber.subscribe(topic);
|
||||
mqtt3Subscriber.subscribe(topic);
|
||||
try (Mqtt5TestClient publisher = connectMqtt5("mqtt5-user-properties-pub")) {
|
||||
publisher.publish(topic, payload, 1, properties);
|
||||
}
|
||||
|
||||
MqttPublish received = subscriber.takePublish();
|
||||
assertArrayEquals(payload, received.payload());
|
||||
assertEquals(sent, received.userProperties());
|
||||
assertArrayEquals(payload, mqtt3Subscriber.takePublish().payload());
|
||||
}
|
||||
}
|
||||
|
||||
/** 8 KB correlation data plus user properties of the given key and value size, as AWS counts them. */
|
||||
private static MqttProperties awsMaximumProperties(int userPropertiesSize) {
|
||||
MqttProperties properties = new MqttProperties();
|
||||
@@ -960,7 +991,7 @@ class IotMqttEnabledIntegrationTest {
|
||||
out.writeBytes(bytes);
|
||||
}
|
||||
|
||||
private record MqttPublish(String topic, byte[] payload, int qos, boolean retained) {
|
||||
private record MqttPublish(String topic, byte[] payload, int qos, boolean retained, List<UserProperty> userProperties) {
|
||||
}
|
||||
|
||||
private static final class MqttTestClient implements AutoCloseable {
|
||||
@@ -982,7 +1013,7 @@ class IotMqttEnabledIntegrationTest {
|
||||
|
||||
@Override
|
||||
public void messageArrived(String topic, MqttMessage message) {
|
||||
testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained()));
|
||||
testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained(), List.of()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1076,7 +1107,8 @@ class IotMqttEnabledIntegrationTest {
|
||||
|
||||
@Override
|
||||
public void messageArrived(String topic, org.eclipse.paho.mqttv5.common.MqttMessage message) {
|
||||
testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained()));
|
||||
testClient.publishes.add(new MqttPublish(topic, message.getPayload(), message.getQos(), message.isRetained(),
|
||||
message.getProperties().getUserProperties()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user