Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.logging.Logger;
import javax.security.auth.Subject;
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTConnection;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;
import org.apache.activemq.artemis.core.security.CheckType;
import org.apache.activemq.artemis.core.security.Role;
Expand Down Expand Up @@ -77,7 +78,7 @@
}

@Override
public Subject authenticate(

Check failure on line 81 in manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=openremote_openremote&issues=AaIIOTLZWlO6AdAqHVjg&open=AaIIOTLZWlO6AdAqHVjg&pullRequest=3608
String user, String password, RemotingConnection remotingConnection, String securityDomain)
throws NoCacheLoginException {

Expand Down Expand Up @@ -141,6 +142,16 @@
}
}

// In Artemis 2.57.0, MQTTProtocolHandler.handleConnect() registers the client ID after
// authentication returns. If the transport closes during token lookup, disconnect cleanup can
// finish before there is an entry to remove. Registering the closed connection afterward
// blocks reconnects with the same ID because we set allowLinkStealing=false. This check narrows
// the race to the interval between isOpen() and addConnectedClient().
if (remotingConnection instanceof MQTTConnection
&& !remotingConnection.getTransportConnection().isOpen()) {
return null;
}

Subject subject = new Subject(true, principals, Set.of(), Set.of());
// Set subject here so any code that calls this method behaves like a normal ActiveMQ
// SecurityStoreImpl::authenticate call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2026, OpenRemote Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package org.openremote.manager.mqtt

import org.apache.activemq.artemis.core.management.impl.ManagementRemotingConnection
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTConnection
import org.apache.activemq.artemis.spi.core.remoting.Connection
import org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal
import org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal
import org.openremote.container.security.IdentityService
import org.openremote.container.security.OIDCTokenResponse
import org.openremote.container.security.TokenPrincipal
import org.openremote.manager.security.RemotingConnectionPrincipal
import spock.lang.Specification

import javax.security.auth.Subject
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutorService

class ActiveMQORSecurityManagerTest extends Specification {

private final OIDCTokenResponse token = Stub(OIDCTokenResponse) {
getToken() >> "access-token"
}
private final TokenPrincipal principal = Stub(TokenPrincipal) {
getName() >> "service-user-id"
}

def "authenticates a connection with an empty password as anonymous"() {
given:
def brokerService = Stub(MQTTBrokerService)
def executorService = Stub(ExecutorService)
def identityService = Mock(IdentityService)
def securityManager = new ActiveMQORSecurityManager(brokerService, executorService, identityService)
Subject connectionSubject = null
def connection = Stub(MQTTConnection) {
getSubject() >> { connectionSubject }
setSubject(_ as Subject) >> { Subject subject -> connectionSubject = subject }
getRemoteAddress() >> "tcp://127.0.0.1:1883"
getClientID() >> "862000000006159"
getTransportConnection() >> Stub(Connection) {
isOpen() >> true
}
}

when:
def subject = securityManager.authenticate("broker.example/862000000006159", "", connection, null)

then:
subject.is(connectionSubject)
subject.principals.any {
it instanceof UserPrincipal && it.name == ActiveMQORSecurityManager.ANONYMOUS_USERNAME
}
subject.principals.any {
it instanceof RolePrincipal && it.name == ActiveMQORSecurityManager.ANONYMOUS_USERNAME
}
subject.principals.any {
it instanceof RemotingConnectionPrincipal
}
0 * identityService.authenticate(_, _, _)
}

def "MQTT authentication requires an open transport after token lookup (open=#openAfterTokenLookup)"() {
given:
boolean transportOpen = true
def transport = Stub(Connection) {
isOpen() >> { transportOpen }
}
def connection = Mock(MQTTConnection) {
getTransportConnection() >> transport
}
def identityService = Stub(IdentityService) {
authenticate("master", "serviceuser", "secret") >> {
transportOpen = openAfterTokenLookup
CompletableFuture.completedFuture(token)
}
verify("master", "access-token") >> principal
}
def securityManager = new ActiveMQORSecurityManager(null, null, identityService)
int expectedSubjectWrites = openAfterTokenLookup ? 1 : 0

when:
def subject = securityManager.authenticate("master:serviceuser", "secret", connection, null)

then:
(subject != null) == openAfterTokenLookup
expectedSubjectWrites * connection.setSubject(_ as Subject)

where:
openAfterTokenLookup << [true, false]
}

def "authenticates valid credentials on a transportless management connection"() {
given:
def connection = new ManagementRemotingConnection()
def identityService = Stub(IdentityService) {
authenticate("master", "serviceuser", "secret") >> CompletableFuture.completedFuture(token)
verify("master", "access-token") >> principal
}
def securityManager = new ActiveMQORSecurityManager(null, null, identityService)

when:
def subject = securityManager.authenticate("master:serviceuser", "secret", connection, null)

then:
subject.is(connection.subject)
subject.principals.contains(principal)
}
}

This file was deleted.

Loading