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
Major: Client event bus event authorisation separated from processing and logging cleanup (#837)
Rich Turner2ae4f3b82c6f
diff --git a/.ci_cd/aws/cloudformation-create-ec2.yml b/.ci_cd/aws/cloudformation-create-ec2.ymlindex e98ed9ad1..d28a75626 100644--- a/.ci_cd/aws/cloudformation-create-ec2.yml+++ b/.ci_cd/aws/cloudformation-create-ec2.yml@@ -704,7 +704,7 @@ Resources:DiskUtilizationAlarm:Type: AWS::CloudWatch::AlarmProperties:- Namespace: Cloudwatch+ Namespace: CWAgentMetricName: disk_used_percentStatistic: AveragePeriod: 600@@ -718,6 +718,36 @@ Resources:Dimensions:- Name: InstanceIdValue: !Ref EC2Instance+ - Name: ImageId+ Value: !Ref ImageId+ - Name: InstanceType+ Value: !Ref InstanceType+ - Name: path+ Value: /+ - Name: fstype+ Value: xfs++ MemoryUtilizationAlarm:+ Type: AWS::CloudWatch::Alarm+ Properties:+ Namespace: CWAgent+ MetricName: mem_used_percent+ Statistic: Average+ Period: 600+ EvaluationPeriods: 3+ ComparisonOperator: GreaterThanThreshold+ Threshold: 90+ AlarmActions:+ - !Ref SnsTopic+ OKActions:+ - !Ref SnsTopic+ Dimensions:+ - Name: InstanceId+ Value: !Ref EC2Instance+ - Name: ImageId+ Value: !Ref ImageId+ - Name: InstanceType+ Value: !Ref InstanceTypeHealthCheckAlarm:Type: AWS::CloudWatch::Alarmdiff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.javaindex 5df01c480..6127eb4b1 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java@@ -240,6 +240,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen}if (success) {+ connectRetry = null;onConnectionStatusChanged(ConnectionStatus.CONNECTED);execution.recordResult(null);} else {@@ -343,6 +344,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen}protected void doDisconnect() {+ LOG.finer("Performing disconnect: " + getClientUri());try {if (channelStartFuture != null) {channelStartFuture.cancel(true);diff --git a/container/src/main/java/org/openremote/container/web/OAuthFilter.java b/container/src/main/java/org/openremote/container/web/OAuthFilter.javaindex 905dc2eeb..bc330e7e1 100644--- a/container/src/main/java/org/openremote/container/web/OAuthFilter.java+++ b/container/src/main/java/org/openremote/container/web/OAuthFilter.java@@ -91,7 +91,7 @@ public class OAuthFilter implements ClientRequestFilter {response = requestTokenUsingRefresh();if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {// Maybe the refresh token is not valid so do full auth- LOG.info("OAuth token refresh failed, trying a full authentication");+ LOG.fine("OAuth token refresh failed, trying a full authentication");authServerResponse = null;updateToken();return;diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.javaindex 091452e7b..adf7422f7 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java@@ -81,7 +81,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResourthrow new NotFoundException("Parent asset does not exist");}if (realm != null && !asset.getRealm().equals(realm)) {- throw new NotAuthorizedException("Parent asset not in the correct realm: agent ID =" + parentId);+ throw new ForbiddenException("Parent asset not in the correct realm: agent ID =" + parentId);}}@@ -119,7 +119,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour}if (realm != null && !realm.equals(agent.getRealm())) {- throw new NotAuthorizedException("Agent not in the correct realm: agent ID =" + agentId);+ throw new ForbiddenException("Agent not in the correct realm: agent ID =" + agentId);}List<AssetTreeNode> foundAssets = new ArrayList<>();@@ -159,7 +159,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour}if (realm != null && !realm.equals(agent.getRealm())) {- throw new NotAuthorizedException("Agent not in the correct realm: agent ID =" + agentId);+ throw new ForbiddenException("Agent not in the correct realm: agent ID =" + agentId);}List<AssetTreeNode> foundAssets = new ArrayList<>();@@ -196,9 +196,10 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour// TODO: Allow user to select which assets/attributes are actually added to the DBprotected void persistAssets(AssetTreeNode[] assets, Asset<?> parentAsset, String realm) {try {+ LOG.fine("Persisting assets");if (assets == null || assets.length == 0) {- LOG.info("No assets to import");+ LOG.fine("No assets to import");return;}@@ -206,7 +207,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResourAsset<?> asset = assetNode.asset;if (asset == null) {- LOG.info("Skipping node as asset not set");+ LOG.fine("Skipping node as asset not set");continue;}diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex ec48b51b2..24d71b619 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -478,7 +478,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn;}- LOG.info("Linking asset '" + assetId + "' attributes linked to protocol: assetId=" + assetId + ", attributes=" + attributes.size() + ", protocol=" + protocol);+ LOG.fine("Linking asset '" + assetId + "' attributes linked to protocol: assetId=" + assetId + ", attributes=" + attributes.size() + ", protocol=" + protocol);attributes.forEach(attribute -> {AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());@@ -502,7 +502,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn;}- LOG.info("Unlinking asset '" + assetId + "' attributes linked to protocol: assetId=" + assetId + ", attributes=" + attributes.size() + ", protocol=" + protocol);+ LOG.fine("Unlinking asset '" + assetId + "' attributes linked to protocol: assetId=" + assetId + ", attributes=" + attributes.size() + ", protocol=" + protocol);attributes.forEach(attribute -> {try {@@ -612,10 +612,10 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse// Fully load agent asset if path and parent info not loadedif (agent.getPath() == null || (agent.getPath().length > 1 && agent.getParentId() == null)) {- LOG.info("Agent is not fully loaded so retrieving the agent from the DB: " + agent.getId());+ LOG.fine("Agent is not fully loaded so retrieving the agent from the DB: " + agent.getId());final Agent<?, ?, ?> loadedAgent = assetStorageService.find(agent.getId(), true, Agent.class);if (loadedAgent == null) {- LOG.info("Agent not found in the DB, maybe it has been removed: " + agent.getId());+ LOG.fine("Agent not found in the DB, maybe it has been removed: " + agent.getId());return null;}agent = loadedAgent;@@ -665,7 +665,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse@Overridepublic void subscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {if (!getAgents().containsKey(agentId)) {- LOG.info("Attempt to subscribe to child asset changes with an invalid agent ID: " +agentId);+ LOG.fine("Attempt to subscribe to child asset changes with an invalid agent ID: " +agentId);return;}@@ -719,7 +719,7 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseFuture<Void> discoveryFuture = instanceDiscovery.startInstanceDiscovery(onDiscovered);discoveryFuture.get();} catch (InterruptedException e) {- LOG.info("Protocol instance discovery was cancelled");+ LOG.fine("Protocol instance discovery was cancelled");} catch (Exception e) {LOG.log(Level.WARNING, "Failed to do protocol instance discovery: Provider = " + instanceDiscoveryProviderClass, e);} finally {@@ -758,7 +758,7 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseFuture<Void> discoveryFuture = assetDiscovery.startAssetDiscovery(onDiscovered);discoveryFuture.get();} catch (InterruptedException e) {- LOG.info("Protocol asset discovery was cancelled");+ LOG.fine("Protocol asset discovery was cancelled");} catch (Exception e) {LOG.log(Level.WARNING, "Failed to do protocol asset discovery: Agent = " + agent, e);} finally {@@ -800,7 +800,7 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseFuture<Void> discoveryFuture = assetImport.startAssetImport(fileData, onDiscovered);discoveryFuture.get();} catch (InterruptedException e) {- LOG.info("Protocol asset import was cancelled");+ LOG.fine("Protocol asset import was cancelled");} catch (Exception e) {LOG.log(Level.WARNING, "Failed to do protocol asset import: Agent = " + agent, e);} finally {@@ -818,7 +818,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asseprotected void okToContinueWithImportOrDiscovery(String agentId) {if (agentDiscoveryImportFutureMap.containsKey(agentId)) {String msg = "Protocol asset discovery or import already running for requested agent: " + agentId;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex d3b4fcf36..ef3239ce3 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -47,19 +47,15 @@ import org.openremote.model.value.MetaItemType;import org.openremote.model.value.ValueType;import javax.persistence.EntityManager;-import java.util.ArrayList;-import java.util.Date;-import java.util.List;-import java.util.Optional;+import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;import static org.openremote.container.concurrent.GlobalLock.withLock;-import static org.openremote.model.attribute.AttributeWriteFailure.*;import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;import static org.openremote.model.attribute.AttributeEvent.Source.*;-import static org.openremote.model.value.MetaItemType.AGENT_LINK;+import static org.openremote.model.attribute.AttributeWriteFailure.*;/*** Receives {@link AttributeEvent} from {@link Source} and processes them.@@ -149,7 +145,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerStringBuilder error = new StringBuilder();- Source source = exchange.getIn().getHeader(HEADER_SOURCE, "unknown source", Source.class);+ Source source = exchange.getIn().getHeader(HEADER_SOURCE, CLIENT, Source.class);if (source != null) {error.append("Error processing from ").append(source);}@@ -202,6 +198,71 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerreturn assetEventAuthorizer.authorise(requestedRealm, auth, subscription);});+ // TODO: Introduce caching here similar to ActiveMQ auth caching+ clientEventService.addEventAuthorizer((requestedRealm, authContext, event) -> {++ if (!(event instanceof AttributeEvent attributeEvent)) {+ return false;+ }++ if (authContext != null && authContext.isSuperUser()) {+ return true;+ }++ // Check realm against user+ if (!identityService.getIdentityProvider().isRealmActiveAndAccessible(authContext,+ requestedRealm)) {+ LOG.fine("Realm is inactive, inaccessible or nonexistent: " + requestedRealm);+ return false;+ }++ // Users must have write attributes role+ if (authContext != null && !authContext.hasResourceRoleOrIsSuperUser(ClientRole.WRITE_ATTRIBUTES.getValue(),+ Constants.KEYCLOAK_CLIENT_ID)) {+ LOG.finer("User doesn't have required role '" + ClientRole.WRITE_ATTRIBUTES + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ return false;+ }++ // Have to load the asset and attribute to perform additional checks - should permissions be moved out of the+ // asset model (possibly if the performance is determined to be not good enough)+ // TODO: Use a targeted query to retrieve just the info we need+ Asset<?> asset = assetStorageService.find(attributeEvent.getAssetId());+ Attribute<?> attribute = asset != null ? asset.getAttribute(attributeEvent.getAttributeName()).orElse(null) : null;++ if (asset == null || !asset.hasAttribute(attributeEvent.getAttributeName())) {+ LOG.finer("Cannot authorize asset event as asset and/or attribute doesn't exist: " + attributeEvent.getAssetId());+ return false;+ } else if (!Objects.equals(requestedRealm, asset.getRealm())) {+ LOG.finer("Asset is not in the requested realm: requestedRealm=" + requestedRealm + ", ref=" + attributeEvent.getAttributeRef());+ return false;+ }++ if (authContext != null) {+ // Check restricted user+ if (identityService.getIdentityProvider().isRestrictedUser(authContext)) {+ // Must be asset linked to user+ if (!assetStorageService.isUserAsset(authContext.getUserId(),+ attributeEvent.getAssetId())) {+ LOG.finer("User is not linked to asset '" + attributeEvent.getAssetId() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ return false;+ }++ if (attribute == null || !attribute.getMetaValue(MetaItemType.ACCESS_RESTRICTED_WRITE).orElse(false)) {+ LOG.finer("Asset attribute doesn't support restricted write on '" + attributeEvent.getAttributeRef() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ return false;+ }+ }+ } else {+ // Check attribute has public write flag for anonymous write+ if (attribute == null || !attribute.hasMeta(MetaItemType.ACCESS_PUBLIC_WRITE)) {+ LOG.finer("Asset doesn't support public write on '" + attributeEvent.getAttributeRef() + "': username=null");+ return false;+ }+ }++ return true;+ });+processors.add(gatewayService);processors.add(agentService);processors.add(rulesService);@@ -246,7 +307,6 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer- Make AssetUpdateProcessor transactional with a two-phase commit API- Replace at-most-once ClientEventService with at-least-once capable, embeddable message broker/protocol- See pseudocode here: http://activemq.apache.org/should-i-use-xa.html- - Do we want JMS/AMQP/WSS or SOME_API/MQTT/WSS? ActiveMQ or Moquette?*/from(ASSET_QUEUE).routeId("AssetQueueProcessor")@@ -293,52 +353,6 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerthrow new AssetProcessingException(ATTRIBUTE_NOT_FOUND);}- switch (source) {- case CLIENT -> {- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);- if (authContext == null) {- // Check attribute has public write flag- if (!oldAttribute.hasMeta(MetaItemType.ACCESS_PUBLIC_WRITE)) {- throw new AssetProcessingException(NO_AUTH_CONTEXT);- }- } else {- // Check realm, must be accessible- if (!identityService.getIdentityProvider().isRealmActiveAndAccessible(authContext,- asset.getRealm())) {- throw new AssetProcessingException(INVALID_REALM);- }-- // Regular user must have write attributes role- if (!authContext.hasResourceRoleOrIsSuperUser(ClientRole.WRITE_ATTRIBUTES.getValue(),- Constants.KEYCLOAK_CLIENT_ID)) {- throw new AssetProcessingException(INSUFFICIENT_ACCESS);- }-- // Check restricted user- if (identityService.getIdentityProvider().isRestrictedUser(authContext)) {- // Must be asset linked to user- if (!assetStorageService.isUserAsset(authContext.getUserId(),- event.getAssetId())) {- throw new AssetProcessingException(INSUFFICIENT_ACCESS);- }- // Must be writable by restricted client- if (!oldAttribute.getMetaValue(MetaItemType.ACCESS_RESTRICTED_WRITE).orElse(false)) {- throw new AssetProcessingException(INSUFFICIENT_ACCESS);- }- }- }- }- case SENSOR -> {- Optional<Protocol<?>> protocol = oldAttribute.getMetaValue(AGENT_LINK)- .map(agentLink -> agentService.getProtocolInstance(agentLink.getId()));-- // Sensor event must be for an attribute linked to an agent- if (protocol.isEmpty()) {- throw new AssetProcessingException(INVALID_AGENT_LINK);- }- }- }-// For executable attributes, non-sensor sources can set a writable attribute execute statusif (oldAttribute.getType() == ValueType.EXECUTION_STATUS && source != SENSOR) {Optional<AttributeExecuteStatus> status = event.getValue()@@ -354,7 +368,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerObject value = event.getValue().map(eventValue -> {Class<?> attributeValueType = oldAttribute.getType().getType();return ValueUtil.getValueCoerced(eventValue, attributeValueType).orElseThrow(() -> {- LOG.info("Failed to coerce attribute event value into the correct value type: event value type=" + eventValue.getClass() + ", attribute value type=" + attributeValueType);+ LOG.fine("Failed to coerce attribute event value into the correct value type: event value type=" + eventValue.getClass() + ", attribute value type=" + attributeValueType);return new AssetProcessingException(INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE);});diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 2e6473245..de82a89f8 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -22,6 +22,7 @@ package org.openremote.manager.asset;import com.fasterxml.jackson.databind.node.NullNode;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.timer.TimerService;+import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.Constants;@@ -40,6 +41,7 @@ import javax.persistence.OptimisticLockException;import javax.validation.ConstraintViolationException;import javax.ws.rs.BadRequestException;import javax.ws.rs.ForbiddenException;+import javax.ws.rs.NotAuthorizedException;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.HttpHeaders;import javax.ws.rs.core.MediaType;@@ -59,14 +61,17 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourprivate static final Logger LOG = Logger.getLogger(AssetResourceImpl.class.getName());protected final AssetStorageService assetStorageService;protected final MessageBrokerService messageBrokerService;+ protected final ClientEventService clientEventService;public AssetResourceImpl(TimerService timerService,ManagerIdentityService identityService,AssetStorageService assetStorageService,- MessageBrokerService messageBrokerService) {+ MessageBrokerService messageBrokerService,+ ClientEventService clientEventService) {super(timerService, identityService);this.assetStorageService = assetStorageService;this.messageBrokerService = messageBrokerService;+ this.clientEventService = clientEventService;}@Override@@ -76,18 +81,17 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourreturn new Asset<?>[0];}- AssetQuery assetQuery = new AssetQuery();+ if (!isAuthenticated()) {+ throw new NotAuthorizedException("Must be authenticated");+ }- if (!isRestrictedUser()) {- assetQuery- .realm(new RealmPredicate(getAuthenticatedRealmName()))- .recursive(true);- } else {- assetQuery- .userIds(getUserId());+ AssetQuery query = new AssetQuery().userIds(getUserId());++ if (!assetStorageService.authorizeAssetQuery(query, getAuthContext(), getRequestRealmName())) {+ throw new ForbiddenException("User not authorized to execute specified query");}- List<Asset<?>> assets = assetStorageService.findAll(assetQuery);+ List<Asset<?>> assets = assetStorageService.findAll(query);// Compress response (the request attribute enables the interceptor)request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");@@ -249,6 +253,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour// Check restrictedif (isRestrictedUser()) {if (!assetStorageService.isUserAsset(getUserId(), assetId)) {+ LOG.fine("Forbidden access for restricted user: username=" + getUsername() + ", assetID=" + assetId);throw new WebApplicationException(FORBIDDEN);}asset = assetStorageService.find(assetId, loadComplete, Access.PROTECTED);@@ -260,7 +265,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourthrow new WebApplicationException(NOT_FOUND);if (!isRealmActiveAndAccessible(asset.getRealm())) {- LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);+ LOG.fine("Forbidden access (realm '" + asset.getRealm() + "' nonexistent, inactive or inaccessible) for user: " + getUsername());throw new WebApplicationException(FORBIDDEN);}@@ -276,25 +281,30 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour@Overridepublic void update(RequestParams requestParams, String assetId, Asset<?> asset) {++ LOG.fine("Updating asset: assetID=" + assetId);+try {Asset<?> storageAsset = assetStorageService.find(assetId, true);- if (storageAsset == null)+ if (storageAsset == null) {+ LOG.fine("Asset not found: assetID=" + assetId);throw new WebApplicationException(NOT_FOUND);+ }// Realm of asset must be accessibleif (!isRealmActiveAndAccessible(storageAsset.getRealm())) {- LOG.info("Realm not accessible by user '" + getUsername() + "', can't update: " + storageAsset);+ LOG.fine("Realm '" + storageAsset.getRealm() + "' is nonexistent, inactive or inaccessible: username=" + getUsername() + ", assetID=" + assetId);throw new WebApplicationException(FORBIDDEN);}if (!storageAsset.getRealm().equals(asset.getRealm())) {- LOG.info("Cannot change asset's realm: " + storageAsset);+ LOG.fine("Cannot change asset's realm: existingRealm=" + storageAsset.getRealm() + ", requestedRealm=" + asset.getRealm());throw new WebApplicationException(FORBIDDEN);}if (!storageAsset.getType().equals(asset.getType())) {- LOG.info("Cannot change asset's type: " + storageAsset);+ LOG.fine("Cannot change asset's type: existingType=" + storageAsset.getType() + ", requestedType=" + asset.getType());throw new WebApplicationException(FORBIDDEN);}@@ -330,7 +340,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour// If the existing attribute is not writable by restricted client, ignore itif (!existingAttribute.getMetaValue(ACCESS_RESTRICTED_WRITE).orElse(false)) {- LOG.info("Existing attribute not writable by restricted client, ignoring update of: " + updatedAttributeName);+ LOG.fine("Existing attribute not writable by restricted client, ignoring update of: " + updatedAttributeName);continue;}@@ -411,20 +421,19 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourvalue = null;}- // Process asynchronously but block for a little while waiting for the result- Map<String, Object> headers = new HashMap<>();- headers.put(AttributeEvent.HEADER_SOURCE, CLIENT);+ AttributeEvent event = new AttributeEvent(assetId, attributeName, value);- if (isAuthenticated()) {- headers.put(Constants.AUTH_CONTEXT, getAuthContext());+ // Check authorisation+ if (!clientEventService.authorizeEventWrite(getRequestRealmName(), getAuthContext(), event)) {+ throw new ForbiddenException("Cannot write specified attribute: " + event);}- AttributeWriteResult result = doAttributeWrite(new AttributeRef(assetId, attributeName), value, headers);+ // Process asynchronously but block for a little while waiting for the result+ AttributeWriteResult result = doAttributeWrite(event);if (result.getFailure() != null) {switch (result.getFailure()) {case ILLEGAL_SOURCE:- case NO_AUTH_CONTEXT:case INSUFFICIENT_ACCESS:case INVALID_REALM:status = FORBIDDEN;@@ -451,16 +460,13 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourpublic AttributeWriteResult[] writeAttributeValues(RequestParams requestParams, AttributeState[] attributeStates) {// Process asynchronously but block for a little while waiting for the result- Map<String, Object> headers = new HashMap<>();- headers.put(AttributeEvent.HEADER_SOURCE, CLIENT);-- if (isAuthenticated()) {- headers.put(Constants.AUTH_CONTEXT, getAuthContext());- }-- return Arrays.stream(attributeStates).map(attributeState ->- doAttributeWrite(attributeState.getRef(), attributeState.getValue().orElse(null), headers)- ).toArray(AttributeWriteResult[]::new);+ return Arrays.stream(attributeStates).map(attributeState -> {+ AttributeEvent event = new AttributeEvent(attributeState);+ if (!clientEventService.authorizeEventWrite(getRequestRealmName(), getAuthContext(), event)) {+ return new AttributeWriteResult(event.getAttributeRef(), AttributeWriteFailure.INSUFFICIENT_ACCESS);+ }+ return doAttributeWrite(event);+ }).toArray(AttributeWriteResult[]::new);}@Override@@ -479,7 +485,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourif (asset.getRealm() == null || asset.getRealm().length() == 0) {asset.setRealm(getAuthenticatedRealm().getName());} else if (!isRealmActiveAndAccessible(asset.getRealm())) {- LOG.info("Forbidden access for user '" + getUsername() + "', can't create: " + asset);+ LOG.fine("Forbidden access for user '" + getUsername() + "', can't create: " + asset);throw new WebApplicationException(FORBIDDEN);}@@ -530,6 +536,11 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour@Overridepublic void delete(RequestParams requestParams, List<String> assetIds) {++ if (LOG.isLoggable(Level.FINE)) {+ LOG.fine("Deleting assets: " + assetIds);+ }+try {if (assetIds == null || assetIds.isEmpty()) {throw new WebApplicationException(BAD_REQUEST);@@ -546,7 +557,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour}if (assets.stream().map(Asset::getRealm).distinct().anyMatch(asset -> !isRealmActiveAndAccessible(asset))) {- LOG.info("Forbidden access for user '" + getUsername() + "', can't delete requested assets");+ LOG.fine("One or more assets in an nonexistent, inactive or inaccessible realm: username=" + getUsername());throw new WebApplicationException(FORBIDDEN);}@@ -560,35 +571,33 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour@Overridepublic Asset<?>[] queryAssets(RequestParams requestParams, AssetQuery query) {- try {- query = assetStorageService.prepareAssetQuery(query, getAuthContext(), getRequestRealmName());+ if (query == null) {+ query = new AssetQuery();+ }- List<Asset<?>> result = assetStorageService.findAll(query);+ if (!assetStorageService.authorizeAssetQuery(query, getAuthContext(), getRequestRealmName())) {+ throw new ForbiddenException("User not authorized to execute specified query");+ }- // Compress response (the request attribute enables the interceptor)- request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");+ List<Asset<?>> result = assetStorageService.findAll(query);- return result.toArray(new Asset[0]);+ // Compress response (the request attribute enables the interceptor)+ request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");- } catch (IllegalStateException ex) {- throw new BadRequestException(ex);- }+ return result.toArray(new Asset[0]);}- protected AttributeWriteResult doAttributeWrite(AttributeRef ref, Object value, Map<String, Object> headers) {+ protected AttributeWriteResult doAttributeWrite(AttributeEvent event) {AttributeWriteFailure failure = null;try {- AttributeEvent event = new AttributeEvent(- ref, value, timerService.getCurrentTimeMillis()- );-- LOG.info("Write attribute value request: " + event);+ if (LOG.isLoggable(Level.FINE)) {+ LOG.fine("Write attribute value request: " + event);+ }// Process synchronously- Object result = messageBrokerService.getProducerTemplate().requestBodyAndHeaders(- AssetProcessingService.ASSET_QUEUE, event, headers- );+ Object result = messageBrokerService.getProducerTemplate().requestBodyAndHeader(+ AssetProcessingService.ASSET_QUEUE, event, AttributeEvent.HEADER_SOURCE, CLIENT);if (result instanceof AssetProcessingException) {AssetProcessingException processingException = (AssetProcessingException) result;@@ -601,22 +610,20 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourfailure = AttributeWriteFailure.UNKNOWN;}- return new AttributeWriteResult(ref, failure);+ return new AttributeWriteResult(event.getAttributeRef(), failure);}@Overridepublic void updateParent(RequestParams requestParams, String parentId, List<String> assetIds) {AssetQuery query = new AssetQuery();- query.ids = assetIds.stream().toArray(String[]::new);+ query.ids = assetIds.toArray(String[]::new);List<Asset<?>> assets = this.assetStorageService.findAll(query);- LOG.info("Get " + assets.size() + " assets from query");+ LOG.fine("Updating parent for assets: count=" + assets.size() + ", newParentID=" + parentId);for (Asset asset : assets) {asset.setParentId(parentId);-- LOG.info("Set asset " + asset.getId() + " new parent " + parentId);-+ LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=" + parentId);assetStorageService.merge(asset);}}@@ -624,16 +631,14 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour@Overridepublic void updateNoneParent(RequestParams requestParams, List<String> assetIds) {AssetQuery query = new AssetQuery();- query.ids = assetIds.stream().toArray(String[]::new);+ query.ids = assetIds.toArray(String[]::new);List<Asset<?>> assets = this.assetStorageService.findAll(query);- LOG.info("Get " + assets.size() + " assets from query");+ LOG.fine("Updating parent for assets: count=" + assets.size() + ", newParentID=NONE");- for (Asset asset : assets) {+ for (Asset<?> asset : assets) {asset.setParentId(null);-- LOG.info("Set asset " + asset.getId() + " with no parent");-+ LOG.fine("Updating asset parent: assetID=" + asset.getId() + ", newParentID=NONE");assetStorageService.merge(asset);}}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 5c826f313..c977de532 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -76,7 +76,6 @@ import java.util.stream.Collectors;import java.util.stream.IntStream;import static java.util.stream.Collectors.groupingBy;-import static org.apache.camel.builder.PredicateBuilder.or;import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;@@ -179,7 +178,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}// Regular user must have role- if (!filter.isPublicEvents() && !isAnonymous && !auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {+ if (!filter.isPublicEvents() && (isAnonymous || !auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID))) {return false;}@@ -325,12 +324,31 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn assetEventAuthorizer.authorise(realm, auth, subscription);});+ clientEventService.addEventAuthorizer((realm, auth, event) -> {+ boolean authorize = event instanceof HasAssetQuery;++ if (event instanceof ReadAssetEvent readAssetEvent) {+ if (readAssetEvent.getAssetQuery() == null) {+ LOG.fine("Read asset event must specify an asset ID");+ return false;+ }+ } else if (event instanceof ReadAttributeEvent readAttributeEvent) {+ if (readAttributeEvent.getAssetQuery() == null) {+ LOG.fine("Read attribute event must specify an asset ID");+ return false;+ }+ }++ return authorize && authorizeAssetQuery(((HasAssetQuery)event).getAssetQuery(), auth, realm);+ });+container.getService(ManagerWebService.class).addApiSingleton(new AssetResourceImpl(container.getService(TimerService.class),identityService,this,- container.getService(MessageBrokerService.class)+ container.getService(MessageBrokerService.class),+ clientEventService));@@ -366,108 +384,63 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// React if a client wants to read assets and attributesfrom(CLIENT_EVENT_TOPIC).routeId("FromClientReadRequests")- .filter(- or(body().isInstanceOf(ReadAssetsEvent.class), body().isInstanceOf(ReadAssetEvent.class), body().isInstanceOf(ReadAttributeEvent.class)))- .choice()- .when(or(body().isInstanceOf(ReadAssetEvent.class), body().isInstanceOf(ReadAttributeEvent.class)))- .process(exchange -> {- LOG.finer("Handling from client: " + exchange.getIn().getBody());-- String assetId;- String attributeName = null;-- if (exchange.getIn().getBody() instanceof ReadAssetEvent) {- assetId = exchange.getIn().getBody(ReadAssetEvent.class).getAssetId();+ .filter(body().isInstanceOf(HasAssetQuery.class))+ .process(exchange -> {+ HasAssetQuery hasAssetQuery = exchange.getIn().getBody(HasAssetQuery.class);+ AssetQuery assetQuery = hasAssetQuery.getAssetQuery();+ String sessionKey = ClientEventService.getSessionKey(exchange);+ String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);+ Object response = null;++ if (hasAssetQuery instanceof ReadAssetsEvent) {+ List<Asset<?>> assets = findAll(assetQuery);+ response = new AssetsEvent(assets);+ } else {+ Asset<?> asset = find(assetQuery);+ String assetId;+ String attributeName = null;++ if (asset != null) {+ if (hasAssetQuery instanceof ReadAttributeEvent readAttributeEvent) {+ assetId = readAttributeEvent.getAttributeRef().getId();+ attributeName = readAttributeEvent.getAttributeRef().getName();} else {- ReadAttributeEvent assetAttributeEvent = exchange.getIn().getBody(ReadAttributeEvent.class);- assetId = assetAttributeEvent.getAttributeRef().getId();- attributeName = assetAttributeEvent.getAttributeRef().getName();+ assetId = ((ReadAssetEvent) hasAssetQuery).getAssetId();}- if (TextUtil.isNullOrEmpty(assetId)) {- return;- }-- String sessionKey = ClientEventService.getSessionKey(exchange);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);- boolean isAttributeRead = !TextUtil.isNullOrEmpty(attributeName);- String requestRealm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);-- AssetQuery assetQuery = new AssetQuery()- .ids(assetId);-- assetQuery = prepareAssetQuery(assetQuery, authContext, requestRealm);-- Asset<?> asset = find(assetQuery);-- if (asset != null) {- String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);- Object response = null;-- if (isAttributeRead) {- Attribute<?> assetAttribute = asset.getAttributes().get(attributeName).orElse(null);- if (assetAttribute != null) {-- // Check access constraints- if (assetQuery.access == null- || assetQuery.access == PRIVATE- || (assetQuery.access == PUBLIC && assetAttribute.getMetaValue(ACCESS_PUBLIC_READ).orElse(false))- || (assetQuery.access == PROTECTED && assetAttribute.getMetaValue(ACCESS_RESTRICTED_READ).orElse(false))) {- response = new AttributeEvent(assetId, attributeName, assetAttribute.getValue().orElse(null), assetAttribute.getTimestamp().orElse(0L));- }- }- } else {-+ if (!TextUtil.isNullOrEmpty(attributeName)) {+ Attribute<?> assetAttribute = asset.getAttributes().get(attributeName).orElse(null);+ if (assetAttribute != null) {// Check access constraints- if (assetQuery.access != PUBLIC || asset.isAccessPublicRead()) {- response = new AssetEvent(AssetEvent.Cause.READ, asset, null);+ if (assetQuery.access == null+ || assetQuery.access == PRIVATE+ || (assetQuery.access == PUBLIC && assetAttribute.getMetaValue(ACCESS_PUBLIC_READ).orElse(false))+ || (assetQuery.access == PROTECTED && assetAttribute.getMetaValue(ACCESS_RESTRICTED_READ).orElse(false))) {+ response = new AttributeEvent(assetId, attributeName, assetAttribute.getValue().orElse(null), assetAttribute.getTimestamp().orElse(0L));}}-- if (response != null) {- if (!isNullOrEmpty(messageId)) {- response = new EventRequestResponseWrapper<>(messageId, (SharedEvent)response);- }- clientEventService.sendToSession(sessionKey, response);- }- }- })- .when(body().isInstanceOf(ReadAssetsEvent.class))- .process(exchange -> {- ReadAssetsEvent readAssets = exchange.getIn().getBody(ReadAssetsEvent.class);- String sessionKey = ClientEventService.getSessionKey(exchange);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);- String requestRealm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);- AssetQuery query = readAssets.getAssetQuery();- try {- query = prepareAssetQuery(query, authContext, requestRealm);-- List<Asset<?>> assets = findAll(query);-- String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);-- if (isNullOrEmpty(messageId)) {- clientEventService.sendToSession(sessionKey, new AssetsEvent(assets));- } else {- clientEventService.sendToSession(sessionKey, new EventRequestResponseWrapper<>(messageId, new AssetsEvent(assets)));- }- } catch (IllegalStateException ignored) {+ } else {+ response = new AssetEvent(AssetEvent.Cause.READ, asset, null);}- })- .stop()- .endChoice()+ }+ }++ if (response != null) {+ if (!isNullOrEmpty(messageId)) {+ response = new EventRequestResponseWrapper<>(messageId, (SharedEvent)response);+ }+ clientEventService.sendToSession(sessionKey, response);+ }++ }).end();}/**- * Prepares an {@link AssetQuery} by validating it against security constraints and/or applying default options to the query+ * Authorizes an {@link AssetQuery} by validating it against security constraints and/or applying default options to the query* based on security constraints.*/- public AssetQuery prepareAssetQuery(AssetQuery query, AuthContext authContext, String requestRealm) throws IllegalStateException {-- if (query == null) {- query = new AssetQuery();- }+ public boolean authorizeAssetQuery(AssetQuery query, AuthContext authContext, String requestRealm) {boolean isAnonymous = authContext == null;boolean isSuperUser = authContext != null && authContext.isSuperUser();@@ -480,21 +453,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (TextUtil.isNullOrEmpty(realm)) {String msg = "Realm must be specified to read assets";LOG.finer(msg);- throw new IllegalStateException(msg);+ return false;}if (isAnonymous) {if (query.access != null && query.access != PUBLIC) {String msg = "Only public access allowed for anonymous requests";LOG.finer(msg);- throw new IllegalStateException(msg);+ return false;}query.access = PUBLIC;} else if (isRestricted) {if (query.access == PRIVATE) {String msg = "Only public or restricted access allowed for restricted requests";LOG.finer(msg);- throw new IllegalStateException(msg);+ return false;}if (query.access == null) {query.access = PROTECTED;@@ -503,14 +476,14 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (query.access != PUBLIC && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {String msg = "User must have '" + ClientRole.READ_ASSETS.getValue() + "' role to read non public assets";- LOG.fine(msg);- throw new IllegalStateException(msg);+ LOG.finer(msg);+ return false;}if (query.access != PUBLIC && !realm.equals(authContext.getAuthenticatedRealmName())) {String msg = "Realm must match authenticated realm for non public access queries";LOG.finer(msg);- throw new IllegalStateException(msg);+ return false;}query.realm = new RealmPredicate(realm);@@ -523,10 +496,10 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (!identityService.getIdentityProvider().isRealmActiveAndAccessible(authContext, realm)) {String msg = "Realm is not present or is inactive";LOG.finer(msg);- throw new IllegalStateException(msg);+ return false;}- return query;+ return true;}public Asset<?> find(String assetId) {@@ -665,6 +638,9 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic*/@SuppressWarnings("unchecked")public <T extends Asset<?>> T merge(T asset, boolean overrideVersion, boolean skipGatewayCheck, String userName) throws IllegalStateException, ConstraintViolationException {++ LOG.fine("Merging asset: " + asset);+return persistenceService.doReturningTransaction(em -> {String gatewayId = gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId());@@ -677,7 +653,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// Validate realmif (asset.getRealm() == null) {String msg = "Asset realm must be set : asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}@@ -698,13 +674,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// Verify type has not been changedif (!existingAsset.getType().equals(asset.getType())) {String msg = "Asset type cannot be changed: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}if (!existingAsset.getRealm().equals(asset.getRealm())) {String msg = "Asset realm cannot be changed: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}@@ -730,13 +706,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (!identityService.getIdentityProvider().realmExists(asset.getRealm())) {String msg = "Asset realm not found or is inactive: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}if (asset.getParentId() != null && asset.getParentId().equals(asset.getId())) {String msg = "Asset parent cannot be the asset: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}@@ -749,21 +725,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// The parent must existif (parent == null) {String msg = "Asset parent not found: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}// The parent can not be a child of the assetif (parent.pathContains(asset.getId())) {String msg = "Asset parent cannot be a descendant of the asset: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}// The parent should be in the same realmif (!parent.getRealm().equals(asset.getRealm())) {String msg = "Asset parent must be in the same realm: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}@@ -772,7 +748,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicString childAssetType = parent.getAttributes().getValue(GroupAsset.CHILD_ASSET_TYPE).orElseThrow(() -> {String msg = "Asset parent is of type GROUP but the childAssetType attribute is invalid: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);return new IllegalStateException(msg);});@@ -787,7 +763,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (!typeMatch) {String msg = "Asset type does not match parent GROUP asset's childAssetType attribute: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}}@@ -799,7 +775,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic.map(childAssetTypeString -> TextUtil.isNullOrEmpty(childAssetTypeString) ? null : childAssetTypeString).orElseThrow(() -> {String msg = "Asset of type GROUP childAssetType attribute must be a valid string: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);return new IllegalStateException(msg);});@@ -807,13 +783,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic.getChildAssetType().orElseThrow(() -> {String msg = "Asset of type GROUP childAssetType attribute must be a valid string: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);return new IllegalStateException(msg);}) : childAssetType;if (!childAssetType.equals(existingChildAssetType)) {String msg = "Asset of type GROUP so childAssetType attribute cannot be changed: asset=" + asset;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}}@@ -833,7 +809,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicuser = identityService.getIdentityProvider().getUserByUsername(asset.getRealm(), userName);if (user == null) {String msg = "User not found: " + userName;- LOG.info(msg);+ LOG.fine(msg);throw new IllegalStateException(msg);}}@@ -1296,7 +1272,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicboolean containsCalendarPredicate = queryAndContainsCalendarPredicate.value;if (containsCalendarPredicate && (query.select != null && (query.select.attributes == null))) {- LOG.info("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");+ LOG.fine("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");throw new IllegalArgumentException("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");}diff --git a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.javaindex b9415f5b9..d9f5d3d01 100644--- a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java+++ b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java@@ -161,7 +161,7 @@ public class EnergyOptimisationService extends RouteBuilder implements Container}protected void processAssetChange(PersistenceEvent<EnergyOptimisationAsset> persistenceEvent) {- LOG.info("Processing optimisation asset change: " + persistenceEvent);+ LOG.fine("Processing optimisation asset change: " + persistenceEvent);stopOptimisation(persistenceEvent.getEntity().getId());if (persistenceEvent.getCause() != PersistenceEvent.Cause.DELETE) {@@ -185,7 +185,7 @@ public class EnergyOptimisationService extends RouteBuilder implements Container&& attributeEvent.<Boolean>getValue().orElse(false)) {// Look for forced charge assetif (forceChargeAssetIds.remove(attributeEvent.getAssetId())) {- LOG.info("Previously force charged asset has now been disconnected so clearing force charge flag: " + attributeEvent.getAssetId());+ LOG.fine("Previously force charged asset has now been disconnected so clearing force charge flag: " + attributeEvent.getAssetId());}return;}@@ -205,15 +205,15 @@ public class EnergyOptimisationService extends RouteBuilder implements Containerdouble powerImportMax = storageAsset.getPowerImportMax().orElse(Double.MAX_VALUE);double maxEnergyLevel = getElectricityStorageAssetEnergyLevelMax(storageAsset);double currentEnergyLevel = storageAsset.getEnergyLevel().orElse(0d);- LOG.info("Request to force charge asset '" + attributeEvent.getAssetId() + "': attempting to set powerSetpoint=" + powerImportMax);+ LOG.fine("Request to force charge asset '" + attributeEvent.getAssetId() + "': attempting to set powerSetpoint=" + powerImportMax);if (forceChargeAssetIds.contains(attributeEvent.getAssetId())) {- LOG.info("Request to force charge asset will be ignored as force charge already requested for asset: " + storageAsset);+ LOG.fine("Request to force charge asset will be ignored as force charge already requested for asset: " + storageAsset);return;}if (currentEnergyLevel >= maxEnergyLevel) {- LOG.info("Request to force charge asset will be ignored as asset is already at or above maxEnergyLevel: " + storageAsset);+ LOG.fine("Request to force charge asset will be ignored as asset is already at or above maxEnergyLevel: " + storageAsset);return;}diff --git a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.javaindex 8add49488..221f09088 100644--- a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java+++ b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java@@ -122,7 +122,7 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi@Overridepublic void start(Container container) throws Exception {if (forecastSolarApiKey == null) {- LOG.info("No value found for OR_FORECAST_SOLAR_API_KEY, ForecastSolarService won't start");+ LOG.fine("No value found for OR_FORECAST_SOLAR_API_KEY, ForecastSolarService won't start");return;}@@ -180,7 +180,7 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServireturn;}- LOG.info("Processing producer solar asset attribute event: " + attributeEvent);+ LOG.fine("Processing producer solar asset attribute event: " + attributeEvent);stopProcessing(attributeEvent.getAssetId());// Get latest asset from storage@@ -203,7 +203,7 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServi}protected void processAssetChange(PersistenceEvent<ElectricityProducerSolarAsset> persistenceEvent) {- LOG.info("Processing producer solar asset change: " + persistenceEvent);+ LOG.fine("Processing producer solar asset change: " + persistenceEvent);stopProcessing(persistenceEvent.getEntity().getId());if (persistenceEvent.getCause() != PersistenceEvent.Cause.DELETE) {diff --git a/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java b/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.javaindex e107ee5f9..749c6256a 100644--- a/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java+++ b/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java@@ -155,7 +155,7 @@ public class ForecastWindService extends RouteBuilder implements ContainerServic@Overridepublic void start(Container container) throws Exception {if (openWeatherAppId == null) {- LOG.info("No value found for OR_OPEN_WEATHER_API_APP_ID, ForecastWindService won't start");+ LOG.fine("No value found for OR_OPEN_WEATHER_API_APP_ID, ForecastWindService won't start");return;}@@ -217,7 +217,7 @@ public class ForecastWindService extends RouteBuilder implements ContainerServicreturn;}- LOG.info("Processing producer wind asset attribute event: " + attributeEvent);+ LOG.fine("Processing producer wind asset attribute event: " + attributeEvent);stopCalculation(attributeEvent.getAssetId());// Get latest asset from storage@@ -240,7 +240,7 @@ public class ForecastWindService extends RouteBuilder implements ContainerServic}protected void processAssetChange(PersistenceEvent<ElectricityProducerWindAsset> persistenceEvent) {- LOG.info("Processing producer wind asset change: " + persistenceEvent);+ LOG.fine("Processing producer wind asset change: " + persistenceEvent);stopCalculation(persistenceEvent.getEntity().getId());if (persistenceEvent.getCause() != PersistenceEvent.Cause.DELETE) {diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 303c938bf..cef3d8e51 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -35,7 +35,10 @@ import org.openremote.model.ContainerService;import org.openremote.model.event.shared.*;import org.openremote.model.syslog.SyslogEvent;-import java.util.*;+import java.util.Collection;+import java.util.HashSet;+import java.util.Objects;+import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ConcurrentMap;import java.util.concurrent.CopyOnWriteArraySet;@@ -115,6 +118,7 @@ public class ClientEventService implements ContainerService {public static final String CLIENT_EVENT_QUEUE = "seda://ClientEventQueue?multipleConsumers=false&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&size=25000";final protected Collection<EventSubscriptionAuthorizer> eventSubscriptionAuthorizers = new CopyOnWriteArraySet<>();+ final protected Collection<EventAuthorizer> eventAuthorizers = new CopyOnWriteArraySet<>();final protected Collection<Consumer<Exchange>> exchangeInterceptors = new CopyOnWriteArraySet<>();protected ConcurrentMap<String, SessionInfo> sessionKeyInfoMap = new ConcurrentHashMap<>();protected TimerService timerService;@@ -175,33 +179,8 @@ public class ClientEventService implements ContainerService {from("websocket://" + WEBSOCKET_EVENTS).routeId("FromClientWebsocketEvents")- .process(exchange -> exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, HEADER_CONNECTION_TYPE_WEBSOCKET))- .to(ClientEventService.CLIENT_EVENT_QUEUE)- .end();-- from(ClientEventService.CLIENT_EVENT_QUEUE)- .routeId("ClientEvents")- .choice()- .when(header(ConnectionConstants.SESSION_OPEN))- .process(exchange -> {- String sessionKey = getSessionKey(exchange);- sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));- passToInterceptors(exchange);- })- .stop()- .when(or(- header(ConnectionConstants.SESSION_CLOSE),- header(ConnectionConstants.SESSION_CLOSE_ERROR)- ))- .process(exchange -> {- String sessionKey = getSessionKey(exchange);- sessionKeyInfoMap.remove(sessionKey);- eventSubscriptions.cancelAll(sessionKey);- passToInterceptors(exchange);- })- .stop()- .end().process(exchange -> {+ exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, HEADER_CONNECTION_TYPE_WEBSOCKET);// Do basic formatting of exchangeEventRequestResponseWrapper<?> requestResponse = null;@@ -227,6 +206,54 @@ public class ClientEventService implements ContainerService {}}+ // Perform authorisation+ if (exchange.getIn().getBody() instanceof SharedEvent) {+ SharedEvent event = exchange.getIn().getBody(SharedEvent.class);+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);+ String realm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);++ if (!authorizeEventWrite(realm, authContext, event)) {+ stopMessage(exchange);+ }+ } else if (exchange.getIn().getBody() instanceof EventSubscription<?>) {+ EventSubscription<?> subscription = exchange.getIn().getBody(EventSubscription.class);+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);+ String realm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);+ String sessionKey = getSessionKey(exchange);++ if (!authorizeEventSubscription(realm, authContext, subscription)) {+ sendToSession(sessionKey, new UnauthorizedEventSubscription<>(subscription));+ stopMessage(exchange);+ }+ }+ })+ .to(ClientEventService.CLIENT_EVENT_QUEUE)+ .end();++ from(ClientEventService.CLIENT_EVENT_QUEUE)+ .routeId("ClientEvents")+ .choice()+ .when(header(ConnectionConstants.SESSION_OPEN))+ .process(exchange -> {+ String sessionKey = getSessionKey(exchange);+ sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));+ passToInterceptors(exchange);+ })+ .stop()+ .when(or(+ header(ConnectionConstants.SESSION_CLOSE),+ header(ConnectionConstants.SESSION_CLOSE_ERROR)+ ))+ .process(exchange -> {+ String sessionKey = getSessionKey(exchange);+ sessionKeyInfoMap.remove(sessionKey);+ eventSubscriptions.cancelAll(sessionKey);+ passToInterceptors(exchange);+ })+ .stop()+ .end()+ .process(exchange -> {+ // Set timestamp if not setif (exchange.getIn().getBody() instanceof SharedEvent) {SharedEvent event = exchange.getIn().getBody(SharedEvent.class);// If there is no timestamp in event, set to system time@@ -241,23 +268,9 @@ public class ClientEventService implements ContainerService {.process(exchange -> {String sessionKey = getSessionKey(exchange);EventSubscription<?> subscription = exchange.getIn().getBody(EventSubscription.class);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);- boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext);- boolean anonymousUser = authContext == null;- String username = authContext == null ? "anonymous" : authContext.getUsername();- String realm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);-- if (authorizeEventSubscription(realm, authContext, subscription)) {- eventSubscriptions.createOrUpdate(sessionKey, restrictedUser, anonymousUser, subscription);- subscription.setSubscribed(true);- sendToSession(sessionKey, subscription);- } else {- LOG.warning("Unauthorized subscription from '"- + username + "' in realm '" + realm- + "': " + subscription- );- sendToSession(sessionKey, new UnauthorizedEventSubscription<>(subscription));- }+ eventSubscriptions.createOrUpdate(sessionKey, subscription);+ subscription.setSubscribed(true);+ sendToSession(sessionKey, subscription);}).stop().when(body().isInstanceOf(CancelEventSubscription.class))@@ -268,7 +281,7 @@ public class ClientEventService implements ContainerService {.stop().when(body().isInstanceOf(SharedEvent.class)).choice()- .when(header(HEADER_CONNECTION_TYPE).isNotNull()) // Inbound messages from clients+ .when(header(HEADER_CONNECTION_TYPE).isNotNull()) // Inbound messages from client.to(ClientEventService.CLIENT_EVENT_TOPIC).stop().when(header(HEADER_CONNECTION_TYPE).isNull()) // Outbound message to clients@@ -280,7 +293,7 @@ public class ClientEventService implements ContainerService {.stop().endChoice().otherwise()- .process(exchange -> LOG.info("Unsupported message body: " + exchange.getIn().getBody()))+ .process(exchange -> LOG.fine("Unsupported message body: " + exchange.getIn().getBody())).end();}});@@ -288,7 +301,7 @@ public class ClientEventService implements ContainerService {// Add pending internal subscriptionsif (!pendingInternalSubscriptions.isEmpty()) {pendingInternalSubscriptions.forEach(subscription ->- eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, false, subscription));+ eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, subscription));}pendingInternalSubscriptions = null;@@ -310,7 +323,7 @@ public class ClientEventService implements ContainerService {}pendingInternalSubscriptions.add(subscription);} else {- eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, false, subscription);+ eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, subscription);}return subscriptionId;}@@ -345,9 +358,44 @@ public class ClientEventService implements ContainerService {this.eventSubscriptionAuthorizers.add(authorizer);}+ public void addEventAuthorizer(EventAuthorizer authorizer) {+ this.eventAuthorizers.add(authorizer);+ }++ /**+ * This handles basic authorisation checks for clients that want to subscribe to events in the system+ */public boolean authorizeEventSubscription(String realm, AuthContext authContext, EventSubscription<?> subscription) {- return eventSubscriptionAuthorizers.stream()+ boolean authorized = eventSubscriptionAuthorizers.stream().anyMatch(authorizer -> authorizer.authorise(realm, authContext, subscription));++ if (!authorized) {+ if (authContext != null) {+ LOG.info("Client not authorised to subscribe: subscription=" + subscription + ", requestRealm=" + realm + ", username=" + authContext.getAuthenticatedRealmName() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ } else {+ LOG.info("Client not authorised to subscribe: subscription=" + subscription + ", requestRealm=" + realm + ", user=null");+ }+ }++ return authorized;+ }++ /**+ * This handles basic authorisation checks for clients that want to write an event to the system+ */+ public <T extends SharedEvent> boolean authorizeEventWrite(String realm, AuthContext authContext, T event) {+ boolean authorized = eventAuthorizers.stream()+ .anyMatch(authorizer -> authorizer.authorise(realm, authContext, event));++ if (!authorized) {+ if (authContext != null) {+ LOG.info("Client not authorised to send event: type=" + event.getEventType() + ", requestRealm=" + realm + ", username=" + authContext.getAuthenticatedRealmName() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ } else {+ LOG.info("Client not authorised to send event: type=" + event.getEventType() + ", requestRealm=" + realm + ", user=null");+ }+ }++ return authorized;}public <T extends SharedEvent> void publishEvent(T event) {@@ -371,7 +419,7 @@ public class ClientEventService implements ContainerService {LOG.finer("Sending to session '" + sessionKey + "': " + data);SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);if (sessionInfo == null) {- LOG.info("Cannot send to requested session it doesn't exist or is disconnected");+ LOG.fine("Cannot send to requested session it doesn't exist or is disconnected");return;}if (sessionInfo.connectionType.equals(HEADER_CONNECTION_TYPE_WEBSOCKET)) {diff --git a/manager/src/main/java/org/openremote/manager/event/EventAuthorizer.java b/manager/src/main/java/org/openremote/manager/event/EventAuthorizer.javanew file mode 100644index 000000000..b01a513e2--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/event/EventAuthorizer.java@@ -0,0 +1,32 @@+/*+ * 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.event;++import org.openremote.container.security.AuthContext;+import org.openremote.model.event.shared.EventSubscription;+import org.openremote.model.event.shared.SharedEvent;++/**+ * Authorizes events sent from clients.+ */+@FunctionalInterface+public interface EventAuthorizer {+ boolean authorise(String requestRealm, AuthContext authContext, SharedEvent event);+}diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.javaindex 610f09577..726ff82cf 100644--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java@@ -46,7 +46,7 @@ public class EventSubscriptions {final protected ConcurrentMap<String, SessionSubscriptions> sessionSubscriptionIdMap = new ConcurrentHashMap<>();class SessionSubscriptions extends HashSet<SessionSubscription<?>> {- protected void createOrUpdate(boolean restrictedUser, boolean anonymousUser, EventSubscription<?> eventSubscription) {+ protected void createOrUpdate(EventSubscription<?> eventSubscription) {if (TextUtil.isNullOrEmpty(eventSubscription.getSubscriptionId())) {cancelByType(eventSubscription.getEventType());@@ -54,7 +54,7 @@ public class EventSubscriptions {cancelById(eventSubscription.getSubscriptionId());}- add(new SessionSubscription<>(restrictedUser, anonymousUser, timerService.getCurrentTimeMillis(), eventSubscription));+ add(new SessionSubscription<>(timerService.getCurrentTimeMillis(), eventSubscription));}protected void cancelByType(String eventType) {@@ -67,35 +67,30 @@ public class EventSubscriptions {}static class SessionSubscription<T extends SharedEvent> {- boolean restrictedUser;- boolean anonymousUser;long timestamp;final EventSubscription<T> subscription;final String subscriptionId;- public SessionSubscription(boolean restrictedUser, boolean anonymousUser, long timestamp, EventSubscription<T> subscription) {- this.restrictedUser = restrictedUser;- this.anonymousUser = anonymousUser;+ public SessionSubscription(long timestamp, EventSubscription<T> subscription) {this.timestamp = timestamp;this.subscription = subscription;this.subscriptionId = subscription.getSubscriptionId();}public boolean matches(SharedEvent event) {- return (!restrictedUser || event.canAccessRestrictedRead()) && (!anonymousUser || event.canAccessPublicRead()) && subscription.getEventType().equals(event.getEventType());+ return subscription.getEventType().equals(event.getEventType());}}public EventSubscriptions(TimerService timerService) {- LOG.info("Starting background task checking for expired event subscriptions from clients");this.timerService = timerService;}- protected void createOrUpdate(String sessionKey, boolean restrictedUser, boolean anonymousUser, EventSubscription<?> subscription) {+ protected void createOrUpdate(String sessionKey, EventSubscription<?> subscription) {LOG.finer("For session '" + sessionKey + "', creating/updating: " + subscription);SessionSubscriptions sessionSubscriptions =this.sessionSubscriptionIdMap.computeIfAbsent(sessionKey, k -> new SessionSubscriptions());- sessionSubscriptions.createOrUpdate(restrictedUser, anonymousUser, subscription);+ sessionSubscriptions.createOrUpdate(subscription);}protected void cancel(String sessionKey, CancelEventSubscription subscription) {@@ -132,13 +127,7 @@ public class EventSubscriptions {if (event == null)return messageList;- Set<Map.Entry<String, SessionSubscriptions>> sessionSubscriptionsSet;-- synchronized (this.sessionSubscriptionIdMap) {- sessionSubscriptionsSet = new HashSet<>(sessionSubscriptionIdMap.entrySet());- }-- for (Map.Entry<String, SessionSubscriptions> entry : sessionSubscriptionsSet) {+ for (Map.Entry<String, SessionSubscriptions> entry : sessionSubscriptionIdMap.entrySet()) {String sessionKey = entry.getKey();SessionSubscriptions subscriptions = entry.getValue();@@ -148,9 +137,9 @@ public class EventSubscriptions {continue;SessionSubscription<T> sessionSub = (SessionSubscription<T>) sessionSubscription;+ T filteredEvent = sessionSub.subscription.getFilter() == null ? event : sessionSub.subscription.getFilter().apply(event);- if (sessionSub.subscription.getFilter() == null- || sessionSub.subscription.getFilter().apply(event)) {+ if (filteredEvent != null) {LOG.finer("Creating message for subscribed session '" + sessionKey + "': " + event);List<T> events = Collections.singletonList(event);TriggeredEventSubscription<T> triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSub.subscriptionId);diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.javaindex 5edb28d28..264715a19 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java@@ -158,7 +158,7 @@ public class GatewayConnector {this.disconnectRunnable = disconnectRunnable;initialSyncInProgress = true;- LOG.info("Gateway connector starting: Gateway ID=" + gatewayId);+ LOG.fine("Gateway connector starting: Gateway ID=" + gatewayId);assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTING), AttributeEvent.Source.GATEWAY);// Reinitialise state@@ -180,7 +180,7 @@ public class GatewayConnector {return;}- LOG.info("Gateway connector disconnected: Gateway ID=" + gatewayId);+ LOG.fine("Gateway connector disconnected: Gateway ID=" + gatewayId);this.gatewayMessageConsumer = null;Runnable disconnectRunnable = this.disconnectRunnable;@@ -228,7 +228,7 @@ public class GatewayConnector {if (isConnected()) {disconnect();}- LOG.info("Gateway connector disabled: Gateway ID=" + gatewayId);+ LOG.fine("Gateway connector disabled: Gateway ID=" + gatewayId);assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISABLED), AttributeEvent.Source.GATEWAY);} else {LOG.info("Gateway connector enabled: Gateway ID=" + gatewayId);diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.javaindex 878b7c48d..699383890 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java@@ -23,7 +23,6 @@ import org.apache.camel.Exchange;import org.apache.camel.Predicate;import org.apache.camel.builder.RouteBuilder;import org.openremote.container.message.MessageBrokerService;-import org.openremote.model.PersistenceEvent;import org.openremote.container.web.ConnectionConstants;import org.openremote.manager.asset.AssetProcessingException;import org.openremote.manager.asset.AssetProcessingService;@@ -34,15 +33,21 @@ import org.openremote.manager.rules.RulesService;import org.openremote.manager.rules.RulesetStorageService;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.ContainerService;+import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.impl.GatewayAsset;-import org.openremote.model.attribute.*;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeMap;+import org.openremote.model.attribute.AttributeWriteFailure;import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.gateway.GatewayDisconnectEvent;import org.openremote.model.query.AssetQuery;import org.openremote.model.rules.Ruleset;+import org.openremote.model.security.ClientRole;import org.openremote.model.security.Realm;import org.openremote.model.security.User;import org.openremote.model.syslog.SyslogCategory;@@ -72,6 +77,7 @@ import static org.openremote.model.syslog.SyslogCategory.GATEWAY;*/public class GatewayService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {+ // Need a high priority so that event authorizer can get the events before other authorizerspublic static final int PRIORITY = HIGH_PRIORITY + 100;public static final String GATEWAY_CLIENT_ID_PREFIX = "gateway-";private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, GatewayService.class.getName());@@ -164,6 +170,19 @@ public class GatewayService extends RouteBuilder implements ContainerService, AsidentityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();container.getService(MessageBrokerService.class).getContext().addRoutes(this);clientEventService.addExchangeInterceptor(this::onMessageIntercept);++ // Gateways can send AssetsEvents into this central manager so we need to authorize those+ clientEventService.addEventAuthorizer((requestRealm, authContext, event) -> {++ if (authContext == null) {+ return false;+ }++ String clientId = authContext.getClientId();++ // TODO: Introduce gateway realm role+ return isGatewayClientId(clientId);+ });}}@@ -616,11 +635,22 @@ public class GatewayService extends RouteBuilder implements ContainerService, AsString secret = gateway.getClientSecret().orElseGet(() -> UUID.randomUUID().toString());try {- identityProvider.createUpdateUser(gateway.getRealm(), new User()- .setServiceAccount(true)- .setSystemAccount(true)- .setUsername(clientId)- .setEnabled(!gateway.getDisabled().orElse(false)), secret);+ User gatewayUser = identityProvider.getUserByUsername(gateway.getRealm(), User.SERVICE_ACCOUNT_PREFIX + clientId);+ boolean userExists = gatewayUser != null;++ if (gatewayUser == null || gatewayUser.getEnabled() == gateway.getDisabled().orElse(false) || Objects.equals(gatewayUser.getSecret(), gateway.getClientSecret().orElse(null))) {++ gatewayUser = identityProvider.createUpdateUser(gateway.getRealm(), new User()+ .setServiceAccount(true)+ .setSystemAccount(true)+ .setUsername(clientId)+ .setEnabled(!gateway.getDisabled().orElse(false)), secret);+ }++ if (!userExists && gatewayUser != null) {+ // Configure roles for this gateway user+ identityProvider.updateUserRoles(gateway.getRealm(), gatewayUser.getId(), Constants.KEYCLOAK_CLIENT_ID, ClientRole.WRITE.getValue());+ }if (!clientId.equals(gateway.getClientId().orElse(null)) || !secret.equals(gateway.getClientSecret().orElse(null))) {gateway.setClientId(clientId);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javaindex c6c199294..cfad8cd37 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -42,6 +42,7 @@ import java.util.logging.Level;import java.util.logging.Logger;import static org.apache.activemq.artemis.core.remoting.CertificateUtil.getCertsFromConnection;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;import static org.openremote.model.syslog.SyslogCategory.API;/**@@ -129,8 +130,6 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {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 firedremotingConnection.setSubject(subject);}@@ -162,18 +161,19 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {@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 addresstopic = Topic.fromAddress(address, brokerService.getWildcardConfiguration());} catch (IllegalArgumentException e) {- LOG.log(Level.WARNING, "Invalid topic provided by client '" + address + "', connection=" + connection, e);+ LOG.log(Level.INFO, "Invalid topic provided by client '" + address, e);return false;}+ KeycloakSecurityContext securityContext = KeycloakIdentityProvider.getSecurityContext(subject);+ RemotingConnection connection = brokerService.getConnectionFromSubjectAndTopic(subject, topic);+if (isWrite && topic.hasWildcard()) {return false;}@@ -181,7 +181,7 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {// See if a custom handler wants to handle authorisation for this topic pub/subfor (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);+ LOG.fine("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", " + connectionToString(connection));boolean result;if (isWrite) {@@ -189,11 +189,18 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {} else {result = handler.checkCanSubscribe(connection, securityContext, topic);}+ if (LOG.isLoggable(Level.FINE)) {+ if (result) {+ LOG.fine("Handler has authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(connection));+ } else {+ LOG.fine("Handler has not authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(connection));+ }+ }return result;}}- LOG.fine("No handler has allowed " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", connection=" + connection);+ LOG.fine("No handler has allowed " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(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 b22757beb..fa812ba36 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -20,7 +20,6 @@package org.openremote.manager.mqtt;import io.netty.buffer.ByteBuf;-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;@@ -41,11 +40,14 @@ import org.openremote.model.util.ValueUtil;import java.nio.charset.StandardCharsets;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.atomic.AtomicInteger;import java.util.function.Consumer;import java.util.function.Function;import java.util.logging.Logger;import java.util.regex.Pattern;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;import static org.openremote.model.Constants.ASSET_ID_REGEXP;import static org.openremote.model.syslog.SyslogCategory.API;@@ -58,11 +60,11 @@ public class DefaultMQTTHandler extends MQTTHandler {public static final int PRIORITY = Integer.MIN_VALUE + 1000;public static final String ASSET_TOPIC = "asset";public static final String ATTRIBUTE_TOPIC = "attribute";- public static final String ATTRIBUTE_WRITE_TOPIC = "writeattribute";public static final String ATTRIBUTE_VALUE_TOPIC = "attributevalue";public static final String ATTRIBUTE_VALUE_WRITE_TOPIC = "writeattributevalue";private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);protected AssetStorageService assetStorageService;+ protected final Map<RemotingConnection, Integer> subscribedConnections = new HashMap<>();@Overridepublic int getPriority() {@@ -79,11 +81,15 @@ public class DefaultMQTTHandler extends MQTTHandler {@Overridepublic boolean onConnect(RemotingConnection connection) {super.onConnect(connection);- Map<String, Object> headers = prepareHeaders(connection);+ Map<String, Object> headers = prepareHeaders(null, connection);headers.put(ConnectionConstants.SESSION_OPEN, true);// Put a close connection runnable into the headers for the client event service- Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection);+ Runnable closeRunnable = () -> {+ if (mqttBrokerService != null) {+ mqttBrokerService.doForceDisconnect(connection);+ }+ };headers.put(ConnectionConstants.SESSION_TERMINATOR, closeRunnable);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);return true;@@ -93,22 +99,24 @@ public class DefaultMQTTHandler extends MQTTHandler {public void onDisconnect(RemotingConnection connection) {super.onDisconnect(connection);- Map<String, Object> headers = prepareHeaders(connection);+ Map<String, Object> headers = prepareHeaders(null, connection);headers.put(ConnectionConstants.SESSION_CLOSE, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);+ subscribedConnections.remove(connection);}@Overridepublic void onConnectionLost(RemotingConnection connection) {super.onConnectionLost(connection);- Map<String, Object> headers = prepareHeaders(connection);+ Map<String, Object> headers = prepareHeaders(null, connection);headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);+ subscribedConnections.remove(connection);}@Overridepublic boolean topicMatches(Topic topic) {- return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeWriteTopic(topic) || isAttributeValueWriteTopic(topic);+ return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeValueWriteTopic(topic);}@Override@@ -128,7 +136,7 @@ public class DefaultMQTTHandler extends MQTTHandler {AuthContext authContext = getAuthContextFromSecurityContext(securityContext);if (authContext == null) {- LOG.fine("Anonymous connection not supported: topic=" + topic + ", connection=" + connection);+ LOG.fine("Anonymous connection not supported: topic=" + topic + ", " + connectionToString(connection));return false;}@@ -136,58 +144,58 @@ public class DefaultMQTTHandler extends MQTTHandler {boolean isAssetTopic = isAssetTopic(topic);if (!isAssetTopic && !isAttributeTopic) {- LOG.fine("Topic must have 3 or more tokens and third token must be 'asset, attribute or attributevalue': topic=" + topic + ", connection=" + connection);+ LOG.fine("Topic must have 3 or more tokens and third token must be 'asset, attribute or attributevalue': topic=" + topic + ", " + connectionToString(connection));return false;}if (isAssetTopic) {if (topic.getTokens().size() < 4 || topic.getTokens().size() > 5) {- LOG.fine("Asset subscribe token count should be 4 or 5: topic=" + topic + ", connection=" + connection);+ LOG.fine("Asset subscribe token count should be 4 or 5: topic=" + topic + ", " + connectionToString(connection));return false;}if (topic.getTokens().size() == 4) {if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 3))&& !TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))&& !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))) {- LOG.fine("Asset subscribe forth token must be an asset ID or wildcard: topic=" + topic + ", connection=" + connection);+ LOG.fine("Asset subscribe forth token must be an asset ID or wildcard: topic=" + topic + ", " + connectionToString(connection));return false;}} else if (topic.getTokens().size() == 5) {if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 3))) {- LOG.fine("Asset subscribe forth token must be an asset ID: topic=" + topic + ", connection=" + connection);+ LOG.fine("Asset subscribe forth token must be an asset ID: topic=" + topic + ", " + connectionToString(connection));return false;}if (!TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))&& !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))) {- LOG.fine("Asset subscribe fifth token must be a wildcard: topic=" + topic + ", connection=" + connection);+ LOG.fine("Asset subscribe fifth token must be a wildcard: topic=" + topic + ", " + connectionToString(connection));return false;}}} else {// Attribute topicif (topic.getTokens().size() < 5 || topic.getTokens().size() > 6) {- LOG.fine("Attribute subscribe token count should be 5 or 6: topic=" + topic + ", connection=" + connection);+ LOG.fine("Attribute subscribe token count should be 5 or 6: topic=" + topic + ", " + connectionToString(connection));return false;}if (topic.getTokens().size() == 5) {if (TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))) {- LOG.fine("Attribute subscribe multi level wildcard must be last token: topic=" + topic + ", connection=" + connection);+ LOG.fine("Attribute subscribe multi level wildcard must be last token: topic=" + topic + ", " + connectionToString(connection));return false;}if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))&& !TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))&& !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))) {- LOG.fine("Attribute subscribe fifth token must be an asset ID or a wildcard: topic=" + topic + ", connection=" + connection);+ LOG.fine("Attribute subscribe fifth token must be an asset ID or a wildcard: topic=" + topic + ", " + connectionToString(connection));return false;}} else if (topic.getTokens().size() == 6) {if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {- LOG.fine("Attribute subscribe fifth token must be an asset ID: topic=" + topic + ", connection=" + connection);+ LOG.fine("Attribute subscribe fifth token must be an asset ID: topic=" + topic + ", " + connectionToString(connection));return false;}if (!TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 5))&& !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 5))) {- LOG.fine("Attribute subscribe sixth token must be a wildcard: topic=" + topic + ", connection=" + connection);+ LOG.fine("Attribute subscribe sixth token must be a wildcard: topic=" + topic + ", " + connectionToString(connection));return false;}}@@ -197,7 +205,7 @@ public class DefaultMQTTHandler extends MQTTHandler {AssetFilter<?> filter = buildAssetFilter(topic);if (filter == null) {- LOG.info("Failed to process subscription topic: topic=" + topic + ", connection=" + connection);+ LOG.fine("Failed to process subscription topic: topic=" + topic + ", " + connectionToString(connection));return false;}@@ -207,7 +215,7 @@ public class DefaultMQTTHandler extends MQTTHandler {);if (!clientEventService.authorizeEventSubscription(topicRealm(topic), authContext, subscription)) {- LOG.info("Subscription was not authorised for this user and topic: topic=" + topic + ", subject=" + authContext);+ LOG.fine("Subscription was not authorised for this user and topic: topic=" + topic + ", subject=" + authContext);return false;}@@ -248,25 +256,27 @@ public class DefaultMQTTHandler extends MQTTHandler {AuthContext authContext = getAuthContextFromSecurityContext(securityContext);if (authContext == null) {- LOG.fine("Anonymous publish not supported: topic=" + topic + ", connection=" + connection.getTransportConnection());+ LOG.fine("Anonymous publish not supported: topic=" + topic + ", connection=" + connectionToString(connection));return false;}- if (isAttributeWriteTopic(topic)) {- if (topic.getTokens().size() != 3) {- LOG.fine("Publish attribute events topic should be {realm}/{clientId}/writeattribute: topic=" + topic + ", connection=" + connection);- return false;- }- return true;- } else if (isAttributeValueWriteTopic(topic)) {+ if (isAttributeValueWriteTopic(topic)) {if (topic.getTokens().size() != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {- LOG.fine("Publish attribute value topic should be {realm}/{clientId}/writeattributevalue/{attributeName}/{assetId}: topic=" + topic + ", connection=" + connection);+ LOG.fine("Publish attribute value topic should be {realm}/{clientId}/writeattributevalue/{attributeName}/{assetId}: topic=" + topic + ", connection=" + connectionToString(connection));return false;}- return true;+ } else {+ return false;+ }++ // We don't know the value at this point so just use a null value for authorization (value type will be handled+ // when the event is processed)+ if (!clientEventService.authorizeEventWrite(topicRealm(topic), authContext, buildAttributeEvent(topic.getTokens(), null))) {+ LOG.info("Publish was not authorised for this user and topic: topic=" + topic + ", subject=" + authContext);+ return false;}- return false;+ return true;}@SuppressWarnings({"rawtypes", "unchecked"})@@ -292,25 +302,44 @@ public class DefaultMQTTHandler extends MQTTHandler {eventConsumer);- Map<String, Object> headers = prepareHeaders(connection);+ Map<String, Object> headers = prepareHeaders(topicRealm(topic), connection);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);++ // Track connection subscriptions for restricted user asset link changes (to determine if the client should be disconnected)+ synchronized (subscribedConnections) {+ subscribedConnections.compute(connection, (existingConnection, subscriptionCount) -> {+ if (subscriptionCount == null) {+ subscriptionCount = 0;+ }+ return subscriptionCount + 1;+ });+ }}- @SuppressWarnings({"unchecked", "rawtypes"})+ @SuppressWarnings({"unchecked", "rawtypes", "ConstantConditions"})@Overridepublic void onUnsubscribe(RemotingConnection connection, Topic topic) {String subscriptionId = topic.toString();boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);- Map<String, Object> headers = prepareHeaders(connection);+ Map<String, Object> headers = prepareHeaders(topicRealm(topic), connection);Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);++ // Track connection subscriptions for restricted user asset link changes (to determine if the client should be disconnected)+ synchronized (subscribedConnections) {+ subscribedConnections.compute(connection, (existingConnection, subscriptionCount) -> {+ if (subscriptionCount == 1) {+ return null;+ }+ return subscriptionCount - 1;+ });+ }}@Overridepublic 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);}@@ -318,26 +347,25 @@ public class DefaultMQTTHandler extends MQTTHandler {@Overridepublic 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 = body.toString(StandardCharsets.UTF_8);- AttributeEvent attributeEvent;-- if (isValueWrite) {- String attributeName = topicTokens.get(3);- String assetId = topicTokens.get(4);- Object value = ValueUtil.parse(payloadContent).orElse(null);- attributeEvent = new AttributeEvent(assetId, attributeName, value);- } else {- attributeEvent = ValueUtil.parse(payloadContent, AttributeEvent.class).orElse(null);- }+ Object value = ValueUtil.parse(payloadContent).orElse(null);+ AttributeEvent attributeEvent = buildAttributeEvent(topicTokens, value);+ Map<String, Object> headers = prepareHeaders(topicRealm(topic), connection);+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);+ }- if (attributeEvent == null) {- LOG.fine("Failed to parse payload for publish topic '" + topic + "': " + connection);- return;+ @Override+ public void onUserAssetLinksChanged(RemotingConnection connection) {+ if (subscribedConnections.containsKey(connection)) {+ LOG.fine("User asset links have changed for a connected user with active subscriptions so force disconnecting them: " + connectionToString(connection));+ mqttBrokerService.doForceDisconnect(connection);}+ }- Map<String, Object> headers = prepareHeaders(connection);- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);+ protected static AttributeEvent buildAttributeEvent(List<String> topicTokens, Object value) {+ String attributeName = topicTokens.get(3);+ String assetId = topicTokens.get(4);+ return new AttributeEvent(assetId, attributeName, value);}protected static AssetFilter<?> buildAssetFilter(Topic topic) {@@ -479,10 +507,6 @@ public class DefaultMQTTHandler extends MQTTHandler {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));}@@ -491,13 +515,11 @@ public class DefaultMQTTHandler extends MQTTHandler {return ASSET_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));}- protected static Map<String, Object> prepareHeaders(RemotingConnection connection) {- Optional<AuthContext> authContext = getAuthContextFromConnection(connection);+ protected static Map<String, Object> prepareHeaders(String requestRealm, RemotingConnection 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));+ headers.put(Constants.REALM_PARAM_NAME, requestRealm);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.javaindex 273914559..4f413c72b 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java@@ -57,6 +57,7 @@ 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.Constants;import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.PersistenceEvent;@@ -87,6 +88,8 @@ import static org.openremote.model.syslog.SyslogCategory.API;public class MQTTBrokerService extends RouteBuilder implements ContainerService, ActiveMQServerConnectionPlugin, ActiveMQServerSessionPlugin {+ public static final String MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS = "MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS";+ public static int MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS_DEFAULT = 5000;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";@@ -111,6 +114,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,protected String host;protected int port;protected EmbeddedActiveMQ server;+ protected ClientProducer producer;protected ClientSessionInternal internalSession;@Override@@ -122,14 +126,14 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,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);-+ int debounceMillis = getInteger(container.getConfig(), MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS, MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS_DEFAULT);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);+ userAssetDisconnectDebouncer = new Debouncer<>(executorService, this::processUserAssetLinkChange, debounceMillis);if (!identityService.isKeycloakEnabled()) {LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");@@ -148,11 +152,6 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,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 brokerConfiguration config = new ConfigurationImpl();config.addAcceptorConfiguration("in-vm", "vm://0?protocols=core");@@ -185,10 +184,8 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}));server.start();-LOG.fine("Started MQTT broker");-// Add notification handler for subscribe/unsubscribe and publish eventsserver.getActiveMQServer().getManagementService().addNotificationListener(notification -> {if (notification.getType() == CoreNotificationType.CONSUMER_CREATED || notification.getType() == CoreNotificationType.CONSUMER_CLOSED) {@@ -220,6 +217,14 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,serverSession.disableSecurity();internalSession.start();+ // Create producer+ producer = internalSession.createProducer();++ // Load custom handlers+ this.customHandlers = stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)+ .sorted(Comparator.comparingInt(MQTTHandler::getPriority))+ .collect(Collectors.toList());+// Start each custom handlerfor (MQTTHandler handler : customHandlers) {try {@@ -257,15 +262,14 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}if (forceDisconnect) {- LOG.info("User modified or deleted so force closing any sessions for this user: " + user);+ LOG.fine("User modified or deleted so force closing any sessions for this user: " + user);// Find existing connection for this user- forceDisconnectUser(user.getId());+ getUserConnections(user.getId()).forEach(this::doForceDisconnect);}} else if (persistenceEvent.getEntity() instanceof UserAssetLink userAssetLink) {String userID = userAssetLink.getId().getUserId();-- // Debounce force disconnect of this user's sessions+ // Debounce force disconnect check of this user's sessions as there could be many asset links changinguserAssetDisconnectDebouncer.call(userID);}});@@ -273,6 +277,9 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,@Overridepublic void stop(Container container) throws Exception {++ userAssetDisconnectDebouncer.cancelAll(true);+server.stop();LOG.fine("Stopped MQTT broker");@@ -290,6 +297,8 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,@Overridepublic void afterCreateConnection(RemotingConnection connection) throws ActiveMQException {+ LOG.fine("New client connection: "+ connectionToString(connection));+// MQTT seems to only use failure callback even if closed gracefully (need to look at the exception for details)connection.addFailureListener(new FailureListener() {@Override@@ -329,7 +338,6 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}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@@ -341,7 +349,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,@Overridepublic void afterDestroyConnection(RemotingConnection connection) throws ActiveMQException {-+ LOG.fine("Destroyed client connection: " + connectionToString(connection));}public void onSubscribe(RemotingConnection connection, String topicStr) {@@ -349,7 +357,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,for (MQTTHandler handler : getCustomHandlers()) {if (handler.handlesTopic(topic)) {- LOG.fine("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ LOG.fine("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", " + connectionToString(connection));handler.onSubscribe(connection, topic);break;}@@ -361,7 +369,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,for (MQTTHandler handler : getCustomHandlers()) {if (handler.handlesTopic(topic)) {- LOG.fine("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);+ LOG.fine("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", " + connectionToString(connection));handler.onUnsubscribe(connection, topic);break;}@@ -372,15 +380,26 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return customHandlers;}- public Runnable getForceDisconnectRunnable(RemotingConnection connection) {- return () -> doForceDisconnect(connection);- }-- public void forceDisconnectUser(String userID) {+ public void processUserAssetLinkChange(String userID) {if (TextUtil.isNullOrEmpty(userID)) {return;}- getUserConnections(userID).forEach(this::doForceDisconnect);++ // Check if user has any active connections+ Set<RemotingConnection> userConnections = getUserConnections(userID);+ Subject subject = userConnections.stream().filter(connection -> connection.getSubject() != null).findFirst().map(RemotingConnection::getSubject).orElse(null);++ // Only notify handlers if subject is a restricted user+ if (subject != null && KeycloakIdentityProvider.getSecurityContext(subject).getToken().getRealmAccess().isUserInRole(Constants.RESTRICTED_USER_REALM_ROLE)) {+ LOG.fine("User asset links modified for connected restricted user so passing to handlers to decide what to do: user=" + subject);+ // Pass to handlers to decide what to do+ userConnections.forEach(connection -> {+ for (MQTTHandler handler : customHandlers) {+ connection.setSubject(subject);+ handler.onUserAssetLinksChanged(connection);+ }+ });+ }}public Set<RemotingConnection> getUserConnections(String userID) {@@ -403,18 +422,17 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}protected void doForceDisconnect(RemotingConnection connection) {- LOG.fine("Force disconnecting session: " + connection);+ LOG.fine("Force disconnecting client connection: " + connectionToString(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);+ producer.send(MQTTUtil.convertMqttTopicFilterToCoreAddress(topic, server.getConfiguration().getWildcardConfiguration()), message);}} catch (Exception e) {LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);@@ -434,6 +452,13 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return ID instanceof ChannelId ? ((ChannelId)ID).asLongText() : ID.toString();}+ public static String connectionToString(RemotingConnection connection) {+ if (connection == null) {+ return "";+ }+ return "connection=" + connection.getRemoteAddress() + ", clientID=" + connection.getClientID() + ", subject=" + connection.getSubject();+ }+public RemotingConnection getConnectionFromClientID(String clientID) {if (TextUtil.isNullOrEmpty(clientID)) {return null;@@ -444,9 +469,9 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,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();+ for (RemotingConnection remotingConnection : server.getActiveMQServer().getRemotingService().getConnections()) {+ if (Objects.equals(clientID, remotingConnection.getClientID())) {+ connection = remotingConnection;clientIDConnectionMap.put(clientID, connection);break;}@@ -456,6 +481,25 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return connection;}+ // TODO: Remove this if/when ActiveMQ implements https://issues.apache.org/jira/browse/ARTEMIS-4059+ /**+ * Tries to find the correct {@link RemotingConnection} for the specified subject with the assumption that the+ * client ID is contained in the address (this is important for multiple connections from the same subject)+ */+ protected RemotingConnection getConnectionFromSubjectAndTopic(Subject subject, Topic topic) {+ String clientID = MQTTHandler.topicClientID(topic);++ if (clientID == null) {+ return null;+ }++ return server.getActiveMQServer().getRemotingService().getConnections().stream().filter(connection ->+ connection.getSubject() == subject && Objects.equals(clientID, connection.getClientID())+ )+ .findFirst()+ .orElse(null);+ }+// TODO: Remove thispublic void addTransientCredentials(RemotingConnection connection, Triple<String, String, String> userIDNameAndPassword) {LOG.fine("Adding transient user credentials: connection ID=" + connection.getID() + " ,username=" + userIDNameAndPassword.getMiddle());diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.javadeleted file mode 100644index 4dfc4e4b8..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTConnectionPrincipal.java+++ /dev/null@@ -1,56 +0,0 @@-/*- * 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 3f88c076e..f33c33269 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java@@ -36,7 +36,6 @@ 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.util.Pair;import javax.security.auth.Subject;import java.util.Objects;@@ -45,6 +44,8 @@ import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;+/*** 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@@ -100,7 +101,7 @@ public abstract class MQTTHandler {protected void addPublishConsumer(String topic) {try {- getLogger().info("Adding publish consumer for topic '" + topic + "': handler=" + getName());+ getLogger().fine("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);@@ -166,11 +167,11 @@ public abstract class MQTTHandler {*/public boolean checkCanSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (securityContext == null) {- getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic);+ getLogger().finest("Anonymous connection subscriptions not supported by this handler, topic=" + topic + ", " + connectionToString(connection));return false;}if (!topicRealmAllowed(securityContext, topic) || !topicClientIdMatches(connection, topic)) {- getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", user=" + securityContext);+ getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", " + connectionToString(connection));return false;}return canSubscribe(connection, securityContext, topic);@@ -182,16 +183,23 @@ public abstract class MQTTHandler {*/public boolean checkCanPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {if (securityContext == null) {- getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic);+ getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic + ", " + connectionToString(connection));return false;}if (!topicRealmAllowed(securityContext, topic) || !topicClientIdMatches(connection, topic)) {- getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", user=" + securityContext);+ getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", " + connectionToString(connection));return false;}return canPublish(connection, securityContext, topic);}+ /**+ * Called when {@link org.openremote.model.asset.UserAssetLink}s for a restricted user are changed and that user+ * has an active connection (subject can be accessed from the connection).+ */+ public void onUserAssetLinksChanged(RemotingConnection connection) {+ }+/*** Indicates if this handler will handle the specified topic; independent of whether it is a publish or subscribe.* Should generally check the third token onwards unless {@link #handlesTopic} has been overridden.@@ -237,12 +245,16 @@ public abstract class MQTTHandler {return topicTokenIndexToString(topic, 0);}+ public static String topicClientID(Topic topic) {+ return topicTokenIndexToString(topic, 1);+ }+public static boolean topicRealmAllowed(KeycloakSecurityContext securityContext, Topic topic) {- return securityContext.getRealm().equals(topicRealm(topic)) || KeycloakIdentityProvider.isSuperUser(securityContext);+ return securityContext != null && securityContext.getRealm().equals(topicRealm(topic)) || KeycloakIdentityProvider.isSuperUser(securityContext);}public static boolean topicClientIdMatches(RemotingConnection connection, Topic topic) {- return Objects.equals(connection.getClientID(), topicTokenIndexToString(topic, 1));+ return connection != null && Objects.equals(connection.getClientID(), topicTokenIndexToString(topic, 1));}public static boolean topicTokenCountGreaterThan(Topic topic, int size) {@@ -257,15 +269,6 @@ public abstract class MQTTHandler {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);}diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex 897f5bbc3..0095650ac 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -197,7 +197,7 @@ public class EmailNotificationHandler implements NotificationHandler {}).toList();if (userTargets.isEmpty()) {- LOG.info("No email targets have been mapped");+ LOG.fine("No email targets have been mapped");} else {mappedTargets.addAll(userTargetsdiff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java b/manager/src/main/java/org/openremote/manager/notification/NotificationService.javaindex 0896d670f..d8253840f 100644--- a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationService.java@@ -220,7 +220,7 @@ public class NotificationService extends RouteBuilder implements ContainerServicbreak;}- LOG.info("Sending " + notification.getMessage().getType() + " notification '" + notification.getName() + "': '" + source + ":" + sourceId.get() + "' -> " + notification.getTargets());+ LOG.fine("Sending " + notification.getMessage().getType() + " notification '" + notification.getName() + "': '" + source + ":" + sourceId.get() + "' -> " + notification.getTargets());// Check access permissionscheckAccess(source, sourceId.get(), notification.getTargets(), realm, userId, isSuperUser, isRestrictedUser, assetId);@@ -269,7 +269,7 @@ public class NotificationService extends RouteBuilder implements ContainerServicnotification.getMessage());if (result.isSuccess()) {- LOG.info("Notification sent '" + id + "': " + target);+ LOG.fine("Notification sent '" + id + "': " + target);} else {LOG.warning("Notification failed '" + id + "': " + target + ", reason=" + result.getMessage());sentNotification.setError(TextUtil.isNullOrEmpty(result.getMessage()) ? "Unknown error" : result.getMessage());@@ -403,7 +403,7 @@ public class NotificationService extends RouteBuilder implements ContainerServic}if (isRemove && fromTimestamp == null && toTimestamp == null && counter == 0) {- LOG.info("No filters set for remove notifications request so not allowed");+ LOG.fine("No filters set for remove notifications request so not allowed");throw new IllegalArgumentException("No criteria specified");}diff --git a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.javaindex d01e2534b..01a032f07 100644--- a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java@@ -282,7 +282,7 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio}if (consoleAssetIds.isEmpty()) {- LOG.info("No console asset targets have been mapped");+ LOG.fine("No console asset targets have been mapped");} else {mappedTargets.addAll(consoleAssetIdsdiff --git a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.javaindex 1e936f817..d9ca33588 100644--- a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java@@ -58,6 +58,7 @@ import java.util.logging.Logger;import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;import static org.openremote.model.Constants.RESTRICTED_USER_REALM_ROLE;import static org.openremote.model.syslog.SyslogCategory.API;@@ -218,7 +219,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {ProvisioningMessage provisioningMessage = ValueUtil.parse(payloadContent, ProvisioningMessage.class).orElseGet(() -> {- LOG.fine("Failed to parse message from client: topic=" + topic + ", connection=" + connection);+ LOG.fine("Failed to parse message from client: topic=" + topic+ connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.MESSAGE_INVALID), MqttQoS.AT_MOST_ONCE);return null;});@@ -262,8 +263,10 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {protected void processX509ProvisioningMessage(RemotingConnection connection, Topic topic, X509ProvisioningMessage provisioningMessage) {+ LOG.finer("Processing provisioning message on: topic=" + topic + connectionToString(connection));+if (TextUtil.isNullOrEmpty(provisioningMessage.getCert())) {- LOG.fine("Certificate is missing from X509 provisioning message: topic=" + topic + ", connection=" + connection);+ LOG.info("Certificate is missing from X509 provisioning message: topic=" + topic + connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -273,7 +276,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {try {clientCertificate = ProvisioningUtil.getX509Certificate(provisioningMessage.getCert());} catch (CertificateException e) {- LOG.log(Level.INFO, "Failed to parse client X.509 certificate: topic=" + topic + ", connection=" + connection, e);+ LOG.log(Level.INFO, "Failed to parse client X.509 certificate: topic=" + topic+ connectionToString(connection), e);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -281,14 +284,14 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {X509ProvisioningConfig matchingConfig = getMatchingX509ProvisioningConfig(connection, clientCertificate);if (matchingConfig == null) {- LOG.fine("No matching provisioning config found for client certificate: topic=" + topic + ", connection=" + connection);+ LOG.fine("No matching provisioning config found for client certificate: topic=" + topic+ connectionToString(connection));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);+ LOG.fine("Matching provisioning config is disabled for client certificate: topic=" + topic+ connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CONFIG_DISABLED), MqttQoS.AT_MOST_ONCE);return;}@@ -298,13 +301,13 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {String uniqueId = topicTokenIndexToString(topic, 1);if (TextUtil.isNullOrEmpty(certUniqueId)) {- LOG.info("Client X.509 certificate missing unique ID in subject CN: topic=" + topic + ", connection=" + connection);+ LOG.info("Client X.509 certificate missing unique ID in subject CN: topic=" + topic+ connectionToString(connection));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);+ LOG.info("Client X.509 certificate unique ID doesn't match topic unique ID: topic=" + topic+ connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);return;}@@ -324,7 +327,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (serviceUser != null) {if (!serviceUser.getEnabled()) {- LOG.info("Client service user has been disabled: topic=" + topic + ", connection=" + connection);+ LOG.info("Client service user has been disabled: topic=" + topic+ connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.USER_DISABLED), MqttQoS.AT_MOST_ONCE);return;}@@ -332,7 +335,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {serviceUser = createClientServiceUser(realm, serviceUsername, matchingConfig);}} catch (Exception e) {- LOG.log(Level.WARNING, "Failed to retrieve/create service user: topic=" + topic + ", connection=" + connection, e);+ LOG.log(Level.WARNING, "Failed to retrieve/create service user: topic=" + topic+ connectionToString(connection), e);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);return;}@@ -341,17 +344,17 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {// Prepend realm name to unique ID to generate asset ID to further improve uniquenessString assetId = UniqueIdentifierGenerator.generateId(matchingConfig.getRealm() + uniqueId);- LOG.fine("Client unique ID '" + uniqueId + "' mapped to asset ID '" + assetId + "': topic=" + topic + ", connection=" + connection);+ LOG.fine("Client unique ID '" + uniqueId + "' mapped to asset ID '" + assetId + "': topic=" + topic+ connectionToString(connection));try {// Look for existing assetasset = assetStorageService.find(assetId);if (asset != null) {- LOG.finer("Client asset found: topic=" + topic + ", connection=" + connection + ", assetId=" + assetId);+ LOG.finer("Client asset found: topic=" + topic+ connectionToString(connection) + ", assetId=" + assetId);if (!matchingConfig.getRealm().equals(asset.getRealm())) {- LOG.info("Client asset realm mismatch : topic=" + topic + ", connection=" + connection + ", assetId=" + assetId);+ LOG.info("Client asset realm mismatch : topic=" + topic+ connectionToString(connection) + ", assetId=" + assetId);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.ASSET_ERROR), MqttQoS.AT_MOST_ONCE);return;}@@ -359,12 +362,12 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {asset = createClientAsset(realm, assetId, uniqueId, serviceUser, matchingConfig);}} catch (Exception e) {- LOG.log(Level.WARNING, "Failed to retrieve/create asset: topic=" + topic + ", connection=" + connection + ", config=" + matchingConfig, e);+ LOG.log(Level.WARNING, "Failed to retrieve/create asset: topic=" + topic+ connectionToString(connection) + ", config=" + matchingConfig, e);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);+ LOG.fine("Client successfully initialised: topic=" + topic+ connectionToString(connection) + ", config=" + matchingConfig);// Update connection with service user credentialsmqttBrokerService.addTransientCredentials(connection, new ImmutableTriple<>(serviceUser.getId(), realm + ":" + serviceUser.getUsername(), serviceUser.getSecret()));diff --git a/manager/src/main/java/org/openremote/manager/rules/FlowRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/FlowRulesBuilder.javaindex 430d68ab6..8a020bc1e 100644--- a/manager/src/main/java/org/openremote/manager/rules/FlowRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/FlowRulesBuilder.java@@ -52,7 +52,7 @@ public class FlowRulesBuilder {for (Node node : collection.getNodes()) {if (node.getType() != NodeType.OUTPUT) continue;try {- RulesEngine.RULES_LOG.info("Flow rule created");+ RulesEngine.RULES_LOG.fine("Flow rule created");rules.add(createRule(collection.getName() + " - " + count, collection, node));count++;} catch (Exception e) {diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesResourceImpl.java b/manager/src/main/java/org/openremote/manager/rules/RulesResourceImpl.javaindex a4a404e6f..74ab700d3 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesResourceImpl.java@@ -253,7 +253,7 @@ public class RulesResourceImpl extends ManagerWebResource implements RulesResourthrow new WebApplicationException(BAD_REQUEST);}if (!isRealmActiveAndAccessible(realm) || isRestrictedUser()) {- LOG.info("Forbidden access for user '" + getUsername() + "': " + realm);+ LOG.fine("Realm '" + realm + "' is nonexistent, inactive or inaccessible: username=" + getUsername());throw new WebApplicationException(Response.Status.FORBIDDEN);}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex 2e21cd89c..b344a4685 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -250,7 +250,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Assepublic void start(Container container) throws Exception {if (!geofenceAssetAdapters.isEmpty()) {- LOG.info("GeoefenceAssetAdapters found: " + geofenceAssetAdapters.size());+ LOG.fine("GeoefenceAssetAdapters found: " + geofenceAssetAdapters.size());locationPredicateRulesConsumer = this::onEngineLocationRulesChanged;for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {@@ -258,7 +258,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse}}- LOG.info("Deploying global rulesets");+ LOG.fine("Deploying global rulesets");rulesetStorageService.findAll(GlobalRuleset.class,new RulesetQuery()@@ -266,7 +266,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.setFullyPopulate(true)).forEach(this::deployGlobalRuleset);- LOG.info("Deploying realm rulesets");+ LOG.fine("Deploying realm rulesets");realms = Arrays.stream(identityService.getIdentityProvider().getRealms()).filter(Realm::getEnabled).toArray(Realm[]::new);rulesetStorageService.findAll(RealmRuleset.class,@@ -279,7 +279,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.anyMatch(realm -> rd.getRealm().equals(realm.getName()))).forEach(this::deployRealmRuleset);- LOG.info("Deploying asset rulesets");+ LOG.fine("Deploying asset rulesets");// Group by asset ID then realm and check realm is enabled//noinspection ResultOfMethodCallIgnoreddeployAssetRulesets(@@ -290,7 +290,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.setFullyPopulate(true))).count();//Needed in order to execute the stream. TODO: can this be done differently?- LOG.info("Loading all assets with fact attributes to initialize state of rules engines");+ LOG.fine("Loading all assets with fact attributes to initialize state of rules engines");Stream<Pair<Asset<?>, Stream<Attribute<?>>>> stateAttributes = findRuleStateAttributes();// Push each attribute as an asset update through the rule engine chaindiff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.javaindex 39b683b71..2eef24478 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java@@ -468,12 +468,12 @@ public class RulesetDeployment {NodeCollection nodeCollection = ValueUtil.JSON.readValue(ruleset.getRules(), NodeCollection.class);flowRulesBuilder.add(nodeCollection);for (Rule rule : flowRulesBuilder.build()) {- RulesEngine.LOG.info("Registering rule: " + rule.getName());+ RulesEngine.LOG.info("Compiling flow rule: " + rule.getName());rules.register(rule);}return true;} catch (Exception e) {- RulesEngine.LOG.log(Level.SEVERE, "Error evaluating ruleset: " + ruleset, e);+ RulesEngine.LOG.log(Level.SEVERE, "Error evaluating flow rule ruleset: " + ruleset, e);setError(e);return false;}diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.javaindex 164dafb3e..032b2fb78 100644--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java@@ -173,12 +173,12 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofeif (assetStateLocationPredicates.getLocationPredicates().isEmpty()) {if (assetLocationPredicatesMap.remove(assetStateLocationPredicates.getAssetId()) != null) {- LOG.info("Clearing location predicates for asset: " + assetStateLocationPredicates.getAssetId());+ LOG.fine("Clearing location predicates for asset: " + assetStateLocationPredicates.getAssetId());notifyAssets.add(assetStateLocationPredicates.getAssetId());notifierDebounce.set(true);}} else {- LOG.info("Setting "+ LOG.fine("Setting "+ assetStateLocationPredicates.getLocationPredicates().size()+ " location predicate(s) for asset: " + assetStateLocationPredicates.getAssetId());@@ -187,7 +187,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe}} else if (assetLocationPredicatesMap.remove(assetStateLocationPredicates.getAssetId()) != null) {// Used to be in this map so must have been deleted so ask console to delete its geofences also- LOG.info("Clearing location predicates for asset: " + assetStateLocationPredicates.getAssetId());+ LOG.fine("Clearing location predicates for asset: " + assetStateLocationPredicates.getAssetId());notifyAssets.add(assetStateLocationPredicates.getAssetId());notifierDebounce.set(true);}@@ -215,7 +215,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements GeofeString realm = consoleIdRealmMap.get(assetId);if (realm == null) {- LOG.info("Console ID not found in map so cannot retrieve geofences");+ LOG.fine("Console ID not found in map so cannot retrieve geofences");// Asset<?> not supported by this adapterreturn null;}@@ -274,7 +274,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofenotification.setTargets(subTargets);executorService.schedule(() -> {- LOG.info("Notifiying consoles that geofences have changed: " + notification.getTargets());+ LOG.fine("Notifiying consoles that geofences have changed: " + notification.getTargets());notificationService.sendNotification(notification);}, (long) i * NOTIFY_ASSETS_BATCH_MILLIS, TimeUnit.MILLISECONDS);});diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.javaindex 619ca3e11..644e6f9dd 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java@@ -87,8 +87,8 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}@Override- public User getUser(String realm, String userId) {- return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, realm, userId);+ public User getUser(String userId) {+ return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, userId);}@Overridediff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.javaindex 8333601ac..045805fc2 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java@@ -45,7 +45,7 @@ public interface ManagerIdentityProvider extends IdentityProvider {User[] queryUsers(UserQuery userQuery);- User getUser(String realm, String userId);+ User getUser(String userId);User getUserByUsername(String realm, String username);@@ -251,11 +251,10 @@ public interface ManagerIdentityProvider extends IdentityProvider {});}- static User getUserByIdFromDb(PersistenceService persistenceService, String realm, String userId) {+ static User getUserByIdFromDb(PersistenceService persistenceService, String userId) {return persistenceService.doReturningTransaction(em -> {List<User> result =- em.createQuery("select u from User u where u.realm = :realm and u.id = :userId", User.class)- .setParameter("realm", realm)+ em.createQuery("select u from User u where u.id = :userId", User.class).setParameter("userId", userId).getResultList();return result.size() > 0 ? result.get(0) : null;diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 0bdd380a1..b54728df9 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -211,8 +211,8 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}@Override- public User getUser(String realm, String userId) {- return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, realm, userId);+ public User getUser(String userId) {+ return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, userId);}@Override@@ -232,7 +232,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imuser.setUsername(user.getUsername().toLowerCase(Locale.ROOT));boolean isUpdate = false;- User existingUser = user.getId() != null ? getUser(realm, user.getId()) : getUserByUsername(realm, user.getUsername());+ User existingUser = user.getId() != null ? getUser(user.getId()) : getUserByUsername(realm, user.getUsername());ClientRepresentation clientRepresentation;UserRepresentation userRepresentation;@@ -242,7 +242,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imUserRepresentation userRep = clientResource.getServiceAccountUser();if (userRep == null) {String msg = "Attempt to update/create service user but a regular client with same client ID as this username already exists: User=" + user;- LOG.info(msg);+ LOG.fine(msg);throw new NotAllowedException(msg);}return userRep;@@ -255,7 +255,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imif (existingUser != null && user.getId() != null && !existingUser.getId().equals(user.getId())) {String msg = "Attempt to update user but retrieved user ID doesn't match supplied so ignoring: User=" + user;- LOG.info(msg);+ LOG.fine(msg);throw new BadRequestException(msg);}@@ -264,13 +264,13 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imif (existingUser.isServiceAccount() != user.isServiceAccount()) {String msg = "Attempt to update user service account flag not allowed: User=" + user;- LOG.info(msg);+ LOG.fine(msg);throw new NotAllowedException(msg);}if (existingUser.isServiceAccount() && !existingUser.getUsername().equals(user.getUsername())) {String msg = "Attempt to update username of service user not allowed: User=" + user;- LOG.info(msg);+ LOG.fine(msg);throw new NotAllowedException(msg);}}@@ -368,7 +368,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im@Overridepublic void deleteUser(String realm, String userId) {- User user = getUser(realm, userId);+ User user = getUser(userId);if (user == null) {return;@@ -967,7 +967,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imtry (InputStream is = Files.newInputStream(grantPath)) {String grantJson = IOUtils.toString(is, StandardCharsets.UTF_8);grant = ValueUtil.parse(grantJson, OAuthGrant.class).orElseGet(() -> {- LOG.info("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantFile);+ LOG.warning("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantFile);return null;});} catch (Exception ex) {@@ -1049,7 +1049,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imResponse response = clientsResource.create(client);response.close();if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {- LOG.info("Failed to create client response=" + response.getStatusInfo().getStatusCode() + ": " + client);+ LOG.fine("Failed to create client response=" + response.getStatusInfo().getStatusCode() + ": " + client);return null;}@@ -1071,11 +1071,11 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imRealmResource realmResource = realmsResource.realm(realm);if (realmResource == null) {- LOG.info("Invalid realm provided for deleteClient call: " + realm);+ LOG.fine("Invalid realm provided for deleteClient call: " + realm);return null;}- LOG.info("Deleting client: realm=" + realm + ", client ID=" + clientId);+ LOG.fine("Deleting client: realm=" + realm + ", client ID=" + clientId);return withClientResource(realm, clientId, realmsResource, (clientRepresentation, clientResource) -> {clientResource.remove();return null;diff --git a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.javaindex 6623ca683..1885dc54b 100644--- a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java@@ -92,7 +92,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcetry {return identityService.getIdentityProvider().getUser(- realm, userId+ userId);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());diff --git a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.javaindex 5437d498b..f2216a909 100644--- a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java+++ b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java@@ -21,15 +21,13 @@ package org.openremote.manager.simulator;import org.apache.camel.builder.RouteBuilder;import org.openremote.agent.protocol.simulator.SimulatorProtocol;-import org.openremote.manager.agent.AgentService;-import org.openremote.model.Container;-import org.openremote.model.ContainerService;import org.openremote.container.message.MessageBrokerService;-import org.openremote.container.security.AuthContext;+import org.openremote.manager.agent.AgentService;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;-import org.openremote.model.Constants;+import org.openremote.model.Container;+import org.openremote.model.ContainerService;import org.openremote.model.asset.agent.Protocol;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.simulator.RequestSimulatorState;@@ -85,6 +83,17 @@ public class SimulatorService extends RouteBuilder implements ContainerService {return false;});+ clientEventService.addEventAuthorizer((realm, auth, event) -> {+ if (event instanceof RequestSimulatorState) {++ // Only super users can use this+ return auth.isSuperUser();++ // TODO Should realm admins be able to work with simulators in their realm?+ }+ return false;+ });+container.getService(MessageBrokerService.class).getContext().addRoutes(this);}@@ -108,11 +117,6 @@ public class SimulatorService extends RouteBuilder implements ContainerService {LOG.finer("Handling from client: " + event);String sessionKey = getSessionKey(exchange);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);-- // Superuser can get all- if (!authContext.isSuperUser())- return;// TODO Should realm admins be able to work with simulators in their realm?publishSimulatorState(sessionKey, event.getAgentId());diff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.propertiesindex c73680609..98a8ae769 100644--- a/manager/src/main/resources/logging-dev.properties+++ b/manager/src/main/resources/logging-dev.properties@@ -8,7 +8,7 @@handlers=java.util.logging.ConsoleHandler, \org.openremote.manager.syslog.SyslogService-# Log on STDOUT, to a file, and internal syslog service (monitoring in Manager UI)+# Log extra data to file for debug purposes#handlers=java.util.logging.ConsoleHandler, \# java.util.logging.FileHandler \# org.openremote.manager.syslog.SyslogService@@ -29,22 +29,23 @@ java.util.logging.ConsoleHandler.level=FINER#java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogFormatter+################################################################################ File handler###############################################################################-# Set to FINEST to get all enabled log categories on file handler-java.util.logging.FileHandler.level=FINER--# Output file and format-java.util.logging.FileHandler.pattern=/deployment/openremote.log-java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter-# Rotate between 10 log files-java.util.logging.FileHandler.count=10-# 10 mb per file-java.util.logging.FileHandler.limit=10000000-# Set to false to clear log on restart-java.util.logging.FileHandler.append=false+## Set to FINEST to get all enabled log categories on file handler+#java.util.logging.FileHandler.level=FINER+#+## Output file and format+#java.util.logging.FileHandler.pattern=/deployment/openremote.log+#java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter+## Rotate between 10 log files+#java.util.logging.FileHandler.count=10+## 10 mb per file+#java.util.logging.FileHandler.limit=10000000+## Set to false to clear log on restart+#java.util.logging.FileHandler.append=false################################################################################ Categories@@ -102,4 +103,3 @@ org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNINGorg.apache.camel.impl.DefaultShutdownStrategy.level=SEVEREorg.jeasy.rules.level=WARNINGorg.postgresql.jdbc.level=INFO-io.moquette.broker.level=INFOdiff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.propertiesindex 0f014f49b..ac3433fe6 100644--- a/manager/src/main/resources/logging.properties+++ b/manager/src/main/resources/logging.properties@@ -5,12 +5,7 @@################################################################################ Log on STDOUT and internal syslog service (monitoring in Manager UI)-#handlers=java.util.logging.ConsoleHandler, \-# org.openremote.manager.syslog.SyslogService--# Log on STDOUT, to a file, and internal syslog service (monitoring in Manager UI)handlers=java.util.logging.ConsoleHandler, \- java.util.logging.FileHandler \org.openremote.manager.syslog.SyslogService###############################################################################@@ -18,7 +13,7 @@ handlers=java.util.logging.ConsoleHandler, \################################################################################ Set to FINEST to get all enabled log categories on console handler-java.util.logging.ConsoleHandler.level=INFO+java.util.logging.ConsoleHandler.level=FINE# Console format, you can colorize this in IntelliJ IDEA "Run Application " console# with the GrepConsole plugin and after copying the configuration file of your@@ -29,23 +24,6 @@ java.util.logging.ConsoleHandler.level=INFO#java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogFormatter-###############################################################################-# File handler-###############################################################################--# Set to FINEST to get all enabled log categories on file handler-java.util.logging.FileHandler.level=INFO--# Output file and format-java.util.logging.FileHandler.pattern=/deployment/openremote.log-java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter-# Rotate between 10 log files-java.util.logging.FileHandler.count=10-# 10 mb per file-java.util.logging.FileHandler.limit=10000000-# Set to false to clear log on restart-java.util.logging.FileHandler.append=true-################################################################################ Categories###############################################################################@@ -101,4 +79,3 @@ org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNINGorg.apache.camel.impl.DefaultShutdownStrategy.level=SEVEREorg.jeasy.rules.level=WARNINGorg.postgresql.jdbc.level=INFO-io.moquette.broker.level=WARNINGdiff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.javaindex 94e9ec2c4..e013369a1 100644--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java@@ -78,7 +78,7 @@ public class AssetEvent extends SharedEvent implements AssetInfo {@Overridepublic String[] getAttributeNames() {- return updatedProperties;+ return updatedProperties != null && Arrays.asList(updatedProperties).contains("attributes") && asset != null && asset.attributes != null ? asset.attributes.keySet().toArray(new String[0]) : new String[0];}public Cause getCause() {@@ -93,16 +93,10 @@ public class AssetEvent extends SharedEvent implements AssetInfo {return updatedProperties;}- @Override- public boolean canAccessPublicRead() {+ public boolean isAccessPublicRead() {return asset.isAccessPublicRead();}- @Override- public boolean canAccessRestrictedRead() {- return true;- }-@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/asset/AssetFilter.java b/model/src/main/java/org/openremote/model/asset/AssetFilter.javaindex 958f4507e..62b790788 100644--- a/model/src/main/java/org/openremote/model/asset/AssetFilter.java+++ b/model/src/main/java/org/openremote/model/asset/AssetFilter.java@@ -19,13 +19,18 @@*/package org.openremote.model.asset;+import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.shared.AssetInfo;import org.openremote.model.event.shared.EventFilter;import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.util.TextUtil;+import org.openremote.model.util.ValueUtil;+import org.openremote.model.value.MetaItemDescriptor;+import org.openremote.model.value.MetaItemType;import java.util.Arrays;import java.util.List;+import java.util.stream.Collectors;// TODO: Merge this with AssetQuery and use AssetQueryPredicate to resolvepublic class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<T> {@@ -115,48 +120,79 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<return FILTER_TYPE;}+ @SuppressWarnings("unchecked")@Override- public boolean apply(T event) {+ public T apply(T event) {- if (restrictedEvents && !event.canAccessRestrictedRead()) {- return false;+ MetaItemDescriptor<?> filterAttributesBy = null;++ if (restrictedEvents) {+ if (event instanceof AttributeEvent attributeEvent) {+ if (!attributeEvent.isRestrictedRead()) {+ return null;+ }+ } else {+ filterAttributesBy = MetaItemType.ACCESS_RESTRICTED_READ;+ }}- if (publicEvents && !event.canAccessPublicRead()) {- return false;+ if (publicEvents) {+ if (event instanceof AttributeEvent attributeEvent) {+ if (!attributeEvent.isPublicRead()) {+ return null;+ }+ } else if (event instanceof AssetEvent assetEvent) {+ if (!assetEvent.isAccessPublicRead()) {+ return null;+ } else {+ filterAttributesBy = MetaItemType.ACCESS_PUBLIC_READ;+ }+ }}if (assetIds != null && assetIds.length > 0) {if (!Arrays.asList(assetIds).contains(event.getAssetId())) {- return false;+ return null;}}if (parentIds != null && parentIds.length > 0) {if (!Arrays.asList(parentIds).contains(event.getParentId())) {- return false;+ return null;}}if(path != null && path.length > 0) {List<String> pathList = Arrays.asList(event.getPath());if (Arrays.stream(path).noneMatch(pathList::contains)) {- return false;+ return null;}}if (!TextUtil.isNullOrEmpty(realm)) {if (!realm.equals(event.getRealm())) {- return false;+ return null;}}if (attributeNames != null && attributeNames.length > 0) {+ if (filterAttributesBy != null) {+ // Filter attributes before doing name match+ AssetEvent assetEvent = (AssetEvent) event;+ if (assetEvent.getAsset() != null) {+ Asset<?> asset = ValueUtil.clone(assetEvent.getAsset());+ MetaItemDescriptor<?> finalFilterAttributesBy = filterAttributesBy;+ asset.setAttributes(asset.getAttributes().values().stream().filter(attribute -> attribute.hasMeta(finalFilterAttributesBy)).collect(Collectors.toList()));+ event = (T) new AssetEvent(assetEvent.getCause(), asset, assetEvent.getUpdatedProperties());+ }+ }List<String> eventAttributeNames = Arrays.asList(event.getAttributeNames());- return Arrays.stream(attributeNames).anyMatch(eventAttributeNames::contains);+ if (Arrays.stream(attributeNames).noneMatch(eventAttributeNames::contains)) {+ return null;+ }}- return true;+ return event;}@Overridediff --git a/model/src/main/java/org/openremote/model/asset/HasAssetQuery.java b/model/src/main/java/org/openremote/model/asset/HasAssetQuery.javanew file mode 100644index 000000000..cbde227ef--- /dev/null+++ b/model/src/main/java/org/openremote/model/asset/HasAssetQuery.java@@ -0,0 +1,26 @@+/*+ * 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.asset;++import org.openremote.model.query.AssetQuery;++public interface HasAssetQuery {+ AssetQuery getAssetQuery();+}diff --git a/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.javaindex 928b70709..83c76fef3 100644--- a/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java@@ -22,6 +22,8 @@ package org.openremote.model.asset;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;import org.openremote.model.event.shared.SharedEvent;+import org.openremote.model.query.AssetQuery;+import org.openremote.model.util.TextUtil;/*** A client sends this event to the server to read the specified asset, optionally restricting which attributes@@ -29,9 +31,10 @@ import org.openremote.model.event.shared.SharedEvent;* the right permissions, or if anything else is not in order (e.g. the asset doesn't exist), the server might not react* at all.*/-public class ReadAssetEvent extends SharedEvent {+public class ReadAssetEvent extends SharedEvent implements HasAssetQuery {protected String assetId;+ protected AssetQuery assetQuery;@JsonCreatorpublic ReadAssetEvent(@JsonProperty("assetId") String assetId) {@@ -46,6 +49,15 @@ public class ReadAssetEvent extends SharedEvent {this.assetId = assetId;}+ public AssetQuery getAssetQuery() {+ if (assetQuery == null) {+ if (!TextUtil.isNullOrEmpty(assetId)) {+ assetQuery = new AssetQuery().ids(assetId);+ }+ }+ return assetQuery;+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.javaindex 0ff803496..7d59998f4 100644--- a/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java@@ -29,7 +29,7 @@ import org.openremote.model.query.AssetQuery;* A client sends this event to the server to query assets, expecting* the server to answer "soon" with an {@link AssetsEvent} with the results.*/-public class ReadAssetsEvent extends SharedEvent {+public class ReadAssetsEvent extends SharedEvent implements HasAssetQuery {protected AssetQuery assetQuery;@@ -39,6 +39,9 @@ public class ReadAssetsEvent extends SharedEvent {}public AssetQuery getAssetQuery() {+ if (assetQuery == null) {+ assetQuery = new AssetQuery();+ }return assetQuery;}diff --git a/model/src/main/java/org/openremote/model/asset/ReadAttributeEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAttributeEvent.javaindex dc28ce5da..ad664ebcc 100644--- a/model/src/main/java/org/openremote/model/asset/ReadAttributeEvent.java+++ b/model/src/main/java/org/openremote/model/asset/ReadAttributeEvent.java@@ -24,15 +24,18 @@ import com.fasterxml.jackson.annotation.JsonProperty;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.event.shared.SharedEvent;+import org.openremote.model.query.AssetQuery;+import org.openremote.model.util.TextUtil;/*** A client sends this event to the server to refresh the value of the specified attribute, expecting the server to* answer "soon" with an {@link AttributeEvent}. If the server decides that the client doesn't have the right* permissions, or if anything else is not in order (e.g. the asset doesn't exist), the server might not react at all.*/-public class ReadAttributeEvent extends SharedEvent {+public class ReadAttributeEvent extends SharedEvent implements HasAssetQuery {protected AttributeRef ref;+ protected AssetQuery assetQuery;@JsonCreatorpublic ReadAttributeEvent(@JsonProperty("ref") AttributeRef attributeRef) {@@ -47,8 +50,13 @@ public class ReadAttributeEvent extends SharedEvent {return ref;}- public void setAttributeRef(AttributeRef ref) {- this.ref = ref;+ public AssetQuery getAssetQuery() {+ if (assetQuery == null) {+ if (ref != null && !TextUtil.isNullOrEmpty(ref.getId())) {+ assetQuery = new AssetQuery().ids(ref.getId());+ }+ }+ return assetQuery;}@Overridediff --git a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.javaindex e9b063393..3ce16561b 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java@@ -182,13 +182,11 @@ public class AttributeEvent extends SharedEvent implements AssetInfo {return this;}- @Override- public boolean canAccessPublicRead() {+ public boolean isPublicRead() {return publicRead;}- @Override- public boolean canAccessRestrictedRead() {+ public boolean isRestrictedRead() {return restrictedRead;}diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java b/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.javaindex c8758c1dd..dc09dc41a 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java@@ -71,11 +71,6 @@ public enum AttributeWriteFailure {*/INVALID_ATTRIBUTE_EXECUTE_STATUS,- /**- * No authentication/authorization context available.- */- NO_AUTH_CONTEXT,-/*** Realm configuration or user privileges do not allow update (e.g. realm inactive, user is missing required role,* attribute is read-only).diff --git a/model/src/main/java/org/openremote/model/event/shared/EventFilter.java b/model/src/main/java/org/openremote/model/event/shared/EventFilter.javaindex ac22bed30..76c55289c 100644--- a/model/src/main/java/org/openremote/model/event/shared/EventFilter.java+++ b/model/src/main/java/org/openremote/model/event/shared/EventFilter.java@@ -50,7 +50,7 @@ public abstract class EventFilter<E extends SharedEvent> {/***- * @return <code>false</code> if the filter doesn't match the given event.+ * @return the event or a filtered copy of it if the filter matches; do not modify the supplied event.*/- public abstract boolean apply(E event);+ public abstract E apply(E event);}diff --git a/model/src/main/java/org/openremote/model/event/shared/RealmFilter.java b/model/src/main/java/org/openremote/model/event/shared/RealmFilter.javaindex 769864a94..0bf4c8ec3 100644--- a/model/src/main/java/org/openremote/model/event/shared/RealmFilter.java+++ b/model/src/main/java/org/openremote/model/event/shared/RealmFilter.java@@ -42,8 +42,8 @@ public class RealmFilter<T extends RealmScopedEvent> extends EventFilter<T> {}@Override- public boolean apply(RealmScopedEvent event) {- return getName().equals(event.getRealm());+ public RealmScopedEvent apply(RealmScopedEvent event) {+ return getName().equals(event.getRealm()) ? event : null;}@Overridediff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.javaindex f2946a909..386ebebc8 100644--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java@@ -68,12 +68,4 @@ public abstract class SharedEvent extends Event {public SharedEvent() {}-- public boolean canAccessPublicRead() {- return false;- }-- public boolean canAccessRestrictedRead() {- return false;- }}diff --git a/model/src/main/java/org/openremote/model/provisioning/ErrorResponseMessage.java b/model/src/main/java/org/openremote/model/provisioning/ErrorResponseMessage.javaindex 62fc31123..43fa9a6b1 100644--- a/model/src/main/java/org/openremote/model/provisioning/ErrorResponseMessage.java+++ b/model/src/main/java/org/openremote/model/provisioning/ErrorResponseMessage.java@@ -45,4 +45,11 @@ public class ErrorResponseMessage extends ProvisioningMessage {public Error getError() {return error;}++ @Override+ public String toString() {+ return getClass().getSimpleName() + "{" ++ "error=" + error ++ '}';+ }}diff --git a/model/src/main/java/org/openremote/model/syslog/SyslogEvent.java b/model/src/main/java/org/openremote/model/syslog/SyslogEvent.javaindex e75d8cb8b..8d737b72a 100644--- a/model/src/main/java/org/openremote/model/syslog/SyslogEvent.java+++ b/model/src/main/java/org/openremote/model/syslog/SyslogEvent.java@@ -70,9 +70,9 @@ public class SyslogEvent extends SharedEvent {}@Override- public boolean apply(SyslogEvent event) {+ public SyslogEvent apply(SyslogEvent event) {return (getCategories().isEmpty() || getCategories().contains(event.getCategory()))- && (getLevel() == null || getLevel().ordinal() <= event.getLevel().ordinal());+ && (getLevel() == null || getLevel().ordinal() <= event.getLevel().ordinal()) ? event : null;}@Overridediff --git a/model/src/main/java/org/openremote/model/util/Debouncer.java b/model/src/main/java/org/openremote/model/util/Debouncer.javaindex e9238d363..997c440b8 100644--- a/model/src/main/java/org/openremote/model/util/Debouncer.java+++ b/model/src/main/java/org/openremote/model/util/Debouncer.java@@ -19,8 +19,11 @@*/package org.openremote.model.util;+import java.util.ArrayList;+import java.util.List;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ScheduledExecutorService;+import java.util.concurrent.ScheduledFuture;import java.util.concurrent.TimeUnit;import java.util.function.Consumer;@@ -50,11 +53,18 @@ public class Debouncer<T> {} while (prev != null && !prev.extend()); // Exit only if new task was added to map, or existing task was extended successfully}+ public void cancelAll(boolean mayInterruptIfRunning) {+ List<TimerTask> tasks = new ArrayList<>(delayedMap.values());+ delayedMap.clear();+ tasks.forEach(task -> task.cancel(mayInterruptIfRunning));+ }+// The task that wakes up when the wait time elapsesprotected class TimerTask implements Runnable {private final T key;private long dueTime;private final Object lock = new Object();+ private ScheduledFuture<?> scheduledFuture;public TimerTask(T key) {this.key = key;@@ -85,5 +95,11 @@ public class Debouncer<T> {}}}++ public void cancel(boolean mayInterruptIfRunning) {+ if (scheduledFuture != null) {+ scheduledFuture.cancel(mayInterruptIfRunning);+ }+ }}}diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovyindex 2e4fcfde9..2db66230b 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy@@ -1,25 +1,23 @@package org.openremote.test.assetsimport org.openremote.manager.setup.SetupService-import org.openremote.model.attribute.AttributeState-import org.openremote.model.attribute.AttributeWriteFailure-import org.openremote.test.setup.KeycloakTestSetup-import org.openremote.test.setup.ManagerTestSetupimport org.openremote.model.asset.Assetimport org.openremote.model.asset.AssetResourceimport org.openremote.model.asset.impl.BuildingAssetimport org.openremote.model.asset.impl.RoomAsset-import org.openremote.model.attribute.Attribute-import org.openremote.model.attribute.MetaItem-import org.openremote.model.attribute.MetaMap+import org.openremote.model.attribute.*import org.openremote.model.query.AssetQueryimport org.openremote.model.query.filter.ParentPredicateimport org.openremote.model.query.filter.RealmPredicateimport org.openremote.test.ManagerContainerTrait+import org.openremote.test.setup.KeycloakTestSetup+import org.openremote.test.setup.ManagerTestSetupimport spock.lang.Specificationimport spock.util.concurrent.PollingConditionsimport javax.ws.rs.BadRequestException+import javax.ws.rs.ForbiddenException+import javax.ws.rs.NotAuthorizedExceptionimport javax.ws.rs.WebApplicationExceptionimport static org.openremote.container.util.MapAccess.getString@@ -241,12 +239,8 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTraiwhen: "the assets of the authenticated user are retrieved"def assets = assetResource.getCurrentUserAssets(null)- then: "result should match (all assets in the master realm as user is not restricted)"- assets.length == 5- assets.find {it.id == managerTestSetup.smartOfficeId}.attributes.size() == 7- // Assets should not be completely loaded (no path or parent info)- assets.find {it.id == managerTestSetup.smartOfficeId}.path == null- assets.find {it.id == managerTestSetup.smartOfficeId}.parentId == null+ then: "no assets should be returned as there are no assets linked to this user"+ assets.length == 0when: "the root assets of the authenticated realm are retrieved"assets = assetResource.queryAssets(null,@@ -287,9 +281,9 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai.parents(new ParentPredicate(null)))- then: "a bad request exception should be thrown"+ then: "a not authorized exception should be thrown"WebApplicationException ex = thrown()- assert ex instanceof BadRequestException+ assert ex instanceof ForbiddenExceptionwhen: "the child assets of an asset in a foreign realm are retrieved"assets = assetResource.queryAssets(null,@@ -402,15 +396,11 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai/* ############################################## READ ####################################### */- when: "the home assets of the authenticated user are retrieved"+ when: "the assets of the authenticated user are retrieved"def assets = assetResource.getCurrentUserAssets(null)- then: "result should match (all assets in the building realm as user is not restricted)"- assets.length == 13- assets.find {it.id == managerTestSetup.smartBuildingId}.attributes.size() == 7- // Assets should not be completely loaded (no path or parent info)- assets.find {it.id == managerTestSetup.smartBuildingId}.path == null- assets.find {it.id == managerTestSetup.smartBuildingId}.parentId == null+ then: "no assets should be returned as there are no assets linked to this user"+ assets.length == 0when: "the root assets of a foreign realm are retrieved"assets = assetResource.queryAssets(null,@@ -419,8 +409,8 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai.parents(new ParentPredicate(null)))- then: "a bad request exception should be thrown"- thrown(BadRequestException)+ then: "a not authorized exception should be thrown"+ thrown(ForbiddenException)when: "the root assets of the authenticated realm are retrieved"assets = assetResource.queryAssets(null,@@ -440,8 +430,8 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai.parents(new ParentPredicate(null)))- then: "result should match"- thrown(BadRequestException)+ then: "a forbidden exception should be thrown"+ thrown(ForbiddenException)when: "the child assets of an asset in a foreign realm are retrieved"assets = assetResource.queryAssets(null,@@ -521,7 +511,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrairesponse.status == 403}- def "Access assets as testuser3 (restricted user)"() {+ def "Access assets as restricted testuser3"() {given: "the server container is started"def container = startContainer(defaultConfig(), defaultServices())def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)@@ -556,7 +546,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTraiapartment1.parentId == managerTestSetup.smartBuildingIdapartment1.path[0] == managerTestSetup.smartBuildingIdapartment1.path[1] == managerTestSetup.apartment1Id- apartment1.attributes.size() == 12+ apartment1.attributes.size() == 7Asset apartment1Livingroom = assets[1]apartment1Livingroom.id == managerTestSetup.apartment1LivingroomId@@ -585,8 +575,8 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai.parents(new ParentPredicate(null)))- then: "a bad request exception should be thrown"- thrown(BadRequestException)+ then: "a forbidden exception should be thrown"+ thrown(ForbiddenException)when: "the root assets of the authenticated realm are retrieved"assets = assetResource.queryAssets(null,@@ -604,8 +594,8 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai.parents(new ParentPredicate(null)))- then: "a bad request exception should be thrown"- thrown(BadRequestException)+ then: "a forbidden exception should be thrown"+ thrown(ForbiddenException)when: "the child assets of linked asset are retrieved"assets = assetResource.queryAssets(null,@@ -748,26 +738,20 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTraiwhen: "a private asset attribute is written on a user asset"def response = assetResource.writeAttributeValue(null, managerTestSetup.apartment1LivingroomId, "lightSwitch", false)- then: "the attribute should be not found"- response.status == 404-- when: "a restricted read-only asset attribute is written on a user asset"- response = assetResource.writeAttributeValue(null, managerTestSetup.apartment1LivingroomId, "currentTemperature", 22.123)-- then: "the request should be forbidden"+ then: "access should be forbidden"response.status == 403when: "an attribute is written on a non-existent user asset"response = assetResource.writeAttributeValue(null, "doesnotexist", "lightSwitch", false)- then: "the attribute should be not found"- response.status == 404+ then: "access should be forbidden"+ response.status == 403when: "an non-existent attribute is written on a user asset"response = assetResource.writeAttributeValue(null, managerTestSetup.apartment1LivingroomId, "doesnotexist", '"foo"')- then: "the attribute should be not found"- response.status == 404+ then: "access should be forbidden"+ response.status == 403when: "an asset attribute is written on a non-user asset"response = assetResource.writeAttributeValue(null, managerTestSetup.apartment3LivingroomId, "lightSwitch", false)@@ -868,6 +852,6 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTraithen: "the request should have succeeded but only the apartment1 location event should have been successful"assert writeResults.size() == 2assert writeResults.find {it.ref.id == managerTestSetup.apartment1Id && it.ref.name == Asset.LOCATION.name}.failure == null- assert writeResults.find {it.ref.id == managerTestSetup.apartment2LivingroomId && it.ref.name == Asset.LOCATION.name}.failure == AttributeWriteFailure.NO_AUTH_CONTEXT+ assert writeResults.find {it.ref.id == managerTestSetup.apartment2LivingroomId && it.ref.name == Asset.LOCATION.name}.failure == AttributeWriteFailure.INSUFFICIENT_ACCESS}}diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 83ed7efcf..03b6635db 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -18,6 +18,7 @@ import org.openremote.manager.mqtt.MQTTHandlerimport org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.asset.AssetEvent+import org.openremote.model.asset.UserAssetLinkimport org.openremote.model.asset.agent.ConnectionStatusimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.Attribute@@ -46,7 +47,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {given: "the container environment is started"List<SharedEvent> receivedEvents = []List<Object> receivedValues = []-+ MQTT_IOClient client = null+ MQTT_IOClient newClient = nulldef conditions = new PollingConditions(timeout: 10, initialDelay: 0.1, delay: 0.2)def container = startContainer(defaultConfig(), defaultServices())def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)@@ -64,7 +66,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {when: "a mqtt client connects with invalid credentials"def wrongUsername = "master:" + keycloakTestSetup.serviceUser.username- MQTT_IOClient client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(wrongUsername, password), null, null)+ client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(wrongUsername, password), null, null)client.connect()then: "the client connection status should be in error"@@ -82,6 +84,31 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert client.getConnectionStatus() == ConnectionStatus.CONNECTEDassert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.sessionKeyInfoMap.containsKey(getConnectionIDString(connection))+ }++ when: "the client disconnects"+ def currentConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ client.disconnect()++ then: "the client resources should be freed"+ conditions.eventually {+ assert clientEventService.sessionKeyInfoMap.isEmpty()+ assert !clientEventService.sessionKeyInfoMap.containsKey(getConnectionIDString(currentConnection))+ }++ when: "a mqtt client connects with valid credentials"+ mqttClientId = UniqueIdentifierGenerator.generateId()+ client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, null)+ client.connect()++ then: "mqtt connection should exist"+ conditions.eventually {+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]+ assert clientEventService.sessionKeyInfoMap.containsKey(getConnectionIDString(connection))}when: "a mqtt client subscribes to an asset in another realm"@@ -166,14 +193,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {receivedEvents.clear()when: "a mqtt client publishes to an asset attribute"- topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()- def payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 70)).get()+ topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()+ def payload = "70"client.sendMessage(new MQTTMessage<String>(topic, payload))then: "The value of the attribute should be updated and the client should have received the event"new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert asset.getAttribute("motionSensor").get().value.orElse(0) == 70d+ assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 70dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -183,13 +209,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {receivedEvents.clear()when: "a mqtt client publishes to an asset attribute"- payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "lights", false)).get()+ topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/lights/${managerTestSetup.apartment1HallwayId}".toString()+ payload = Boolean.FALSE.toString()client.sendMessage(new MQTTMessage<String>(topic, payload))then: "the value of the attribute should be updated and the client should have received the event"conditions.eventually {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert !asset.getAttribute("lights").get().value.orElse(true)+ assert !assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("lights").get().value.orElse(true)assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -205,8 +231,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "the value of the attribute should be updated and the client should have received the event"conditions.eventually {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert asset.getAttribute("lights").get().value.orElse(false)+ assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("lights").get().value.orElse(false)assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -463,7 +488,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {newClientId = "newClient"def username2 = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser2.username // realm and OAuth client iddef 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 = 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"@@ -476,14 +501,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {client.addMessageConsumer(topic, eventConsumer)and: "the new client publishes to a an attribute topic"- topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()- payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 170)).get()+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()+ payload = "170"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 {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert asset.getAttribute("motionSensor").get().value.orElse(0) == 170d+ assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 170dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -505,8 +529,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {and: "The last will message should have updated the value of the attribute and the first client should have received the event"new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert asset.getAttribute("motionSensor").get().value.orElse(0) == 1000d+ assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 1000dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -516,14 +539,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {receivedEvents.clear()when: "the new client tries to publish again"- topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()- payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 170)).get()- client.sendMessage(new MQTTMessage<String>(topic, payload))+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()+ payload = "170"+ 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 {- def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)- assert asset.getAttribute("motionSensor").get().value.orElse(0) == 170d+ assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 170dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEventassert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId@@ -570,6 +592,55 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert asst.getAttribute("co2Level").get().value.orElse(-100) == currentCO2Level}+ when: "a restricted mqtt client subscribes to an unlinked asset"+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1KitchenId".toString()+ newClient.addMessageConsumer(topic, {msg ->})++ then: "no subscription should exist"+ conditions.eventually {+ assert newClient.topicConsumerMap.get(topic) == null // Consumer added and removed on failure+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ }++ when: "a restricted mqtt client subscribes to a linked asset"+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()+ newClient.addMessageConsumer(topic, {msg ->})++ then: "a subscription should exist"+ conditions.eventually {+ assert newClient.topicConsumerMap.get(topic) != null+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(getConnectionIDString(connection))+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(getConnectionIDString(connection)).size() == 1+ }++ when: "user asset links are updated for a connected restricted user"+ existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]+ assetStorageService.storeUserAssetLinks(List.of(+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),+ keycloakTestSetup.serviceUser2.getId(),+ managerTestSetup.apartment1BathroomId)))++ then: "the existing connection should be terminated and the client should reconnect"+ conditions.eventually {+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection+ }++ // TODO: Further MQTT tests+// when: "an attribute event occurs on an attribute that the restricted user can access"+//+// then: "they should receive the event"+//+// when: "an attribute event occurs on an attribute that the restricted user cannot access"+//+// then: "they should not receive the event"+//+// when: "an asset event occurs on an asset that the restricted user can access"+//+// then: "they should receive the event but it should only contain restricted attributes"+when: "both MQTT clients disconnect"client.disconnect()newClient.disconnect()@@ -581,5 +652,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).isEmpty()}++ cleanup: "disconnect the clients"+ if (client != null) {+ client.disconnect()+ }+ if (newClient != null) {+ newClient.disconnect()+ }}}diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovyindex 2c1c8cde3..cde5f778b 100644--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy@@ -41,6 +41,7 @@ import org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandlerimport org.openremote.manager.security.ManagerIdentityServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.asset.AssetEvent+import org.openremote.model.asset.UserAssetLinkimport org.openremote.model.asset.agent.ConnectionStatusimport org.openremote.model.asset.impl.WeatherAssetimport org.openremote.model.attribute.Attribute@@ -72,6 +73,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContagiven: "expected conditions"def conditions = new PollingConditions(timeout: 10, delay: 0.2)+ MQTT_IOClient device1Client+ MQTT_IOClient deviceNClientand: "the container starts"def container = startContainer(defaultConfig(), defaultServices())@@ -148,7 +151,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaList<String> subscribeFailures = []List<ConnectionStatus> connectionStatuses = []Consumer<String> subscribeFailureCallback = {String topic -> subscribeFailures.add(topic)}- MQTT_IOClient device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null, null)+ device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null, null)device1Client.setTopicSubscribeFailureConsumer(subscribeFailureCallback)device1Client.addConnectionStatusConsumer({connectionStatus ->connectionStatuses.add(connectionStatus)})@@ -175,6 +178,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta}when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"+ def existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)device1Client.sendMessage(new MQTTMessage<String>(device1RequestTopic, ValueUtil.asJSON(new X509ProvisioningMessage(getClass().getResource("/org/openremote/test/provisioning/device1.pem").text)@@ -192,9 +196,14 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaassert weatherAsset.getAttribute("serialNumber").flatMap{it.getValue()}.orElse(null) == device1UniqueId}+ and: "the connection should have been maintained"+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 0.5).eventually {+ assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) == existingConnection+ }+when: "the client gets abruptly disconnected"device1Responses.clear()- def existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)+ 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()@@ -261,8 +270,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContawhen: "the client updates one of the provisioned asset's attributes"device1Client.sendMessage(new MQTTMessage<String>(- "$provisioningConfig.realm/$mqttDevice1ClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC",- ValueUtil.asJSON(new AttributeEvent(asset.id, "customAttribute", 99d)).orElse(null)+ "$provisioningConfig.realm/$mqttDevice1ClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/customAttribute/${asset.id}",+ "99"))@@ -384,7 +393,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContadef deviceNRequestTopic = "$PROVISIONING_TOKEN/$deviceNUniqueId/$REQUEST_TOKEN".toString()def deviceNResponseTopic = "$PROVISIONING_TOKEN/$deviceNUniqueId/$RESPONSE_TOKEN".toString()def mqttDeviceNClientId = UniqueIdentifierGenerator.generateId("deviceN")- MQTT_IOClient deviceNClient = new MQTT_IOClient(mqttDeviceNClientId, mqttHost, mqttPort, false, false, null, null, null)+ deviceNClient = new MQTT_IOClient(mqttDeviceNClientId, mqttHost, mqttPort, false, false, null, null, null)deviceNClient.setTopicSubscribeFailureConsumer(subscribeFailureCallback)deviceNClient.connect()diff --git a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.javaindex 780288cc8..43f36b26f 100644--- a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java+++ b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java@@ -20,6 +20,7 @@package org.openremote.test.setup;import org.openremote.container.util.UniqueIdentifierGenerator;+import org.openremote.manager.security.ManagerIdentityProvider;import org.openremote.manager.setup.AbstractKeycloakSetup;import org.openremote.model.Constants;import org.openremote.model.Container;@@ -32,6 +33,7 @@ import java.util.logging.Logger;import java.util.stream.Stream;import static org.openremote.model.Constants.MASTER_REALM;+import static org.openremote.model.Constants.RESTRICTED_USER_REALM_ROLE;/*** We have the following demo users:@@ -127,5 +129,11 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {Constants.KEYCLOAK_CLIENT_ID,Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES).map(ClientRole::getValue).toArray(String[]::new));++ // ################################ Make users restricted ###################################+ ManagerIdentityProvider identityProvider = identityService.getIdentityProvider();+ identityProvider.updateUserRealmRoles(realmBuilding.getName(), testuser3Id, identityProvider.addRealmRoles(realmBuilding.getName(), testuser3Id, RESTRICTED_USER_REALM_ROLE));+ identityProvider.updateUserRealmRoles(realmBuilding.getName(), buildingUserId, identityProvider.addRealmRoles(realmBuilding.getName(), buildingUserId, RESTRICTED_USER_REALM_ROLE));+ identityProvider.updateUserRealmRoles(realmBuilding.getName(), serviceUser2.getId(), identityProvider.addRealmRoles(realmBuilding.getName(), serviceUser2.getId(), RESTRICTED_USER_REALM_ROLE));}}diff --git a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.javaindex 5c3f84471..ba4048edd 100644--- a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java+++ b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java@@ -603,12 +603,6 @@ public class ManagerTestSetup extends ManagerSetup {keycloakTestSetup.serviceUser2.getId(),apartment1HallwayId)));- // ################################ Make users restricted ###################################- ManagerIdentityProvider identityProvider = identityService.getIdentityProvider();- identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.testuser3Id, identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.testuser3Id, RESTRICTED_USER_REALM_ROLE));- identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.buildingUserId, identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.buildingUserId, RESTRICTED_USER_REALM_ROLE));- identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.serviceUser2.getId(), identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.serviceUser2.getId(), RESTRICTED_USER_REALM_ROLE));-// ################################ Realm smartcity ###################################CityAsset smartCity = new CityAsset("Smart city");diff --git a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovyindex 31a8c4f36..9bd6b7219 100644--- a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy+++ b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy@@ -5,6 +5,7 @@ import org.apache.camel.spi.BrowsableEndpointimport org.openremote.container.Containerimport org.openremote.container.message.MessageBrokerServiceimport org.openremote.container.timer.TimerService+import org.openremote.manager.mqtt.MQTTBrokerServiceimport org.openremote.manager.rules.RulesServiceimport org.openremote.model.ContainerServiceimport spock.util.concurrent.PollingConditions@@ -26,7 +27,8 @@ trait ManagerContainerTrait extends ContainerTrait {}[(OR_WEBSERVER_LISTEN_PORT): Integer.toString(serverPort),- (MQTT_SERVER_LISTEN_HOST) : "127.0.0.1", // Works best for cross platform test running+ (MQTT_SERVER_LISTEN_HOST) : "127.0.0.1", // Works best for cross platform test running,+ (MQTTBrokerService.MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS): "10",(TIMER_CLOCK_TYPE) : PSEUDO.name()]}