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:
Mario Toffia authored and GitHub committed 2026-11-09 14:28:25 -04:00
1 parent c2d7456dda
commit 64b0977c45
4 files changed
+59 -15

No files matched your search

@@ -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