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
Show MQTT sessions and allow MQTT connection status to be linked to attributes (#915)
Rich Turner760559f39fb3
diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.javaindex 84d323c0b..1c688aca2 100644--- a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java@@ -492,6 +492,18 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {.map(Principal::getName).orElse(null);}+ public static String getSubjectNameAndRealm(Subject subject) {+ if (subject == null || subject.getPrincipals() == null) {+ return null;+ }++ return subject.getPrincipals().stream().filter(p -> p instanceof KeycloakPrincipal<?>).findFirst()+ .map(keycloakPrincipal -> {+ String realm = ((KeycloakPrincipal<?>)keycloakPrincipal).getKeycloakSecurityContext().getRealm();+ return realm + ":" + keycloakPrincipal.getName();+ }).orElse(null);+ }+public static String getSubjectId(Subject subject) {if (subject == null || subject.getPrincipals() == null) {return null;diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex ede1f5fc4..e33a9a5ba 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -300,8 +300,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Assebreak;case UPDATE:- int attributesIndex = Arrays.asList(persistenceEvent.getPropertyNames()).indexOf("attributes");- if (attributesIndex < 0) {+ if (!persistenceEvent.hasPropertyChanged("attributes")) {return;}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 63c5e6cee..583988ce8 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -1439,8 +1439,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbreak;case UPDATE:- String[] updatedProperties = persistenceEvent.getPropertyNames();- boolean attributesChanged = Arrays.asList(updatedProperties).contains("attributes");+ boolean attributesChanged = persistenceEvent.hasPropertyChanged("attributes");// String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {// Object oldValue = persistenceEvent.getPreviousState(propertyName);@@ -1458,7 +1457,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicLOG.finer("Asset updated: " + persistenceEvent);clientEventService.publishEvent(- new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, updatedProperties)+ new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, persistenceEvent.getPropertyNames().toArray(String[]::new)));// Did any attributes change if so raise attribute events on the event busdiff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.javaindex 09bd28de6..db3e9e146 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java@@ -61,7 +61,6 @@ import java.util.function.Consumer;import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;-import java.util.stream.IntStream;import static org.apache.camel.builder.PredicateBuilder.and;import static org.apache.camel.builder.PredicateBuilder.or;@@ -555,13 +554,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As}connector.setDisabled(isNowDisabled);- int attributeIndex = persistenceEvent.getPropertyNames() != null- ? IntStream.range(0, persistenceEvent.getPropertyNames().length)- .filter(i -> "attributes".equals(persistenceEvent.getPropertyNames()[i]))- .findFirst()- .orElse(-1)- : -1;- if (attributeIndex >= 0) {+ if (persistenceEvent.hasPropertyChanged("attributes")) {// Check if disabled attribute has changedAttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");boolean wasDisabled = oldAttributes.getValue(GatewayAsset.DISABLED).orElse(false);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javaindex b5f7916a0..f9d386529 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -77,7 +77,7 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {@Overridepublic Subject authenticate(String user, String password, RemotingConnection remotingConnection, String securityDomain) {try {- return getAuthenticatedSubject(user, password, remotingConnection, securityDomain);+ return remotingConnection.getSubject() != null ? remotingConnection.getSubject() : getAuthenticatedSubject(user, password, remotingConnection, securityDomain);} catch (LoginException e) {return null;}@@ -92,17 +92,6 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();ClassLoader thisLoader = this.getClass().getClassLoader();- // A bit of a hack to allow auto provisioned clients to remain connected after authentication and to then have- // the usual service user authentication without having to disconnect and reconnect- if (user == null) {- Triple<String, String, String> transientCredentials = brokerService.transientCredentials.get(remotingConnection.getID());- if (transientCredentials != null) {- LOG.finer("Using transient credentials (user=" + transientCredentials.getMiddle() + ") for connection: " + brokerService.connectionToString(remotingConnection));- user = transientCredentials.getMiddle();- password = transientCredentials.getRight();- }- }-if (user != null) {String[] realmAndUsername = user.split(":");if (realmAndUsername.length == 2) {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.javanew file mode 100644index 000000000..6eb98de95--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java@@ -0,0 +1,332 @@+/*+ * Copyright 2023, 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.netty.buffer.ByteBuf;+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.apache.camel.builder.RouteBuilder;+import org.keycloak.KeycloakSecurityContext;+import org.openremote.container.message.MessageBrokerService;+import org.openremote.container.persistence.PersistenceService;+import org.openremote.container.security.keycloak.KeycloakIdentityProvider;+import org.openremote.manager.asset.AssetProcessingService;+import org.openremote.manager.asset.AssetStorageService;+import org.openremote.manager.gateway.GatewayService;+import org.openremote.manager.security.ManagerIdentityProvider;+import org.openremote.model.Container;+import org.openremote.model.PersistenceEvent;+import org.openremote.model.asset.Asset;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeMap;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.query.AssetQuery;+import org.openremote.model.query.filter.AttributePredicate;+import org.openremote.model.query.filter.NameValuePredicate;+import org.openremote.model.security.User;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.Pair;+import org.openremote.model.value.ValueType;++import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;+import java.util.concurrent.ExecutorService;+import java.util.logging.Level;+import java.util.logging.Logger;+import java.util.stream.Collectors;++import static java.util.stream.Collectors.mapping;+import static java.util.stream.Collectors.toList;+import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;+import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;+import static org.openremote.manager.gateway.GatewayService.isNotForGateway;+import static org.openremote.model.syslog.SyslogCategory.API;+import static org.openremote.model.value.MetaItemType.USER_CONNECTED;++/**+ * This {@link MQTTHandler} just monitors connected users and handles updating of+ * {@link org.openremote.model.attribute.Attribute}s with {@link org.openremote.model.value.MetaItemType#USER_CONNECTED}+ * {@link org.openremote.model.attribute.MetaItem}. It doesn't handle any publishes or subscriptions.+ */+public class ConnectionMonitorHandler extends MQTTHandler {++ protected static final Logger LOG = SyslogCategory.getLogger(API, ConnectionMonitorHandler.class);+ protected MQTTBrokerService mqttBrokerService;+ protected ExecutorService executorService;+ protected AssetStorageService assetStorageService;+ protected AssetProcessingService assetProcessingService;+ protected GatewayService gatewayService;+ protected PersistenceService persistenceService;+ protected ConcurrentMap<String, Set<AttributeRef>> userIDAttributeRefs = new ConcurrentHashMap<>();++ @Override+ public void init(Container container) throws Exception {+ super.init(container);+ executorService = container.getExecutorService();+ mqttBrokerService = container.getService(MQTTBrokerService.class);+ assetStorageService = container.getService(AssetStorageService.class);+ assetProcessingService = container.getService(AssetProcessingService.class);+ gatewayService = container.getService(GatewayService.class);+ persistenceService = container.getService(PersistenceService.class);+ MessageBrokerService messageBrokerService = container.getService(MessageBrokerService.class);+ messageBrokerService.getContext().addRoutes(+ new RouteBuilder() {+ @SuppressWarnings("unchecked")+ @Override+ public void configure() throws Exception {+ from(PERSISTENCE_TOPIC)+ .routeId("MQTTConnectionMonitorHandlerQueue")+ .filter(isPersistenceEventForEntityType(Asset.class))+ .filter(isNotForGateway(gatewayService))+ .process(exchange -> {+ PersistenceEvent<Asset<?>> persistenceEvent = (PersistenceEvent<Asset<?>>)exchange.getIn().getBody(PersistenceEvent.class);++ if (persistenceEvent.hasPropertyChanged("attributes")) {+ Asset<?> asset = persistenceEvent.getEntity();+ AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");+ AttributeMap newAttributes = persistenceEvent.getCurrentState("attributes");++ if (oldAttributes != null) {+ oldAttributes.stream().filter(ConnectionMonitorHandler::attributeMatches)+ .forEach(attr -> {+ String userID = attr.getMetaValueOrDefault(USER_CONNECTED);+ if (userID != null) {+ removeSessionAttribute(userID, new AttributeRef(asset.getId(), attr.getName()));+ }+ });+ }++ if (newAttributes != null) {+ List<Pair<String, Attribute<?>>> connectedAttributes = newAttributes.stream()+ .filter(ConnectionMonitorHandler::attributeMatches)+ .map(attr -> new Pair<String, Attribute<?>>(asset.getId(), attr))+ .toList();++ addSessionAttributes(asset.getRealm(), connectedAttributes);+ }+ }+ })+ .end();+ }+ }+ );+ // TODO: Register with broker service for persistence events+ }++ @Override+ public void start(Container container) throws Exception {+ super.start(container);++ // Don't block start+ executorService.submit(() -> {+ // Get all assets that have attributes with user connected meta and initialise them+ List<Asset<?>> assets = assetStorageService.findAll(+ new AssetQuery()+ .attributes(+ new AttributePredicate().meta(+ new NameValuePredicate(USER_CONNECTED, null)+ )+ )+ );++ Map<String, List<Asset<?>>> realmAttributeMap = assets.stream().collect(+ Collectors.groupingBy(Asset::getRealm)+ );++ realmAttributeMap.forEach(+ (realm, realmAssets) -> {+ List<Pair<String, Attribute<?>>> assetIdsAttrs = realmAssets.stream()+ .flatMap(asset ->+ asset.getAttributes().stream()+ .filter(ConnectionMonitorHandler::attributeMatches)+ .map(attr -> new Pair<String, Attribute<?>>(asset.getId(), attr)))+ .toList();++ addSessionAttributes(realm, assetIdsAttrs);+ });+ });+ }++ @Override+ public void stop() throws Exception {+ super.stop();+ }++ @Override+ public void onConnect(RemotingConnection connection) {+ super.onConnect(connection);++ Pair<String, Set<AttributeRef>> userIDAndAttributeRefs = getUserIDAndAttributeRefs(connection);+ if (userIDAndAttributeRefs != null) {+ updateUserConnectedStatus(userIDAndAttributeRefs.key, userIDAndAttributeRefs.value, true);+ }+ }++ @Override+ public void onDisconnect(RemotingConnection connection) {+ super.onDisconnect(connection);++ Pair<String, Set<AttributeRef>> userIDAndAttributeRefs = getUserIDAndAttributeRefs(connection);+ if (userIDAndAttributeRefs != null) {+ updateUserConnectedStatus(userIDAndAttributeRefs.key, userIDAndAttributeRefs.value, false);+ }+ }++ @Override+ public void onConnectionLost(RemotingConnection connection) {+ super.onConnectionLost(connection);++ Pair<String, Set<AttributeRef>> userIDAndAttributeRefs = getUserIDAndAttributeRefs(connection);+ if (userIDAndAttributeRefs != null) {+ updateUserConnectedStatus(userIDAndAttributeRefs.key, userIDAndAttributeRefs.value, false);+ }+ }++ @Override+ public void onConnectionAuthenticated(RemotingConnection connection) {+ super.onConnectionAuthenticated(connection);++ Pair<String, Set<AttributeRef>> userIDAndAttributeRefs = getUserIDAndAttributeRefs(connection);+ if (userIDAndAttributeRefs != null) {+ updateUserConnectedStatus(userIDAndAttributeRefs.key, userIDAndAttributeRefs.value, true);+ }+ }++ @Override+ protected boolean topicMatches(Topic topic) {+ return false;+ }++ @Override+ protected Logger getLogger() {+ return LOG;+ }++ @Override+ public boolean canSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {+ return false;+ }++ @Override+ public boolean canPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {+ return false;+ }++ @Override+ public Set<String> getPublishListenerTopics() {+ return null;+ }++ @Override+ public void onPublish(RemotingConnection connection, Topic topic, ByteBuf body) {+ }++ @Override+ public void onSubscribe(RemotingConnection connection, Topic topic) {+ }++ @Override+ public void onUnsubscribe(RemotingConnection connection, Topic topic) {+ }++ protected void addSessionAttributes(String realm, List<Pair<String, Attribute<?>>> assetIdsAttrs) {+ LOG.finer("Adding '" + assetIdsAttrs.size() + "' attributes(s) with user linked attributes in realm: " + realm);++ List<String> usernames = assetIdsAttrs.stream().map(assetIdAttr -> assetIdAttr.getValue().getMetaValue(USER_CONNECTED).orElse(null))+ .filter(Objects::nonNull)+ .distinct()+ .map(username -> User.SERVICE_ACCOUNT_PREFIX + username)+ .toList();++ // Convert usernames to userIds+ List<String> userIds = ManagerIdentityProvider.getUserIds(persistenceService, realm, usernames);++ assetIdsAttrs.forEach(assetIdAttr ->+ assetIdAttr.getValue().getMetaValue(USER_CONNECTED).ifPresent(username -> {+ String userID = userIds.get(usernames.indexOf(User.SERVICE_ACCOUNT_PREFIX + username));++ if (userID == null) {+ LOG.warning("Invalid username so skipping: " + username);+ } else {+ addSessionAttribute(userID, new AttributeRef(assetIdAttr.key, assetIdAttr.getValue().getName()));+ }+ }));+ }++ protected void addSessionAttribute(String userID, AttributeRef attributeRef) {+ LOG.finest("Adding userID '" + userID + "' monitoring for attribute: " + attributeRef);+ updateUserConnectedStatus(userID, Collections.singletonList(attributeRef), !mqttBrokerService.getUserConnections(userID).isEmpty());+ Set<AttributeRef> refs = userIDAttributeRefs.computeIfAbsent(userID, ID -> ConcurrentHashMap.newKeySet());+ refs.add(attributeRef);+ }++ protected void removeSessionAttribute(String userID, AttributeRef attributeRef) {+ LOG.finest("Removing userID '" + userID + "' monitoring for attribute: " + attributeRef);+ userIDAttributeRefs.computeIfPresent(userID, (ID, refs) -> {+ refs.remove(attributeRef);+ return refs.isEmpty() ? null : refs;+ });+ }++ protected void updateUserConnectedStatus(String userID, Collection<AttributeRef> attributeRefs, boolean connected) {++ // Only update statuses if this is the only connection+ Set<RemotingConnection> connections = mqttBrokerService.getUserConnections(userID);++ if (connected) {+ if (connections.size() > 1) {+ LOG.finest("Connections already exist for user so skipping status update: " + userID);+ return;+ }+ } else {+ if (!connections.isEmpty()) {+ LOG.finest("Other connections remain for user so skipping status update: " + userID);+ return;+ }+ }++ LOG.finer("Updating connected status for '" + userID + "' on " + attributeRefs.size() + " attribute(s) connected=" + connected);+ attributeRefs.forEach(attributeRef ->+ assetProcessingService.sendAttributeEvent(new AttributeEvent(attributeRef, connected)));+ }++ protected Pair<String, Set<AttributeRef>> getUserIDAndAttributeRefs(RemotingConnection connection) {+ String userID = KeycloakIdentityProvider.getSubjectId(connection.getSubject());++ if (userID == null) {+ if (LOG.isLoggable(Level.FINEST)) {+ LOG.finest("Anonymous connection so cannot determine userID: " + mqttBrokerService.connectionToString(connection));+ }+ return null;+ }++ Set<AttributeRef> attributeRefs = userIDAttributeRefs.get(userID);+ if (attributeRefs == null) {+ return null;+ }++ return new Pair<>(userID, attributeRefs);+ }++ protected static boolean attributeMatches(Attribute<?> attr) {+ return Objects.equals(attr.getType(), ValueType.BOOLEAN) && attr.hasMeta(USER_CONNECTED);+ }+}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex c40328c49..8574424a9 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -137,7 +137,7 @@ public class DefaultMQTTHandler extends MQTTHandler {}@Override- public boolean onConnect(RemotingConnection connection) {+ public void onConnect(RemotingConnection connection) {super.onConnect(connection);Map<String, Object> headers = prepareHeaders(null, connection);headers.put(ConnectionConstants.SESSION_OPEN, true);@@ -154,7 +154,6 @@ public class DefaultMQTTHandler extends MQTTHandler {.withHeaders(headers).to(CLIENT_EVENT_QUEUE).asyncSend();- return true;}@Overridediff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.javaindex 1cd16d618..9c91fba25 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java@@ -36,7 +36,6 @@ import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;import org.apache.activemq.artemis.core.remoting.FailureListener;import org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnection;-import org.apache.activemq.artemis.core.security.impl.SecurityStoreImpl;import org.apache.activemq.artemis.core.server.ServerSession;import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerConnectionPlugin;@@ -47,7 +46,6 @@ import org.apache.activemq.artemis.spi.core.security.jaas.PrincipalConversionLogimport org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal;import org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal;import org.apache.camel.builder.RouteBuilder;-import org.apache.commons.lang3.tuple.Triple;import org.apache.http.client.utils.URIBuilder;import org.keycloak.KeycloakPrincipal;import org.keycloak.adapters.jaas.AbstractKeycloakLoginModule;@@ -55,6 +53,8 @@ import org.openremote.container.message.MessageBrokerService;import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.container.timer.TimerService;import org.openremote.container.util.UniqueIdentifierGenerator;+import org.openremote.manager.asset.AssetProcessingService;+import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.AuthorisationService;import org.openremote.manager.security.ManagerIdentityService;@@ -102,24 +102,25 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,protected final WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();protected static final Logger LOG = SyslogCategory.getLogger(API, MQTTBrokerService.class);+ protected AssetStorageService assetStorageService;protected AuthorisationService authorisationService;protected ManagerKeycloakIdentityProvider identityProvider;protected ClientEventService clientEventService;protected MessageBrokerService messageBrokerService;protected ScheduledExecutorService executorService;protected TimerService timerService;+ protected AssetProcessingService assetProcessingService;protected List<MQTTHandler> customHandlers = new ArrayList<>();protected ConcurrentMap<String, RemotingConnection> clientIDConnectionMap = new ConcurrentHashMap<>();protected ConcurrentMap<String, RemotingConnection> connectionIDConnectionMap = new ConcurrentHashMap<>();- protected ConcurrentMap<Object, Triple<String, String, String>> transientCredentials = new ConcurrentHashMap<>();protected Debouncer<String> userAssetDisconnectDebouncer;// Stores disconnected connections for a short period to allow last will publishes to be processedprotected Cache<String, RemotingConnection> disconnectedConnectionCache;-protected boolean active;protected String host;protected int port;protected EmbeddedActiveMQ server;+ protected ActiveMQORSecurityManager securityManager;protected ClientProducer producer;protected ClientSessionInternal internalSession;@@ -133,12 +134,15 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,host = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, "0.0.0.0");port = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, 1883);int debounceMillis = getInteger(container.getConfig(), MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS, MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS_DEFAULT);+ assetStorageService = container.getService(AssetStorageService.class);authorisationService = container.getService(AuthorisationService.class);clientEventService = container.getService(ClientEventService.class);ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);messageBrokerService = container.getService(MessageBrokerService.class);executorService = container.getExecutorService();timerService = container.getService(TimerService.class);+ assetProcessingService = container.getService(AssetProcessingService.class);+userAssetDisconnectDebouncer = new Debouncer<>(executorService, this::processUserAssetLinkChange, debounceMillis);disconnectedConnectionCache = CacheBuilder.newBuilder().maximumSize(10000)@@ -200,20 +204,22 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,server = new EmbeddedActiveMQ();server.setConfiguration(config);- server.setSecurityManager(new ActiveMQORSecurityManager(authorisationService, this, realm -> identityProvider.getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID), "", new SecurityConfiguration() {++ securityManager = new ActiveMQORSecurityManager(authorisationService, this, realm -> identityProvider.getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID), "", new SecurityConfiguration() {@Overridepublic AppConfigurationEntry[] getAppConfigurationEntry(String name) {return new AppConfigurationEntry[]{- new AppConfigurationEntry(GuestLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT, Map.of("debug", "true", "credentialsInvalidate", "true", "org.apache.activemq.jaas.guest.user", ANONYMOUS_USERNAME, "org.apache.activemq.jaas.guest.role", ANONYMOUS_USERNAME)),- new AppConfigurationEntry(MultiTenantClientCredentialsGrantsLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUISITE, Map.of(- MultiTenantClientCredentialsGrantsLoginModule.INCLUDE_REALM_ROLES_OPTION, "true",- AbstractKeycloakLoginModule.ROLE_PRINCIPAL_CLASS_OPTION, RolePrincipal.class.getName()- )),- new AppConfigurationEntry(PrincipalConversionLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUISITE, Map.of(PrincipalConversionLoginModule.PRINCIPAL_CLASS_LIST, KeycloakPrincipal.class.getName()))+ new AppConfigurationEntry(GuestLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT, Map.of("debug", "true", "credentialsInvalidate", "true", "org.apache.activemq.jaas.guest.user", ANONYMOUS_USERNAME, "org.apache.activemq.jaas.guest.role", ANONYMOUS_USERNAME)),+ new AppConfigurationEntry(MultiTenantClientCredentialsGrantsLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUISITE, Map.of(+ MultiTenantClientCredentialsGrantsLoginModule.INCLUDE_REALM_ROLES_OPTION, "true",+ AbstractKeycloakLoginModule.ROLE_PRINCIPAL_CLASS_OPTION, RolePrincipal.class.getName()+ )),+ new AppConfigurationEntry(PrincipalConversionLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUISITE, Map.of(PrincipalConversionLoginModule.PRINCIPAL_CLASS_LIST, KeycloakPrincipal.class.getName()))};}- }));+ });+ server.setSecurityManager(securityManager);server.start();LOG.fine("Started MQTT broker");@@ -281,10 +287,9 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,if (persistenceEvent.getCause() == PersistenceEvent.Cause.UPDATE) {// Force disconnect if certain properties have changed- forceDisconnect = Arrays.stream(persistenceEvent.getPropertyNames()).anyMatch((propertyName) ->- (propertyName.equals("enabled") && !user.getEnabled())- || propertyName.equals("username")- || propertyName.equals("secret"));+ forceDisconnect = persistenceEvent.hasPropertyChanged("enabled")+ || persistenceEvent.hasPropertyChanged("username")+ || persistenceEvent.hasPropertyChanged("secret");}if (forceDisconnect) {@@ -439,6 +444,9 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}}+ /**+ * Get active connections for the specified user ID+ */public Set<RemotingConnection> getUserConnections(String userID) {if (TextUtil.isNullOrEmpty(userID)) {return Collections.emptySet();@@ -447,20 +455,12 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return server.getActiveMQServer().getRemotingService().getConnections().stream().filter(connection -> {Subject subject = connection.getSubject();String subjectID = KeycloakIdentityProvider.getSubjectId(subject);-- if (subjectID == null) {- // Could be an auto provisioning client- Triple<String, String, String> userIDNameAndPassword = transientCredentials.get(connection.getID());- return userIDNameAndPassword != null && Objects.equals(userID, userIDNameAndPassword.getLeft());- } else {- return userID.equals(subjectID);- }+ return userID.equals(subjectID);}).collect(Collectors.toSet());}protected void doForceDisconnect(RemotingConnection connection) {LOG.info("Force disconnecting client connection: " + connectionToString(connection));- removeTransientCredentials(connection);connection.disconnect(false);}@@ -502,21 +502,24 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,Subject subject = connection.getSubject();if (subject != null) {- username = getSubjectName(subject);+ username = getSubjectNameAndRealm(subject);}- if (username == null || ANONYMOUS_USERNAME.equals(username)) {- // Check if there are transient credentials for this connection- Triple<String, String, String> userIDNameAndPassword = transientCredentials.get(connection.getID());- if (userIDNameAndPassword != null) {- username = userIDNameAndPassword.getMiddle();- }- }return "connection=" + connection.getRemoteAddress() + ", clientID=" + connection.getClientID() + ", subject=" + username;}public static String getSubjectName(Subject subject) {- return subject.getPrincipals().stream().filter(principal -> principal instanceof UserPrincipal).findFirst().map(Principal::getName).orElse(KeycloakIdentityProvider.getSubjectName(subject));+ return subject.getPrincipals().stream().filter(principal -> principal instanceof UserPrincipal)+ .findFirst()+ .map(Principal::getName)+ .orElse(KeycloakIdentityProvider.getSubjectName(subject));+ }++ public static String getSubjectNameAndRealm(Subject subject) {+ return subject.getPrincipals().stream().filter(principal -> principal instanceof UserPrincipal)+ .findFirst()+ .map(Principal::getName)+ .orElse(KeycloakIdentityProvider.getSubjectNameAndRealm(subject));}public RemotingConnection getConnectionFromClientID(String clientID) {@@ -546,15 +549,13 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return connection;}- // TODO: Remove this- public void addTransientCredentials(RemotingConnection connection, Triple<String, String, String> userIDNameAndPassword) {- LOG.fine("Adding transient user credentials: connection ID=" + connection.getID() + " ,username=" + userIDNameAndPassword.getMiddle());- transientCredentials.put(connection.getID(), userIDNameAndPassword);- }-- // TODO: Remove this- public void removeTransientCredentials(RemotingConnection connection) {- LOG.fine("Removing transient user credentials: connection ID=" + connection.getID());- transientCredentials.remove(connection.getID());+ public void notifyConnectionAuthenticated(RemotingConnection connection) {+ if (connection.getSubject() != null) {+ // Notify handlers that connection authenticated+ LOG.info("Client connection authenticated: " + connectionToString(connection));+ for (MQTTHandler handler : getCustomHandlers()) {+ handler.onConnectionAuthenticated(connection);+ }+ }}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.javaindex 0bfac3dfa..1ae274c57 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java@@ -138,9 +138,8 @@ public abstract class MQTTHandler {/*** Will be called when any client connects; if returns false then subsequent handlers will not be called*/- public boolean onConnect(RemotingConnection connection) {+ public void onConnect(RemotingConnection connection) {- return true;}/**@@ -157,6 +156,15 @@ public abstract class MQTTHandler {}+ /**+ * Called when an existing {@link RemotingConnection} is authenticated sometime after initially connecting+ * (see {@link UserAssetProvisioningMQTTHandler}); the authenticated+ * {@link Subject} can be retrieved using {@link RemotingConnection#getSubject}+ */+ public void onConnectionAuthenticated(RemotingConnection connection) {++ }+/*** Checks that topic is valid and has at least 3 tokens (generally first two tokens should be {realm}/{clientId} but* it is up to the {@link #canSubscribe} and/or {@link #canPublish} methods to validate this).diff --git a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.javasimilarity index 96%rename from manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.javarename to manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.javaindex 5592f4bdd..d9cd0e337 100644--- a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java@@ -17,20 +17,18 @@* 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.provisioning;+package org.openremote.manager.mqtt;import io.netty.buffer.ByteBuf;import io.netty.handler.codec.mqtt.MqttQoS;import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;import org.apache.camel.builder.RouteBuilder;-import org.apache.commons.lang3.tuple.ImmutableTriple;import org.keycloak.KeycloakSecurityContext;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.timer.TimerService;import org.openremote.container.util.UniqueIdentifierGenerator;import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.mqtt.MQTTHandler;-import org.openremote.manager.mqtt.Topic;+import org.openremote.manager.provisioning.ProvisioningService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.Constants;@@ -89,9 +87,8 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (persistenceEvent.getCause() == PersistenceEvent.Cause.UPDATE) {// Force disconnect if the certain properties have changed- forceDisconnect = Arrays.stream(persistenceEvent.getPropertyNames()).anyMatch((propertyName) ->- propertyName.equals(ProvisioningConfig.DISABLED_PROPERTY_NAME)- || propertyName.equals(ProvisioningConfig.DATA_PROPERTY_NAME));+ forceDisconnect = persistenceEvent.hasPropertyChanged(ProvisioningConfig.DISABLED_PROPERTY_NAME)+ || persistenceEvent.hasPropertyChanged(ProvisioningConfig.DATA_PROPERTY_NAME);}if (forceDisconnect) {@@ -234,13 +231,11 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {@Overridepublic void onConnectionLost(RemotingConnection connection) {- mqttBrokerService.removeTransientCredentials(connection);provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));}@Overridepublic void onDisconnect(RemotingConnection connection) {- mqttBrokerService.removeTransientCredentials(connection);provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));}@@ -368,8 +363,11 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {LOG.fine("Client successfully initialised: topic=" + topic+ mqttBrokerService.connectionToString(connection) + ", config=" + matchingConfig);- // Store transient service user credentials (this is used by the custom ActiveMQSecurityManager)- mqttBrokerService.addTransientCredentials(connection, new ImmutableTriple<>(serviceUser.getId(), realm + ":" + serviceUser.getUsername(), serviceUser.getSecret()));+ // Authenticate the connection using this service user's credentials - this will also update the connection's subject+ connection.setSubject(null); // Clear existing anonymous subject+ mqttBrokerService.securityManager.authenticate(realm + ":" + serviceUser.getUsername(), serviceUser.getSecret(), connection, null);+ mqttBrokerService.notifyConnectionAuthenticated(connection);+provisioningConfigAuthenticatedConnectionMap.compute(matchingConfig.getId(), (id, connections) -> {if (connections == null) {connections = new HashSet<>();diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex 62994332d..082eea8d6 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -523,7 +523,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse}case UPDATE: {- boolean attributesChanged = Arrays.asList(persistenceEvent.getPropertyNames()).contains("attributes");+ boolean attributesChanged = persistenceEvent.hasPropertyChanged("attributes");AttributeMap oldAttributes = attributesChanged ? ((AttributeMap) persistenceEvent.getPreviousState("attributes")) : asset.getAttributes();AttributeMap currentAttributes = asset.getAttributes();diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.javaindex dfa5864f1..ad3bddcc0 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java@@ -28,6 +28,7 @@ import org.openremote.model.query.filter.StringPredicate;import org.openremote.model.security.*;import org.openremote.model.util.TextUtil;+import javax.persistence.Tuple;import javax.persistence.TypedQuery;import java.util.*;import java.util.stream.Collectors;@@ -261,6 +262,25 @@ public interface ManagerIdentityProvider extends IdentityProvider {});}+ static List<String> getUserIds(PersistenceService persistenceService, String realm, List<String> usernames) {+ return persistenceService.doReturningTransaction(em -> {+ Map<String, String> usernameIdMap = em.createQuery(+ "select u.username, u.id from User u join Realm r on r.id = u.realmId where u.username in :usernames and r.name = :realm", Tuple.class)+ .setParameter("usernames", usernames)+ .setParameter("realm", realm)+ .getResultList()+ .stream()+ .collect(+ Collectors.toMap(+ tuple -> (String) tuple.get(0),+ tuple -> (String) tuple.get(1)+ )+ );++ return usernames.stream().map(usernameIdMap::get).collect(Collectors.toList());+ });+ }+static Realm[] getRealmsFromDb(PersistenceService persistenceService) {return persistenceService.doReturningTransaction(entityManager -> {List<Realm> realms = entityManager.createQuery(diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.javaindex 1fae2b9d6..cfcd4e8ba 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java@@ -22,6 +22,7 @@ package org.openremote.manager.security;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.IdentityService;import org.openremote.container.timer.TimerService;+import org.openremote.manager.mqtt.MQTTBrokerService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Container;@@ -39,12 +40,14 @@ public class ManagerIdentityService extends IdentityService {public void init(Container container) throws Exception {super.init(container);persistenceService = container.getService(PersistenceService.class);+ MQTTBrokerService mqttBrokerService = container.getService(MQTTBrokerService.class);+ ManagerWebService managerWebService = container.getService(ManagerWebService.class);- container.getService(ManagerWebService.class).addApiSingleton(+ managerWebService.addApiSingleton(new RealmResourceImpl(container.getService(TimerService.class), this, container));- container.getService(ManagerWebService.class).addApiSingleton(- new UserResourceImpl(container.getService(TimerService.class), this)+ managerWebService.addApiSingleton(+ new UserResourceImpl(container.getService(TimerService.class), this, mqttBrokerService));}diff --git a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.javaindex edde21207..b5eb33988 100644--- a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java@@ -20,7 +20,9 @@package org.openremote.manager.security;import org.openremote.container.security.AuthContext;+import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.container.timer.TimerService;+import org.openremote.manager.mqtt.MQTTBrokerService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.Constants;import org.openremote.model.http.RequestParams;@@ -37,8 +39,11 @@ import static org.openremote.model.Constants.MASTER_REALM;public class UserResourceImpl extends ManagerWebResource implements UserResource {- public UserResourceImpl(TimerService timerService, ManagerIdentityService identityService) {+ protected MQTTBrokerService mqttBrokerService;++ public UserResourceImpl(TimerService timerService, ManagerIdentityService identityService, MQTTBrokerService mqttBrokerService) {super(timerService, identityService);+ this.mqttBrokerService = mqttBrokerService;}@Override@@ -322,6 +327,20 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource}}+ @Override+ public UserSession[] getUserSessions(RequestParams requestParams, String realm, String userId) {+ boolean hasAdminReadRole = hasResourceRole(ClientRole.READ_ADMIN.getValue(), Constants.KEYCLOAK_CLIENT_ID);++ if (!hasAdminReadRole && !Objects.equals(getUserId(), userId)) {+ throw new ForbiddenException("Can only retrieve own user sessions unless you have role '" + ClientRole.READ_ADMIN + "'");+ }++ return mqttBrokerService.getUserConnections(userId).stream().map(connection -> new UserSession(+ connection.getSubject() != null ? KeycloakIdentityProvider.getSubjectName(connection.getSubject()) : userId,+ connection.getCreationTime(),+ connection.getRemoteAddress())).toArray(UserSession[]::new);+ }+protected void throwIfIllegalMasterAdminUserDeletion(RequestParams requestParams, String realm, String userId) throws WebApplicationException {if (!realm.equals(MASTER_REALM)) {return;diff --git a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandlerindex 39e1cd182..f6b69e196 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler+++ b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler@@ -1,2 +1,3 @@org.openremote.manager.mqtt.DefaultMQTTHandler-org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler+org.openremote.manager.mqtt.UserAssetProvisioningMQTTHandler+org.openremote.manager.mqtt.ConnectionMonitorHandlerdiff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.jsonindex ed8ef1d45..04a0358c0 100644--- a/manager/src/web/shared/locales/en/or.json+++ b/manager/src/web/shared/locales/en/or.json@@ -621,5 +621,10 @@"reloadPage" : "To see the changes that have been made, reload the page","deleteRealmCustomization": "Delete Style","deleteRealmCustomizationConfirm":"Are you sure you want to delete this Realm style?"- }+ },+ "mqttSessions": "MQTT sessions",+ "address": "Address",+ "since": "Since",+ "roles": "Roles",+ "restrictedUser": "Restricted user"}diff --git a/model/src/main/java/org/openremote/model/PersistenceEvent.java b/model/src/main/java/org/openremote/model/PersistenceEvent.javaindex 05e6a2c2e..03e83519e 100644--- a/model/src/main/java/org/openremote/model/PersistenceEvent.java+++ b/model/src/main/java/org/openremote/model/PersistenceEvent.java@@ -20,6 +20,7 @@package org.openremote.model;import java.util.Arrays;+import java.util.List;public class PersistenceEvent<T> {@@ -29,14 +30,14 @@ public class PersistenceEvent<T> {final protected Cause cause;final protected T entity;- final protected String[] propertyNames;+ final protected List<String> propertyNames;final protected Object[] currentState;final protected Object[] previousState;public PersistenceEvent(Cause cause, T entity, String[] propertyNames, Object[] currentState, Object[] previousState) {this.cause = cause;this.entity = entity;- this.propertyNames = propertyNames;+ this.propertyNames = propertyNames != null ? Arrays.asList(propertyNames) : null;this.currentState = currentState;this.previousState = previousState;}@@ -53,7 +54,7 @@ public class PersistenceEvent<T> {return entity;}- public String[] getPropertyNames() {+ public List<String> getPropertyNames() {return propertyNames;}@@ -65,24 +66,32 @@ public class PersistenceEvent<T> {return previousState;}+ public boolean hasPropertyChanged(String propertyName) {+ return propertyNames != null && propertyNames.contains(propertyName);+ }+@SuppressWarnings("unchecked")public <E> E getPreviousState(String propertyName) {- return getPreviousState() != null ? (E) getPreviousState()[getPropertyIndex(propertyName)] : null;+ if (propertyNames == null || previousState == null) {+ return null;+ }+ int index = propertyNames.indexOf(propertyName);+ if (index < 0) {+ return null;+ }+ return (E) previousState[index];}@SuppressWarnings("unchecked")public <E> E getCurrentState(String propertyName) {- return (E) getCurrentState()[getPropertyIndex(propertyName)];- }-- protected int getPropertyIndex(String propertyName) {- for (int i = 0; i < getPropertyNames().length; i++) {- String property = getPropertyNames()[i];- if (property.equals(propertyName)) {- return i;- }+ if (propertyNames == null || currentState == null) {+ return null;+ }+ int index = propertyNames.indexOf(propertyName);+ if (index < 0) {+ return null;}- throw new IllegalArgumentException("Property not found: " + propertyName);+ return (E) currentState[index];}@Override@@ -90,7 +99,7 @@ public class PersistenceEvent<T> {return getClass().getSimpleName() + "{" +"cause=" + cause +", entity=" + entity +- ", propertyNames=" + Arrays.toString(propertyNames) ++ ", propertyNames=" + String.join(",", propertyNames) +", currentState=" + Arrays.toString(currentState) +", previousState=" + Arrays.toString(previousState) +'}';diff --git a/model/src/main/java/org/openremote/model/auth/OAuthClientCredentialsGrant.java b/model/src/main/java/org/openremote/model/auth/OAuthClientCredentialsGrant.javaindex 9d09e8a10..bc2d97f99 100644--- a/model/src/main/java/org/openremote/model/auth/OAuthClientCredentialsGrant.java+++ b/model/src/main/java/org/openremote/model/auth/OAuthClientCredentialsGrant.java@@ -28,7 +28,7 @@ public class OAuthClientCredentialsGrant extends OAuthGrant {public static final String CLIENT_CREDENTIALS_GRANT_TYPE = "client_credentials";@JsonCreator- public OAuthClientCredentialsGrant(@JsonProperty("tokenEndpointUri") String tokenEndpointUri,+ public OAuthClientCredentialsGrant(@JsonProperty(VALUE_KEY_TOKEN_ENDPOINT_URI) String tokenEndpointUri,@JsonProperty(VALUE_KEY_CLIENT_ID) String clientId,@JsonProperty(VALUE_KEY_CLIENT_SECRET) String clientSecret,@JsonProperty(VALUE_KEY_SCOPE) String scope) {diff --git a/model/src/main/java/org/openremote/model/auth/OAuthGrant.java b/model/src/main/java/org/openremote/model/auth/OAuthGrant.javaindex 9862f3a23..bf5765689 100644--- a/model/src/main/java/org/openremote/model/auth/OAuthGrant.java+++ b/model/src/main/java/org/openremote/model/auth/OAuthGrant.java@@ -41,6 +41,7 @@ import static org.openremote.model.util.TextUtil.requireNonNullAndNonEmpty;public abstract class OAuthGrant implements Serializable {public static final String VALUE_KEY_GRANT_TYPE = "grant_type";+ public static final String VALUE_KEY_TOKEN_ENDPOINT_URI = "tokenEndpointUri";public static final String VALUE_KEY_CLIENT_ID = "client_id";public static final String VALUE_KEY_CLIENT_SECRET = "client_secret";public static final String VALUE_KEY_SCOPE = "scope";diff --git a/model/src/main/java/org/openremote/model/auth/OAuthPasswordGrant.java b/model/src/main/java/org/openremote/model/auth/OAuthPasswordGrant.javaindex 6c47d2e98..3f266946c 100644--- a/model/src/main/java/org/openremote/model/auth/OAuthPasswordGrant.java+++ b/model/src/main/java/org/openremote/model/auth/OAuthPasswordGrant.java@@ -37,7 +37,7 @@ public class OAuthPasswordGrant extends OAuthClientCredentialsGrant {protected String password;@JsonCreator- public OAuthPasswordGrant(@JsonProperty("tokenEndpointUri") String tokenEndpointUri,+ public OAuthPasswordGrant(@JsonProperty(VALUE_KEY_TOKEN_ENDPOINT_URI) String tokenEndpointUri,@JsonProperty(VALUE_KEY_CLIENT_ID) String clientId,@JsonProperty(VALUE_KEY_CLIENT_SECRET) String clientSecret,@JsonProperty(VALUE_KEY_SCOPE) String scope,diff --git a/model/src/main/java/org/openremote/model/auth/OAuthRefreshTokenGrant.java b/model/src/main/java/org/openremote/model/auth/OAuthRefreshTokenGrant.javaindex 79ba7715d..d2a59e8d7 100644--- a/model/src/main/java/org/openremote/model/auth/OAuthRefreshTokenGrant.java+++ b/model/src/main/java/org/openremote/model/auth/OAuthRefreshTokenGrant.java@@ -33,7 +33,7 @@ public class OAuthRefreshTokenGrant extends OAuthClientCredentialsGrant {@JsonProperty(REFRESH_TOKEN_GRANT_TYPE)protected String refreshToken;@JsonCreator- public OAuthRefreshTokenGrant(@JsonProperty("tokenEndpointUri") String tokenEndpointUri,+ public OAuthRefreshTokenGrant(@JsonProperty(VALUE_KEY_TOKEN_ENDPOINT_URI) String tokenEndpointUri,@JsonProperty(VALUE_KEY_CLIENT_ID) String clientId,@JsonProperty(VALUE_KEY_CLIENT_SECRET) String clientSecret,@JsonProperty(VALUE_KEY_SCOPE) String scope,diff --git a/model/src/main/java/org/openremote/model/security/UserResource.java b/model/src/main/java/org/openremote/model/security/UserResource.javaindex f69ea7129..14b80937a 100644--- a/model/src/main/java/org/openremote/model/security/UserResource.java+++ b/model/src/main/java/org/openremote/model/security/UserResource.java@@ -161,4 +161,9 @@ public interface UserResource {@Consumes(APPLICATION_JSON)@RolesAllowed(Constants.WRITE_ADMIN_ROLE)void updateUserRealmRoles(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("userId") String userId, Role[] roles);++ @GET+ @Path("{realm}/userSessions/{userId}")+ @Produces(APPLICATION_JSON)+ UserSession[] getUserSessions(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("userId") String userId);}diff --git a/model/src/main/java/org/openremote/model/security/UserSession.java b/model/src/main/java/org/openremote/model/security/UserSession.javanew file mode 100644index 000000000..388417900--- /dev/null+++ b/model/src/main/java/org/openremote/model/security/UserSession.java@@ -0,0 +1,49 @@+/*+ * Copyright 2022, 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.model.security;++import com.fasterxml.jackson.annotation.JsonCreator;+import com.fasterxml.jackson.annotation.JsonIgnore;+import com.fasterxml.jackson.annotation.JsonProperty;++public class UserSession {+ protected final String username;+ protected final long startTimeMillis;+ protected final String remoteAddress;++ @JsonCreator+ public UserSession(String username, long startTimeMillis, String remoteAddress) {+ this.username = username;+ this.startTimeMillis = startTimeMillis;+ this.remoteAddress = remoteAddress;+ }++ public String getUsername() {+ return username;+ }++ public long getStartTimeMillis() {+ return startTimeMillis;+ }++ public String getRemoteAddress() {+ return remoteAddress;+ }+}diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.javaindex 15c881015..e6ee1c7d1 100644--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java@@ -25,7 +25,6 @@ import org.openremote.model.asset.agent.AgentLink;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeLink;import org.openremote.model.rules.AssetState;-import org.openremote.model.rules.TemporaryFact;import org.openremote.model.util.TsIgnore;import javax.validation.constraints.Pattern;@@ -188,6 +187,12 @@ public final class MetaItemType {*/public static final MetaItemDescriptor<Boolean> MOMENTARY = new MetaItemDescriptor<>("momentary", ValueType.BOOLEAN);+ /**+ * Can be used on a {@link ValueType#BOOLEAN} attribute to indicate if a service user is connected to the MQTT+ * broker; the value of this meta item should be the username of the user to monitor+ */+ public static final MetaItemDescriptor<String> USER_CONNECTED = new MetaItemDescriptor<>("userConnected", ValueType.TEXT);+protected MetaItemType() {}}diff --git a/setup/src/load1/java/org/openremote/setup/load1/ManagerSetup.java b/setup/src/load1/java/org/openremote/setup/load1/ManagerSetup.javaindex d51f42da9..2735fa82a 100644--- a/setup/src/load1/java/org/openremote/setup/load1/ManagerSetup.java+++ b/setup/src/load1/java/org/openremote/setup/load1/ManagerSetup.java@@ -37,7 +37,7 @@ import org.openremote.model.value.MetaItemType;import java.io.InputStream;import java.nio.charset.StandardCharsets;-import static org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler.UNIQUE_ID_PLACEHOLDER;+import static org.openremote.manager.mqtt.UserAssetProvisioningMQTTHandler.UNIQUE_ID_PLACEHOLDER;import static org.openremote.model.rules.Ruleset.Lang.GROOVY;import static org.openremote.model.value.ValueType.NUMBER;diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovyindex ae4132848..b9f9b95b1 100644--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy@@ -37,7 +37,7 @@ import org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.mqtt.DefaultMQTTHandlerimport org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.provisioning.ProvisioningService-import org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler+import org.openremote.manager.mqtt.UserAssetProvisioningMQTTHandlerimport org.openremote.manager.security.ManagerIdentityServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.asset.AssetEvent@@ -62,7 +62,8 @@ import spock.util.concurrent.PollingConditionsimport java.util.function.Consumerimport static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDString-import static org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler.*+import static org.openremote.manager.mqtt.UserAssetProvisioningMQTTHandler.*+import static org.openremote.model.value.ValueType.BOOLEANimport static org.openremote.model.value.ValueType.NUMBERclass UserAndAssetProvisioningTest extends Specification implements ManagerContainerTrait {@@ -122,7 +123,10 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContanew MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ),new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_WRITE)),- new Attribute<>("serialNumber", ValueType.TEXT, UNIQUE_ID_PLACEHOLDER)+ new Attribute<>("serialNumber", ValueType.TEXT, UNIQUE_ID_PLACEHOLDER),+ new Attribute<>("connected", BOOLEAN).addMeta(+ new MetaItem<>(MetaItemType.USER_CONNECTED, PROVISIONING_USER_PREFIX + "device1")+ ))).orElse("")).setRealm("building")@@ -237,6 +241,13 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert weatherAsset.getAttribute("serialNumber").flatMap{it.getValue()}.orElse(null) == device1UniqueId}+ and: "the connected attribute of the provisioned asset should show as connected"+ conditions.eventually {+ def weatherAssetID = ((SuccessResponseMessage)device1Responses.get(0)).asset.id+ def weatherAsset = assetStorageService.find(weatherAssetID)+ assert weatherAsset.getAttribute("connected").flatMap{it.value}.orElse(false)+ }+when: "the client then subscribes to attribute events for the generated asset and asset events for all assets"def asset = ((SuccessResponseMessage)device1Responses.get(0)).assetdef assetSubscriptionTopic = "$provisioningConfig.realm/$mqttDevice1ClientId/$DefaultMQTTHandler.ASSET_TOPIC/#".toString()@@ -324,6 +335,12 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert device1Client.getConnectionStatus() == ConnectionStatus.DISCONNECTED}+ and: "the connected attribute of the provisioned asset should show as not connected"+ conditions.eventually {+ def weatherAsset = ((SuccessResponseMessage)device1Responses.get(0)).asset+ assert !weatherAsset.getAttribute("connected").flatMap{it.value}.orElse(true)+ }+when: "the client reconnects"device1Client.connect()diff --git a/ui/app/manager/src/pages/page-users.ts b/ui/app/manager/src/pages/page-users.tsindex 088da670a..9f4b53bdb 100644--- a/ui/app/manager/src/pages/page-users.ts+++ b/ui/app/manager/src/pages/page-users.ts@@ -493,8 +493,10 @@ export class PageUsers extends Page<AppStateKeyed> {<p class="panel-title">${user.serviceAccount ? i18next.t('serviceUser') : i18next.t('user')}${i18next.t('settings')}</p>- ${this.getSingleUserView((index != undefined ? mergedUserList[index] : this.creationState.userModel), compositeRoleOptions, realmRoleOptions, ("user" + index), (readonly || this._saveUserPromise != undefined))}+ ${this.getSingleUserView(user, compositeRoleOptions, realmRoleOptions, ("user" + index), (readonly || this._saveUserPromise != undefined))}</div>++ ${user.serviceAccount ? this.getMQTTSessionTemplate(user) : ``}`;})}@@ -731,6 +733,32 @@ export class PageUsers extends Page<AppStateKeyed> {`;}+ protected getMQTTSessionTemplate(user: UserModel): TemplateResult {++ const sessionLoader = async () => {++ const userSessionsResponse = await (manager.rest.api.UserResource.getUserSessions(manager.displayRealm, user.id));++ if (!this.responseAndStateOK(() => userSessionsResponse.status === 200, userSessionsResponse, i18next.t("loadFailedUserInfo"))) {+ return html``;+ }++ const cols = [i18next.t("address"), i18next.t("since")];+ const rows = userSessionsResponse.data.map((session) => {+ return [session.remoteAddress, new Date(session.startTimeMillis)]+ });++ return html`<or-mwc-table .rows="${rows}" .config="${{stickyFirstColumn:false}}" .columns="${cols}"></or-mwc-table>`;+ };++ return html`+ <div id="content" class="panel">+ <p class="panel-title">${i18next.t('mqttSessions')}</p>+ ${until(sessionLoader(), html`${i18next.t('loading')}`)}+ </div>+ `;+ }+protected getSingleUserTemplate(user: UserModel, compositeRoleOptions: string[], realmRoleOptions: [string, string][], suffix: string, readonly: boolean = true): TemplateResult {const isServiceUser = user.serviceAccount;const isSameUser = user.username === manager.username;diff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.tsindex b949776b7..844c9ad5b 100644--- a/ui/component/or-asset-viewer/src/index.ts+++ b/ui/component/or-asset-viewer/src/index.ts@@ -773,6 +773,7 @@ function getPanelContent(id: string, assetInfo: AssetInfo, hostElement: LitElemereturn;}+ const cols = [i18next.t("username"), i18next.t("roles"), i18next.t("restrictedUser")];const rows = assetLinkInfos.sort(Util.sortByString(u => u.usernameAndId)).map(assetLinkInfo => {return [assetLinkInfo.usernameAndId,@@ -781,7 +782,7 @@ function getPanelContent(id: string, assetInfo: AssetInfo, hostElement: LitEleme];});- return html`<or-mwc-table .rows="${rows}" .config="${{stickyFirstColumn:false}}" .columns="${["Username", "Roles", "Restricted user"]}"></or-mwc-table>`;+ return html`<or-mwc-table .rows="${rows}" .config="${{stickyFirstColumn:false}}" .columns="${cols}"></or-mwc-table>`;}}