100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Refactor of MQTTCustomHandler
Rich Turnerb04ceafb9353
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.javanew file mode 100644index 000000000..d5aa26428--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.java@@ -0,0 +1,105 @@+/*+ * Copyright 2021, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * 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 <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.mqtt;++import io.moquette.broker.subscriptions.Topic;+import io.moquette.interception.messages.*;+import org.openremote.container.security.AuthContext;+import org.openremote.model.Container;+import org.openremote.model.syslog.SyslogCategory;++import java.util.Arrays;+import java.util.logging.Logger;++import static org.openremote.model.syslog.SyslogCategory.API;++public class ClientProvisioningCustomHandler implements MQTTCustomHandler {++ private static final Logger LOG = SyslogCategory.getLogger(API, ClientProvisioningCustomHandler.class);+ public static final String TOP_LEVEL_TOPIC = "client";++ @Override+ public String getName() {+ return this.getClass().getSimpleName();+ }++ @Override+ public void start(Container container) throws Exception {++ }++ @Override+ public void stop() throws Exception {++ }++ @Override+ public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {++ }++ @Override+ public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {++ }++ @Override+ public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {++ }++ @Override+ public boolean canSubscribe(AuthContext authContext, MqttConnection connection, Topic topic) {+ if (!TOP_LEVEL_TOPIC.equals(topic.headToken().toString())) {+ return false;+ }++ // client/UNIQUE_ID/connect/response++// topic.getTokens().size() ==+// if (Arrays.stream(this.realms).noneMatch(realm -> realm.equals(connection.getRealm()))) {+// LOG.info("realm mismatch");+// return false;+// }+// return true;++ return false;+ }++ @Override+ public boolean canPublish(AuthContext authContext, MqttConnection connection, Topic topic) {+ return false;+ }++ @Override+ public boolean onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {+ return false;+ }++ @Override+ public void onUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {++ }++ @Override+ public boolean onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {+ return false;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.javadeleted file mode 100644index 09651f1e9..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java+++ /dev/null@@ -1,82 +0,0 @@-/*- * Copyright 2017, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * 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 <http://www.gnu.org/licenses/>.- */-package org.openremote.manager.mqtt;--import io.moquette.broker.security.IAuthenticator;-import org.keycloak.representations.idm.ClientRepresentation;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.security.Tenant;-import org.openremote.model.security.User;-import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.util.TextUtil;--import java.nio.charset.StandardCharsets;-import java.util.Map;-import java.util.logging.Logger;--import static org.openremote.model.syslog.SyslogCategory.API;--public class KeycloakAuthenticator implements IAuthenticator {-- private static final Logger LOG = SyslogCategory.getLogger(API, KeycloakAuthenticator.class);-- protected final ManagerKeycloakIdentityProvider identityProvider;- protected final Map<String, MqttConnection> sessionIdConnectionMap;-- public KeycloakAuthenticator(ManagerKeycloakIdentityProvider managerIdentityProvider, Map<String, MqttConnection> sessionIdConnectionMap) {- identityProvider = managerIdentityProvider;- this.sessionIdConnectionMap = sessionIdConnectionMap;- }-- @Override- public boolean checkValid(String clientId, String username, byte[] password) {- String[] realmAndClientId = username.split(":");- String realm = realmAndClientId[0];- username = realmAndClientId[1]; // This is OAuth clientId- String suppliedClientSecret = new String(password, StandardCharsets.UTF_8);-- if (TextUtil.isNullOrEmpty(realm)- || TextUtil.isNullOrEmpty(username)- || TextUtil.isNullOrEmpty(suppliedClientSecret)) {- LOG.warning("Realm, client ID and/or client secret missing");- return false;- }-- if (sessionIdConnectionMap.containsKey(clientId)) {- LOG.warning("Client already connected");- return false;- }-- Tenant tenant = identityProvider.getTenant(realm);-- if (tenant == null || !tenant.getEnabled()) {- LOG.warning("Realm not found or is inactive: " + realm);- return false;- }-- User user = identityProvider.getUserByUsername(realm, User.SERVICE_ACCOUNT_PREFIX + username);- if (user == null || user.getEnabled() == null || !user.getEnabled() || TextUtil.isNullOrEmpty(user.getSecret())) {- LOG.warning("User not found, disabled or doesn't support client credentials grant type");- return false;- }-- return suppliedClientSecret.equals(user.getSecret());- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.javaindex 4fe4fd902..d1e0575ab 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java@@ -22,24 +22,46 @@ package org.openremote.manager.mqtt;import io.moquette.broker.subscriptions.Topic;import io.moquette.interception.messages.*;import org.openremote.container.security.AuthContext;+import org.openremote.model.Container;/**- * This allows custom handlers to be injected into the {@link MqttBrokerService} so a topic(s) can be handled in a- * custom way.+ * This allows custom handlers to be discovered by the {@link MqttBrokerService} during system startup using the+ * {@link java.util.ServiceLoader} mechanism. This allows topic(s) can be handled in a custom way. Any instances must+ * have a no-arg constructor.*/public interface MQTTCustomHandler {+ /**+ * Gets the priority of this handler which is used to determine the call order; handlers with a lower priority are+ * initialised and started first.+ */+ default int getPriority() {+ return 0;+ }+/*** Provides a name to identify this custom handler for logging purposes etc.*/String getName();+ /**+ * Called when the system starts to allow for initialisation.+ */+ void start(Container container) throws Exception;++ /**+ * Called when the system stops to allow for any cleanup.+ */+ void stop() throws Exception;+/*** Should this handler be called to handle this topic; general security checks are performed before this handler* is called so this handler can focus on the required custom business logic. This handler should return null* if it doesn't want to allow/prevent the pub/sub.*/- Boolean shouldIntercept(AuthContext authContext, MqttConnection connection, Topic topic, boolean isWrite);+ boolean canSubscribe(AuthContext authContext, MqttConnection connection, Topic topic);++ boolean canPublish(AuthContext authContext, MqttConnection connection, Topic topic);/*** Will be called when any client connects@@ -57,17 +79,14 @@ public interface MQTTCustomHandler {void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg);/**- * Will be called for a specific topic if {@link #shouldIntercept} returned true.+ * Should return true if this handler handles the subscribe.*/- void onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);+ boolean onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);- /**- * Will be called for a specific topic if {@link #shouldIntercept} returned true.- */void onUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg);/**- * Will be called for a specific topic if {@link #shouldIntercept} returned true.+ * Should return true if this handler handles the publish.*/- void onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);+ boolean onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.javaindex 1827e0b75..c96905ac1 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java@@ -22,7 +22,7 @@ package org.openremote.manager.mqtt;import io.moquette.BrokerConstants;import io.moquette.broker.Server;import io.moquette.broker.config.MemoryConfig;-import io.moquette.broker.subscriptions.Topic;+import io.moquette.broker.security.IAuthenticator;import io.moquette.interception.InterceptHandler;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;@@ -31,7 +31,6 @@ import io.netty.handler.codec.mqtt.MqttPublishMessage;import io.netty.handler.codec.mqtt.MqttQoS;import org.apache.camel.builder.RouteBuilder;import org.openremote.container.message.MessageBrokerService;-import org.openremote.container.security.AuthContext;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;@@ -44,24 +43,28 @@ import org.openremote.model.asset.AssetFilter;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.TriggeredEventSubscription;import org.openremote.model.event.shared.SharedEvent;+import org.openremote.model.security.Tenant;+import org.openremote.model.security.User;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;import java.nio.charset.Charset;+import java.nio.charset.StandardCharsets;import java.util.*;-import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.CopyOnWriteArraySet;import java.util.function.Consumer;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Pattern;+import java.util.stream.Collectors;+import static java.util.stream.StreamSupport.stream;import static org.openremote.container.util.MapAccess.getInteger;import static org.openremote.container.util.MapAccess.getString;import static org.openremote.manager.event.ClientEventService.getSessionKey;import static org.openremote.model.syslog.SyslogCategory.API;-public class MqttBrokerService implements ContainerService {+public class MqttBrokerService implements ContainerService, IAuthenticator {public static final int PRIORITY = MED_PRIORITY;private static final Logger LOG = SyslogCategory.getLogger(API, MqttBrokerService.class);@@ -79,11 +82,8 @@ public class MqttBrokerService implements ContainerService {protected ManagerKeycloakIdentityProvider identityProvider;protected ClientEventService clientEventService;protected MessageBrokerService messageBrokerService;- // Moquette doesn't provide any session id so cannot have multiple connections per realm-clientId combo- protected Map<String, MqttConnection> sessionIdConnectionMap = new HashMap<>();- protected final Set<MQTTCustomHandler> customHandlers = new CopyOnWriteArraySet<>();- // This is not ideal (it'll keep filling with topics) but Moquette SPI is crappy - no association between Authoriser and Interceptor- protected final Map<String, MQTTCustomHandler> topicCustomHandlerMap = new ConcurrentHashMap<>();+ protected Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();+ protected List<MQTTCustomHandler> customHandlers = new ArrayList<>();protected boolean active;protected String host;@@ -126,7 +126,7 @@ public class MqttBrokerService implements ContainerService {String sessionKey = getSessionKey(exchange);@SuppressWarnings("unchecked")TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);- MqttConnection connection = sessionIdConnectionMap.get(sessionKey);+ MqttConnection connection = clientIdConnectionMap.get(sessionKey);if (connection == null) {return;@@ -141,14 +141,29 @@ public class MqttBrokerService implements ContainerService {@Overridepublic void start(Container container) throws Exception {+ AssetStorageService assetStorageService = container.getService(AssetStorageService.class);Properties properties = new Properties();properties.setProperty(BrokerConstants.HOST_PROPERTY_NAME, host);properties.setProperty(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(port));properties.setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, String.valueOf(false));- List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, sessionIdConnectionMap));+ List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, clientIdConnectionMap));- AssetStorageService assetStorageService = container.getService(AssetStorageService.class);- mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, new KeycloakAuthenticator(identityProvider, sessionIdConnectionMap), new ORAuthorizatorPolicy(identityProvider, this, assetStorageService, clientEventService));+ // Load custom handlers+ this.customHandlers = stream(ServiceLoader.load(MQTTCustomHandler.class).spliterator(), false)+ .sorted(Comparator.comparingInt(MQTTCustomHandler::getPriority))+ .collect(Collectors.toList());++ // Start each custom handler+ for (MQTTCustomHandler handler : customHandlers) {+ try {+ handler.start(container);+ } catch (Exception e) {+ LOG.log(Level.WARNING, "MQTT custom handler threw an exception whilst starting: handler=" + handler.getName(), e);+ throw e;+ }+ }++ mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, this, new ORAuthorizatorPolicy(identityProvider, this, assetStorageService, clientEventService));LOG.fine("Started MQTT broker");}@@ -156,15 +171,20 @@ public class MqttBrokerService implements ContainerService {public void stop(Container container) throws Exception {mqttBroker.stopServer();LOG.fine("Stopped MQTT broker");- }- public void addCustomHandler(MQTTCustomHandler customHandler) {- customHandlers.add(customHandler);+ stream(ServiceLoader.load(MQTTCustomHandler.class).spliterator(), false)+ .sorted(Comparator.comparingInt(MQTTCustomHandler::getPriority).reversed())+ .forEach(handler -> {+ try {+ handler.stop();+ } catch (Exception e) {+ LOG.log(Level.WARNING, "MQTT custom handler threw an exception whilst stopping: handler=" + handler.getName(), e);+ }+ });}- public void removeCustomHandler(MQTTCustomHandler customHandler) {- customHandlers.remove(customHandler);- topicCustomHandlerMap.values().removeIf(h -> h == customHandler);+ public Iterable<MQTTCustomHandler> getCustomHandlers() {+ return customHandlers;}public void sendToSession(String sessionId, String topic, Object data, MqttQoS qoS) {@@ -183,6 +203,47 @@ public class MqttBrokerService implements ContainerService {}}+ /**+ * Validates an incoming connection and if rejected it will close the connection without calling intercept handler+ */+ @Override+ public boolean checkValid(String clientId, String username, byte[] password) {+ String realm = null;+ if (username != null) {+ String[] realmAndClientId = username.split(":");+ realm = realmAndClientId[0];+ username = realmAndClientId[1]; // This is OAuth clientId+ }+ String suppliedClientSecret = password != null ? new String(password, StandardCharsets.UTF_8) : null;++ if (clientIdConnectionMap.containsKey(clientId)) {+ LOG.info("Client with this ID already connected");+ return false;+ }++ if (TextUtil.isNullOrEmpty(realm)+ || TextUtil.isNullOrEmpty(username)+ || TextUtil.isNullOrEmpty(suppliedClientSecret)) {+ LOG.fine("Realm, client ID and/or client secret missing so this is an anonymous session with limited capabilities: " + clientId);+ return true;+ }++ Tenant tenant = identityProvider.getTenant(realm);++ if (tenant == null || !tenant.getEnabled()) {+ LOG.warning("Realm not found or is inactive: " + realm);+ return false;+ }++ User user = identityProvider.getUserByUsername(realm, User.SERVICE_ACCOUNT_PREFIX + username);+ if (user == null || user.getEnabled() == null || !user.getEnabled() || TextUtil.isNullOrEmpty(user.getSecret())) {+ LOG.warning("User not found, disabled or doesn't support client credentials grant type");+ return false;+ }++ return suppliedClientSecret.equals(user.getSecret());+ }+public static boolean isAttributeTopic(List<String> tokens) {return tokens.get(2).equals(ATTRIBUTE_TOPIC) || tokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);}@@ -226,10 +287,13 @@ public class MqttBrokerService implements ContainerService {assetIds.add(assetId);}} else {- if(assetId != null) {+ if (assetId != null) {//realm/clientId/attribute/assetIdassetIds.add(assetId);- } else {+ } else if (singleLevelIndex == 3) {+ //realm/clientId/attribute/++ parentIds.add(null);+ } else if (multiLevelIndex != 3) {//realm/clientId/attribute/attributeNameattributeNames.add(topicTokens.get(3));}@@ -328,6 +392,7 @@ public class MqttBrokerService implements ContainerService {}});}+protected Consumer<SharedEvent> getEventConsumer(MqttConnection connection, String topic, boolean isValueSubscription, MqttQoS mqttQoS) {return ev -> {List<String> topicTokens = Arrays.asList(topic.split("/"));@@ -338,7 +403,7 @@ public class MqttBrokerService implements ContainerService {if (wildCardIndex > 0) {topicTokens.set(wildCardIndex, assetEvent.getAssetId());}- sendToSession(connection.getSessionId(), String.join("/", topicTokens), ev, mqttQoS);+ sendToSession(connection.getClientId(), String.join("/", topicTokens), ev, mqttQoS);}if (ev instanceof AttributeEvent) {@@ -356,34 +421,12 @@ public class MqttBrokerService implements ContainerService {topicTokens.set(wildCardIndex, attributeEvent.getAttributeName());}}- if(isValueSubscription) {- sendToSession(connection.getSessionId(), String.join("/", topicTokens), attributeEvent.getValue().orElse(null), mqttQoS);+ if (isValueSubscription) {+ sendToSession(connection.getClientId(), String.join("/", topicTokens), attributeEvent.getValue().orElse(null), mqttQoS);} else {- sendToSession(connection.getSessionId(), String.join("/", topicTokens), ev, mqttQoS);+ sendToSession(connection.getClientId(), String.join("/", topicTokens), ev, mqttQoS);}}};}-- protected Boolean customHandlerAuthorises(AuthContext authContext, MqttConnection connection, Topic topic, boolean isWrite) {- // See if a custom handler wants to handle this topic- for (MQTTCustomHandler handler : customHandlers) {- Boolean result = handler.shouldIntercept(authContext, connection, topic, isWrite);- if (result != null) {- topicCustomHandlerMap.put(topic.toString(), handler);- LOG.info("Custom handler intercepted request: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);- return result;- }- }-- return null;- }-- /**- * Looks for a custom handler that authorised this topic and if found it return the {@link InterceptHandler}- * otherwise returns null.- */- protected MQTTCustomHandler getCustomInterceptHandler(String topic) {- return topicCustomHandlerMap.get(topic);- }}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.javaindex d5e2abd1a..bdb685478 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java@@ -21,25 +21,27 @@ public class MqttConnection {protected final String realm;protected final String username; // This is OAuth clientIdprotected final String password;+ protected final boolean credentials;protected final Map<String, Consumer<SharedEvent>> subscriptionHandlerMap = new HashMap<>();- protected final String sessionId;+ protected final String clientId;protected Supplier<String> tokenSupplier;- public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String sessionId, String realm, String username, String password) {+ public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password) {this.realm = realm;this.username = username;this.password = password;- this.sessionId = sessionId;+ this.clientId = clientId;String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();- if (!TextUtil.isNullOrEmpty(realm)+ credentials = !TextUtil.isNullOrEmpty(realm)&& !TextUtil.isNullOrEmpty(username)- && !TextUtil.isNullOrEmpty(password)) {+ && !TextUtil.isNullOrEmpty(password);+ if (credentials) {OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);tokenSupplier = identityProvider.getAccessTokenSupplier(grant);} else {- LOG.info("Invalid credentials provided, MQTT connection is not valid: " + this);+ LOG.fine("MQTT connection with no credentials so will have limited capabilities: " + this);}}@@ -59,23 +61,23 @@ public class MqttConnection {return this.subscriptionHandlerMap;}- /**- * Doesn't mean that credentials are valid just that correct info is set- */- public boolean isValid() {- return tokenSupplier != null;- }-public String getAccessToken() {- if (!isValid()) {+ if (tokenSupplier == null) {return null;}return tokenSupplier.get();}- public String getSessionId() {- return sessionId;+ /**+ * This is MQTT client ID not to be confused with OAuth client ID+ */+ public String getClientId() {+ return clientId;+ }++ public boolean hasCredentials() {+ return credentials;}@Override@@ -83,7 +85,7 @@ public class MqttConnection {return this.getClass().getSimpleName() + "{" +"realm='" + realm + '\'' +", username='" + username + '\'' +- ", sessionId='" + sessionId + '\'' ++ ", sessionId='" + clientId + '\'' +'}';}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.javaindex 891358e87..8f326c472 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java@@ -20,7 +20,6 @@ import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.shared.EventSubscription;import org.openremote.model.security.ClientRole;import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.value.RegexValueFilter;import java.util.List;import java.util.Optional;@@ -70,7 +69,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {@SuppressWarnings({"rawtypes", "unchecked"})protected boolean verifyRights(Topic topic, String sessionId, String realm, String username, boolean isWrite) {- MqttConnection connection = brokerService.sessionIdConnectionMap.get(sessionId);+ MqttConnection connection = brokerService.clientIdConnectionMap.get(sessionId);AuthContext authContext;if (connection == null) {@@ -91,9 +90,24 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- Boolean result = brokerService.customHandlerAuthorises(authContext, connection, topic, isWrite);- if (result != null) {- return result;+ boolean handled = false;++ // See if a custom handler wants to handle this topic pub/sub+ for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {+ if (isWrite) {+ handled = handler.canPublish(authContext, connection, topic);+ } else {+ handled = handler.canSubscribe(authContext, connection, topic);+ }++ if (handled) {+ LOG.info("Custom handler has allowed pub/sub: handler=" + handler.getName() + ", topic=" + topic + ", publish=" + isWrite + ", connection=" + connection);+ break;+ }+ }++ if (handled) {+ return true;}List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());@@ -103,7 +117,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- if (!connection.sessionId.equals(topicTokens.get(1))) {+ if (!connection.clientId.equals(topicTokens.get(1))) {LOG.warning("Second topic level should match clientId: " + connection);return false;}@@ -116,7 +130,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- if(topicTokens.size() > 3) {+ if (topicTokens.size() > 3) {String assetId = topicTokens.get(3);if (Pattern.matches(Constants.ASSET_ID_REGEXP, assetId)) {@@ -143,7 +157,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {}}}- } else if(!isWrite) {+ } else if (!isWrite) {LOG.fine("Topic must contain at least three tokens '" + topic + "': " + connection);return false;}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.javaindex d4562fc6d..3781594ad 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java@@ -91,20 +91,26 @@ public class ORInterceptHandler extends AbstractInterceptHandler {@Overridepublic void onConnect(InterceptConnectMessage msg) {- String[] realmAndUsername = msg.getUsername().split(":");- String realm = realmAndUsername[0];- String username = realmAndUsername[1];+ String realm = null;+ String username = null;String password = msg.getPassword() != null ? new String(msg.getPassword(), Charsets.UTF_8) : null;++ if (msg.getUsername() != null) {+ String[] realmAndUsername = msg.getUsername().split(":");+ realm = realmAndUsername[0];+ username = realmAndUsername[1];+ }+MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password);- sessionIdConnectionMap.put(connection.getSessionId(), connection);+ sessionIdConnectionMap.put(connection.getClientId(), connection);Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_OPEN, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);LOG.fine("Connected: " + connection);- // No topic info here (not sure what willTopic is??) so just notify all custom handlers- brokerService.customHandlers.forEach(customHandler -> customHandler.onConnect(connection, msg));+ // Notify all custom handlers+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnect(connection, msg));}@Override@@ -118,7 +124,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {LOG.fine("Connection closed: " + connection);// No topic info here so just notify all custom handlers- brokerService.customHandlers.forEach(customHandler -> customHandler.onDisconnect(connection, msg));+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onDisconnect(connection, msg));}}@@ -133,7 +139,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {LOG.fine("Connection lost: " + connection);// No topic info here so just notify all custom handlers- brokerService.customHandlers.forEach(customHandler -> customHandler.onConnectionLost(connection, msg));+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnectionLost(connection, msg));}}@@ -141,22 +147,20 @@ public class ORInterceptHandler extends AbstractInterceptHandler {@Overridepublic void onSubscribe(InterceptSubscribeMessage msg) {- String[] realmAndUsername = msg.getUsername().split(":");- String realm = realmAndUsername[0];- String username = realmAndUsername[1];MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());if (connection == null) {- LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());+ LOG.info("No connection found: clientID=" + msg.getClientID());return;}Topic topic = Topic.asTopic(msg.getTopicFilter());- MQTTCustomHandler customHandler;- if ((customHandler = brokerService.getCustomInterceptHandler(msg.getTopicFilter())) != null) {- customHandler.onSubscribe(connection, topic, msg);- return;+ for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {+ if (handler.onSubscribe(connection, topic, msg)) {+ LOG.info("Custom handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ return;+ }}List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());@@ -189,23 +193,17 @@ public class ORInterceptHandler extends AbstractInterceptHandler {@Overridepublic void onUnsubscribe(InterceptUnsubscribeMessage msg) {- String[] realmAndUsername = msg.getUsername().split(":");- String realm = realmAndUsername[0];- String username = realmAndUsername[1];MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());if (connection == null) {- LOG.warning("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());+ LOG.info("No connection found: clientID=" + msg.getClientID());return;}Topic topic = Topic.asTopic(msg.getTopicFilter());- MQTTCustomHandler customHandler;- if ((customHandler = brokerService.getCustomInterceptHandler(topic.toString())) != null) {- customHandler.onUnsubscribe(connection, topic, msg);- return;- }+ // Notify all custom handlers+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onUnsubscribe(connection, topic, msg));String subscriptionId = topic.toString();Consumer<? extends SharedEvent> eventConsumer = connection.subscriptionHandlerMap.remove(subscriptionId);@@ -234,16 +232,17 @@ public class ORInterceptHandler extends AbstractInterceptHandler {Topic topic = Topic.asTopic(msg.getTopicName());- MQTTCustomHandler customHandler;- if ((customHandler = brokerService.getCustomInterceptHandler(topic.toString())) != null) {- customHandler.onPublish(connection, topic, msg);- return;+ for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {+ if (handler.onPublish(connection, topic, msg)) {+ LOG.info("Custom handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ return;+ }}List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());boolean isValueWrite = topicTokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);- AttributeEvent attributeEvent = null;+ AttributeEvent attributeEvent;if (isValueWrite) {String assetId = topicTokens.get(3);@@ -265,7 +264,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {protected Map<String, Object> prepareHeaders(MqttConnection connection) {Map<String, Object> headers = new HashMap<>();- headers.put(ConnectionConstants.SESSION_KEY, connection.getSessionId());+ headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);try {diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 7cc2e7159..11d69f6b4 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -684,18 +684,18 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im@Overridepublic boolean isMasterRealmAdmin(String userId) {- List<UserRepresentation> adminUsers = getRealms(realmsResource ->+ Optional<UserRepresentation> adminUser = getRealms(realmsResource ->realmsResource.realm(MASTER_REALM).users()- .search(MASTER_REALM_ADMIN_USER, null, null));+ .search(MASTER_REALM_ADMIN_USER, null, null))+ .stream()+ .filter(user -> user.getUsername().equals(MASTER_REALM_ADMIN_USER))+ .findFirst();-- if (adminUsers.size() == 0) {+ if (!adminUser.isPresent()) {throw new IllegalStateException("Can't load master realm admin user");- } else if (adminUsers.size() > 1) {- throw new IllegalStateException("Several master realm admin users, this should not be possible.");}- return adminUsers.get(0).getId().equals(userId);+ return adminUser.map(UserRepresentation::getId).map(id -> id.equals(userId)).orElse(false);}@Overridediff --git a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandler b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandlernew file mode 100644index 000000000..1c4f22add--- /dev/null+++ b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandler@@ -0,0 +1 @@+org.openremote.manager.mqtt.ClientProvisioningCustomHandlerdiff --git a/model/src/main/java/org/openremote/model/ContainerService.java b/model/src/main/java/org/openremote/model/ContainerService.javaindex f0f2b31af..dfe998d96 100644--- a/model/src/main/java/org/openremote/model/ContainerService.java+++ b/model/src/main/java/org/openremote/model/ContainerService.java@@ -56,20 +56,17 @@ public interface ContainerService {/*** All services are initialized in the order they have been added to the container (if container started with* explicit list of services) otherwise they are initialized in order of {@link #getPriority}.- * @param container*/void init(Container container) throws Exception;/*** After initialization, services are started in the order they have been added to the container (if container* started with explicit list of services) otherwise they are started in order of {@link #getPriority}.- * @param container*/void start(Container container) throws Exception;/*** When the container is shutting down, it stops all services in the reverse order they were started.- * @param container*/void stop(Container container) throws Exception;}diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex f1531136b..711b647d3 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -106,7 +106,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "no connection should be made"conditions.eventually {assert !client.isConnected()- assert mqttBrokerService.sessionIdConnectionMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.size() == 0}when: "a mqtt client connects"@@ -142,7 +142,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "mqtt connection should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId) != null+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null}when: "a mqtt client subscribes to an asset in another realm"@@ -163,7 +163,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes to an non existing asset"@@ -184,7 +184,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes with clientId missing"@@ -205,7 +205,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes with different clientId"@@ -226,7 +226,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes to an asset"@@ -247,7 +247,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "A subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1}when: "An asset attribute changed the client is subscribed on"@@ -325,7 +325,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "Another asset attribute changed without any subscriptions"@@ -355,7 +355,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the subscription should be created"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)}when: "that attribute changed"@@ -394,7 +394,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes with multilevel on asset"@@ -415,7 +415,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "A subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1}when: "An child asset attribute changed the client is subscribed on"@@ -445,8 +445,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the subscription should be created"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)}when: "that attribute changed"@@ -485,7 +485,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1}when: " a client disconnects"@@ -497,7 +497,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "no connection should be left"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.size() == 0}when: "a mqtt client reconnects"@@ -533,7 +533,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "mqtt connection should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId) != null+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null}when: "a mqtt client subscribes to an asset in another realm"@@ -554,7 +554,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes to an non existing asset"@@ -575,7 +575,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0}when: "a mqtt client subscribes to an asset"@@ -596,7 +596,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "A subscription should exist"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1}when: "An asset is updated with a new attribute"@@ -627,7 +627,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {and: "a second subscription is in place"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2}and: "a asset is added as child to the subcribed asset"@@ -659,7 +659,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "Only one subscription should be left"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1}when: "MQTT client disconnects"@@ -671,7 +671,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "no connection should be left"conditions.eventually {- assert mqttBrokerService.sessionIdConnectionMap.size() == 0+ assert mqttBrokerService.clientIdConnectionMap.size() == 0}}}