ARTEMIS-3678 return proper CONNACK code when MQTT 3.x auth fails

This commit is contained in:
Justin Bertram 2022-02-09 21:06:28 -06:00
parent 7a34194ce0
commit 27d018327a
No known key found for this signature in database
GPG Key ID: F41830B875BB8633
2 changed files with 11 additions and 3 deletions

View File

@ -234,6 +234,8 @@ public class MQTTProtocolHandler extends ChannelInboundHandlerAdapter {
} catch (ActiveMQSecurityException e) {
if (session.is5()) {
session.getProtocolHandler().sendConnack(MQTTReasonCodes.BAD_USER_NAME_OR_PASSWORD);
} else {
session.getProtocolHandler().sendConnack(MQTTReasonCodes.NOT_AUTHORIZED_3);
}
disconnect(true);
return;

View File

@ -16,12 +16,13 @@
*/
package org.apache.activemq.artemis.tests.integration.mqtt;
import java.io.EOFException;
import java.util.Arrays;
import org.apache.activemq.artemis.tests.util.Wait;
import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.MQTTException;
import org.fusesource.mqtt.codec.CONNACK;
import org.junit.Test;
public class MQTTSecurityTest extends MQTTTestSupport {
@ -52,7 +53,7 @@ public class MQTTSecurityTest extends MQTTTestSupport {
}
}
@Test(timeout = 30000, expected = EOFException.class)
@Test(timeout = 30000)
public void testConnectionWithNullPassword() throws Exception {
for (String version : Arrays.asList("3.1", "3.1.1")) {
@ -66,8 +67,13 @@ public class MQTTSecurityTest extends MQTTTestSupport {
connection = mqtt.blockingConnection();
connection.connect();
fail("Connect should fail");
} catch (MQTTException e) {
assertEquals(CONNACK.Code.CONNECTION_REFUSED_NOT_AUTHORIZED, e.connack.code());
} catch (Exception e) {
fail("Should have caught an MQTTException");
} finally {
if (connection != null && connection.isConnected()) connection.disconnect();
if (connection != null && connection.isConnected())
connection.disconnect();
}
}
}