Skip to content

Commit

Permalink
[#2955] Fix MQTT5 Connect Reason Codes
Browse files Browse the repository at this point in the history
MQTT5 defines new reason codes to be included in CONNACK packets when
connection establishment fails. The abstract adapter base class has been
changed accordingly.

Also added integration tests based on HiveMQ client for testing
connection establishment.
  • Loading branch information
sophokles73 committed Nov 19, 2024
1 parent 4de4ad5 commit a50dc65
Show file tree
Hide file tree
Showing 7 changed files with 1,234 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
import io.netty.handler.codec.mqtt.MqttProperties;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.netty.handler.codec.mqtt.MqttVersion;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.log.Fields;
Expand Down Expand Up @@ -518,7 +519,7 @@ final void handleEndpointConnection(final MqttEndpoint endpoint) {
log.debug("rejecting connection request from client [clientId: {}], cause:",
endpoint.clientIdentifier(), t);

final MqttConnectReturnCode code = getConnectReturnCode(t);
final MqttConnectReturnCode code = getConnectReturnCode(t, endpoint);
rejectConnectionRequest(endpoint, code, span);
TracingHelper.logError(span, t);
}
Expand Down Expand Up @@ -1106,28 +1107,33 @@ final MqttDeviceEndpoint createMqttDeviceEndpoint(
return mqttDeviceEndpoint;
}

private static MqttConnectReturnCode getConnectReturnCode(final Throwable e) {
private static MqttConnectReturnCode getConnectReturnCode(final Throwable e, final MqttEndpoint endpoint) {

if (e instanceof MqttConnectionException) {
return ((MqttConnectionException) e).code();
final boolean isPreMqtt5 = ((int) MqttVersion.MQTT_5.protocolLevel()) > endpoint.protocolVersion();
if (e instanceof MqttConnectionException connectionException) {
return connectionException.code();
} else if (e instanceof AdapterConnectionsExceededException) {
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE
: MqttConnectReturnCode.CONNECTION_REFUSED_QUOTA_EXCEEDED;
} else if (e instanceof AuthorizationException) {
if (e instanceof AdapterConnectionsExceededException) {
return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
} else {
return MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
}
} else if (e instanceof ServiceInvocationException) {
switch (((ServiceInvocationException) e).getErrorCode()) {
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED
: MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED_5;
} else if (e instanceof ServiceInvocationException exception) {
switch (exception.getErrorCode()) {
case HttpURLConnection.HTTP_UNAUTHORIZED:
case HttpURLConnection.HTTP_NOT_FOUND:
return MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD
: MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USERNAME_OR_PASSWORD;
case HttpURLConnection.HTTP_UNAVAILABLE:
return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE
: MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE_5;
default:
return MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED
: MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED_5;
}
} else {
return MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
return isPreMqtt5 ? MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED
: MqttConnectReturnCode.CONNECTION_REFUSED_UNSPECIFIED_ERROR;
}
}

Expand Down
6 changes: 6 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ quarkus.vertx.max-event-loop-execute-time=${max.event-loop.execute-time:20000}
</dependency>

<!-- Testing -->
<dependency>
<groupId>com.hivemq</groupId>
<artifactId>hivemq-mqtt-client</artifactId>
<version>1.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.hono</groupId>
<artifactId>core-test-utils</artifactId>
Expand Down
60 changes: 30 additions & 30 deletions legal/src/main/resources/legal/DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
maven/mavencentral/biz.paluch.logging/logstash-gelf/1.15.1, MIT, approved, clearlydefined
maven/mavencentral/ch.qos.logback/logback-classic/1.5.6, EPL-1.0 AND LGPL-2.1-only, approved, #13282
maven/mavencentral/ch.qos.logback/logback-core/1.5.6, EPL-1.0 AND LGPL-2.1-only, approved, #13283
maven/mavencentral/ch.qos.logback/logback-classic/1.5.6, EPL-1.0 AND LGPL-2.1-only, approved, #15279
maven/mavencentral/ch.qos.logback/logback-core/1.5.6, EPL-1.0 AND LGPL-2.1-only, approved, #15210
maven/mavencentral/com.aayushatharva.brotli4j/brotli4j/1.16.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.aayushatharva.brotli4j/native-linux-x86_64/1.16.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.aayushatharva.brotli4j/service/1.16.0, Apache-2.0, approved, clearlydefined
Expand All @@ -27,7 +27,7 @@ maven/mavencentral/com.google.auth/google-auth-library-oauth2-http/1.23.0, BSD-3
maven/mavencentral/com.google.auto.value/auto-value-annotations/1.10.4, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.cloud/google-cloud-core/2.35.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.cloud/google-cloud-pubsub/1.127.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, Apache-2.0, approved, #20
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, Apache-2.0 and CC-BY-2.5, approved, #15220
maven/mavencentral/com.google.code.gson/gson/2.10.1, Apache-2.0, approved, #6159
maven/mavencentral/com.googlecode.juniversalchardet/juniversalchardet/1.0.3, MPL-1.1, approved, CQ10305
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.24.0, Apache-2.0, approved, #12448
Expand All @@ -45,7 +45,7 @@ maven/mavencentral/com.mchange/c3p0/0.9.5.5, LGPL-2.1-only OR EPL-1.0, approved,
maven/mavencentral/com.mchange/mchange-commons-java/0.2.19, LGPL-2.1-only OR EPL-1.0, approved, clearlydefined
maven/mavencentral/commons-codec/commons-codec/1.16.1, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #9157
maven/mavencentral/commons-io/commons-io/2.15.1, Apache-2.0, approved, #11244
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #15227
maven/mavencentral/com.squareup.okio/okio/3.6.0, Apache-2.0, approved, #11155
maven/mavencentral/com.squareup.okio/okio-jvm/3.6.0, Apache-2.0, approved, #11158
maven/mavencentral/com.squareup/protoparser/4.0.3, Apache-2.0, approved, clearlydefined
Expand Down Expand Up @@ -249,29 +249,29 @@ maven/mavencentral/io.smallrye/smallrye-fault-tolerance-vertx/6.2.6, Apache-2.0,
maven/mavencentral/io.smallrye/smallrye-health/4.1.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.smallrye/smallrye-health-api/4.1.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.smallrye/smallrye-health-provided-checks/4.1.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-auth-common/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-auth-jdbc/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-auth-mongo/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-bridge-common/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-codegen/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-core/4.5.7, Apache-2.0 AND EPL-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-grpc/4.5.7, Apache-2.0 AND EPL-1.0 AND Apache-2.0, approved, #14668
maven/mavencentral/io.vertx/vertx-grpc-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, #14667
maven/mavencentral/io.vertx/vertx-grpc-common/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-grpc-server/4.5.7, EPL-2.0 OR Apache-2.0, approved, #14669
maven/mavencentral/io.vertx/vertx-health-check/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-jdbc-client/4.5.7, Apache-2.0 AND EPL-1.0 AND (EPL-2.0 OR Apache-2.0) AND Apache-2.0, approved, #14671
maven/mavencentral/io.vertx/vertx-junit5/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-kafka-client/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-micrometer-metrics/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-mongo-client/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-mqtt/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-proton/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-sql-client/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-uri-template/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-web/4.5.7, Apache-2.0 AND EPL-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-web-client/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-web-common/4.5.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.vertx/vertx-auth-common/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-auth-jdbc/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-auth-mongo/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-bridge-common/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-codegen/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-core/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-grpc/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-grpc-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-grpc-common/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-grpc-server/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-health-check/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-jdbc-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-junit5/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-kafka-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-micrometer-metrics/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-mongo-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-mqtt/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-proton/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-sql-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-uri-template/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-web/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-web-client/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/io.vertx/vertx-web-common/4.5.7, EPL-2.0 OR Apache-2.0, approved, rt.vertx
maven/mavencentral/jakarta.activation/jakarta.activation-api/2.1.2, EPL-2.0 OR BSD-3-Clause OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jaf
maven/mavencentral/jakarta.annotation/jakarta.annotation-api/2.1.1, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.ca
maven/mavencentral/jakarta.el/jakarta.el-api/5.0.1, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.el
Expand All @@ -288,7 +288,7 @@ maven/mavencentral/net.java.dev.jna/jna/5.8.0, Apache-2.0 OR LGPL-2.1-or-later,
maven/mavencentral/org.apache.commons/commons-collections4/4.4, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apache.commons/commons-compress/1.26.1, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #13288
maven/mavencentral/org.apache.commons/commons-lang3/3.14.0, Apache-2.0, approved, #11677
maven/mavencentral/org.apache.httpcomponents/httpclient/4.5.14, Apache-2.0 AND LicenseRef-Public-Domain, approved, CQ23527
maven/mavencentral/org.apache.httpcomponents/httpclient/4.5.14, Apache-2.0, approved, #15248
maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.16, Apache-2.0, approved, CQ23528
maven/mavencentral/org.apache.kafka/kafka-clients/3.6.1, Apache-2.0 AND (Apache-2.0 AND MIT) AND (Apache-2.0 AND BSD-3-Clause), approved, #11084
maven/mavencentral/org.apache.logging.log4j/log4j-api/2.22.1, Apache-2.0, approved, #12576
Expand All @@ -312,7 +312,7 @@ maven/mavencentral/org.eclipse.parsson/parsson/1.1.5, EPL-2.0, approved, ee4j.pa
maven/mavencentral/org.fusesource.jansi/jansi/2.4.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.graalvm.sdk/nativeimage/23.1.2, UPL-1.0, approved, #10921
maven/mavencentral/org.graalvm.sdk/word/23.1.2, UPL-1.0, approved, #10917
maven/mavencentral/org.hdrhistogram/HdrHistogram/2.1.12, BSD-2-Clause OR LicenseRef-Public-Domain, approved, CQ13192
maven/mavencentral/org.hdrhistogram/HdrHistogram/2.1.12, CC0-1.0, approved, #15259
maven/mavencentral/org.infinispan/infinispan-client-hotrod-jakarta/14.0.27.Final, Apache-2.0, approved, #10936
maven/mavencentral/org.infinispan/infinispan-commons-jakarta/14.0.27.Final, Apache-2.0 AND LicenseRef-Public-Domain, approved, #10937
maven/mavencentral/org.infinispan/infinispan-core-jakarta/14.0.27.Final, Apache-2.0, approved, #10938
Expand All @@ -338,7 +338,7 @@ maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.9.22, Apache-2.0, a
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.9.22, Apache-2.0, approved, #14185
maven/mavencentral/org.jgroups/jgroups/5.2.23.Final, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.jline/jline/3.26.1, BSD-3-Clause AND Apache-2.0, approved, #14872
maven/mavencentral/org.latencyutils/LatencyUtils/2.0.3, BSD-2-Clause, approved, CQ17408
maven/mavencentral/org.latencyutils/LatencyUtils/2.0.3, CC0-1.0, approved, #15280
maven/mavencentral/org.locationtech.jts/jts-core/1.17.0, EPL-2.0, approved, locationtech.jts
maven/mavencentral/org.lz4/lz4-java/1.8.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.mongodb/bson/4.11.1, Apache-2.0, approved, clearlydefined
Expand Down
5 changes: 5 additions & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@
<artifactId>vertx-mqtt</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hivemq</groupId>
<artifactId>hivemq-mqtt-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -36,7 +36,7 @@
import io.vertx.mqtt.messages.MqttConnAckMessage;

/**
* Base class for MQTT adapter integration tests.
* Base class for MQTT adapter integration tests using MQTT 3.1.1.
*
*/
public abstract class MqttTestBase {
Expand Down Expand Up @@ -70,7 +70,7 @@ public abstract class MqttTestBase {
protected Context context;

/**
* Creates default AMQP client options.
* Creates default MQTT client options.
*/
@BeforeAll
public static void init() {
Expand Down
Loading

0 comments on commit a50dc65

Please sign in to comment.