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
Migrate MQTT broker to ActiveMQ (#819)
Rich Turner409a974c3641
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.javaindex 8d042edc1..9b249a35d 100644--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java@@ -26,6 +26,7 @@ import org.flywaydb.core.Flyway;import org.flywaydb.core.api.MigrationInfo;import org.flywaydb.core.api.output.MigrateResult;import org.hibernate.Session;+import org.hibernate.annotations.Formula;import org.hibernate.cfg.AvailableSettings;import org.hibernate.engine.spi.SharedSessionContractImplementor;import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;@@ -418,7 +419,10 @@ public class PersistenceService implements ContainerService, Consumer<Persistenc/*** Generate {@link PersistenceEvent}s for entities not managed by JPA (i.e. Keycloak entities)*/- public void publishPersistenceEvent(PersistenceEvent.Cause cause, Object currentEntity, Object previousEntity, Field[] propertyFields) {+ public void publishPersistenceEvent(PersistenceEvent.Cause cause, Object currentEntity, Object previousEntity, Class<?> clazz, List<String> includeFields, List<String> excludeFields) {++ Field[] propertyFields = getEntityPropertyFields(clazz, includeFields, excludeFields);+switch (cause) {case CREATE:publishPersistenceEvent(cause, currentEntity, null, null, null);@@ -540,4 +544,11 @@ public class PersistenceService implements ContainerService, Consumer<Persistenc", persistenceUnitName='" + persistenceUnitName + '\'' +'}';}++ public static Field[] getEntityPropertyFields(Class<?> clazz, List<String> includeFields, List<String> excludeFields) {+ return Arrays.stream(clazz.getDeclaredFields())+ .filter(field -> ((field.isAnnotationPresent(Column.class) || field.isAnnotationPresent(EmbeddedId.class) || field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(Formula.class)) && (excludeFields == null || !excludeFields.contains(field.getName())))+ || (includeFields != null && includeFields.contains(field.getName())))+ .toArray(Field[]::new);+ }}diff --git a/container/src/main/java/org/openremote/container/security/AuthContext.java b/container/src/main/java/org/openremote/container/security/AuthContext.javaindex 53d611d63..d703da857 100644--- a/container/src/main/java/org/openremote/container/security/AuthContext.java+++ b/container/src/main/java/org/openremote/container/security/AuthContext.java@@ -24,6 +24,7 @@ import org.openremote.model.Constants;/*** Services should use this interface to access a user's identity and perform authorization checks.*/+// TODO: Remove this and just use AccessToken from Subject's KeycloakPrincipalpublic interface AuthContext {String getAuthenticatedRealmName();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 6ba35383f..42147ed7d 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@@ -31,27 +31,34 @@ import io.undertow.servlet.api.LoginConfig;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;+import org.keycloak.KeycloakPrincipal;+import org.keycloak.KeycloakSecurityContext;import org.keycloak.adapters.KeycloakConfigResolver;import org.keycloak.adapters.KeycloakDeployment;import org.keycloak.adapters.KeycloakDeploymentBuilder;import org.keycloak.admin.client.resource.RealmsResource;+import org.keycloak.representations.AccessToken;import org.keycloak.representations.adapters.config.AdapterConfig;import org.openremote.container.security.IdentityProvider;import org.openremote.container.web.OAuthFilter;import org.openremote.container.web.WebClient;import org.openremote.container.web.WebService;import org.openremote.container.web.WebTargetBuilder;+import org.openremote.model.Constants;import org.openremote.model.Container;import org.openremote.model.auth.OAuthGrant;import org.openremote.model.auth.OAuthPasswordGrant;+import javax.security.auth.Subject;import javax.ws.rs.NotFoundException;import javax.ws.rs.core.Response;import javax.ws.rs.core.UriBuilder;import java.net.URI;+import java.security.Principal;import java.util.Arrays;import java.util.List;import java.util.Objects;+import java.util.Optional;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.TimeUnit;import java.util.function.Function;@@ -377,6 +384,9 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {keycloakServiceUri.clone().build().toString());+ // Set preferred username as principal attribute+ adapterConfig.setPrincipalAttribute("preferred_username");+return KeycloakDeploymentBuilder.build(adapterConfig);}};@@ -403,4 +413,38 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {* There must be _some_ valid redirect URIs for the application or authentication will not be possible.*/abstract protected void addClientRedirectUris(String client, List<String> redirectUrls, boolean devMode);++ public static KeycloakSecurityContext getSecurityContext(Subject subject) {+ if (subject == null || subject.getPrincipals() == null) {+ return null;+ }++ return subject.getPrincipals().stream().filter(p -> p instanceof KeycloakPrincipal<?>).findFirst()+ .map(keycloakPrincipal ->+ ((KeycloakPrincipal<?>)keycloakPrincipal).getKeycloakSecurityContext()).orElse(null);+ }++ public static String getSubjectName(Subject subject) {+ if (subject == null || subject.getPrincipals() == null) {+ return null;+ }++ return subject.getPrincipals().stream().filter(p -> p instanceof KeycloakPrincipal<?>).findFirst()+ .map(Principal::getName).orElse(null);+ }++ public static String getSubjectId(Subject subject) {+ if (subject == null || subject.getPrincipals() == null) {+ return null;+ }++ return Optional.ofNullable(getSecurityContext(subject))+ .map(KeycloakSecurityContext::getToken)+ .map(AccessToken::getSubject)+ .orElse(null);+ }++ public static boolean isSuperUser(KeycloakSecurityContext securityContext) {+ return securityContext != null && Constants.MASTER_REALM.equals(securityContext.getRealm()) && securityContext.getToken().getRealmAccess().isUserInRole(Constants.REALM_ADMIN_ROLE);+ }}diff --git a/container/src/main/java/org/openremote/container/timer/TimerService.java b/container/src/main/java/org/openremote/container/timer/TimerService.javaindex 5ffe53902..8137ff491 100644--- a/container/src/main/java/org/openremote/container/timer/TimerService.java+++ b/container/src/main/java/org/openremote/container/timer/TimerService.java@@ -21,6 +21,7 @@ package org.openremote.container.timer;import org.openremote.model.Container;import org.openremote.model.ContainerService;+import org.openremote.model.rules.RulesClock;import java.time.Instant;import java.util.Date;@@ -36,7 +37,7 @@ import static org.openremote.container.util.MapAccess.getString;/*** Wall real clock timer or pseudo clock time (for testing).*/-public class TimerService implements ContainerService {+public class TimerService implements ContainerService, RulesClock {private static final Logger LOG = Logger.getLogger(TimerService.class.getName());public static final String TIMER_CLOCK_TYPE = "TIMER_CLOCK_TYPE";@@ -105,14 +106,13 @@ public class TimerService implements ContainerService {@Overridepublic void start() {- if (stopTime != null) {- stopTime = null;- }+ stopTime = null;}@Overridepublic void reset() {offset.set(0L);+ stopTime = null;}};diff --git a/gradle.properties b/gradle.propertiesindex 76442a720..70fe4b2f7 100644--- a/gradle.properties+++ b/gradle.properties@@ -42,7 +42,6 @@ firebaseAdminVersion = 7.1.0simpleJavaMailVersion = 5.0.4jaywayJsonPathVersion = 2.7.0typescriptGeneratorVersion = 2.28.785-moquetteVersion = 0.15californiumCoreVersion = 2.1.0californiumScandiumVersion = 3.5.0zwaveVersion = 3.4.0-SNAPSHOT@@ -57,3 +56,4 @@ failsafeVersion=3.1.0suncalcVersion=3.5snapshotsRepoUrl=https://s01.oss.sonatype.org/content/repositories/snapshots/releasesRepoUrl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/+activeMQVersion = 2.26.0diff --git a/manager/build.gradle b/manager/build.gradleindex d1922f49e..7fcb69a6c 100644--- a/manager/build.gradle+++ b/manager/build.gradle@@ -28,8 +28,18 @@ dependencies {force = true}- api ("io.moquette:moquette-broker:$moquetteVersion") {- exclude module: "slf4j-log4j12" // Don't want log4J+ // Exposed via MQTT API+ api ("io.netty:netty-codec-mqtt:$nettyVersion") {+ force = true+ }++ implementation("org.apache.activemq:artemis-mqtt-protocol:$activeMQVersion") {+ exclude group: "org.osgi"+ }++ // Needed for reference in tests+ api("org.apache.activemq:artemis-server:$activeMQVersion") {+ exclude group: "org.osgi"}implementation("org.quartz-scheduler:quartz:$quartzVersion") {diff --git a/manager/src/consoleappconfig/master.json b/manager/src/consoleappconfig/master.jsondeleted file mode 100644index ab1bf582d..000000000--- a/manager/src/consoleappconfig/master.json+++ /dev/null@@ -1,11 +0,0 @@-{- "realm": "master",- "initialUrl": "/console_loader/?realm=master&consoleProviders=geofence push storage",- "url": "/manager/?realm=master&consoleProviders=geofence push storage",- "menuEnabled": false,- "menuPosition": "BOTTOM_RIGHT",- "menuImage": null,- "primaryColor": "#4D9D2A",- "secondaryColor": "#CFCFCF",- "links": null-}\ No newline at end of filediff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 89a8a1548..2e6473245 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -200,7 +200,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourthrow new WebApplicationException(FORBIDDEN);}- assetStorageService.deleteUserAssetsByUserId(userId);+ assetStorageService.deleteUserAssetLinks(userId);}@Overridediff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex ee1b4eeae..5c826f313 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -674,6 +674,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn gatewayService.mergeGatewayAsset(gatewayId, asset);}+ // Validate realm+ if (asset.getRealm() == null) {+ String msg = "Asset realm must be set : asset=" + asset;+ LOG.info(msg);+ throw new IllegalStateException(msg);+ }+// Do standard JSR-380 validation on the asset (includes custom validation)Set<ConstraintViolation<Asset<?>>> validationFailures = ValueUtil.validate(asset, Asset.AssetSave.class);@@ -721,13 +728,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}}- // Validate realm- if (asset.getRealm() == null) {- String msg = "Asset realm must be set : asset=" + asset;- LOG.info(msg);- throw new IllegalStateException(msg);- }-if (!identityService.getIdentityProvider().realmExists(asset.getRealm())) {String msg = "Asset realm not found or is inactive: asset=" + asset;LOG.info(msg);@@ -841,7 +841,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicT updatedAsset = em.merge(asset);if (user != null) {- storeUserAssetLinks(em, Collections.singletonList(new UserAssetLink(user.getRealm(), user.getId(), updatedAsset.getId())));+ createUserAssetLinks(em, Collections.singletonList(new UserAssetLink(user.getRealm(), user.getId(), updatedAsset.getId())));}return updatedAsset;@@ -1075,30 +1075,33 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn Collections.emptyList();}- return persistenceService.doReturningTransaction(entityManager -> {- StringBuilder sb = new StringBuilder();- Map<String, Object> parameters = new HashMap<>(3);- sb.append("select ua from UserAssetLink ua where 1=1");+ return persistenceService.doReturningTransaction(em ->+ buildFindUserAssetLinksQuery(em, realm, userIds, assetIds).getResultList());+ }- if (!isNullOrEmpty(realm)) {- sb.append(" and ua.id.realm in :realm");- parameters.put("realm", realm);- }- if (userIds != null && !userIds.isEmpty()) {- sb.append(" and ua.id.userId in :userId");- parameters.put("userId", userIds);- }- if (assetIds != null && !assetIds.isEmpty()) {- sb.append(" and ua.id.assetId in :assetId");- parameters.put("assetId", assetIds);- }+ protected TypedQuery<UserAssetLink> buildFindUserAssetLinksQuery(EntityManager em, String realm, List<String> userIds, List<String> assetIds) {+ StringBuilder sb = new StringBuilder();+ Map<String, Object> parameters = new HashMap<>(3);+ sb.append("select ua from UserAssetLink ua where 1=1");- sb.append(" order by ua.createdOn desc");+ if (!isNullOrEmpty(realm)) {+ sb.append(" and ua.id.realm in :realm");+ parameters.put("realm", realm);+ }+ if (userIds != null && !userIds.isEmpty()) {+ sb.append(" and ua.id.userId in :userId");+ parameters.put("userId", userIds);+ }+ if (assetIds != null && !assetIds.isEmpty()) {+ sb.append(" and ua.id.assetId in :assetId");+ parameters.put("assetId", assetIds);+ }- TypedQuery<UserAssetLink> query = entityManager.createQuery(sb.toString(), UserAssetLink.class);- parameters.forEach(query::setParameter);- return query.getResultList();- });+ sb.append(" order by ua.createdOn desc");++ TypedQuery<UserAssetLink> query = em.createQuery(sb.toString(), UserAssetLink.class);+ parameters.forEach(query::setParameter);+ return query;}/* ####################################################################################### */@@ -1107,7 +1110,29 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic* Delete specific {@link UserAssetLink}s.*/public void deleteUserAssetLinks(List<UserAssetLink> userAssetLinks) {++ if (userAssetLinks == null || userAssetLinks.isEmpty()) {+ return;+ }++ Set<String> assetIds = new HashSet<>(userAssetLinks.size());+ Set<String> userIds = new HashSet<>(userAssetLinks.size());++ userAssetLinks.forEach(userAssetLink -> {+ userIds.add(userAssetLink.getId().getUserId());+ assetIds.add(userAssetLink.getId().getAssetId());+ });++ List<UserAssetLink> existingLinks = new ArrayList<>();+persistenceService.doTransaction(entityManager -> {+ existingLinks.addAll(buildFindUserAssetLinksQuery(entityManager, null, userIds.stream().toList(), assetIds.stream().toList())+ .getResultList().stream().filter(userAssetLinks::contains).toList());++ if (existingLinks.size() != userAssetLinks.size()) {+ throw new IllegalArgumentException("Cannot delete one or more requested user asset links as they don't exist");+ }+StringBuilder sb = new StringBuilder("DELETE FROM user_asset_link WHERE (1=0");IntStream.range(0, userAssetLinks.size()).forEach(i -> sb.append(" OR (asset_id=?")@@ -1131,27 +1156,24 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicint deleteCount = query.executeUpdate();if (deleteCount != userAssetLinks.size()) {- throw new IllegalArgumentException("Cannot delete one or more requested user asset link as they don't exist");+ throw new IllegalArgumentException("Cannot delete one or more requested user asset links as they don't exist");}});- }- /**- * Delete all {@link UserAssetLink}s for the specified realm (must be called before the realm is removed)- */- public void deleteUserAssetsByRealm(String realm) {- persistenceService.doTransaction(entityManager -> {- Query query = entityManager.createQuery("DELETE FROM UserAssetLink ual WHERE ual.id.realm = ?0");- query.setParameter(0, realm);- int deleteCount = query.executeUpdate();- LOG.fine("Deleted all user asset links for realm: realm=" + realm + ", count=" + deleteCount);- });+ existingLinks.forEach(userAssetLink ->+ persistenceService.publishPersistenceEvent(+ PersistenceEvent.Cause.DELETE,+ null,+ userAssetLink,+ UserAssetLink.class,+ null,+ null));}/**- * Delete all {@link UserAssetLink}s for the specified {@link User} (must be called before the user is removed)+ * Delete all {@link UserAssetLink}s for the specified {@link User}*/- public void deleteUserAssetsByUserId(String userId) {+ public void deleteUserAssetLinks(String userId) {persistenceService.doTransaction(entityManager -> {Query query = entityManager.createQuery("DELETE FROM UserAssetLink ual WHERE ual.id.userId = ?0");query.setParameter(0, userId);@@ -1160,30 +1182,36 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic});}- /**- * Delete all {@link UserAssetLink}s for the specified {@link Asset} (must be called before the asset is removed)- */- public void deleteUserAssetsByAssetId(String assetId) {- persistenceService.doTransaction(entityManager -> {- Query query = entityManager.createQuery("DELETE FROM UserAssetLink ual WHERE ual.id.assetId = ?0");- query.setParameter(0, assetId);- int deleteCount = query.executeUpdate();- LOG.fine("Deleted all user asset links for asset: asset ID=" + assetId + ", count=" + deleteCount);- });- }-/*** Create specified {@link UserAssetLink}s.*/public void storeUserAssetLinks(List<UserAssetLink> userAssetLinks) {- if (userAssetLinks.isEmpty()) {+ if (userAssetLinks == null || userAssetLinks.isEmpty()) {return;}- persistenceService.doTransaction(em -> storeUserAssetLinks(em, userAssetLinks));+ Set<String> assetIds = new HashSet<>(userAssetLinks.size());+ Set<String> userIds = new HashSet<>(userAssetLinks.size());++ userAssetLinks.forEach(userAssetLink -> {+ userIds.add(userAssetLink.getId().getUserId());+ assetIds.add(userAssetLink.getId().getAssetId());+ });++ persistenceService.doTransaction(em -> {+ List<UserAssetLink> existingLinks = buildFindUserAssetLinksQuery(em, null, userIds.stream().toList(), assetIds.stream().toList())+ .getResultList();++ List<UserAssetLink> newLinks = userAssetLinks.stream()+ .filter(userAssetLink -> !existingLinks.contains(userAssetLink))+ .toList();++ createUserAssetLinks(em, newLinks);+ });}- protected void storeUserAssetLinks(EntityManager em, List<UserAssetLink> userAssets) {++ protected void createUserAssetLinks(EntityManager em, List<UserAssetLink> userAssets) {em.unwrap(Session.class).doWork(connection -> {@@ -1200,6 +1228,17 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicst.addBatch();}st.executeBatch();++ // Create a persistence event for each one+ userAssets.forEach(userAssetLink ->+ persistenceService.publishPersistenceEvent(+ PersistenceEvent.Cause.CREATE,+ userAssetLink,+ null,+ UserAssetLink.class,+ null,+ null)+ );} catch (Exception e) {String msg = "Failed to create user assets: count=" + userAssets.size();LOG.log(Level.WARNING, msg, e);diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 0acee334d..303c938bf 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -26,7 +26,7 @@ import org.openremote.container.security.AuthContext;import org.openremote.container.timer.TimerService;import org.openremote.container.web.ConnectionConstants;import org.openremote.manager.gateway.GatewayService;-import org.openremote.manager.mqtt.MqttBrokerService;+import org.openremote.manager.mqtt.MQTTBrokerService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Constants;@@ -35,12 +35,11 @@ import org.openremote.model.ContainerService;import org.openremote.model.event.shared.*;import org.openremote.model.syslog.SyslogEvent;-import javax.websocket.CloseReason;-import javax.websocket.Session;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;import java.util.concurrent.CopyOnWriteArraySet;import java.util.function.Consumer;-import java.util.logging.Level;import java.util.logging.Logger;import static org.apache.camel.builder.Builder.header;@@ -117,7 +116,7 @@ public class ClientEventService implements ContainerService {final protected Collection<EventSubscriptionAuthorizer> eventSubscriptionAuthorizers = new CopyOnWriteArraySet<>();final protected Collection<Consumer<Exchange>> exchangeInterceptors = new CopyOnWriteArraySet<>();- protected Map<String, SessionInfo> sessionKeyInfoMap = new HashMap<>();+ protected ConcurrentMap<String, SessionInfo> sessionKeyInfoMap = new ConcurrentHashMap<>();protected TimerService timerService;protected MessageBrokerService messageBrokerService;protected ManagerIdentityService identityService;@@ -316,8 +315,12 @@ public class ClientEventService implements ContainerService {return subscriptionId;}- public void cancelInternalSubscription(String subscriptionId) {- eventSubscriptions.cancel(INTERNAL_SESSION_KEY, new CancelEventSubscription(subscriptionId));+ public void cancelInternalSubscription(String sessionId) {+ eventSubscriptions.cancel(INTERNAL_SESSION_KEY, new CancelEventSubscription(sessionId));+ }++ public void cancelSubscriptions(String sessionId) {+ eventSubscriptions.cancelAll(sessionId);}@Override@@ -379,7 +382,7 @@ public class ClientEventService implements ContainerService {);} else if (sessionInfo.connectionType.equals(HEADER_CONNECTION_TYPE_MQTT)) {messageBrokerService.getProducerTemplate().sendBodyAndHeader(- MqttBrokerService.MQTT_CLIENT_QUEUE,+ MQTTBrokerService.MQTT_CLIENT_QUEUE,data,ConnectionConstants.SESSION_KEY, sessionKey);diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.javaindex b77021f5f..610f09577 100644--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java@@ -31,6 +31,8 @@ import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.util.TextUtil;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;import java.util.logging.Logger;/**@@ -41,7 +43,7 @@ public class EventSubscriptions {private static final Logger LOG = Logger.getLogger(EventSubscriptions.class.getName());final protected TimerService timerService;- final protected Map<String, SessionSubscriptions> sessionSubscriptionIdMap = new HashMap<>();+ final protected ConcurrentMap<String, SessionSubscriptions> sessionSubscriptionIdMap = new ConcurrentHashMap<>();class SessionSubscriptions extends HashSet<SessionSubscription<?>> {protected void createOrUpdate(boolean restrictedUser, boolean anonymousUser, EventSubscription<?> eventSubscription) {@@ -90,41 +92,35 @@ public class EventSubscriptions {}protected void createOrUpdate(String sessionKey, boolean restrictedUser, boolean anonymousUser, EventSubscription<?> subscription) {- synchronized (this.sessionSubscriptionIdMap) {- LOG.finer("For session '" + sessionKey + "', creating/updating: " + subscription);- SessionSubscriptions sessionSubscriptions =- this.sessionSubscriptionIdMap.computeIfAbsent(sessionKey, k -> new SessionSubscriptions());- sessionSubscriptions.createOrUpdate(restrictedUser, anonymousUser, subscription);- }+ LOG.finer("For session '" + sessionKey + "', creating/updating: " + subscription);+ SessionSubscriptions sessionSubscriptions =+ this.sessionSubscriptionIdMap.computeIfAbsent(sessionKey, k -> new SessionSubscriptions());+ sessionSubscriptions.createOrUpdate(restrictedUser, anonymousUser, subscription);}protected void cancel(String sessionKey, CancelEventSubscription subscription) {- synchronized (this.sessionSubscriptionIdMap) {- if (!this.sessionSubscriptionIdMap.containsKey(sessionKey)) {- return;- }- if (subscription.getEventType() == null && subscription.getSubscriptionId() == null) {- return;- }- LOG.finer("For session '" + sessionKey + "', cancelling: " + subscription);- SessionSubscriptions sessionSubscriptions = this.sessionSubscriptionIdMap.get(sessionKey);- if (!TextUtil.isNullOrEmpty(subscription.getSubscriptionId())) {- sessionSubscriptions.cancelById(subscription.getSubscriptionId());- } else {- sessionSubscriptions.cancelByType(subscription.getEventType());- }- if (sessionSubscriptions.isEmpty()) {- this.sessionSubscriptionIdMap.remove(sessionKey);- }+ if (!this.sessionSubscriptionIdMap.containsKey(sessionKey)) {+ return;+ }+ if (subscription.getEventType() == null && subscription.getSubscriptionId() == null) {+ return;+ }+ LOG.finer("For session '" + sessionKey + "', cancelling: " + subscription);+ SessionSubscriptions sessionSubscriptions = this.sessionSubscriptionIdMap.get(sessionKey);+ if (!TextUtil.isNullOrEmpty(subscription.getSubscriptionId())) {+ sessionSubscriptions.cancelById(subscription.getSubscriptionId());+ } else {+ sessionSubscriptions.cancelByType(subscription.getEventType());+ }+ if (sessionSubscriptions.isEmpty()) {+ this.sessionSubscriptionIdMap.remove(sessionKey);}}protected void cancelAll(String sessionKey) {- synchronized (this.sessionSubscriptionIdMap) {- if (this.sessionSubscriptionIdMap.containsKey(sessionKey)) {- LOG.finer("Cancelling all subscriptions for session: " + sessionKey);- this.sessionSubscriptionIdMap.remove(sessionKey);- }+ if (this.sessionSubscriptionIdMap.containsKey(sessionKey)) {+ LOG.finer("Cancelling all subscriptions for session: " + sessionKey);+ this.sessionSubscriptionIdMap.remove(sessionKey);}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javanew file mode 100644index 000000000..c6c199294--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -0,0 +1,200 @@+/*+ * 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.manager.mqtt;++import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;+import org.apache.activemq.artemis.core.security.CheckType;+import org.apache.activemq.artemis.core.security.Role;+import org.apache.activemq.artemis.core.server.ActiveMQServer;+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager;+import org.apache.commons.lang3.tuple.Triple;+import org.keycloak.KeycloakSecurityContext;+import org.keycloak.adapters.KeycloakDeployment;+import org.openremote.container.security.keycloak.KeycloakIdentityProvider;+import org.openremote.manager.security.AuthorisationService;+import org.openremote.manager.security.MultiTenantJaasCallbackHandler;+import org.openremote.model.syslog.SyslogCategory;++import javax.security.auth.Subject;+import javax.security.auth.login.LoginContext;+import javax.security.auth.login.LoginException;+import java.util.Set;+import java.util.function.Function;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.apache.activemq.artemis.core.remoting.CertificateUtil.getCertsFromConnection;+import static org.openremote.model.syslog.SyslogCategory.API;++/**+ * A security manager that uses the {@link org.openremote.manager.security.MultiTenantJaasCallbackHandler} with a+ * dynamic {@link org.keycloak.adapters.KeycloakDeployment} resolver.+ *+ * Unfortunately lots of private methods and fields in super class.+ */+public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {++ private static final Logger LOG = SyslogCategory.getLogger(API, ActiveMQORSecurityManager.class);+ protected AuthorisationService authorisationService;+ protected MQTTBrokerService brokerService;+ protected Function<String, KeycloakDeployment> deploymentResolver;++ // Duplicate fields due to being private in super class+ protected String certificateConfigName;+ protected String configName;+ protected SecurityConfiguration config;+ protected SecurityConfiguration certificateConfig;+ protected ActiveMQServer server;++ public ActiveMQORSecurityManager(AuthorisationService authorisationService, MQTTBrokerService brokerService, Function<String, KeycloakDeployment> deploymentResolver, String configurationName, SecurityConfiguration configuration) {+ super(configurationName, configuration);+ this.authorisationService = authorisationService;+ this.brokerService = brokerService;+ this.deploymentResolver = deploymentResolver;+ this.configName = configurationName;+ this.config = configuration;+ }++ @Override+ public Subject authenticate(String user, String password, RemotingConnection remotingConnection, String securityDomain) {+ try {+ return getAuthenticatedSubject(user, password, remotingConnection, securityDomain);+ } catch (LoginException e) {+ return null;+ }+ }++ protected Subject getAuthenticatedSubject(String user,+ String password,+ final RemotingConnection remotingConnection,+ final String securityDomain) throws LoginException {+ LoginContext lc;+ String realm = null;+ 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 connection: " + remotingConnection.getID() + ", user=" + transientCredentials.getMiddle());+ user = transientCredentials.getMiddle();+ password = transientCredentials.getRight();+ }+ }++ if (user != null) {+ String[] realmAndUsername = user.split(":");+ if (realmAndUsername.length == 2) {+ realm = realmAndUsername[0];+ user = realmAndUsername[1];+ }+ }++ try {+ if (thisLoader != currentLoader) {+ Thread.currentThread().setContextClassLoader(thisLoader);+ }+ if (securityDomain != null) {+ lc = new LoginContext(securityDomain, null, new MultiTenantJaasCallbackHandler(deploymentResolver, realm, user, password, remotingConnection), null);+ } else if (certificateConfigName != null && certificateConfigName.length() > 0 && getCertsFromConnection(remotingConnection) != null) {+ lc = new LoginContext(certificateConfigName, null, new MultiTenantJaasCallbackHandler(deploymentResolver, realm, user, password, remotingConnection), certificateConfig);+ } else {+ lc = new LoginContext(configName, null, new MultiTenantJaasCallbackHandler(deploymentResolver, realm, user, password, remotingConnection), config);+ }+ try {+ lc.login();+ } catch (LoginException e) {+ throw e;+ }+ Subject subject = lc.getSubject();++ if (subject != null) {+ // Insert remoting connection for use in authorisation+ subject.getPrincipals().add(new MQTTConnectionPrincipal(remotingConnection));+ // Ensure subject is available when afterCreateConnection is fired+ remotingConnection.setSubject(subject);+ }++ return subject;+ } finally {+ if (thisLoader != currentLoader) {+ Thread.currentThread().setContextClassLoader(currentLoader);+ }+ }+ }++ @Override+ public boolean authorize(Subject subject, Set<Role> roles, CheckType checkType, String address) {++ return switch (checkType) {+ case SEND -> verifyRights(subject, address, true);+ case CONSUME -> {+ int index = address.indexOf("::");+ address = address.substring(0, index);+ yield verifyRights(subject, address, false);+ }+ case CREATE_ADDRESS, DELETE_ADDRESS, CREATE_DURABLE_QUEUE, DELETE_DURABLE_QUEUE, CREATE_NON_DURABLE_QUEUE, DELETE_NON_DURABLE_QUEUE ->+ // All MQTT clients must be able to create addresses and queues (every session and subscription will create a queue within the topic address)+ true;+ case MANAGE, BROWSE -> false;+ };+ }++ @SuppressWarnings({"rawtypes", "unchecked"})+ protected boolean verifyRights(Subject subject, String address, boolean isWrite) {+ KeycloakSecurityContext securityContext = KeycloakIdentityProvider.getSecurityContext(subject);+ RemotingConnection connection = MQTTHandler.getConnectionFromSubject(subject);+ Topic topic;++ try {+ // Get MQTT topic from address+ topic = Topic.fromAddress(address, brokerService.getWildcardConfiguration());+ } catch (IllegalArgumentException e) {+ LOG.log(Level.WARNING, "Invalid topic provided by client '" + address + "', connection=" + connection, e);+ return false;+ }++ if (isWrite && topic.hasWildcard()) {+ return false;+ }++ // See if a custom handler wants to handle authorisation for this topic pub/sub+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {+ if (handler.handlesTopic(topic)) {+ LOG.fine("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ boolean result;++ if (isWrite) {+ result = handler.checkCanPublish(connection, securityContext, topic);+ } else {+ result = handler.checkCanSubscribe(connection, securityContext, topic);+ }+ return result;+ }+ }++ LOG.fine("No handler has allowed " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", connection=" + connection);+ return false;+ }++}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex ab99aeea7..b22757beb 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -19,20 +19,15 @@*/package org.openremote.manager.mqtt;-import io.moquette.broker.subscriptions.Token;-import io.moquette.broker.subscriptions.Topic;-import io.moquette.interception.messages.*;import io.netty.buffer.ByteBuf;-import io.netty.buffer.Unpooled;-import io.netty.handler.codec.mqtt.*;-import org.openremote.agent.protocol.mqtt.MQTTLastWill;-import org.openremote.container.message.MessageBrokerService;+import io.netty.channel.ChannelId;+import io.netty.handler.codec.mqtt.MqttQoS;+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.keycloak.KeycloakSecurityContext;import org.openremote.container.security.AuthContext;import org.openremote.container.web.ConnectionConstants;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.event.ClientEventService;-import org.openremote.manager.security.ManagerIdentityService;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.Constants;import org.openremote.model.Container;import org.openremote.model.asset.AssetEvent;@@ -45,15 +40,11 @@ import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.ValueUtil;import java.nio.charset.StandardCharsets;-import java.util.ArrayList;-import java.util.HashMap;-import java.util.List;-import java.util.Map;+import java.util.*;import java.util.function.Consumer;import java.util.function.Function;import java.util.logging.Logger;import java.util.regex.Pattern;-import java.util.stream.Collectors;import static org.openremote.model.Constants.ASSET_ID_REGEXP;import static org.openremote.model.syslog.SyslogCategory.API;@@ -73,22 +64,6 @@ public class DefaultMQTTHandler extends MQTTHandler {private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);protected AssetStorageService assetStorageService;- public static boolean isAttributeTopic(Topic topic) {- return ATTRIBUTE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2)) || ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));- }-- public static boolean isAttributeWriteTopic(Topic topic) {- return ATTRIBUTE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));- }-- public static boolean isAttributeValueWriteTopic(Topic topic) {- return ATTRIBUTE_VALUE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));- }-- public static boolean isAssetTopic(Topic topic) {- return ASSET_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));- }-@Overridepublic int getPriority() {// This handler is intended to be the final handler but this can obviously be overridden by another handler@@ -102,33 +77,21 @@ public class DefaultMQTTHandler extends MQTTHandler {}@Override- public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {- super.onConnect(connection, msg);-- // Moquette is awful and client ID rather than socket descriptors are used everywhere so cannot distinguish- // between multiple connections using the same client ID- // Force disconnect any existing session that is using this client ID- clientEventService.closeSession(connection.getClientId());-- // Clear existing subscriptions by sending session close message+ public boolean onConnect(RemotingConnection connection) {+ super.onConnect(connection);Map<String, Object> headers = prepareHeaders(connection);- headers.put(ConnectionConstants.SESSION_CLOSE, true);- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);-- headers.remove(ConnectionConstants.SESSION_CLOSE);headers.put(ConnectionConstants.SESSION_OPEN, true);// Put a close connection runnable into the headers for the client event service- // Get the current session which is specific to the current connection- Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection.getClientId());+ Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection);headers.put(ConnectionConstants.SESSION_TERMINATOR, closeRunnable);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);return true;}@Override- public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {- super.onDisconnect(connection, msg);+ public void onDisconnect(RemotingConnection connection) {+ super.onDisconnect(connection);Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE, true);@@ -136,31 +99,11 @@ public class DefaultMQTTHandler extends MQTTHandler {}@Override- public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {- super.onConnectionLost(connection, msg);+ public void onConnectionLost(RemotingConnection connection) {+ super.onConnectionLost(connection);Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);-- // Try and publish last will if configured- if (connection.hasLastWill()) {- // Pass through standard canPublish/publish- MQTTLastWill lastWill = connection.getLastWill();- Topic lastWillTopic = Topic.asTopic(lastWill.getTopic());-- if (handlesTopic(lastWillTopic)) {-- getLogger().info("Processing last will for client: " + connection);-- if (canPublish(connection, lastWillTopic)) {- MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, true, MqttQoS.EXACTLY_ONCE, false, 0);- MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(lastWill.getTopic(), 0);- ByteBuf payload = Unpooled.wrappedBuffer(lastWill.getPayload().toString().getBytes());- InterceptPublishMessage message = new InterceptPublishMessage(new MqttPublishMessage(fixedHeader, varHeader, payload), msg.getClientID(), msg.getUsername());- doPublish(connection, lastWillTopic, message);- }- }- }}@Override@@ -175,14 +118,14 @@ public class DefaultMQTTHandler extends MQTTHandler {@SuppressWarnings({"rawtypes", "unchecked"})@Override- public boolean canSubscribe(MqttConnection connection, Topic topic) {+ public boolean canSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (!isKeycloak) {LOG.fine("Identity provider is not keycloak");return false;}- AuthContext authContext = connection.getAuthContext();+ AuthContext authContext = getAuthContextFromSecurityContext(securityContext);if (authContext == null) {LOG.fine("Anonymous connection not supported: topic=" + topic + ", connection=" + connection);@@ -251,7 +194,7 @@ public class DefaultMQTTHandler extends MQTTHandler {}// Build filter for the topic and verify that the filter is OK for given auth context- AssetFilter<?> filter = buildAssetFilter(connection, topic);+ AssetFilter<?> filter = buildAssetFilter(topic);if (filter == null) {LOG.info("Failed to process subscription topic: topic=" + topic + ", connection=" + connection);@@ -263,8 +206,8 @@ public class DefaultMQTTHandler extends MQTTHandler {filter);- if (!clientEventService.authorizeEventSubscription(connection.getRealm(), authContext, subscription)) {- LOG.info("Subscription was not authorised for this user and topic: topic=" + topic + ", connection=" + connection);+ if (!clientEventService.authorizeEventSubscription(topicRealm(topic), authContext, subscription)) {+ LOG.info("Subscription was not authorised for this user and topic: topic=" + topic + ", subject=" + authContext);return false;}@@ -295,17 +238,17 @@ public class DefaultMQTTHandler extends MQTTHandler {}@Override- public boolean canPublish(MqttConnection connection, Topic topic) {-- AuthContext authContext = connection.getAuthContext();+ public boolean canPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (!isKeycloak) {LOG.fine("Identity provider is not keycloak");return false;}+ AuthContext authContext = getAuthContextFromSecurityContext(securityContext);+if (authContext == null) {- LOG.fine("Anonymous publish not supported: topic=" + topic + ", connection=" + connection);+ LOG.fine("Anonymous publish not supported: topic=" + topic + ", connection=" + connection.getTransportConnection());return false;}@@ -328,11 +271,11 @@ public class DefaultMQTTHandler extends MQTTHandler {@SuppressWarnings({"rawtypes", "unchecked"})@Override- public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {- boolean isAssetTopic = isAssetTopic(topic);- String subscriptionId = msg.getTopicFilter(); // Use topic as subscription ID+ public void onSubscribe(RemotingConnection connection, Topic topic) {- AssetFilter filter = buildAssetFilter(connection, topic);+ boolean isAssetTopic = isAssetTopic(topic);+ String subscriptionId = topic.getString(); // Use topic as unique subscription ID+ AssetFilter filter = buildAssetFilter(topic);Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;if (filter == null) {@@ -340,7 +283,7 @@ public class DefaultMQTTHandler extends MQTTHandler {return;}- Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic, msg.getRequestedQos());+ Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic);EventSubscription subscription = new EventSubscription(subscriptionClass,@@ -355,7 +298,7 @@ public class DefaultMQTTHandler extends MQTTHandler {@SuppressWarnings({"unchecked", "rawtypes"})@Override- public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {+ public void onUnsubscribe(RemotingConnection connection, Topic topic) {String subscriptionId = topic.toString();boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);Map<String, Object> headers = prepareHeaders(connection);@@ -365,10 +308,18 @@ public class DefaultMQTTHandler extends MQTTHandler {}@Override- public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {- List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());+ public Set<String> getPublishListenerTopics() {+ return Set.of(+ TOKEN_SINGLE_LEVEL_WILDCARD + "/" + TOKEN_SINGLE_LEVEL_WILDCARD + "/" + ATTRIBUTE_WRITE_TOPIC + "/" + TOKEN_MULTI_LEVEL_WILDCARD,+ TOKEN_SINGLE_LEVEL_WILDCARD + "/" + TOKEN_SINGLE_LEVEL_WILDCARD + "/" + ATTRIBUTE_VALUE_WRITE_TOPIC + "/" + TOKEN_MULTI_LEVEL_WILDCARD+ );+ }++ @Override+ public void onPublish(RemotingConnection connection, Topic topic, ByteBuf body) {+ List<String> topicTokens = topic.getTokens();boolean isValueWrite = topicTokens.get(2).equals(ATTRIBUTE_VALUE_WRITE_TOPIC);- String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);+ String payloadContent = body.toString(StandardCharsets.UTF_8);AttributeEvent attributeEvent;if (isValueWrite) {@@ -389,11 +340,11 @@ public class DefaultMQTTHandler extends MQTTHandler {messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);}- public static AssetFilter<?> buildAssetFilter(MqttConnection connection, Topic topic) {+ protected static AssetFilter<?> buildAssetFilter(Topic topic) {boolean isAttributeTopic = isAttributeTopic(topic);boolean isAssetTopic = isAssetTopic(topic);- String realm = connection.getRealm();+ String realm = topicRealm(topic);List<String> assetIds = new ArrayList<>();List<String> parentIds = new ArrayList<>();List<String> paths = new ArrayList<>();@@ -472,10 +423,13 @@ public class DefaultMQTTHandler extends MQTTHandler {return assetFilter;}- protected Consumer<SharedEvent> getSubscriptionEventConsumer(MqttConnection connection, Topic topic, MqttQoS mqttQoS) {+ protected Consumer<SharedEvent> getSubscriptionEventConsumer(RemotingConnection connection, Topic topic) {boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));boolean isAssetTopic = isAssetTopic(topic);+ // Always publish asset/attribute messages with QoS 0+ MqttQoS mqttQoS = MqttQoS.AT_MOST_ONCE;+// Build topic expander (replace wildcards) so it isn't computed for each eventFunction<SharedEvent, String> topicExpander;@@ -520,4 +474,30 @@ public class DefaultMQTTHandler extends MQTTHandler {}};}++ protected static boolean isAttributeTopic(Topic topic) {+ return ATTRIBUTE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2)) || ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));+ }++ protected static boolean isAttributeWriteTopic(Topic topic) {+ return ATTRIBUTE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));+ }++ protected static boolean isAttributeValueWriteTopic(Topic topic) {+ return ATTRIBUTE_VALUE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));+ }++ protected static boolean isAssetTopic(Topic topic) {+ return ASSET_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));+ }++ protected static Map<String, Object> prepareHeaders(RemotingConnection connection) {+ Optional<AuthContext> authContext = getAuthContextFromConnection(connection);+ Map<String, Object> headers = new HashMap<>();+ headers.put(ConnectionConstants.SESSION_KEY, MQTTBrokerService.getConnectionIDString(connection));+ headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);+ headers.put(Constants.AUTH_CONTEXT, authContext.orElse(null));+ headers.put(Constants.REALM_PARAM_NAME, authContext.map(AuthContext::getAuthenticatedRealmName));+ return headers;+ }}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.javanew file mode 100644index 000000000..273914559--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java@@ -0,0 +1,473 @@+/*+ * 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.manager.mqtt;++import io.netty.channel.ChannelId;+import io.netty.handler.codec.mqtt.MqttQoS;+import org.apache.activemq.artemis.api.core.ActiveMQException;+import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;+import org.apache.activemq.artemis.api.core.client.*;+import org.apache.activemq.artemis.api.core.management.CoreNotificationType;+import org.apache.activemq.artemis.api.core.management.ManagementHelper;+import org.apache.activemq.artemis.core.client.impl.ClientSessionInternal;+import org.apache.activemq.artemis.core.config.Configuration;+import org.apache.activemq.artemis.core.config.WildcardConfiguration;+import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;+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;+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerSessionPlugin;+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.apache.activemq.artemis.spi.core.security.jaas.GuestLoginModule;+import org.apache.activemq.artemis.spi.core.security.jaas.PrincipalConversionLoginModule;+import org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal;+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;+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.event.ClientEventService;+import org.openremote.manager.security.AuthorisationService;+import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;+import org.openremote.manager.security.MultiTenantClientCredentialsGrantsLoginModule;+import org.openremote.model.Container;+import org.openremote.model.ContainerService;+import org.openremote.model.PersistenceEvent;+import org.openremote.model.asset.UserAssetLink;+import org.openremote.model.security.User;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.Debouncer;+import org.openremote.model.util.TextUtil;+import org.openremote.model.util.ValueUtil;++import javax.security.auth.Subject;+import javax.security.auth.login.AppConfigurationEntry;+import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;+import java.util.concurrent.ScheduledExecutorService;+import java.util.logging.Level;+import java.util.logging.Logger;+import java.util.stream.Collectors;++import static java.util.stream.StreamSupport.stream;+import static org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil.MQTT_QOS_LEVEL_KEY;+import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;+import static org.openremote.container.util.MapAccess.getInteger;+import static org.openremote.container.util.MapAccess.getString;+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;+import static org.openremote.model.syslog.SyslogCategory.API;++public class MQTTBrokerService extends RouteBuilder implements ContainerService, ActiveMQServerConnectionPlugin, ActiveMQServerSessionPlugin {++ public static final int PRIORITY = MED_PRIORITY;+ public static final String MQTT_CLIENT_QUEUE = "seda://MqttClientQueue?waitForTaskToComplete=IfReplyExpected&timeout=10000&purgeWhenStopping=true&discardIfNoConsumers=false&size=25000";+ public static final String MQTT_SERVER_LISTEN_HOST = "MQTT_SERVER_LISTEN_HOST";+ public static final String MQTT_SERVER_LISTEN_PORT = "MQTT_SERVER_LISTEN_PORT";+ protected final WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();+ protected static final Logger LOG = SyslogCategory.getLogger(API, MQTTBrokerService.class);++ protected AuthorisationService authorisationService;+ protected ManagerKeycloakIdentityProvider identityProvider;+ protected ClientEventService clientEventService;+ protected MessageBrokerService messageBrokerService;+ protected ScheduledExecutorService executorService;+ protected TimerService timerService;+ protected List<MQTTHandler> customHandlers = new ArrayList<>();+ protected ConcurrentMap<String, RemotingConnection> clientIDConnectionMap = new ConcurrentHashMap<>();+ // TODO: Make auto provisioning clients disconnect and reconnect with credentials+ // Temp to prevent breaking existing auto provisioning API+ protected ConcurrentMap<Object, Triple<String, String, String>> transientCredentials = new ConcurrentHashMap<>();+ protected Debouncer<String> userAssetDisconnectDebouncer;++ protected boolean active;+ protected String host;+ protected int port;+ protected EmbeddedActiveMQ server;+ protected ClientSessionInternal internalSession;++ @Override+ public int getPriority() {+ return PRIORITY;+ }++ @Override+ public void init(Container container) throws Exception {+ host = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, "0.0.0.0");+ port = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, 1883);++ 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);+ userAssetDisconnectDebouncer = new Debouncer<>(executorService, this::forceDisconnectUser, 10000);++ if (!identityService.isKeycloakEnabled()) {+ LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");+ active = false;+ } else {+ active = true;+ identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();+ container.getService(MessageBrokerService.class).getContext().addRoutes(this);+ }+ }++ @Override+ public void start(Container container) throws Exception {++ if (!active) {+ return;+ }++ // Load custom handlers+ this.customHandlers = stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)+ .sorted(Comparator.comparingInt(MQTTHandler::getPriority))+ .collect(Collectors.toList());++ // Configure and start the broker+ Configuration config = new ConfigurationImpl();+ config.addAcceptorConfiguration("in-vm", "vm://0?protocols=core");+ String serverURI = new URIBuilder().setScheme("tcp").setHost(host).setPort(port).setParameter("protocols", "MQTT").build().toString();+ config.addAcceptorConfiguration("tcp", serverURI);++ // TODO: Make this configurable+ config.setMaxDiskUsage(-1);+ config.setSecurityInvalidationInterval(30000);+ config.registerBrokerPlugin(this);++ // Register notification plugin+ config.setWildCardConfiguration(wildcardConfiguration);+ config.setPersistenceEnabled(false);++ server = new EmbeddedActiveMQ();+ server.setConfiguration(config);+ server.setSecurityManager(new ActiveMQORSecurityManager(authorisationService, this, realm -> identityProvider.getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID), "", new SecurityConfiguration() {+ @Override+ public AppConfigurationEntry[] getAppConfigurationEntry(String name) {+ return new AppConfigurationEntry[] {+ new AppConfigurationEntry(GuestLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT, Map.of("debug", "true", "credentialsInvalidate", "true")),+ 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.start();++ LOG.fine("Started MQTT broker");+++ // Add notification handler for subscribe/unsubscribe and publish events+ server.getActiveMQServer().getManagementService().addNotificationListener(notification -> {+ if (notification.getType() == CoreNotificationType.CONSUMER_CREATED || notification.getType() == CoreNotificationType.CONSUMER_CLOSED) {+ boolean isSubscribe = notification.getType() == CoreNotificationType.CONSUMER_CREATED;+ String sessionId = notification.getProperties().getSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME).toString();+ String topic = notification.getProperties().getSimpleStringProperty(ManagementHelper.HDR_ADDRESS).toString();+ ServerSession session = server.getActiveMQServer().getSessionByID(sessionId);++ // Ignore internal subscriptions+ boolean isInternal = session.getRemotingConnection().getTransportConnection() instanceof InVMConnection;+ if (isInternal) {+ return;+ }++ if (isSubscribe) {+ onSubscribe(session.getRemotingConnection(), MQTTUtil.convertCoreAddressToMqttTopicFilter(topic, wildcardConfiguration));+ } else {+ onUnsubscribe(session.getRemotingConnection(), MQTTUtil.convertCoreAddressToMqttTopicFilter(topic, wildcardConfiguration));+ }+ }+ });++ // Create internal producer for producing and consuming messages+ ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");+ ClientSessionFactory factory = serverLocator.createSessionFactory();+ String internalClientID = UniqueIdentifierGenerator.generateId("Internal client");+ internalSession = (ClientSessionInternal) factory.createSession(null, null, false, true, true, serverLocator.isPreAcknowledge(), serverLocator.getAckBatchSize(), internalClientID);+ ServerSession serverSession = server.getActiveMQServer().getSessionByID(internalSession.getName());+ serverSession.disableSecurity();+ internalSession.start();++ // Start each custom handler+ for (MQTTHandler 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;+ }+ }+ }++ @SuppressWarnings("unchecked")+ @Override+ public void configure() throws Exception {+ from(PERSISTENCE_TOPIC)+ .routeId("UserPersistenceChanges")+ .filter(body().isInstanceOf(PersistenceEvent.class))+ .process(exchange -> {+ PersistenceEvent<?> persistenceEvent = (PersistenceEvent<?>)exchange.getIn().getBody(PersistenceEvent.class);++ if (persistenceEvent.getEntity() instanceof User user) {++ if (!user.isServiceAccount()) {+ return;+ }++ boolean forceDisconnect = persistenceEvent.getCause() == PersistenceEvent.Cause.DELETE;++ 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"));+ }++ if (forceDisconnect) {+ LOG.info("User modified or deleted so force closing any sessions for this user: " + user);+ // Find existing connection for this user+ forceDisconnectUser(user.getId());+ }++ } else if (persistenceEvent.getEntity() instanceof UserAssetLink userAssetLink) {+ String userID = userAssetLink.getId().getUserId();++ // Debounce force disconnect of this user's sessions+ userAssetDisconnectDebouncer.call(userID);+ }+ });+ }++ @Override+ public void stop(Container container) throws Exception {+ server.stop();+ LOG.fine("Stopped MQTT broker");++ stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)+ .sorted(Comparator.comparingInt(MQTTHandler::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);+ }+ });+ }++ @Override+ public void afterCreateConnection(RemotingConnection connection) throws ActiveMQException {++ // MQTT seems to only use failure callback even if closed gracefully (need to look at the exception for details)+ connection.addFailureListener(new FailureListener() {+ @Override+ public void connectionFailed(ActiveMQException exception, boolean failedOver) {+ connectionFailed(exception, failedOver, null);+ }++ @Override+ public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {+ // TODO: Force delete session (don't allow retained/durable sessions)++ if (exception.getType() == ActiveMQExceptionType.REMOTE_DISCONNECT) {// Seems to be the type for graceful close of connection+ // Notify handlers of connection close+ for (MQTTHandler handler : getCustomHandlers()) {+ handler.onDisconnect(connection);+ }+ } else {+ // Notify handlers of connection failure+ for (MQTTHandler handler : getCustomHandlers()) {+ handler.onConnectionLost(connection);+ }+ }++ if (connection.getClientID() != null) {+ clientIDConnectionMap.remove(connection.getClientID());+ }+ }+ });+ }++ @Override+ public void afterCreateSession(ServerSession session) throws ActiveMQException {+ RemotingConnection remotingConnection = session.getRemotingConnection();++ if (remotingConnection == null || remotingConnection.getClientID() == null) {+ return;+ }++ if (!clientIDConnectionMap.containsKey(remotingConnection.getClientID())) {+ LOG.info("Session created");+ clientIDConnectionMap.put(remotingConnection.getClientID(), remotingConnection);++ // We do this here as connection plugin afterCreateConnection callback fires before client ID and subject are populated+ for (MQTTHandler handler : getCustomHandlers()) {+ handler.onConnect(remotingConnection);+ }+ }+ }++ @Override+ public void afterDestroyConnection(RemotingConnection connection) throws ActiveMQException {++ }++ public void onSubscribe(RemotingConnection connection, String topicStr) {+ Topic topic = Topic.parse(topicStr);++ for (MQTTHandler handler : getCustomHandlers()) {+ if (handler.handlesTopic(topic)) {+ LOG.fine("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ handler.onSubscribe(connection, topic);+ break;+ }+ }+ }++ public void onUnsubscribe(RemotingConnection connection, String topicStr) {+ Topic topic = Topic.parse(topicStr);++ for (MQTTHandler handler : getCustomHandlers()) {+ if (handler.handlesTopic(topic)) {+ LOG.fine("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ handler.onUnsubscribe(connection, topic);+ break;+ }+ }+ }++ public Iterable<MQTTHandler> getCustomHandlers() {+ return customHandlers;+ }++ public Runnable getForceDisconnectRunnable(RemotingConnection connection) {+ return () -> doForceDisconnect(connection);+ }++ public void forceDisconnectUser(String userID) {+ if (TextUtil.isNullOrEmpty(userID)) {+ return;+ }+ getUserConnections(userID).forEach(this::doForceDisconnect);+ }++ public Set<RemotingConnection> getUserConnections(String userID) {+ if (TextUtil.isNullOrEmpty(userID)) {+ return Collections.emptySet();+ }++ 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);+ }+ }).collect(Collectors.toSet());+ }++ protected void doForceDisconnect(RemotingConnection connection) {+ LOG.fine("Force disconnecting session: " + connection);+ connection.disconnect(false);+ }++ public void publishMessage(String topic, Object data, MqttQoS qoS) {+ try {+ if (internalSession != null) {+ ClientProducer producer = internalSession.createProducer(MQTTUtil.convertMqttTopicFilterToCoreAddress(topic, server.getConfiguration().getWildcardConfiguration()));+ ClientMessage message = internalSession.createMessage(false);+ message.putIntProperty(MQTT_QOS_LEVEL_KEY, qoS.value());+ message.writeBodyBufferBytes(ValueUtil.asJSON(data).map(String::getBytes).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)));+ producer.send(message);+ }+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);+ }+ }++ public WildcardConfiguration getWildcardConfiguration() {+ return wildcardConfiguration;+ }++ public static String getConnectionIDString(RemotingConnection connection) {+ if (connection == null) {+ return null;+ }++ Object ID = connection.getID();+ return ID instanceof ChannelId ? ((ChannelId)ID).asLongText() : ID.toString();+ }++ public RemotingConnection getConnectionFromClientID(String clientID) {+ if (TextUtil.isNullOrEmpty(clientID)) {+ return null;+ }++ // This logic is needed because the connection clientID isn't populated when afterCreateConnection is called+ RemotingConnection connection = clientIDConnectionMap.get(clientID);++ if (connection == null) {+ // Try and find the client ID from the sessions+ for (ServerSession serverSession : server.getActiveMQServer().getSessions()) {+ if (serverSession.getRemotingConnection() != null && Objects.equals(clientID, serverSession.getRemotingConnection().getClientID())) {+ connection = serverSession.getRemotingConnection();+ clientIDConnectionMap.put(clientID, connection);+ break;+ }+ }+ }++ 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);+ ((SecurityStoreImpl)server.getActiveMQServer().getSecurityStore()).invalidateAuthenticationCache();+ }++ // TODO: Remove this+ public void removeTransientCredentials(RemotingConnection connection) {+ LOG.fine("Removing transient user credentials: connection ID=" + connection.getID());+ transientCredentials.remove(connection.getID());+ ((SecurityStoreImpl)server.getActiveMQServer().getSecurityStore()).invalidateAuthenticationCache();+ ((SecurityStoreImpl)server.getActiveMQServer().getSecurityStore()).invalidateAuthorizationCache();+ }+}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.javanew file mode 100644index 000000000..4dfc4e4b8--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.java@@ -0,0 +1,56 @@+/*+ * 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.manager.mqtt;++import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;++import java.security.Principal;+import java.util.Objects;++public class MQTTConnectionPrincipal implements Principal {++ protected RemotingConnection connection;++ public MQTTConnectionPrincipal(RemotingConnection connection) {+ this.connection = connection;+ }++ @Override+ public String getName() {+ return connection.getProtocolName();+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (o == null || getClass() != o.getClass()) return false;+ MQTTConnectionPrincipal that = (MQTTConnectionPrincipal) o;+ return connection.equals(that.connection);+ }++ @Override+ public int hashCode() {+ return Objects.hash(connection);+ }++ public RemotingConnection getConnection() {+ return 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 d09b2ba87..3f88c076e 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java@@ -19,22 +19,34 @@*/package org.openremote.manager.mqtt;-import io.moquette.broker.subscriptions.Topic;-import io.moquette.interception.messages.*;+import io.netty.buffer.ByteBuf;+import org.apache.activemq.artemis.api.core.ActiveMQException;+import org.apache.activemq.artemis.api.core.QueueConfiguration;+import org.apache.activemq.artemis.api.core.RoutingType;+import org.apache.activemq.artemis.api.core.client.ClientConsumer;+import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;+import org.apache.activemq.artemis.reader.MessageUtil;+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.keycloak.KeycloakSecurityContext;import org.openremote.container.message.MessageBrokerService;-import org.openremote.container.web.ConnectionConstants;+import org.openremote.container.security.AuthContext;+import org.openremote.container.security.keycloak.AccessTokenAuthContext;+import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.Constants;import org.openremote.model.Container;+import org.openremote.model.util.Pair;-import java.util.HashMap;-import java.util.Map;+import javax.security.auth.Subject;+import java.util.Objects;+import java.util.Optional;+import java.util.Set;+import java.util.logging.Level;import java.util.logging.Logger;/**- * This allows custom handlers to be discovered by the {@link MqttBrokerService} during system startup using the+ * 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.*/@@ -43,7 +55,7 @@ public abstract class MQTTHandler {public static final String TOKEN_MULTI_LEVEL_WILDCARD = "#";public static final String TOKEN_SINGLE_LEVEL_WILDCARD = "+";protected ClientEventService clientEventService;- protected MqttBrokerService mqttBrokerService;+ protected MQTTBrokerService mqttBrokerService;protected MessageBrokerService messageBrokerService;protected ManagerKeycloakIdentityProvider identityProvider;protected boolean isKeycloak;@@ -67,7 +79,7 @@ public abstract class MQTTHandler {* Called when the system starts to allow for initialisation.*/public void start(Container container) throws Exception {- mqttBrokerService = container.getService(MqttBrokerService.class);+ mqttBrokerService = container.getService(MQTTBrokerService.class);clientEventService = container.getService(ClientEventService.class);messageBrokerService = container.getService(MessageBrokerService.class);ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);@@ -79,6 +91,28 @@ public abstract class MQTTHandler {isKeycloak = true;identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();}++ Set<String> publishListenerTopics = getPublishListenerTopics();+ if (publishListenerTopics != null) {+ publishListenerTopics.forEach(this::addPublishConsumer);+ }+ }++ protected void addPublishConsumer(String topic) {+ try {+ getLogger().info("Adding publish consumer for topic '" + topic + "': handler=" + getName());+ String coreTopic = MQTTUtil.convertMqttTopicFilterToCoreAddress(topic, mqttBrokerService.wildcardConfiguration);+ mqttBrokerService.internalSession.createQueue(new QueueConfiguration(coreTopic).setRoutingType(RoutingType.ANYCAST).setAutoCreateAddress(true).setAutoCreated(true));+ ClientConsumer consumer = mqttBrokerService.internalSession.createConsumer(coreTopic);+ consumer.setMessageHandler(message -> {+ Topic publishTopic = Topic.parse(MQTTUtil.convertCoreAddressToMqttTopicFilter(message.getAddress(), mqttBrokerService.wildcardConfiguration));+ String clientID = message.getStringProperty(MessageUtil.CONNECTION_ID_PROPERTY_NAME);+ RemotingConnection connection = mqttBrokerService.getConnectionFromClientID(clientID);+ onPublish(connection, publishTopic, message.getReadOnlyBodyBuffer().byteBuf());+ });+ } catch (ActiveMQException e) {+ getLogger().log(Level.WARNING, "Failed to create handler consumer for topic '" + topic + "': handler=" + getName(), e);+ }}/**@@ -91,21 +125,22 @@ public abstract class MQTTHandler {/*** Will be called when any client connects; if returns false then subsequent handlers will not be called*/- public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {+ public boolean onConnect(RemotingConnection connection) {+return true;}/*** Will be called when any client disconnects*/- public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {+ public void onDisconnect(RemotingConnection connection) {}/*** Will be called when any client loses connection*/- public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {+ public void onConnectionLost(RemotingConnection connection) {}@@ -129,40 +164,32 @@ public abstract class MQTTHandler {* Checks that authenticated session and topic realm matches the authenticated user and also that topic client ID* matches the connection client ID.*/- public boolean checkCanSubscribe(MqttConnection connection, Topic topic) {- if (connection.getAuthContext() == null) {- getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic + ", connection" + connection);+ public boolean checkCanSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {+ if (securityContext == null) {+ getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic);return false;}- if (!topicRealmMatchesConnection(connection, topic) || !topicClientIdMatchesConnection(connection, topic)) {- getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", connection" + connection);+ if (!topicRealmAllowed(securityContext, topic) || !topicClientIdMatches(connection, topic)) {+ getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", user=" + securityContext);return false;}- if (!canSubscribe(connection, topic)) {- getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);- return false;- }- return true;+ return canSubscribe(connection, securityContext, topic);}/*** Checks that authenticated sessions and topic realm matches the authenticated user and also that topic client ID* matches the connection client ID.*/- public boolean checkCanPublish(MqttConnection connection, Topic topic) {- if (connection.getAuthContext() == null) {- getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic + ", connection" + connection);- return false;- }- if (!topicRealmMatchesConnection(connection, topic) || !topicClientIdMatchesConnection(connection, topic)) {- getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", connection" + connection);+ public boolean checkCanPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {+ if (securityContext == null) {+ getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic);return false;}- if (!canPublish(connection, topic)) {- getLogger().fine("Cannot publish to this topic, topic=" + topic + ", connection" + connection);+ if (!topicRealmAllowed(securityContext, topic) || !topicClientIdMatches(connection, topic)) {+ getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", user=" + securityContext);return false;}- return true;+ return canPublish(connection, securityContext, topic);}/**@@ -177,35 +204,45 @@ public abstract class MQTTHandler {* Called to authorise a subscription if {@link #handlesTopic} returned true; should return true if the subscription* is allowed otherwise return false.*/- public abstract boolean canSubscribe(MqttConnection connection, Topic topic);+ public abstract boolean canSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic);/*** Called to authorise a publish if {@link #handlesTopic} returned true; should return true if the publish is* allowed otherwise return false.*/- public abstract boolean canPublish(MqttConnection connection, Topic topic);+ public abstract boolean canPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic);/*** Called to handle subscribe if {@link #canSubscribe} returned true.*/- public abstract void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);+ public abstract void onSubscribe(RemotingConnection connection, Topic topic);/*** Called to handle unsubscribe if {@link #handlesTopic} returned true.*/- public abstract void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg);+ public abstract void onUnsubscribe(RemotingConnection connection, Topic topic);++ /**+ * Get the set of topics this handler wants to subscribe to for incoming publish messages; messages that match+ * these topics will be passed to {@link #onPublish}.+ */+ public abstract Set<String> getPublishListenerTopics();/*** Called to handle publish if {@link #canPublish} returned true.*/- public abstract void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);+ public abstract void onPublish(RemotingConnection connection, Topic topic, ByteBuf body);++ public static String topicRealm(Topic topic) {+ return topicTokenIndexToString(topic, 0);+ }- public static boolean topicRealmMatchesConnection(MqttConnection connection, Topic topic) {- return connection.realm.equals(topicTokenIndexToString(topic, 0));+ public static boolean topicRealmAllowed(KeycloakSecurityContext securityContext, Topic topic) {+ return securityContext.getRealm().equals(topicRealm(topic)) || KeycloakIdentityProvider.isSuperUser(securityContext);}- public static boolean topicClientIdMatchesConnection(MqttConnection connection, Topic topic) {- return connection.clientId.equals(topicTokenIndexToString(topic, 1));+ public static boolean topicClientIdMatches(RemotingConnection connection, Topic topic) {+ return Objects.equals(connection.getClientID(), topicTokenIndexToString(topic, 1));}public static boolean topicTokenCountGreaterThan(Topic topic, int size) {@@ -213,15 +250,33 @@ public abstract class MQTTHandler {}public static String topicTokenIndexToString(Topic topic, int tokenNumber) {- return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens().get(tokenNumber).toString() : null;+ return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens().get(tokenNumber) : null;+ }++ protected static Subject getSubjectFromConnection(RemotingConnection connection) {+ return connection != null ? connection.getSubject() : null;+ }++ protected static RemotingConnection getConnectionFromSubject(Subject subject) {+ return subject.getPrincipals()+ .stream()+ .filter(p -> p instanceof MQTTConnectionPrincipal)+ .findFirst()+ .map(p -> ((MQTTConnectionPrincipal)p).getConnection())+ .orElse(null);+ }++ protected static KeycloakSecurityContext getSecurityContextFromSubject(Subject subject) {+ return KeycloakIdentityProvider.getSecurityContext(subject);+ }++ protected static Optional<AuthContext> getAuthContextFromConnection(RemotingConnection connection) {+ return Optional.ofNullable(getSubjectFromConnection(connection))+ .map(MQTTHandler::getSecurityContextFromSubject)+ .map(DefaultMQTTHandler::getAuthContextFromSecurityContext);}- public static Map<String, Object> prepareHeaders(MqttConnection connection) {- Map<String, Object> headers = new HashMap<>();- headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());- headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);- headers.put(Constants.AUTH_CONTEXT, connection.getAuthContext());- headers.put(Constants.REALM_PARAM_NAME, connection.getRealm());- return headers;+ protected static AuthContext getAuthContextFromSecurityContext(KeycloakSecurityContext securityContext) {+ return securityContext == null ? null : new AccessTokenAuthContext(securityContext.getRealm(), securityContext.getToken());}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.javadeleted file mode 100644index fcb7b2f72..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ /dev/null@@ -1,394 +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.BrokerConstants;-import io.moquette.broker.Server;-import io.moquette.broker.SessionRegistry;-import io.moquette.broker.config.MemoryConfig;-import io.moquette.broker.security.IAuthenticator;-import io.moquette.interception.InterceptHandler;-import io.netty.buffer.ByteBuf;-import io.netty.buffer.Unpooled;-import io.netty.handler.codec.mqtt.MqttMessageBuilders;-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.model.PersistenceEvent;-import org.openremote.container.timer.TimerService;-import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.event.ClientEventService;-import org.openremote.manager.security.ManagerIdentityService;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.Container;-import org.openremote.model.ContainerService;-import org.openremote.model.security.Realm;-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.lang.reflect.Field;-import java.lang.reflect.Method;-import java.nio.charset.Charset;-import java.nio.charset.StandardCharsets;-import java.util.*;-import java.util.concurrent.ScheduledExecutorService;-import java.util.logging.Level;-import java.util.logging.Logger;-import java.util.stream.Collectors;--import static java.util.stream.StreamSupport.stream;-import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;-import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;-import static org.openremote.container.util.MapAccess.getInteger;-import static org.openremote.container.util.MapAccess.getString;-import static org.openremote.model.syslog.SyslogCategory.API;--public class MqttBrokerService extends RouteBuilder implements ContainerService, IAuthenticator {-- public static final int PRIORITY = MED_PRIORITY;- public static final String INTERNAL_CLIENT_ID = "ManagerInternal";- private static final Logger LOG = SyslogCategory.getLogger(API, MqttBrokerService.class);-- public static final String MQTT_CLIENT_QUEUE = "seda://MqttClientQueue?waitForTaskToComplete=IfReplyExpected&timeout=10000&purgeWhenStopping=true&discardIfNoConsumers=false&size=25000";- public static final String MQTT_SERVER_LISTEN_HOST = "MQTT_SERVER_LISTEN_HOST";- public static final String MQTT_SERVER_LISTEN_PORT = "MQTT_SERVER_LISTEN_PORT";-- protected ManagerKeycloakIdentityProvider identityProvider;- protected ClientEventService clientEventService;- protected MessageBrokerService messageBrokerService;- protected ScheduledExecutorService executorService;- protected TimerService timerService;- // We store connections that get 'pushed' out by a re-connect which can occur before we are notified of the disconnect- private final Map<String, MqttConnection> obsoleteConnectionMap = new HashMap<>();- private final Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();- protected List<MQTTHandler> customHandlers = new ArrayList<>();-- protected boolean active;- protected String host;- protected int port;- protected Server mqttBroker;- protected SessionRegistry sessionRegistry;-- @Override- public int getPriority() {- return PRIORITY;- }-- @Override- public void init(Container container) throws Exception {- host = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST);- port = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT);-- clientEventService = container.getService(ClientEventService.class);- ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);- messageBrokerService = container.getService(MessageBrokerService.class);- executorService = container.getExecutorService();- timerService = container.getService(TimerService.class);-- if (!identityService.isKeycloakEnabled()) {- LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");- active = false;- } else {- active = true;- identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();- container.getService(MessageBrokerService.class).getContext().addRoutes(this);- }- }-- @Override- public 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(true));- List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, timerService));-- // Load custom handlers- this.customHandlers = stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)- .sorted(Comparator.comparingInt(MQTTHandler::getPriority))- .collect(Collectors.toList());-- // Start each custom handler- for (MQTTHandler 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 = new Server();- mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, this, new ORAuthorizatorPolicy(identityProvider, this, assetStorageService, clientEventService));- LOG.fine("Started MQTT broker");- }--- @SuppressWarnings("unchecked")- @Override- public void configure() throws Exception {- from(PERSISTENCE_TOPIC)- .routeId("UserPersistenceChanges")- .filter(isPersistenceEventForEntityType(User.class))- .process(exchange -> {- PersistenceEvent<User> persistenceEvent = (PersistenceEvent<User>)exchange.getIn().getBody(PersistenceEvent.class);- User user = persistenceEvent.getEntity();-- if (!user.isServiceAccount()) {- return;- }-- boolean forceDisconnect = persistenceEvent.getCause() == PersistenceEvent.Cause.DELETE;-- 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"));- }-- if (forceDisconnect) {- // Find existing connection for this user- Arrays.stream(getConnections())- .filter(connection -> user.getUsername().equals(connection.getUsername()))- .findFirst()- .ifPresent(connection -> {- LOG.info("User modified or deleted so forcing connected client to disconnect: connection=" + connection);- forceDisconnect(connection.getClientId());- });- }- });- }-- @Override- public void stop(Container container) throws Exception {- mqttBroker.stopServer();- LOG.fine("Stopped MQTT broker");-- stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)- .sorted(Comparator.comparingInt(MQTTHandler::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);- }- });- }-- /**- * Validates an incoming authenticated connection and if rejected it will close the connection without calling- * the intercept handler.- */- @Override- public boolean checkValid(String clientId, String username, byte[] password) {- String realmName = null;- if (username != null) {- String[] realmAndClientId = username.split(":");- realmName = realmAndClientId[0];- username = realmAndClientId[1]; // This is OAuth clientId- }- String suppliedClientSecret = password != null ? new String(password, StandardCharsets.UTF_8) : null;-- // Removed client ID check as it won't execute for anonymous connections; single connection per user is more- // important-- if (TextUtil.isNullOrEmpty(realmName)- || TextUtil.isNullOrEmpty(username)- || TextUtil.isNullOrEmpty(suppliedClientSecret)) {- LOG.fine("Realm, client ID and/or client secret missing so this is an anonymous session: " + clientId);- return true;- }-- Realm realm = identityProvider.getRealm(realmName);-- if (realm == null || !realm.isActive(timerService.getCurrentTimeMillis())) {- LOG.warning("Realm not found or is inactive: " + realmName);- return false;- }-- User user = identityProvider.getUserByUsername(realmName, 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: username=" + username);- return false;- }-- return suppliedClientSecret.equals(user.getSecret());- }-- public Iterable<MQTTHandler> getCustomHandlers() {- return customHandlers;- }-- public MqttConnection getConnection(String clientId) {- synchronized (clientIdConnectionMap) {- return clientIdConnectionMap.get(clientId);- }- }-- public MqttConnection[] getConnections() {- synchronized (clientIdConnectionMap) {- return clientIdConnectionMap.values().toArray(new MqttConnection[0]);- }- }-- /**- * We have this as there's no way to configure expiry time of sessions in Moquette so to make things simple we- * clear the session whenever the client disconnects; the client can then re-subscribe as needed- */- public MqttConnection clearConnectionSession(String clientId) {- if (mqttBroker == null) {- return null;- }-- synchronized (clientIdConnectionMap) {- MqttConnection connection = obsoleteConnectionMap.remove(clientId);-- if (connection == null) {- connection = clientIdConnectionMap.remove(clientId);- }-- if (connection != null && LOG.isLoggable(Level.FINEST)) {- LOG.finest("Removing connection: " + connection);- }-- if (connection != null && !connection.isCleanSession()) {- try {- SessionRegistry sessionRegistry = getSessionRegistry();- Object session = getSession(clientId);- if (sessionRegistry == null || session == null) {- throw new IllegalStateException("Couldn't get session registry and/or session for client: " + clientId);- }- Method removeMethod = sessionRegistry.getClass().getDeclaredMethod("remove", session.getClass());- removeMethod.setAccessible(true);- removeMethod.invoke(sessionRegistry, session);- } catch (Exception e) {- LOG.log(Level.WARNING, "Failed to remove Moquette session using reflection", e);- }- }- return connection;- }- }-- protected SessionRegistry getSessionRegistry() {- if (mqttBroker == null) {- return null;- }- if (sessionRegistry == null) {- try {- Field sessionsField = mqttBroker.getClass().getDeclaredField("sessions");- sessionsField.setAccessible(true);- sessionRegistry = (SessionRegistry) sessionsField.get(mqttBroker);- } catch (Exception e) {- LOG.log(Level.WARNING, "Failed to get Moquette session registry using reflection", e);- }- }-- return sessionRegistry;- }-- Object getSession(String clientId) {- if (mqttBroker == null) {- return null;- }-- try {- SessionRegistry sessionRegistry = getSessionRegistry();- Method retrieveSessionMethod = SessionRegistry.class.getDeclaredMethod("retrieve", String.class);- retrieveSessionMethod.setAccessible(true);- return retrieveSessionMethod.invoke(sessionRegistry, clientId);- } catch (Exception e) {- LOG.log(Level.WARNING, "Failed to get Moquette session using reflection", e);- }-- return null;- }-- public Runnable getForceDisconnectRunnable(String clientId) {- MqttConnection connection = getConnection(clientId);- Object session = getSession(clientId);-- return () -> doForceDisconnect(connection, session);- }-- public void forceDisconnect(String clientId) {- MqttConnection connection = getConnection(clientId);- Object session = getSession(clientId);- doForceDisconnect(connection, session);- }-- protected void doForceDisconnect(MqttConnection connection, Object session) {-- if (connection == null || session == null) {- return;- }-- try {- Method closeMethod = session.getClass().getDeclaredMethod("closeImmediately");- closeMethod.setAccessible(true);- Field connectionField = session.getClass().getDeclaredField("mqttConnection");- connectionField.setAccessible(true);- Method disconnectMethod = session.getClass().getDeclaredMethod("disconnect");- disconnectMethod.setAccessible(true);-- Object sessionConnection = connectionField.get(session);- if (sessionConnection != null) {- closeMethod.invoke(session);- }- disconnectMethod.invoke(session);- } catch (Exception e) {- LOG.log(Level.WARNING, "Failed to force disconnect Moquette session using reflection", e);- }- }-- public void addConnection(String clientId, MqttConnection connection) {- synchronized (clientIdConnectionMap) {- MqttConnection obsoleteConnection = clientIdConnectionMap.remove(clientId);- if (obsoleteConnection != null) {- obsoleteConnection.setObsolete();- obsoleteConnectionMap.put(clientId, obsoleteConnection);- }- if (LOG.isLoggable(Level.FINEST)) {- LOG.finest("Adding connection: " + connection);- }- clientIdConnectionMap.put(clientId, connection);- }- }-- public void publishMessage(String topic, Object data, MqttQoS qoS) {- try {- ByteBuf payload = Unpooled.copiedBuffer(ValueUtil.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());-- MqttPublishMessage publishMessage = MqttMessageBuilders.publish()- .qos(qoS)- .topicName(topic)- .payload(payload)- .build();-- mqttBroker.internalPublish(publishMessage, INTERNAL_CLIENT_ID);- } catch (Exception e) {- LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);- }- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.javadeleted file mode 100644index 5a3edfbe5..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java+++ /dev/null@@ -1,161 +0,0 @@-package org.openremote.manager.mqtt;--import org.keycloak.adapters.rotation.AdapterTokenVerifier;-import org.keycloak.common.VerificationException;-import org.keycloak.representations.AccessToken;-import org.openremote.agent.protocol.mqtt.MQTTLastWill;-import org.openremote.container.security.AuthContext;-import org.openremote.container.security.keycloak.AccessTokenAuthContext;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.auth.OAuthClientCredentialsGrant;-import org.openremote.model.auth.OAuthGrant;-import org.openremote.model.util.TextUtil;--import java.util.Objects;-import java.util.function.Supplier;-import java.util.logging.Level;-import java.util.logging.Logger;--import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;--/**- * Handles access token generation and tracks all subscriptions for the connection- */-public class MqttConnection {-- protected static final Logger LOG = Logger.getLogger(MqttConnection.class.getSimpleName());- protected String realm;- protected String username; // This is OAuth clientId- protected String password;- protected boolean credentials;- protected final String clientId;- protected final boolean cleanSession;- protected Supplier<String> tokenSupplier;- protected ManagerKeycloakIdentityProvider identityProvider;- protected long connectionTime;- protected boolean obsolete;- protected MQTTLastWill lastWill;-- public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession, long connectionTime, MQTTLastWill lastWill) {- this.cleanSession = cleanSession;- this.clientId = clientId;- this.identityProvider = identityProvider;- this.connectionTime = connectionTime;- this.lastWill = lastWill;- setCredentials(realm, username, password);- }-- public String getRealm() {- return this.realm;- }-- public String getUsername() {- return this.username;- }-- public String getPassword() {- return this.password;- }-- public boolean isCleanSession() {- return cleanSession;- }-- public String getAccessToken() {- if (tokenSupplier == null) {- return null;- }-- return tokenSupplier.get();- }-- public String getUserId() {- AuthContext authContext = getAuthContext();- return authContext != null ? authContext.getUserId() : null;- }-- public AuthContext getAuthContext() {- AuthContext authContext;-- if (!credentials) {- return null;- }-- try {- AccessToken accessToken = AdapterTokenVerifier.verifyToken(getAccessToken(), identityProvider.getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID));- authContext = accessToken != null ? new AccessTokenAuthContext(realm, accessToken) : null;- } catch (VerificationException e) {- LOG.log(Level.INFO, "Couldn't verify token: " + this, e);- return null;- }-- return authContext;- }-- /**- * This is MQTT client ID not to be confused with OAuth client ID- */- public String getClientId() {- return clientId;- }-- public boolean hasCredentials() {- return credentials;- }-- public boolean hasLastWill() {- return lastWill != null;- }-- public MQTTLastWill getLastWill() {- return lastWill;- }-- public void setCredentials(String realm, String username, String password) {-- this.realm = realm;- this.username = username;- this.password = password;-- credentials = !TextUtil.isNullOrEmpty(realm)- && !TextUtil.isNullOrEmpty(username)- && !TextUtil.isNullOrEmpty(password);-- if (credentials) {- String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();- OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);- tokenSupplier = identityProvider.getAccessTokenSupplier(grant);- } else {- LOG.fine("MQTT connection with no credentials so will have limited capabilities: " + this);- }- }-- protected void setObsolete() {- this.obsolete = true;- }-- @Override- public String toString() {- return this.getClass().getSimpleName() + "{" +- "connectionTime=" + connectionTime +- ", realm='" + realm + '\'' +- ", username='" + username + '\'' +- ", clientId='" + clientId + '\'' +- ", obsolete='" + obsolete + '\'' +- ", lastWill='" + lastWill + '\'' +- '}';- }-- @Override- public boolean equals(Object o) {- if (this == o) return true;- if (o == null || getClass() != o.getClass()) return false;- MqttConnection that = (MqttConnection) o;- return Objects.equals(realm, that.realm) && Objects.equals(username, that.username) && clientId.equals(that.clientId);- }-- @Override- public int hashCode() {- return Objects.hash(realm, username, clientId);- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.javadeleted file mode 100644index 94f89ce3f..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java+++ /dev/null@@ -1,97 +0,0 @@-package org.openremote.manager.mqtt;--import io.moquette.broker.security.IAuthorizatorPolicy;-import io.moquette.broker.subscriptions.Topic;-import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.event.ClientEventService;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.syslog.SyslogCategory;--import java.util.logging.Logger;--import static org.openremote.model.syslog.SyslogCategory.API;--public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {-- private static final Logger LOG = SyslogCategory.getLogger(API, ORAuthorizatorPolicy.class);-- protected final ManagerKeycloakIdentityProvider identityProvider;- protected final AssetStorageService assetStorageService;- protected final ClientEventService clientEventService;- protected final MqttBrokerService brokerService;-- public ORAuthorizatorPolicy(ManagerKeycloakIdentityProvider identityProvider,- MqttBrokerService brokerService,- AssetStorageService assetStorageService,- ClientEventService clientEventService) {- this.identityProvider = identityProvider;- this.brokerService = brokerService;- this.assetStorageService = assetStorageService;- this.clientEventService = clientEventService;- }-- @Override- public boolean canWrite(Topic topic, String username, String clientId) {- String realm = null;-- if (username != null) {- String[] realmAndUsername = username.split(":");- realm = realmAndUsername[0];- username = realmAndUsername[1];- }- return verifyRights(topic, clientId, realm, username, true);- }-- @Override- public boolean canRead(Topic topic, String username, String clientId) {- String realm = null;-- if (username != null) {- String[] realmAndUsername = username.split(":");- realm = realmAndUsername[0];- username = realmAndUsername[1];- }- return verifyRights(topic, clientId, realm, username, false);- }-- @SuppressWarnings({"rawtypes", "unchecked"})- protected boolean verifyRights(Topic topic, String clientId, String realm, String username, boolean isWrite) {- MqttConnection connection = brokerService.getConnection(clientId);- int i=0;-- try {- while (connection == null && i<10) {- // The connection handler gets called after this quite often so try and wait until the connection is registered- connection = brokerService.getConnection(clientId);- i++;- Thread.sleep(100);- }- } catch (InterruptedException e) {- LOG.finer("Interrupted whilst waiting for connection to be initialised: clientId=" + clientId);- }-- if (connection == null) {- LOG.warning("No connection found: clientId=" + clientId);- return false;- }-- // See if a custom handler wants to handle this topic pub/sub- for (MQTTHandler handler : brokerService.getCustomHandlers()) {- if (handler.handlesTopic(topic)) {- LOG.fine("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);- boolean result;-- if (isWrite) {- result = handler.checkCanPublish(connection, topic);- } else {- result = handler.checkCanSubscribe(connection, topic);- }- return result;- }- }-- LOG.fine("No handler has allowed " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", connection=" + 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.javadeleted file mode 100644index ea271ca30..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java+++ /dev/null@@ -1,219 +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 com.google.api.client.util.Charsets;-import io.moquette.broker.subscriptions.Topic;-import io.moquette.interception.AbstractInterceptHandler;-import io.moquette.interception.messages.*;-import org.openremote.agent.protocol.mqtt.MQTTLastWill;-import org.openremote.container.message.MessageBrokerService;-import org.openremote.container.timer.TimerService;-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;-import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.util.ValueUtil;--import java.util.Optional;-import java.util.WeakHashMap;-import java.util.logging.Logger;--import static org.openremote.model.syslog.SyslogCategory.API;--public class ORInterceptHandler extends AbstractInterceptHandler {-- private static final Logger LOG = SyslogCategory.getLogger(API, ORInterceptHandler.class);- protected final MqttBrokerService brokerService;- protected final MessageBrokerService messageBrokerService;- protected final ManagerKeycloakIdentityProvider identityProvider;- protected final TimerService timerService;- protected final WeakHashMap<String, MqttConnection> disconnectedConnections = new WeakHashMap<>();-- public ORInterceptHandler(MqttBrokerService brokerService,- ManagerKeycloakIdentityProvider identityProvider,- MessageBrokerService messageBrokerService,- TimerService timerService) {- this.brokerService = brokerService;- this.identityProvider = identityProvider;- this.messageBrokerService = messageBrokerService;- this.timerService = timerService;- }-- @Override- public String getID() {- return ORInterceptHandler.class.getName();- }-- @Override- public Class<?>[] getInterceptedMessageTypes() {- return new Class[]{- InterceptConnectMessage.class,- InterceptDisconnectMessage.class,- InterceptConnectionLostMessage.class,- InterceptSubscribeMessage.class,- InterceptUnsubscribeMessage.class,- InterceptPublishMessage.class- };- }-- @Override- public void onConnect(InterceptConnectMessage msg) {- String realm = null;- String username = null;- MQTTLastWill lastWill = null;- String password = msg.isPasswordFlag() ? new String(msg.getPassword(), Charsets.UTF_8) : null;-- if (msg.getUsername() != null) {- String[] realmAndUsername = msg.getUsername().split(":");- realm = realmAndUsername[0];- username = realmAndUsername[1];- }-- if (msg.isWillFlag()) {- Optional<String> payload = msg.getWillMessage() != null ? Optional.of(new String(msg.getWillMessage())) : Optional.empty();- lastWill = new MQTTLastWill(msg.getWillTopic(), payload.flatMap(ValueUtil::parse).orElse(null), msg.isWillRetain());- }-- MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password, msg.isCleanSession(), timerService.getCurrentTimeMillis(), lastWill);- LOG.fine("Client connect: " + connection);- brokerService.addConnection(connection.getClientId(), connection);-- // Notify all custom handlers- for (MQTTHandler handler : brokerService.getCustomHandlers()) {- if (!handler.onConnect(connection, msg)) {- LOG.info("Handler returned false from onConnect so not passing to other handlers: " + handler.getName());- break;- }- }- }-- // This is actively initiated by the client when it is gracefully disconnecting- @Override- public void onDisconnect(InterceptDisconnectMessage msg) {- MqttConnection connection;- connection = brokerService.clearConnectionSession(msg.getClientID());-- if (connection != null) {- LOG.fine("Client disconnect: " + connection);-- // No topic info here so just notify all custom handlers- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onDisconnect(connection, msg));-- // Store weak reference to connection for onConnectionLost- synchronized (disconnectedConnections) {- disconnectedConnections.put(msg.getClientID(), connection);- }- }- }-- // This is called on graceful and ungraceful disconnects (should really only be called on ungraceful so we need to- // mimic that here)- @Override- public void onConnectionLost(InterceptConnectionLostMessage msg) {-- // This can fire after onConnect which causes the connection to be removed; seems that onDisconnect also gets- // called when connection is lost and that is called first and before onConnect so we'll just use that for now- MqttConnection connection;- synchronized (disconnectedConnections) {- connection = disconnectedConnections.remove(msg.getClientID());- }-- if (connection == null) {- // Indicates that client didn't initiate graceful DISCONNECT (i.e. true connection lost)- // No topic info here so just notify all custom handlers- connection = brokerService.clearConnectionSession(msg.getClientID());- if (connection != null) {- LOG.fine("Client connection lost: " + connection);- MqttConnection finalConnection = connection;- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnectionLost(finalConnection, msg));- }- }- }-- @Override- public void onSubscribe(InterceptSubscribeMessage msg) {-- MqttConnection connection = brokerService.getConnection(msg.getClientID());-- if (connection == null) {- LOG.info("No connection found: clientID=" + msg.getClientID());- return;- }-- Topic topic = Topic.asTopic(msg.getTopicFilter());-- for (MQTTHandler handler : brokerService.getCustomHandlers()) {- if (handler.handlesTopic(topic)) {- LOG.fine("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);- handler.doSubscribe(connection, topic, msg);- break;- }- }- }-- @Override- public void onUnsubscribe(InterceptUnsubscribeMessage msg) {-- MqttConnection connection = brokerService.getConnection(msg.getClientID());-- if (connection == null) {- LOG.info("No connection found: clientID=" + msg.getClientID());- return;- }-- Topic topic = Topic.asTopic(msg.getTopicFilter());-- for (MQTTHandler handler : brokerService.getCustomHandlers()) {- if (handler.handlesTopic(topic)) {- LOG.fine("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);- handler.doUnsubscribe(connection, topic, msg);- break;- }- }- }-- @Override- public void onPublish(InterceptPublishMessage msg) {- String realm = null;- String username = null;-- if (msg.getUsername() != null) {- String[] realmAndUsername = msg.getUsername().split(":");- realm = realmAndUsername[0];- username = realmAndUsername[1];- }-- MqttConnection connection = brokerService.getConnection(msg.getClientID());-- if (connection == null) {- LOG.warning("No connection found: clientID=" + msg.getClientID() + ", realm=" + realm + ", username=" + username);- return;- }-- Topic topic = Topic.asTopic(msg.getTopicName());-- for (MQTTHandler handler : brokerService.getCustomHandlers()) {- if (handler.handlesTopic(topic)) {- LOG.fine("Handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);- handler.doPublish(connection, topic, msg);- break;- }- }- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/Topic.java b/manager/src/main/java/org/openremote/manager/mqtt/Topic.javanew file mode 100644index 000000000..17d152e44--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/mqtt/Topic.java@@ -0,0 +1,79 @@+/*+ * 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.manager.mqtt;++import org.apache.activemq.artemis.core.config.WildcardConfiguration;+import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;+import org.openremote.model.util.TextUtil;++import java.util.Arrays;+import java.util.Collections;+import java.util.List;++public class Topic {++ public static final String SEPARATOR = "/";+ public static final String SINGLE_LEVEL_TOKEN = "+";+ public static final String MULTI_LEVEL_TOKEN = "#";+ public static final Topic EMPTY_TOPIC = new Topic(SEPARATOR, Collections.emptyList());++ protected String topic;+ protected List<String> tokens;++ public static Topic fromAddress(String address, WildcardConfiguration wildcardConfiguration) throws IllegalArgumentException {+ return Topic.parse(MQTTUtil.convertCoreAddressToMqttTopicFilter(address, wildcardConfiguration));+ }++ public static Topic parse(String topic) throws IllegalArgumentException {+ if (TextUtil.isNullOrEmpty(topic) || SEPARATOR.equals(topic)) {+ return EMPTY_TOPIC;+ }++ int multiLevelPos = topic.indexOf(MULTI_LEVEL_TOKEN);+ if (multiLevelPos >= 0 && multiLevelPos < topic.length() - 1) {+ throw new IllegalArgumentException("Multilevel wildcard token must be at the end of the topic");+ }++ List<String> tokens = Arrays.asList(topic.split(SEPARATOR));+ return new Topic(topic, tokens);+ }++ protected Topic(String topic, List<String> tokens) {+ this.topic = topic;+ this.tokens = tokens;+ }++ public String getString() {+ return topic;+ }++ public boolean hasWildcard() {+ return topic.contains(SINGLE_LEVEL_TOKEN) || topic.contains(MULTI_LEVEL_TOKEN);+ }++ public List<String> getTokens() {+ return tokens;+ }++ @Override+ public String toString() {+ return topic;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.javaindex e9ef5f76d..1e936f817 100644--- a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java@@ -19,25 +19,23 @@*/package org.openremote.manager.provisioning;-import io.moquette.broker.subscriptions.Topic;-import io.moquette.interception.messages.InterceptConnectionLostMessage;-import io.moquette.interception.messages.InterceptPublishMessage;-import io.moquette.interception.messages.InterceptSubscribeMessage;-import io.moquette.interception.messages.InterceptUnsubscribeMessage;+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.model.Constants;-import org.openremote.model.PersistenceEvent;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.MqttBrokerService;-import org.openremote.manager.mqtt.MqttConnection;+import org.openremote.manager.mqtt.Topic;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;+import org.openremote.model.Constants;import org.openremote.model.Container;+import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.UserAssetLink;import org.openremote.model.provisioning.*;@@ -53,6 +51,8 @@ import java.security.cert.CertificateExpiredException;import java.security.cert.CertificateNotYetValidException;import java.security.cert.X509Certificate;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;import java.util.logging.Level;import java.util.logging.Logger;@@ -110,18 +110,16 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {public static final String PROVISIONING_USER_PREFIX = "ps-";protected ProvisioningService provisioningService;protected TimerService timerService;- protected MqttBrokerService brokerService;protected AssetStorageService assetStorageService;protected ManagerKeycloakIdentityProvider identityProvider;protected boolean isKeycloak;- protected final Map<Long, Set<MqttConnection>> provisioningConfigAuthenticatedConnectionMap = new HashMap<>();+ protected final ConcurrentMap<Long, Set<RemotingConnection>> provisioningConfigAuthenticatedConnectionMap = new ConcurrentHashMap<>();@Overridepublic void start(Container container) throws Exception {super.start(container);provisioningService = container.getService(ProvisioningService.class);timerService = container.getService(TimerService.class);- brokerService = container.getService(MqttBrokerService.class);assetStorageService = container.getService(AssetStorageService.class);ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);@@ -142,9 +140,9 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public boolean checkCanSubscribe(MqttConnection connection, Topic topic) {+ public boolean checkCanSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {// Skip standard checks- if (!canSubscribe(connection, topic)) {+ if (!canSubscribe(connection, securityContext, topic)) {getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);return false;}@@ -152,9 +150,9 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public boolean checkCanPublish(MqttConnection connection, Topic topic) {+ public boolean checkCanPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {// Skip standard checks- if (!canPublish(connection, topic)) {+ if (!canPublish(connection, securityContext, topic)) {getLogger().fine("Cannot publish to this topic, topic=" + topic + ", connection" + connection);return false;}@@ -174,7 +172,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public boolean canSubscribe(MqttConnection connection, Topic topic) {+ public boolean canSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (!isKeycloak) {LOG.fine("Identity provider is not keycloak");return false;@@ -186,17 +184,24 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {+ public void onSubscribe(RemotingConnection connection, Topic topic) {// Nothing to do here as we'll publish to this topic in response to client messages}@Override- public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {+ public void onUnsubscribe(RemotingConnection connection, Topic topic) {++ }+ @Override+ public Set<String> getPublishListenerTopics() {+ return Set.of(+ PROVISIONING_TOKEN + "/" + Topic.SINGLE_LEVEL_TOKEN + "/" + REQUEST_TOKEN+ );}@Override- public boolean canPublish(MqttConnection connection, Topic topic) {+ public boolean canPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (!isKeycloak) {LOG.fine("Identity provider is not keycloak");return false;@@ -208,13 +213,13 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {- String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);+ public void onPublish(RemotingConnection connection, Topic topic, ByteBuf body) {+ String payloadContent = body.toString(StandardCharsets.UTF_8);ProvisioningMessage provisioningMessage = ValueUtil.parse(payloadContent, ProvisioningMessage.class).orElseGet(() -> {LOG.fine("Failed to parse message from client: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.MESSAGE_INVALID), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.MESSAGE_INVALID), MqttQoS.AT_MOST_ONCE);return null;});@@ -228,10 +233,15 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}@Override- public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {- synchronized (provisioningConfigAuthenticatedConnectionMap) {- provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));- }+ public void onConnectionLost(RemotingConnection connection) {+ mqttBrokerService.removeTransientCredentials(connection);+ provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));+ }++ @Override+ public void onDisconnect(RemotingConnection connection) {+ mqttBrokerService.removeTransientCredentials(connection);+ provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));}protected static boolean isProvisioningTopic(Topic topic) {@@ -250,11 +260,11 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {return PROVISIONING_TOKEN + "/" + topicTokenIndexToString(topic, 1) + "/" + RESPONSE_TOKEN;}- protected void processX509ProvisioningMessage(MqttConnection connection, Topic topic, X509ProvisioningMessage provisioningMessage) {+ protected void processX509ProvisioningMessage(RemotingConnection connection, Topic topic, X509ProvisioningMessage provisioningMessage) {if (TextUtil.isNullOrEmpty(provisioningMessage.getCert())) {LOG.fine("Certificate is missing from X509 provisioning message: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -264,7 +274,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {clientCertificate = ProvisioningUtil.getX509Certificate(provisioningMessage.getCert());} catch (CertificateException e) {LOG.log(Level.INFO, "Failed to parse client X.509 certificate: topic=" + topic + ", connection=" + connection, e);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -272,14 +282,14 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (matchingConfig == null) {LOG.fine("No matching provisioning config found for client certificate: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNAUTHORIZED), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNAUTHORIZED), MqttQoS.AT_MOST_ONCE);return;}// Check if config is disabledif (matchingConfig.isDisabled()) {LOG.fine("Matching provisioning config is disabled for client certificate: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CONFIG_DISABLED), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CONFIG_DISABLED), MqttQoS.AT_MOST_ONCE);return;}@@ -289,13 +299,13 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (TextUtil.isNullOrEmpty(certUniqueId)) {LOG.info("Client X.509 certificate missing unique ID in subject CN: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);return;}if (TextUtil.isNullOrEmpty(uniqueId) || !certUniqueId.equals(uniqueId)) {LOG.info("Client X.509 certificate unique ID doesn't match topic unique ID: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);return;}@@ -315,7 +325,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (serviceUser != null) {if (!serviceUser.getEnabled()) {LOG.info("Client service user has been disabled: topic=" + topic + ", connection=" + connection);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.USER_DISABLED), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.USER_DISABLED), MqttQoS.AT_MOST_ONCE);return;}} else {@@ -323,7 +333,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}} catch (Exception e) {LOG.log(Level.WARNING, "Failed to retrieve/create service user: topic=" + topic + ", connection=" + connection, e);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);return;}@@ -342,7 +352,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (!matchingConfig.getRealm().equals(asset.getRealm())) {LOG.info("Client asset realm mismatch : topic=" + topic + ", connection=" + connection + ", assetId=" + assetId);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.ASSET_ERROR), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.ASSET_ERROR), MqttQoS.AT_MOST_ONCE);return;}} else {@@ -350,29 +360,26 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}} catch (Exception e) {LOG.log(Level.WARNING, "Failed to retrieve/create asset: topic=" + topic + ", connection=" + connection + ", config=" + matchingConfig, e);- brokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);+ mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);return;}LOG.fine("Client successfully initialised: topic=" + topic + ", connection=" + connection + ", config=" + matchingConfig);// Update connection with service user credentials- connection.setCredentials(realm, serviceUser.getUsername(), serviceUser.getSecret());+ mqttBrokerService.addTransientCredentials(connection, new ImmutableTriple<>(serviceUser.getId(), realm + ":" + serviceUser.getUsername(), serviceUser.getSecret()));+ provisioningConfigAuthenticatedConnectionMap.compute(matchingConfig.getId(), (id, connections) -> {+ if (connections == null) {+ connections = new HashSet<>();+ }+ connections.add(connection);+ return connections;+ });- synchronized (provisioningConfigAuthenticatedConnectionMap) {- provisioningConfigAuthenticatedConnectionMap.compute(matchingConfig.getId(), (id, connections) -> {- if (connections == null) {- connections = new HashSet<>();- }- connections.remove(connection);- connections.add(connection);- return connections;- });- brokerService.publishMessage(getResponseTopic(topic), new SuccessResponseMessage(realm, asset), MqttQoS.AT_MOST_ONCE);- }+ mqttBrokerService.publishMessage(getResponseTopic(topic), new SuccessResponseMessage(realm, asset), MqttQoS.AT_MOST_ONCE);}- protected X509ProvisioningConfig getMatchingX509ProvisioningConfig(MqttConnection connection, X509Certificate clientCertificate) {+ protected X509ProvisioningConfig getMatchingX509ProvisioningConfig(RemotingConnection connection, X509Certificate clientCertificate) {return provisioningService.getProvisioningConfigs().stream()@@ -475,12 +482,17 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {}protected void forceClientDisconnects(long provisioningConfigId) {- synchronized (provisioningConfigAuthenticatedConnectionMap) {- provisioningConfigAuthenticatedConnectionMap.computeIfPresent(provisioningConfigId, (id, connections) -> {- // Force disconnect of each connection and the disconnect handler will remove the connection from the map- connections.forEach(connection -> brokerService.forceDisconnect(connection.getClientId()));- return connections;+ provisioningConfigAuthenticatedConnectionMap.computeIfPresent(provisioningConfigId, (id, connections) -> {+ // Force disconnect of each connection and the disconnect handler will remove the connection from the map+ connections.forEach(connection -> {+ try {+ connection.disconnect(false);+ } catch (Exception e) {+ getLogger().log(Level.INFO, "Failed to disconnect client: connection=" + connection, e);+ }});- }+ connections.clear();+ return connections;+ });}}diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex f787044ab..c911ca657 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -163,16 +163,20 @@ public class JsonRulesBuilder extends RulesBuilder {timePredicate = (time) -> {long nextExecute = nextExecuteMillis.get();if (time >= nextExecute) {- // Advance the calculator to when the previous occurrence ends- sunTimes.set(sunCalculator.on(sunTimes.get().getSet()).execute());+ // Advance the calculator beyond now or next occurrence (whichever is later)+ long calcMillis = Math.max(sunTimes.get().getSet().toEpochSecond()*1000, time);+ sunTimes.set(sunCalculator.on(new Date(calcMillis)).execute());ZonedDateTime nextOccurrence = ruleCondition.sun.getPosition() == SunPositionTrigger.Position.SUNSET ? sunTimes.get().getSet() : sunTimes.get().getRise();+ log(Level.INFO, "Rule condition sun position has triggered at: " + timerService.getCurrentTimeMillis());+if (nextOccurrence == null) {log(Level.WARNING, "Rule condition requested sun position never occurs at the specified location: " + ruleCondition.sun);} else {- nextExecuteMillis.set(nextOccurrence.toInstant().toEpochMilli() + offsetMillis);+ long nextMillis = nextOccurrence.toInstant().toEpochMilli() + offsetMillis;+ log(Level.FINE, "Rule condition sun position next trigger time = " + nextMillis);+ nextExecuteMillis.set(nextMillis);}- log(Level.INFO, "Rule condition sun position has triggered");return true;}return false;@@ -607,7 +611,7 @@ public class JsonRulesBuilder extends RulesBuilder {// Insert a temporal fact if we have a time trigger (this will cause the engine to fire periodically)if (hasTimeTrigger.get()) {String tempFactName = TIMER_TEMPORAL_FACT_NAME_PREFIX + jsonRuleset.getId();- facts.putTemporary(tempFactName, Long.MAX_VALUE - timerService.getCurrentTimeMillis(), ""); // current time + expiration shouldn't overflow+ facts.putTemporary(tempFactName, Long.MAX_VALUE - timerService.getCurrentTimeMillis() - 5000, ""); // current time + expiration shouldn't overflow}}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.javaindex 5c5cc7adf..47933c3f5 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java@@ -32,6 +32,7 @@ import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.GeofencePredicate;import org.openremote.model.rules.AssetState;import org.openremote.model.rules.Assets;+import org.openremote.model.rules.RulesClock;import org.openremote.model.rules.TemporaryFact;import org.openremote.model.util.TimeUtil;@@ -555,7 +556,7 @@ public class RulesFacts extends Facts implements RuleListener {return haveLog;}- public TimerService getClock() {+ public RulesClock getClock() {return timerService;}diff --git a/manager/src/main/java/org/openremote/manager/security/AuthorisationService.java b/manager/src/main/java/org/openremote/manager/security/AuthorisationService.javanew file mode 100644index 000000000..69df64536--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/security/AuthorisationService.java@@ -0,0 +1,47 @@+/*+ * 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.manager.security;++import org.openremote.model.Container;+import org.openremote.model.ContainerService;++/**+ * This service is intended to form a centralised authorisation service for all resources and actions within the+ * system.+ */+public class AuthorisationService implements ContainerService {++ protected ManagerIdentityService managerIdentityService;++ @Override+ public void init(Container container) throws Exception {+ managerIdentityService = container.getService(ManagerIdentityService.class);+ }++ @Override+ public void start(Container container) throws Exception {++ }++ @Override+ public void stop(Container container) throws Exception {++ }+}diff --git a/manager/src/main/java/org/openremote/manager/security/KeycloakDeploymentCallback.java b/manager/src/main/java/org/openremote/manager/security/KeycloakDeploymentCallback.javanew file mode 100644index 000000000..3b222eed7--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/security/KeycloakDeploymentCallback.java@@ -0,0 +1,41 @@+/*+ * 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.manager.security;++import org.keycloak.adapters.KeycloakDeployment;++import javax.security.auth.callback.Callback;++/**+ * A {@link Callback} that should return the {@link org.keycloak.adapters.KeycloakDeployment} for the specified realm+ */+public class KeycloakDeploymentCallback implements Callback, java.io.Serializable {++ protected KeycloakDeployment deployment;++ public KeycloakDeploymentCallback setDeployment(KeycloakDeployment deployment) {+ this.deployment = deployment;+ return this;+ }++ public KeycloakDeployment getDeployment() {+ return deployment;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 9243c9b90..0bdd380a1 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -357,7 +357,10 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im(isUpdate ? PersistenceEvent.Cause.UPDATE : PersistenceEvent.Cause.CREATE),updatedUser,existingUser,- User.getPropertyFields());+ User.class,+ Collections.singletonList("attributes"),+ null);+return updatedUser;});}@@ -386,7 +389,12 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imreturn null;});- persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.DELETE, null, user, User.getPropertyFields());+ persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.DELETE,+ null,+ user,+ User.class,+ Collections.singletonList("attributes"),+ null);}@Override@@ -847,7 +855,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imRealm updatedRealm = convert(realmRepresentation, Realm.class);updatedRealm.setName(realmRepresentation.getRealm());updatedRealm.setRealmRoles((realm.getRealmRoles() == null) ? existingRealmRoles : realm.getNormalisedRealmRoles());- persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.UPDATE, updatedRealm, existingRealm, Realm.getPropertyFields());+ persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.UPDATE, updatedRealm, existingRealm, Realm.class, null, null);return null;});}@@ -889,7 +897,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imRealm createdRealm = convert(realmRepresentation, Realm.class);createdRealm.setName(realmRepresentation.getRealm());createdRealm.setRealmRoles(realm.getRealmRoles());- persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.CREATE, realm, null, Realm.getPropertyFields());+ persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.CREATE, realm, null, Realm.class, null, null);return createdRealm;} catch (Exception e) {LOG.log(Level.INFO, "Failed to create realm: " + realm, e);@@ -940,7 +948,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imrealmsResource.realm(realmName).remove();return null;});- persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.DELETE, null, realm, Realm.getPropertyFields());+ persistenceService.publishPersistenceEvent(PersistenceEvent.Cause.DELETE, null, realm, Realm.class, null, null);});}diff --git a/manager/src/main/java/org/openremote/manager/security/MultiTenantClientCredentialsGrantsLoginModule.java b/manager/src/main/java/org/openremote/manager/security/MultiTenantClientCredentialsGrantsLoginModule.javanew file mode 100644index 000000000..f24c2d2de--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/security/MultiTenantClientCredentialsGrantsLoginModule.java@@ -0,0 +1,275 @@+/*+ * 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.manager.security;++import com.google.common.collect.Sets;+import org.apache.http.HttpEntity;+import org.apache.http.HttpResponse;+import org.apache.http.NameValuePair;+import org.apache.http.client.HttpClient;+import org.apache.http.client.entity.UrlEncodedFormEntity;+import org.apache.http.client.methods.HttpPost;+import org.apache.http.message.BasicNameValuePair;+import org.jboss.logging.Logger;+import org.keycloak.KeycloakPrincipal;+import org.keycloak.OAuth2Constants;+import org.keycloak.adapters.AdapterUtils;+import org.keycloak.adapters.RefreshableKeycloakSecurityContext;+import org.keycloak.adapters.authentication.ClientCredentialsProviderUtils;+import org.keycloak.adapters.jaas.AbstractKeycloakLoginModule;+import org.keycloak.adapters.rotation.AdapterTokenVerifier;+import org.keycloak.common.VerificationException;+import org.keycloak.representations.AccessToken;+import org.keycloak.representations.AccessTokenResponse;+import org.keycloak.representations.idm.OAuth2ErrorRepresentation;+import org.keycloak.util.JsonSerialization;++import javax.security.auth.Subject;+import javax.security.auth.callback.*;+import javax.security.auth.login.LoginException;+import java.io.IOException;+import java.io.InputStream;+import java.io.Serializable;+import java.net.URI;+import java.util.*;++/**+ * A version of the Keycloak {@link org.keycloak.adapters.jaas.AbstractKeycloakLoginModule} that supports client+ * credentials grant and lookup of {@link org.keycloak.adapters.KeycloakDeployment} by using+ * the {@link KeycloakDeploymentCallback}. Copied from {@link org.keycloak.adapters.jaas.DirectAccessGrantsLoginModule}+ * which has private members un-fortunately.+ *+ * Also supports including the realm roles (as well as resource roles) by setting the+ * {@link #INCLUDE_REALM_ROLES_OPTION}.+ */+public class MultiTenantClientCredentialsGrantsLoginModule extends AbstractKeycloakLoginModule {++ public static final String INCLUDE_REALM_ROLES_OPTION = "includeRealmRoles";+ public static final String SCOPE_OPTION = "scope";+ private static final Logger log = Logger.getLogger(MultiTenantClientCredentialsGrantsLoginModule.class);+ protected boolean includeRealmRoles;+ protected String scope;+ protected String refreshToken;++ @Override+ public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {+ super.initialize(subject, callbackHandler, sharedState, options);+ this.scope = (String)options.get(SCOPE_OPTION);++ // This is used just for logout+ Iterator<MultiTenantClientCredentialsGrantsLoginModule.RefreshTokenHolder> iterator = subject.getPrivateCredentials(MultiTenantClientCredentialsGrantsLoginModule.RefreshTokenHolder.class).iterator();+ if (iterator.hasNext()) {+ refreshToken = iterator.next().refreshToken;+ }++ includeRealmRoles = Boolean.parseBoolean((String) options.get(INCLUDE_REALM_ROLES_OPTION));+ }++ @Override+ public boolean login() throws LoginException {+ // get username and password and deployment (if not set)++ boolean hasDeployment = deployment != null;+ Callback[] callbacks = new Callback[(hasDeployment ? 2 : 3)];+ callbacks[0] = new NameCallback("username");+ callbacks[1] = new PasswordCallback("password", false);+ if (!hasDeployment) {+ callbacks[2] = new KeycloakDeploymentCallback();+ }++ try {+ callbackHandler.handle(callbacks);+ String username = ((NameCallback) callbacks[0]).getName();+ char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();+ String password = new String(tmpPassword);+ ((PasswordCallback) callbacks[1]).clearPassword();++ if (!hasDeployment) {+ deployment = ((KeycloakDeploymentCallback) callbacks[2]).getDeployment();++ if (deployment == null) {+ getLogger().warn("Unable to resolve keycloak deployment");+ return false;+ }+ }++ Auth auth = doAuth(username, password);+ if (auth != null) {+ this.auth = auth;+ return true;+ } else {+ return false;+ }+ } catch (UnsupportedCallbackException uce) {+ getLogger().warn("Error: " + uce.getCallback().toString()+ + " not available to gather authentication information from the user");+ return false;+ } catch (Exception e) {+ LoginException le = new LoginException(e.toString());+ le.initCause(e);+ throw le;+ }+ }++ @Override+ protected Logger getLogger() {+ return log;+ }++ @Override+ protected Auth doAuth(String username, String password) throws IOException, VerificationException {+ return clientCredentialsAuth(username, password);+ }++ @Override+ public boolean commit() throws LoginException {+ boolean superCommit = super.commit();++ // refreshToken will be saved to privateCreds of Subject for now+ if (refreshToken != null) {+ MultiTenantClientCredentialsGrantsLoginModule.RefreshTokenHolder refreshTokenHolder = new MultiTenantClientCredentialsGrantsLoginModule.RefreshTokenHolder();+ refreshTokenHolder.refreshToken = refreshToken;+ subject.getPrivateCredentials().add(refreshTokenHolder);+ }++ return superCommit;+ }++ protected Auth clientCredentialsAuth(String username, String password) throws IOException, VerificationException {+ String authServerBaseUrl = deployment.getAuthServerBaseUrl();+ HttpPost post = new HttpPost(deployment.getTokenUrl());+ List<NameValuePair> formparams = new ArrayList<NameValuePair>();+ formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.CLIENT_CREDENTIALS));+ formparams.add(new BasicNameValuePair("client_id", username));+ formparams.add(new BasicNameValuePair("client_secret", password));++ if (scope != null) {+ formparams.add(new BasicNameValuePair(OAuth2Constants.SCOPE, scope));+ }++ //ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);++ UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");+ post.setEntity(form);++ HttpClient client = deployment.getClient();+ HttpResponse response = client.execute(post);+ int status = response.getStatusLine().getStatusCode();+ HttpEntity entity = response.getEntity();+ if (status != 200) {+ StringBuilder errorBuilder = new StringBuilder("Login failed. Invalid status: " + status);+ if (entity != null) {+ InputStream is = entity.getContent();+ OAuth2ErrorRepresentation errorRep = JsonSerialization.readValue(is, OAuth2ErrorRepresentation.class);+ errorBuilder.append(", OAuth2 error. Error: " + errorRep.getError())+ .append(", Error description: " + errorRep.getErrorDescription());+ }+ String error = errorBuilder.toString();+ log.warn(error);+ throw new IOException(error);+ }++ if (entity == null) {+ throw new IOException("No Entity");+ }++ InputStream is = entity.getContent();+ AccessTokenResponse tokenResponse = JsonSerialization.readValue(is, AccessTokenResponse.class);++ // refreshToken will be saved to privateCreds of Subject for now+ refreshToken = tokenResponse.getRefreshToken();++ AdapterTokenVerifier.VerifiedTokens tokens = AdapterTokenVerifier.verifyTokens(tokenResponse.getToken(), tokenResponse.getIdToken(), deployment);+ return postTokenVerification(tokenResponse.getToken(), tokens.getAccessToken());+ }++ @Override+ protected Auth postTokenVerification(String tokenString, AccessToken token) {+ boolean verifyCaller;+ if (deployment.isUseResourceRoleMappings()) {+ verifyCaller = token.isVerifyCaller(deployment.getResourceName());+ } else {+ verifyCaller = token.isVerifyCaller();+ }+ if (verifyCaller) {+ throw new IllegalStateException("VerifyCaller not supported yet in login module");+ }++ RefreshableKeycloakSecurityContext skSession = new RefreshableKeycloakSecurityContext(deployment, null, tokenString, token, null, null, null);+ String principalName = AdapterUtils.getPrincipalName(deployment, token);+ final KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = new KeycloakPrincipal<RefreshableKeycloakSecurityContext>(principalName, skSession);+ Set<String> roles;+ roles = AdapterUtils.getRolesFromSecurityContext(skSession);++ if (includeRealmRoles && !deployment.isUseResourceRoleMappings()) {+ AccessToken accessToken = skSession.getToken();+ roles = Sets.union(roles, accessToken.getRealmAccess().getRoles());+ }+ return new Auth(principal, roles, tokenString);+ }+++ @Override+ public boolean logout() throws LoginException {+ if (refreshToken != null) {+ try {+ URI logoutUri = deployment.getLogoutUrl().clone().build();+ HttpPost post = new HttpPost(logoutUri);++ List<NameValuePair> formparams = new ArrayList<>();+ ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);+ formparams.add(new BasicNameValuePair(OAuth2Constants.REFRESH_TOKEN, refreshToken));++ UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");+ post.setEntity(form);++ HttpClient client = deployment.getClient();+ HttpResponse response = client.execute(post);+ int status = response.getStatusLine().getStatusCode();+ HttpEntity entity = response.getEntity();+ if (status != 204) {+ StringBuilder errorBuilder = new StringBuilder("Logout of refreshToken failed. Invalid status: " + status);+ if (entity != null) {+ InputStream is = entity.getContent();+ if (status == 400) {+ OAuth2ErrorRepresentation errorRep = JsonSerialization.readValue(is, OAuth2ErrorRepresentation.class);+ errorBuilder.append(", OAuth2 error. Error: " + errorRep.getError())+ .append(", Error description: " + errorRep.getErrorDescription());++ } else {+ if (is != null) is.close();+ }+ }++ // Should do something better than warn if logout failed? Perhaps update of refresh tokens on existing subject might be supported too...+ log.warn(errorBuilder.toString());+ }+ } catch (IOException ioe) {+ log.warn(ioe);+ }+ }++ return super.logout();+ }++ private static class RefreshTokenHolder implements Serializable {+ private String refreshToken;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/security/MultiTenantJaasCallbackHandler.java b/manager/src/main/java/org/openremote/manager/security/MultiTenantJaasCallbackHandler.javanew file mode 100644index 000000000..79b65899d--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/security/MultiTenantJaasCallbackHandler.java@@ -0,0 +1,107 @@+/*+ * 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.manager.security;++import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;+import org.apache.activemq.artemis.spi.core.security.jaas.CertificateCallback;+import org.apache.activemq.artemis.spi.core.security.jaas.JaasCallbackHandler;+import org.apache.activemq.artemis.spi.core.security.jaas.PrincipalsCallback;+import org.keycloak.adapters.KeycloakDeployment;++import javax.security.auth.Subject;+import javax.security.auth.callback.*;+import javax.security.auth.kerberos.KerberosPrincipal;+import java.io.IOException;+import java.security.Principal;+import java.util.Set;+import java.util.function.Function;++import static org.apache.activemq.artemis.core.remoting.CertificateUtil.getCertsFromConnection;+import static org.apache.activemq.artemis.core.remoting.CertificateUtil.getPeerPrincipalFromConnection;++/**+ * A version of {@link JaasCallbackHandler} that supports multi-tenancy by introducing support for+ * {@link KeycloakDeploymentCallback}. Unfortunately the fields are private so had to copy most of the code below.+ */+public class MultiTenantJaasCallbackHandler implements CallbackHandler {++ protected final String username;+ protected final String password;+ final RemotingConnection remotingConnection;+ protected final String realm;+ protected final Function<String, KeycloakDeployment> deploymentResolver;++ public MultiTenantJaasCallbackHandler(Function<String, KeycloakDeployment> deploymentResolver, String realm, String username, String password, RemotingConnection remotingConnection) {+ this.username = username;+ this.password = password;+ this.remotingConnection = remotingConnection;+ this.realm = realm;+ this.deploymentResolver = deploymentResolver;+ }++ @Override+ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {+ for (Callback callback : callbacks) {+ if (callback instanceof PasswordCallback) {+ PasswordCallback passwordCallback = (PasswordCallback) callback;+ if (password == null) {+ passwordCallback.setPassword(null);+ } else {+ passwordCallback.setPassword(password.toCharArray());+ }+ } else if (callback instanceof NameCallback) {+ NameCallback nameCallback = (NameCallback) callback;+ if (username == null) {+ nameCallback.setName(null);+ } else {+ nameCallback.setName(username);+ }+ } else if (callback instanceof CertificateCallback) {+ CertificateCallback certCallback = (CertificateCallback) callback;++ certCallback.setCertificates(getCertsFromConnection(remotingConnection));+ } else if (callback instanceof PrincipalsCallback) {+ PrincipalsCallback principalsCallback = (PrincipalsCallback) callback;++ Subject peerSubject = remotingConnection.getSubject();+ if (peerSubject != null) {+ for (KerberosPrincipal principal : peerSubject.getPrivateCredentials(KerberosPrincipal.class)) {+ principalsCallback.setPeerPrincipals(new Principal[]{principal});+ return;+ }+ Set<Principal> principals = peerSubject.getPrincipals();+ if (principals.size() > 0) {+ principalsCallback.setPeerPrincipals(principals.toArray(new Principal[0]));+ return;+ }+ }++ Principal peerPrincipalFromConnection = getPeerPrincipalFromConnection(remotingConnection);+ if (peerPrincipalFromConnection != null) {+ principalsCallback.setPeerPrincipals(new Principal[]{peerPrincipalFromConnection});+ }+ } else if (callback instanceof KeycloakDeploymentCallback keycloakDeploymentCallback) {+ keycloakDeploymentCallback.setDeployment(deploymentResolver.apply(realm));+ } else {+ throw new UnsupportedCallbackException(callback);+ }+ }+ }+}diff --git a/manager/src/main/java/org/openremote/manager/security/ORLoginModule.java b/manager/src/main/java/org/openremote/manager/security/ORLoginModule.javanew file mode 100644index 000000000..a942acbe9--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/security/ORLoginModule.java@@ -0,0 +1,59 @@+/*+ * 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.manager.security;++import javax.security.auth.Subject;+import javax.security.auth.callback.CallbackHandler;+import javax.security.auth.login.LoginException;+import javax.security.auth.spi.LoginModule;+import java.util.Map;++/**+ * A {@link LoginModule} specific to OpenRemote+ */+public class ORLoginModule implements LoginModule {++++ @Override+ public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {++ }++ @Override+ public boolean login() throws LoginException {+ return false;+ }++ @Override+ public boolean commit() throws LoginException {+ return false;+ }++ @Override+ public boolean abort() throws LoginException {+ return false;+ }++ @Override+ public boolean logout() throws LoginException {+ return false;+ }+}diff --git a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerServiceindex 54c240e34..e6c4d6af8 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService+++ b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService@@ -23,9 +23,10 @@ org.openremote.manager.notification.EmailNotificationHandlerorg.openremote.manager.datapoint.AssetPredictedDatapointServiceorg.openremote.manager.gateway.GatewayServiceorg.openremote.manager.gateway.GatewayClientService-org.openremote.manager.mqtt.MqttBrokerService+org.openremote.manager.mqtt.MQTTBrokerServiceorg.openremote.manager.asset.AssetModelServiceorg.openremote.manager.energy.EnergyOptimisationServiceorg.openremote.manager.energy.ForecastWindServiceorg.openremote.manager.energy.ForecastSolarServiceorg.openremote.manager.provisioning.ProvisioningService+org.openremote.manager.security.AuthorisationServicediff --git a/model/src/main/java/org/openremote/model/asset/UserAssetLink.java b/model/src/main/java/org/openremote/model/asset/UserAssetLink.javaindex 32b5aed8a..f1af3676b 100644--- a/model/src/main/java/org/openremote/model/asset/UserAssetLink.java+++ b/model/src/main/java/org/openremote/model/asset/UserAssetLink.java@@ -25,6 +25,8 @@ import org.openremote.model.value.MetaItemType;import javax.persistence.*;import java.io.Serializable;import java.util.Date;+import java.util.List;+import java.util.Objects;/*** An asset can be linked to many users, and a user can have links to many assets.@@ -167,6 +169,19 @@ public class UserAssetLink {this.createdOn = createdOn;}+ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (o == null || getClass() != o.getClass()) return false;+ UserAssetLink that = (UserAssetLink) o;+ return id.equals(that.id);+ }++ @Override+ public int hashCode() {+ return Objects.hash(id);+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/attribute/MetaItem.java b/model/src/main/java/org/openremote/model/attribute/MetaItem.javaindex 40db023f0..3184cc43d 100644--- a/model/src/main/java/org/openremote/model/attribute/MetaItem.java+++ b/model/src/main/java/org/openremote/model/attribute/MetaItem.java@@ -38,7 +38,7 @@ import java.util.Objects;public class MetaItem<T> extends AbstractNameValueHolder<T> {/**- * Serialise the meta item as just the value+ * Serialise the meta item as just the value to make it less verbose*/@SuppressWarnings("rawtypes")public static class MetaItemSerializer extends StdSerializer<MetaItem> {diff --git a/model/src/main/java/org/openremote/model/provisioning/ProvisioningConfig.java b/model/src/main/java/org/openremote/model/provisioning/ProvisioningConfig.javaindex e3248aa0f..d6ee40e23 100644--- a/model/src/main/java/org/openremote/model/provisioning/ProvisioningConfig.java+++ b/model/src/main/java/org/openremote/model/provisioning/ProvisioningConfig.java@@ -185,16 +185,9 @@ public abstract class ProvisioningConfig<T, U extends ProvisioningConfig<T, U>>public String toString() {return getClass().getSimpleName() + "{" +"id=" + id +- ", createdOn=" + createdOn +- ", lastModified=" + lastModified +", name='" + name + '\'' +", type='" + type + '\'' +", realm='" + realm + '\'' +- ", assetTemplateSet='" + (assetTemplate != null) + '\'' +- ", restrictedUser=" + restrictedUser +- ", userRoles=" + Arrays.toString(userRoles) +- ", disabled=" + disabled +- ", data=" + getData() +'}';}}diff --git a/model/src/main/java/org/openremote/model/rules/RulesClock.java b/model/src/main/java/org/openremote/model/rules/RulesClock.javanew file mode 100644index 000000000..279318ad2--- /dev/null+++ b/model/src/main/java/org/openremote/model/rules/RulesClock.java@@ -0,0 +1,29 @@+/*+ * 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.rules;++import java.time.Instant;++public interface RulesClock {++ long getCurrentTimeMillis();++ Instant getNow();+}diff --git a/model/src/main/java/org/openremote/model/security/Realm.java b/model/src/main/java/org/openremote/model/security/Realm.javaindex 990c050b7..c79ec5150 100644--- a/model/src/main/java/org/openremote/model/security/Realm.java+++ b/model/src/main/java/org/openremote/model/security/Realm.java@@ -288,15 +288,6 @@ public class Realm {return this;}- public static Field[] getPropertyFields() {- if (propertyFields == null) {- propertyFields = Arrays.stream(Realm.class.getDeclaredFields())- .filter(field -> field.isAnnotationPresent(Column.class) || field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(Formula.class))- .toArray(Field[]::new);- }- return propertyFields;- }-protected static List<RealmRole> getDefaultRealmRoles(String realm) {if (MASTER_REALM.equals(realm)) {return Arrays.asList(diff --git a/model/src/main/java/org/openremote/model/security/User.java b/model/src/main/java/org/openremote/model/security/User.javaindex 204be06d0..30563125b 100644--- a/model/src/main/java/org/openremote/model/security/User.java+++ b/model/src/main/java/org/openremote/model/security/User.java@@ -266,16 +266,6 @@ public class User {return secret;}- public static Field[] getPropertyFields() {- if (propertyFields == null) {- propertyFields = Arrays.stream(User.class.getDeclaredFields())- .filter(field -> field.isAnnotationPresent(Column.class)- || field.getName().equals("attributes"))- .toArray(Field[]::new);- }- return propertyFields;- }-@Overridepublic String toString() {return getClass().getName() + "{" +@@ -285,7 +275,6 @@ public class User {", firstName='" + firstName + '\'' +", lastName='" + lastName + '\'' +", email='" + email + '\'' +- ", secret='" + secret + '\'' +", enabled=" + enabled +'}';}diff --git a/model/src/main/java/org/openremote/model/util/Debouncer.java b/model/src/main/java/org/openremote/model/util/Debouncer.javanew file mode 100644index 000000000..e9238d363--- /dev/null+++ b/model/src/main/java/org/openremote/model/util/Debouncer.java@@ -0,0 +1,89 @@+/*+ * 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.util;++import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ScheduledExecutorService;+import java.util.concurrent.TimeUnit;+import java.util.function.Consumer;++/**+ * A simple key based debounce utility+ */+public class Debouncer<T> {+ protected final ScheduledExecutorService scheduledExecutorService;+ protected final ConcurrentHashMap<T, TimerTask> delayedMap = new ConcurrentHashMap<>();+ protected final Consumer<T> callback;+ protected final int intervalMillis;++ public Debouncer(ScheduledExecutorService scheduledExecutorService, Consumer<T> c, int intervalMillis) {+ this.scheduledExecutorService = scheduledExecutorService;+ this.callback = c;+ this.intervalMillis = intervalMillis;+ }++ public void call(T key) {+ TimerTask task = new TimerTask(key);++ TimerTask prev;+ do {+ prev = delayedMap.putIfAbsent(key, task);+ if (prev == null)+ scheduledExecutorService.schedule(task, intervalMillis, TimeUnit.MILLISECONDS);+ } while (prev != null && !prev.extend()); // Exit only if new task was added to map, or existing task was extended successfully+ }++ // The task that wakes up when the wait time elapses+ protected class TimerTask implements Runnable {+ private final T key;+ private long dueTime;+ private final Object lock = new Object();++ public TimerTask(T key) {+ this.key = key;+ extend();+ }++ public boolean extend() {+ synchronized (lock) {+ if (dueTime < 0) // Task has been shutdown+ return false;+ dueTime = System.currentTimeMillis() + intervalMillis;+ return true;+ }+ }++ public void run() {+ synchronized (lock) {+ long remaining = dueTime - System.currentTimeMillis();+ if (remaining > 0) { // Re-schedule task+ scheduledExecutorService.schedule(this, remaining, TimeUnit.MILLISECONDS);+ } else { // Mark as terminated and invoke callback+ dueTime = -1;+ try {+ callback.accept(key);+ } finally {+ delayedMap.remove(key);+ }+ }+ }+ }+ }+}diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 60107b805..83ed7efcf 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -4,7 +4,6 @@ import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientViewimport com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigViewimport com.hivemq.client.mqtt.MqttClientConfigimport com.hivemq.client.mqtt.MqttClientConnectionConfig-import io.moquette.BrokerConstantsimport io.netty.channel.socket.SocketChannelimport org.openremote.agent.protocol.mqtt.MQTTLastWillimport org.openremote.agent.protocol.mqtt.MQTTMessage@@ -16,7 +15,7 @@ import org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.mqtt.DefaultMQTTHandlerimport org.openremote.manager.mqtt.MQTTHandler-import org.openremote.manager.mqtt.MqttBrokerService+import org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.asset.AssetEventimport org.openremote.model.asset.agent.ConnectionStatus@@ -36,8 +35,9 @@ import java.util.function.Consumerimport static org.openremote.container.util.MapAccess.getIntegerimport static org.openremote.container.util.MapAccess.getString-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORT+import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_HOST+import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_PORT+import static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDStringimport static org.openremote.model.value.ValueType.TEXTclass MqttBrokerTest extends Specification implements ManagerContainerTrait {@@ -51,7 +51,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {def container = startContainer(defaultConfig(), defaultServices())def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)- def mqttBrokerService = container.getService(MqttBrokerService.class)+ def mqttBrokerService = container.getService(MQTTBrokerService.class)def assetStorageService = container.getService(AssetStorageService.class)def clientEventService = container.getService(ClientEventService.class)def agentService = container.getService(AgentService.class)@@ -59,8 +59,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {def username = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser.username // realm and OAuth client iddef password = keycloakTestSetup.serviceUser.secret- def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)- def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)+ def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, "0.0.0.0")+ def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, 1883)when: "a mqtt client connects with invalid credentials"def wrongUsername = "master:" + keycloakTestSetup.serviceUser.username@@ -81,7 +81,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "mqtt connection should exist"conditions.eventually {assert client.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1}when: "a mqtt client subscribes to an asset in another realm"@@ -91,7 +91,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))}when: "a mqtt client subscribes with clientId missing"@@ -101,7 +102,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))}when: "a mqtt client subscribes with different clientId"@@ -112,8 +114,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))}when: "a mqtt client subscribes to all attributes of an asset"@@ -128,9 +131,10 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.topicConsumerMap.get(topic) != nullassert client.topicConsumerMap.get(topic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "An attribute event occurs for a subscribed attribute"@@ -218,7 +222,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) == null- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))}when: "another asset attribute changed without any subscriptions"@@ -238,8 +243,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.topicConsumerMap.get(topic) != nullassert client.topicConsumerMap.get(topic).size() == 1- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "that attribute changes"@@ -271,7 +277,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) == null- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))}when: "a mqtt client subscribes to attributes for descendants of an asset"@@ -282,8 +289,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.topicConsumerMap.get(topic) != nullassert client.topicConsumerMap.get(topic).size() == 1- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "a child asset of the subscription attribute event occurs"@@ -311,8 +319,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.topicConsumerMap.get(topic) != nullassert client.topicConsumerMap.get(topic).size() == 1- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 2+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 2}when: "a subscribed attribute changes"@@ -353,8 +362,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.topicConsumerMap.get(topic) == nullassert client.topicConsumerMap.size() == 1- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "a client disconnects"@@ -363,7 +373,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the client status should be disconnected"conditions.eventually {assert client.getConnectionStatus() == ConnectionStatus.DISCONNECTED- assert mqttBrokerService.clientIdConnectionMap.size() == 0+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 0}when: "the client reconnects"@@ -373,7 +383,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the connection should exist"conditions.eventually {assert client.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1}when: "a mqtt client subscribes to assets that are direct children of the realm"@@ -382,8 +392,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "A subscription should exist"conditions.eventually {- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "an asset is updated with a new attribute"@@ -408,15 +419,16 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the subscription should exist"conditions.eventually {assert client.topicConsumerMap.get(topic) != null- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 2+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 2}when: "an asset is added as a descendant to the subscribed asset"def childAsset = new ThingAsset("child").setParentId(managerTestSetup.apartment1LivingroomId)- .setRealm(managerTestSetup.realmBuildingName);- childAsset = assetStorageService.merge(childAsset);+ .setRealm(managerTestSetup.realmBuildingName)+ childAsset = assetStorageService.merge(childAsset)then: "another event should be sent"conditions.eventually {@@ -433,8 +445,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "only one subscription should be left"conditions.eventually {assert client.topicConsumerMap.get(topic) == null- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1}when: "the descendant asset is modified"@@ -448,9 +461,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "another client connects with a last will configured to write to an attribute"newClientId = "newClient"- username = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser2.username // realm and OAuth client id- password = keycloakTestSetup.serviceUser2.secret- MQTT_IOClient newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/$managerTestSetup.apartment1HallwayId".toString(), ValueUtil.parse("1000").orElse(null), false))+ def username2 = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser2.username // realm and OAuth client id+ def password2 = keycloakTestSetup.serviceUser2.secret+ MQTT_IOClient newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username2, password2), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/$managerTestSetup.apartment1HallwayId".toString(), ValueUtil.parse("1000").orElse(null), false))newClient.connect()then: "the client should be connected"@@ -463,9 +476,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {client.addMessageConsumer(topic, eventConsumer)and: "the new client publishes to a an attribute topic"- topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 170)).get()- client.sendMessage(new MQTTMessage<String>(topic, payload))+ newClient.sendMessage(new MQTTMessage<String>(topic, payload))then: "The value of the attribute should be updated and the first client should have received the event"new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {@@ -480,14 +493,14 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {receivedEvents.clear()and: "the new client gets abruptly disconnected"- def existingConnection = mqttBrokerService.clientIdConnectionMap.get(newClientId)+ def existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)newClient.client).clientConfig).delegate).connectionConfig.get()).channel).close()then: "the client should reconnect"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null- assert mqttBrokerService.clientIdConnectionMap.get(newClientId) !== existingConnection+ assert !mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection}and: "The last will message should have updated the value of the attribute and the first client should have received the event"@@ -525,30 +538,30 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the status should show as disconnected"conditions.eventually {assert newClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED- assert mqttBrokerService.clientIdConnectionMap.size() == 1+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()}when: "the new client reconnects using a last will topic for an unauthorised asset/attribute"def asset = assetStorageService.find(managerTestSetup.apartment2LivingroomId)def currentCO2Level = asset.getAttribute("co2Level").flatMap{it.getValue()}.orElse(0d)- newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/co2Level/$managerTestSetup.apartment2LivingroomId".toString(), ValueUtil.parse("1000").orElse(null), false))+ newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username2, password2), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/co2Level/$managerTestSetup.apartment2LivingroomId".toString(), ValueUtil.parse("1000").orElse(null), false))newClient.connect()then: "the client should be connected"conditions.eventually {assert newClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1}when: "the new client gets abruptly disconnected"- existingConnection = mqttBrokerService.clientIdConnectionMap.get(newClientId)+ existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)newClient.client).clientConfig).delegate).connectionConfig.get()).channel).close()then: "the client should reconnect"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null- assert mqttBrokerService.clientIdConnectionMap.get(newClientId) !== existingConnection+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection}then: "The last will message should not have updated the value of the attribute"@@ -565,7 +578,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.getConnectionStatus() == ConnectionStatus.DISCONNECTEDassert newClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED- assert mqttBrokerService.clientIdConnectionMap.size() == 0+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).isEmpty()}}}diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovyindex e16784708..c19571153 100644--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy@@ -19,7 +19,6 @@*/package org.openremote.test.protocol.mqtt-import io.moquette.BrokerConstantsimport org.openremote.agent.protocol.mqtt.MQTTAgentimport org.openremote.agent.protocol.mqtt.MQTTAgentLinkimport org.openremote.agent.protocol.mqtt.MQTTProtocol@@ -30,6 +29,7 @@ import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.mqtt.DefaultMQTTHandler+import org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.Constantsimport org.openremote.model.asset.agent.Agent@@ -46,10 +46,6 @@ import org.openremote.test.setup.ManagerTestSetupimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions-import static org.openremote.container.util.MapAccess.getInteger-import static org.openremote.container.util.MapAccess.getString-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORTimport static org.openremote.model.value.MetaItemType.AGENT_LINKimport static org.openremote.model.value.ValueType.NUMBER@@ -66,11 +62,12 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTrdef assetStorageService = container.getService(AssetStorageService.class)def assetProcessingService = container.getService(AssetProcessingService.class)def agentService = container.getService(AgentService.class)+ def brokerService = container.getService(MQTTBrokerService.class)def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)def clientEventService = container.getService(ClientEventService.class)- def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)- def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)+ def mqttHost = brokerService.host+ def mqttPort = brokerService.portwhen: "an MQTT client agent is created to connect to this tests manager"def clientId = UniqueIdentifierGenerator.generateId()diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovyindex db7fb6e59..2c1c8cde3 100644--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy@@ -23,8 +23,8 @@ import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientViewimport com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigViewimport com.hivemq.client.mqtt.MqttClientConfigimport com.hivemq.client.mqtt.MqttClientConnectionConfig-import io.moquette.BrokerConstantsimport io.netty.channel.socket.SocketChannel+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnectionimport org.keycloak.admin.client.resource.RealmResourceimport org.keycloak.representations.idm.RoleRepresentationimport org.openremote.agent.protocol.mqtt.MQTTMessage@@ -35,7 +35,7 @@ import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.mqtt.DefaultMQTTHandler-import org.openremote.manager.mqtt.MqttBrokerService+import org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.provisioning.ProvisioningServiceimport org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandlerimport org.openremote.manager.security.ManagerIdentityService@@ -61,10 +61,7 @@ import spock.util.concurrent.PollingConditionsimport java.util.function.Consumer-import static org.openremote.container.util.MapAccess.getInteger-import static org.openremote.container.util.MapAccess.getString-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORT+import static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDStringimport static org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler.*import static org.openremote.model.value.ValueType.NUMBER@@ -80,14 +77,14 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContadef container = startContainer(defaultConfig(), defaultServices())def assetStorageService = container.getService(AssetStorageService.class)def provisioningService = container.getService(ProvisioningService.class)- def mqttBrokerService = container.getService(MqttBrokerService.class)+ def mqttBrokerService = container.getService(MQTTBrokerService.class)def clientEventService = container.getService(ClientEventService.class)def assetProcessingService = container.getService(AssetProcessingService.class)def identityService = container.getService(ManagerIdentityService.class)def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)def userAssetProvisioningMQTTHandler = mqttBrokerService.customHandlers.find {it instanceof UserAssetProvisioningMQTTHandler} as UserAssetProvisioningMQTTHandler- def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)- def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)+ def mqttHost = mqttBrokerService.host+ def mqttPort = mqttBrokerService.portand: "a realm is created with some custom realm roles"def realm = new Realm()@@ -145,6 +142,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta}when: "a mqtt client connects"+ RemotingConnection connection = nulldef device1UniqueId = "device1"def mqttDevice1ClientId = UniqueIdentifierGenerator.generateId("device1")List<String> subscribeFailures = []@@ -156,10 +154,9 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconnectionStatuses.add(connectionStatus)})device1Client.connect()- then: "mqtt connection should exist"+ then: "mqtt client should be connected"conditions.eventually {assert device1Client.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -175,7 +172,6 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert device1Client.topicConsumerMap.get(device1ResponseTopic) != nullassert device1Client.topicConsumerMap.get(device1ResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"@@ -198,21 +194,21 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContawhen: "the client gets abruptly disconnected"device1Responses.clear()- def existingConnection = mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId)+ def existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).close()then: "the client should reconnect"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) !== existingConnection+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) !== existingConnection}then: "the subscriptions should succeed"conditions.eventually {assert device1Client.topicConsumerMap.get(device1ResponseTopic) != nullassert device1Client.topicConsumerMap.get(device1ResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"@@ -256,9 +252,10 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert device1Client.topicConsumerMap.get(attributeSubscriptionTopic) != nullassert device1Client.topicConsumerMap.get(assetSubscriptionTopic).size() == 1assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttDevice1ClientId).size() == 2+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ assert connection != null+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 2}when: "the client updates one of the provisioned asset's attributes"@@ -310,12 +307,12 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta}when: "the client disconnects"+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)device1Client.disconnect()then: "all subscriptions should be removed and the client should be disconnected"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) == null- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))assert device1Client.getConnectionStatus() == ConnectionStatus.DISCONNECTED}@@ -325,15 +322,17 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the client should be connected and subscriptions reset"conditions.eventually {assert device1Client.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ assert connection != null}and: "the provisioning subscription should be re-instated but the attribute and asset subscriptions should fail"conditions.eventually {assert device1Client.topicConsumerMap.get(assetSubscriptionTopic) == nullassert device1Client.topicConsumerMap.get(attributeSubscriptionTopic) == null- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ assert connection != nullassert subscribeFailures.size() == 2assert subscribeFailures.contains(assetSubscriptionTopic)assert subscribeFailures.contains(attributeSubscriptionTopic)@@ -374,9 +373,10 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert device1Client.topicConsumerMap.get(attributeSubscriptionTopic) != nullassert device1Client.topicConsumerMap.get(assetSubscriptionTopic).size() == 1assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)- assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttDevice1ClientId).size() == 2+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ assert connection != null+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 2}when: "a second device connects"@@ -391,7 +391,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "mqtt connection should exist"conditions.eventually {assert deviceNClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -405,7 +405,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != nullassert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client publishes an expired but valid x509 certificate that has been signed by the CA stored in the provisioning config"@@ -428,7 +428,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the connection should be removed from the broker"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) == null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) == nullassert deviceNClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED}@@ -442,7 +442,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "mqtt connection should exist"conditions.eventually {assert deviceNClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -452,7 +452,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != nullassert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client publishes a forged x509 certificate that has been signed by a forged CA"@@ -475,7 +475,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the connection should be removed from the broker"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) == null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) == nullassert deviceNClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED}@@ -489,7 +489,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "mqtt connection should exist"conditions.eventually {assert deviceNClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -499,7 +499,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != nullassert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config but against an expired certificate"@@ -526,12 +526,12 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the connection should be removed from the broker"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) == null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) == nullassert deviceNClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED}when: "the provisioning config is updated to disabled"- existingConnection = mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId)+ existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)device1Responses.clear()connectionStatuses.clear()provisioningConfig.setDisabled(true)@@ -542,9 +542,10 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert connectionStatuses.size() == 2assert connectionStatuses.get(0) == ConnectionStatus.WAITINGassert connectionStatuses.get(1) == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != existingConnection- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != existingConnection+ connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ assert connection != null}when: "the re-connected client re-authenticates"@@ -569,7 +570,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "mqtt connection should exist"conditions.eventually {assert deviceNClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -579,7 +580,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != nullassert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config but against an expired certificate"@@ -602,7 +603,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the connection should be removed from the broker"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) == null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) == nullassert deviceNClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED}@@ -630,7 +631,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta}when: "a connected device user is disabled and an un-connected device user is disabled"- existingConnection = mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId)+ existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)def device1User = identityService.getIdentityProvider().getUserByUsername(managerTestSetup.realmBuildingName, User.SERVICE_ACCOUNT_PREFIX + PROVISIONING_USER_PREFIX + device1UniqueId)def deviceNUser = identityService.getIdentityProvider().getUserByUsername(managerTestSetup.realmBuildingName, User.SERVICE_ACCOUNT_PREFIX + PROVISIONING_USER_PREFIX + deviceNUniqueId)device1User.setEnabled(false)@@ -640,9 +641,9 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "already connected client that was authenticated should be disconnected, then reconnect"conditions.eventually {- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != existingConnection- assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttDevice1ClientId)+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != existingConnection+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(existingConnection))}when: "the re-connected client re-authenticates"@@ -667,7 +668,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "mqtt connection should exist"conditions.eventually {assert deviceNClient.getConnectionStatus() == ConnectionStatus.CONNECTED- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client subscribes to the provisioning endpoints"@@ -677,7 +678,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaconditions.eventually {assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != nullassert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).size() == 1- assert mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId) != null+ assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config but against an expired certificate"@@ -695,8 +696,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta}when: "a client user is re-enabled"- existingConnection = mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId)- def existingNConnection = mqttBrokerService.clientIdConnectionMap.get(mqttDeviceNClientId)+ existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ def existingNConnection = mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId)device1User.setEnabled(true)device1User = identityService.getIdentityProvider().createUpdateUser(managerTestSetup.realmBuildingName, device1User, null)diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovyindex 2f6bc92a8..dff8663b3 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy@@ -591,8 +591,8 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert thingAsset.getAttribute("sunset").get().getValue().orElse(0) == 100}- when: "time advances past the next sunrise"- sunTimes = sunsetCalculator.on(sunTimes.getSet()).execute()+ when: "time advances past the sunset"+ sunTimes = sunsetCalculator.on(sunTimes.getSet().plusHours(1)).execute()advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getRise()).getSeconds(), TimeUnit.SECONDS, container)and: "the updated attribute is cleared"@@ -609,7 +609,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {}when: "time advances slightly past the next sunset time and the rule engine fires"- advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getSet()).getSeconds()+1, TimeUnit.SECONDS, container)+ advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getSet().plusSeconds(10)).getSeconds()+1, TimeUnit.SECONDS, container)then: "the rule engine should have fired again and the rule should have triggered"conditions.eventually {diff --git a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovyindex 9320ab69e..31a8c4f36 100644--- a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy+++ b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy@@ -16,7 +16,7 @@ import static org.openremote.container.timer.TimerService.Clock.PSEUDOimport static org.openremote.container.timer.TimerService.Clock.REALimport static org.openremote.container.timer.TimerService.TIMER_CLOCK_TYPEimport static org.openremote.container.web.WebService.OR_WEBSERVER_LISTEN_PORT-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST+import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_HOSTtrait ManagerContainerTrait extends ContainerTrait {diff --git a/ui/app/manager/src/pages/page-provisioning.ts b/ui/app/manager/src/pages/page-provisioning.tsindex 0ce936944..fb714772e 100644--- a/ui/app/manager/src/pages/page-provisioning.ts+++ b/ui/app/manager/src/pages/page-provisioning.ts@@ -373,7 +373,7 @@ export class PageProvisioning extends Page<AppStateKeyed> {<or-mwc-input .label="${i18next.t("ignoreExpiryDate")}".type="${InputType.CHECKBOX}".value="${data.ignoreExpiryDate}"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.disabled = !e.detail.value}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => data.ignoreExpiryDate = !e.detail.value}"style="height: 56px;"></or-mwc-input>`;} else {@@ -432,7 +432,7 @@ export class PageProvisioning extends Page<AppStateKeyed> {<or-mwc-input .label="${i18next.t("createAsRestrictedUser")}".type="${InputType.CHECKBOX}".value="${config.restrictedUser}"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.disabled = e.detail.value}"></or-mwc-input>+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.restrictedUser = e.detail.value}"></or-mwc-input><or-mwc-input .label="${i18next.t("disabled")}".type="${InputType.CHECKBOX}".value="${config.disabled}"diff --git a/ui/component/or-mwc-components/src/or-mwc-input.ts b/ui/component/or-mwc-components/src/or-mwc-input.tsindex 6b8841e99..c8fd3d69f 100644--- a/ui/component/or-mwc-components/src/or-mwc-input.ts+++ b/ui/component/or-mwc-components/src/or-mwc-input.ts@@ -1530,7 +1530,7 @@ export class OrMwcInput extends LitElement {slider.setDisabled(this.disabled || this.readonly);// slider.getDefaultFoundation(). getDefaultFoundation()..getMax() = this.min;// slider.max = this.max;- // slider.setValue(this.value);+ slider.setValue(this.value);} else if (this.type === InputType.SWITCH && this._mdcComponent) {const swtch = this._mdcComponent as MDCSwitch;swtch.checked = this.value;