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
Cleanup of ManagerIdentityProvider to abstract away Keycloak as much as possible and improved websocket handling
Rich Turner3195afae072b
diff --git a/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java b/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.javaindex 4ffc4c6ae..acc9ded46 100644--- a/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java+++ b/client/src/main/java/org/openremote/app/client/event/EventServiceImpl.java@@ -125,8 +125,9 @@ public class EventServiceImpl implements EventService {data = data.substring(UnauthorizedEventSubscription.MESSAGE_PREFIX.length());UnauthorizedEventSubscription<?> failure = unauthorizedEventSubscriptionMapper.read(data);eventBus.dispatch(new SubscriptionFailureEvent(failure.getSubscription().getEventType()));- } else if (data.startsWith(SharedEvent.MESSAGE_PREFIX)) {- data = data.substring(SharedEvent.MESSAGE_PREFIX.length());+ } else if (data.startsWith(TriggeredEventSubscription.MESSAGE_PREFIX)) {+ data = data.substring(TriggeredEventSubscription.MESSAGE_PREFIX.length());+if (data.startsWith("{")) {TriggeredEventSubscription<?> triggered = triggeredEventSubscriptionMapper.read(data);if (triggered.getEvents() == null) {@@ -135,7 +136,10 @@ public class EventServiceImpl implements EventService {} else {triggered.getEvents().forEach(eventBus::dispatch);}- } else if (data.startsWith("[")) {+ }+ } else if (data.startsWith(SharedEvent.MESSAGE_PREFIX)) {+ data = data.substring(SharedEvent.MESSAGE_PREFIX.length());+ if (data.startsWith("[")) {// Handle array of eventsSharedEvent[] events = sharedEventArrayMapper.read(data);for (SharedEvent event : events) {diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.javaindex 0e27455ee..e399c69e1 100644--- a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java+++ b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java@@ -33,23 +33,21 @@ import org.keycloak.adapters.KeycloakConfigResolver;import org.keycloak.adapters.KeycloakDeployment;import org.keycloak.adapters.KeycloakDeploymentBuilder;import org.keycloak.admin.client.resource.RealmsResource;-import org.keycloak.common.enums.SslRequired;import org.keycloak.representations.adapters.config.AdapterConfig;import org.keycloak.representations.idm.PublishedRealmRepresentation;-import org.keycloak.representations.idm.RealmRepresentation;import org.openremote.container.Container;import org.openremote.container.security.IdentityProvider;import org.openremote.container.web.ClientRequestInfo;import org.openremote.container.web.ProxyWebClientBuilder;import org.openremote.container.web.WebClient;import org.openremote.container.web.WebService;-import org.openremote.model.util.TextUtil;import javax.ws.rs.HttpMethod;import javax.ws.rs.NotFoundException;import javax.ws.rs.client.Client;import javax.ws.rs.core.Response;import javax.ws.rs.core.UriBuilder;+import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import java.util.logging.Level;@@ -70,7 +68,12 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {// an access token from authentication directly, which gives us full access to import/delete// demo data as needed.public static final String ADMIN_CLI_CLIENT_ID = "admin-cli";-+ public static final List<String> DEFAULT_CLIENTS = Arrays.asList(+ "account",+ ADMIN_CLI_CLIENT_ID,+ "broker",+ "master-realm",+ "security-admin-console");// TODO: Below is here ready for Resteasy 4.x but their maven packages are a mess at the moment// protected static class EngineBuilder extends ClientHttpEngineBuilder43 {@@ -126,26 +129,21 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {// return engine;// }// }-- private static final Logger LOG = Logger.getLogger(KeycloakIdentityProvider.class.getName());-public static final String KEYCLOAK_HOST = "KEYCLOAK_HOST";public static final String KEYCLOAK_HOST_DEFAULT = "localhost";public static final String KEYCLOAK_PORT = "KEYCLOAK_PORT";public static final int KEYCLOAK_PORT_DEFAULT = 8081;public static final String KEYCLOAK_CONNECT_TIMEOUT = "KEYCLOAK_CONNECT_TIMEOUT";public static final int KEYCLOAK_CONNECT_TIMEOUT_DEFAULT = 2000;-public static final String KEYCLOAK_REQUEST_TIMEOUT = "KEYCLOAK_REQUEST_TIMEOUT";public static final int KEYCLOAK_REQUEST_TIMEOUT_DEFAULT = 10000;public static final String KEYCLOAK_CLIENT_POOL_SIZE = "KEYCLOAK_CLIENT_POOL_SIZE";public static final int KEYCLOAK_CLIENT_POOL_SIZE_DEFAULT = 20;-public static final String IDENTITY_SESSION_MAX_MINUTES = "IDENTITY_SESSION_MAX_MINUTES";public static final int IDENTITY_SESSION_MAX_MINUTES_DEFAULT = 60 * 24; // 1 daypublic static final String IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES = "IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES";public static final int IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES_DEFAULT = 60 * 24 * 14; // 14 days-+ private static final Logger LOG = Logger.getLogger(KeycloakIdentityProvider.class.getName());// Each realm in Keycloak has a client application with this identifierfinal protected String clientId;@@ -154,27 +152,21 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {// The (internal) URI where Keycloak can be foundfinal protected UriBuilder keycloakServiceUri;-+ // Configuration options for new realms+ final protected int sessionTimeoutSeconds;+ final protected int sessionMaxSeconds;+ final protected int sessionOfflineTimeoutSeconds;+ // This will pass authentication ("NOT ATTEMPTED" state), but later fail any role authorization+ final protected KeycloakDeployment notAuthenticatedKeycloakDeployment = new KeycloakDeployment();// The client we use to access Keycloakprotected Client httpClient;-// Cache Keycloak deployment per realm/client so we don't have to access Keycloak for every token validationprotected LoadingCache<KeycloakRealmClient, KeycloakDeployment> keycloakDeploymentCache;-// The configuration for the Keycloak servlet extension, looks up the client application per realmprotected KeycloakConfigResolver keycloakConfigResolver;-// Optional reverse proxy that listens to AUTH_PATH and forwards requests to Keycloakprotected HttpHandler authProxyHandler;- // Configuration options for new realms- final protected int sessionTimeoutSeconds;- final protected int sessionMaxSeconds;- final protected int sessionOfflineTimeoutSeconds;-- // This will pass authentication ("NOT ATTEMPTED" state), but later fail any role authorization- final protected KeycloakDeployment notAuthenticatedKeycloakDeployment = new KeycloakDeployment();-@SuppressWarnings("deprecation")public KeycloakIdentityProvider(String clientId, UriBuilder externalServerUri, Container container) {this.clientId = clientId;@@ -306,15 +298,7 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {public KeycloakResource getExternalKeycloak() {return getTarget(httpClient, keycloakServiceUri.build(), null, null, externalServerUri.build())- .proxy(KeycloakResource.class);- }-- public RealmsResource getRealms(ClientRequestInfo clientRequestInfo) {- return getRealms(clientRequestInfo.getRemoteAddress(), clientRequestInfo.getAccessToken());- }-- public RealmsResource getRealms(String accessToken) {- return getRealms(null, accessToken);+ .proxy(KeycloakResource.class);}//There is a bug in {@link org.keycloak.admin.client.resource.UserStorageProviderResource#syncUsers} which misses the componentId as parameter@@ -337,41 +321,16 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {}/**- * @param forwardFor Must be the client source address if this is effectively a forwarded request and the- * access token was obtained by the client (can be null e.g. for Admin CLI calls during setup- * or tests where the access token was obtained directly). This should not be overloaded because- * we want to know who is calling this method with "null", as this can lead to subtle runtime- * problems.+ * Must be the client source address if this is effectively a forwarded request and the access token was obtained by+ * the client (can be null e.g. for Admin CLI calls during setup or tests where the access token was obtained+ * directly). This should not be overloaded because we want to know who is calling this method with "null", as this+ * can lead to subtle runtime problems.*/- final protected RealmsResource getRealms(String forwardFor, String accessToken) {- return getTarget(httpClient, keycloakServiceUri.build(), accessToken, forwardFor, forwardFor != null ? externalServerUri.build() : null)+ final protected RealmsResource getRealms(ClientRequestInfo clientRequestInfo) {+ return getTarget(httpClient, keycloakServiceUri.build(), clientRequestInfo.getAccessToken(), clientRequestInfo.getRemoteAddress(), clientRequestInfo.getRemoteAddress() != null ? externalServerUri.build() : null).proxy(RealmsResource.class);}- public void configureRealm(RealmRepresentation realmRepresentation, int accessTokenLifespanSeconds) {- realmRepresentation.setDisplayNameHtml(- realmRepresentation.getDisplayName().replaceAll("[^A-Za-z0-9]", "")- );- realmRepresentation.setAccessTokenLifespan(accessTokenLifespanSeconds);-- if(TextUtil.isNullOrEmpty(realmRepresentation.getLoginTheme())) {- realmRepresentation.setLoginTheme("openremote");- }- if(TextUtil.isNullOrEmpty(realmRepresentation.getAccountTheme())) {- realmRepresentation.setAccountTheme("openremote");- }- if(TextUtil.isNullOrEmpty(realmRepresentation.getEmailTheme())) {- realmRepresentation.setEmailTheme("openremote");- }-- realmRepresentation.setSsoSessionIdleTimeout(sessionTimeoutSeconds);- realmRepresentation.setSsoSessionMaxLifespan(sessionMaxSeconds);- realmRepresentation.setOfflineSessionIdleTimeout(sessionOfflineTimeoutSeconds);-- // Service-internal network (between manager and keycloak service containers) does not use SSL- realmRepresentation.setSslRequired(SslRequired.NONE.toString());- }-protected void waitForKeycloak() {boolean keycloakAvailable = false;while (!keycloakAvailable) {@@ -463,9 +422,9 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {LOG.info("Enabling auth reverse proxy (passing requests through to Keycloak) on web context: /" + KeycloakResource.KEYCLOAK_CONTEXT_PATH);webService.getRequestHandlers().add(0, pathStartsWithHandler(- "Keycloak auth proxy",- "/" + KeycloakResource.KEYCLOAK_CONTEXT_PATH,- authProxyHandler));+ "Keycloak auth proxy",+ "/" + KeycloakResource.KEYCLOAK_CONTEXT_PATH,+ authProxyHandler));}/**diff --git a/gradle.properties b/gradle.propertiesindex 81b8780fe..2b90c4e81 100644--- a/gradle.properties+++ b/gradle.properties@@ -1,4 +1,4 @@-org.gradle.jvmargs=-Xmx1g+org.gradle.jvmargs=-Xms256m -Xmx1024mopenremoteVersion = 3.0-SNAPSHOTdiff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex f146da45e..2615282a7 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -32,6 +32,7 @@ import org.openremote.container.timer.TimerService;import org.openremote.manager.agent.AgentService;import org.openremote.manager.datapoint.AssetDatapointService;import org.openremote.manager.event.ClientEventService;+import org.openremote.manager.event.EventSubscriptionAuthorizer;import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.rules.RulesService;import org.openremote.manager.security.ManagerIdentityService;@@ -161,56 +162,13 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerassetAttributeLinkingService = container.getService(AssetAttributeLinkingService.class);messageBrokerService = container.getService(MessageBrokerService.class);clientEventService = container.getService(ClientEventService.class);+ EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, assetStorageService);clientEventService.addSubscriptionAuthorizer((auth, subscription) -> {- if (!subscription.isEventType(AttributeEvent.class) && !subscription.isEventType(AssetEvent.class)) {+ if (!subscription.isEventType(AttributeEvent.class)) {return false;}-- // Only Asset ID filters allowed- if (subscription.getFilter() != null && !(subscription.getFilter() instanceof AssetFilter)) {- return false;- }-- // Attribute event subscriptions must have a filter, as you can't subscribe to ALL asset attribute events- if (subscription.isEventType(AttributeEvent.class) && subscription.getFilter() == null) {- return false;- }-- if (subscription.getFilter() != null) {- AssetFilter filter = (AssetFilter) subscription.getFilter();-- // Superuser can get events for any asset- if (auth.isSuperUser())- return true;-- // Regular user must have role- if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {- return false;- }-- boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth.getUserId());-- // Client can subscribe to several assets- for (String assetId : filter.getAssetIds()) {- Asset asset = assetStorageService.find(assetId);- // If the asset doesn't exist, subscription must fail- if (asset == null)- return false;- if (isRestrictedUser) {- // Restricted users can only get events for their linked assets- if (!assetStorageService.isUserAsset(auth.getUserId(), assetId))- return false;- // TODO Restricted clients should only receive events for PROTECTED attributes!- } else {- // Regular users can only get events for assets in their realm- if (!asset.getRealm().equals(auth.getAuthenticatedRealm()))- return false;- }- }- return true;- }- return false;+ return assetEventAuthorizer.apply(auth, subscription);});processors.add(gatewayService);diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex de173ef4e..c0a91928c 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -31,6 +31,7 @@ import org.openremote.container.security.AuthContext;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.console.ConsoleResourceImpl;import org.openremote.manager.event.ClientEventService;+import org.openremote.manager.event.EventSubscriptionAuthorizer;import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.rules.AssetQueryPredicate;import org.openremote.manager.security.ManagerIdentityService;@@ -44,6 +45,9 @@ import org.openremote.model.attribute.MetaItemDescriptor;import org.openremote.model.attribute.MetaItemType;import org.openremote.model.calendar.CalendarEvent;import org.openremote.model.event.TriggeredEventSubscription;+import org.openremote.model.event.shared.AssetInfo;+import org.openremote.model.event.shared.EventSubscription;+import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.event.shared.TenantFilter;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.LogicGroup;@@ -61,7 +65,6 @@ import org.postgresql.util.PGobject;import javax.persistence.EntityManager;import javax.persistence.NoResultException;import javax.persistence.TypedQuery;-import javax.ws.rs.WebApplicationException;import java.sql.*;import java.util.Date;import java.util.*;@@ -74,7 +77,7 @@ import java.util.stream.Collectors;import static java.util.stream.Collectors.groupingBy;import static java.util.stream.Collectors.joining;-import static javax.ws.rs.core.Response.Status.BAD_REQUEST;+import static org.apache.camel.builder.PredicateBuilder.or;import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;@@ -124,6 +127,76 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicpublic static final int PRIORITY = MED_PRIORITY;protected static String META_ITEM_RESTRICTED_READ_SQL_FRAGMENT;protected static String META_ITEM_PUBLIC_READ_SQL_FRAGMENT;++ public static <T extends SharedEvent & AssetInfo> EventSubscriptionAuthorizer assetInfoAuthorizer(ManagerIdentityService identityService, AssetStorageService assetStorageService) {++ return (auth, sub) -> {++ @SuppressWarnings("unchecked")+ EventSubscription<T> subscription = (EventSubscription<T>)sub;++ // Only Asset filters allowed+ if (subscription.getFilter() != null && !(subscription.getFilter() instanceof AssetFilter)) {+ return false;+ }++ AssetFilter<T> filter = (AssetFilter<T>) subscription.getFilter();++ // Superuser can get events for any asset+ if (auth.isSuperUser())+ return true;++ // Regular user must have role+ if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {+ return false;+ }++ boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth.getUserId());++ if (filter == null) {+ filter = new AssetFilter<>();+ subscription.setFilter(filter);+ }++ // Regular and restricted users can only subscribe to assets in their realm+ filter.setRealm(auth.getAuthenticatedRealm());++ // Restricted user can only subscribe to assets they are linked to+ boolean skipAssetIdCheck = false;+ if (isRestrictedUser && (filter.getAssetIds() == null || filter.getAssetIds().length == 0)) {+ filter.setAssetIds(+ assetStorageService.findUserAssets(auth.getAuthenticatedRealm(), auth.getUserId(), null)+ .stream()+ .map(userAsset -> userAsset.getId().getAssetId())+ .toArray(String[]::new)+ );+ skipAssetIdCheck = true;+ }++ if (!skipAssetIdCheck && filter.getAssetIds() != null) {+ // Client can subscribe to several assets+ for (String assetId : filter.getAssetIds()) {+ Asset asset = assetStorageService.find(assetId, false);+ // If the asset doesn't exist, subscription must fail+ if (asset == null)+ return false;+ if (isRestrictedUser) {+ // Restricted users can only get events for their linked assets+ if (!assetStorageService.isUserAsset(auth.getUserId(), assetId))+ return false;+ // TODO Restricted clients should only receive events for PROTECTED attributes!+ } else {+ // Regular users can only get events for assets in their realm+ if (!asset.getRealm().equals(auth.getAuthenticatedRealm()))+ return false;+ }+ }+ }++ return true;+ };+ };+protected TimerService timerService;protected PersistenceService persistenceService;protected ManagerIdentityService identityService;@@ -214,6 +287,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicidentityService = container.getService(ManagerIdentityService.class);clientEventService = container.getService(ClientEventService.class);gatewayService = container.getService(GatewayService.class);+ EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, this);META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =" ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";@@ -229,6 +303,14 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicClientRole.READ_ASSETS));+ clientEventService.addSubscriptionAuthorizer((auth, subscription) -> {+ if (!subscription.isEventType(AssetEvent.class)) {+ return false;+ }++ return assetEventAuthorizer.apply(auth, subscription);+ });+container.getService(ManagerWebService.class).getApiSingletons().add(new AssetModelResourceImpl(container.getService(TimerService.class),@@ -275,40 +357,69 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// React if a client wants to read assets and attributesfrom(CLIENT_EVENT_TOPIC).routeId("FromClientReadRequests")- .filter(exchange ->- (exchange.getIn().getBody() instanceof ReadAssetAttributesEvent) || (exchange.getIn().getBody() instanceof ReadAssetEvent))- .process(exchange -> {- ReadAssetEvent event = exchange.getIn().getBody(ReadAssetEvent.class);- LOG.fine("Handling from client: " + event);- boolean isAttributeRead = event instanceof ReadAssetAttributesEvent;+ .filter(+ or(body().isInstanceOf(ReadAssetsEvent.class), body().isInstanceOf(ReadAssetEvent.class)))+ .choice()+ .when(body().isInstanceOf(ReadAssetEvent.class))+ .process(exchange -> {+ ReadAssetEvent event = exchange.getIn().getBody(ReadAssetEvent.class);+ LOG.fine("Handling from client: " + event);+ boolean isAttributeRead = event instanceof ReadAssetAttributesEvent;++ if (event.getAssetId() == null || event.getAssetId().isEmpty())+ return;++ String sessionKey = getSessionKey(exchange);+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);++ // Superuser can get all, User must have role+ if (!authContext.isSuperUser() && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {+ return;+ }- if (event.getAssetId() == null || event.getAssetId().isEmpty())- return;+ Access access = authContext.isSuperUser() || !identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId()) ? PRIVATE : PROTECTED;- String sessionKey = getSessionKey(exchange);- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);+ Asset asset = find(+ new AssetQuery()+ .ids(event.getAssetId())+ .access(access));+ if (asset != null) {+ if (isAttributeRead) {+ replyWithAttributeEvents(sessionKey, event.getSubscriptionId(), asset, ((ReadAssetAttributesEvent) event).getAttributeNames());+ } else {+ replyWithAssetEvent(sessionKey, event.getSubscriptionId(), asset);+ }+ }+ })+ .when(body().isInstanceOf(ReadAssetsEvent.class))+ .process(exchange -> {+ ReadAssetsEvent readAssets = exchange.getIn().getBody(ReadAssetsEvent.class);+ AssetQuery query = readAssets.getAssetQuery();++ String sessionKey = getSessionKey(exchange);+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);++ // Superuser can get all, User must have role+ if (!authContext.isSuperUser() && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {+ return;+ }- // Superuser can get all, User must have role- if (!authContext.isSuperUser() && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {- return;- }+ Access access = authContext.isSuperUser() || !identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId()) ? PRIVATE : PROTECTED;- Access access = authContext.isSuperUser() || !identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId()) ? PRIVATE : PROTECTED;+ if (!authContext.isSuperUser()) {+ // Force realm to match users+ query.tenant(new TenantPredicate(authContext.getAuthenticatedRealm()));+ }- Asset asset = find(- new AssetQuery()- .ids(event.getAssetId())- .access(access));+ // Set access requirements+ query.access(access);- if (asset != null) {- if (isAttributeRead) {- replyWithAttributeEvents(sessionKey, event.getSubscriptionId(), asset, ((ReadAssetAttributesEvent) event).getAttributeNames());- } else {- replyWithAssetEvent(sessionKey, event.getSubscriptionId(), asset);- }- }- });+ List<Asset> assets = findAll(query);+ clientEventService.sendToSession(sessionKey, new AssetsEvent(readAssets.getMessageId(), assets));+ })+ .endChoice()+ .end();}public Asset find(String assetId) {@@ -323,7 +434,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicpublic Asset find(String assetId, boolean loadComplete) {if (assetId == null)throw new IllegalArgumentException("Can't query null asset identifier");- return find(new AssetQuery().select(loadComplete ? null : Select.selectExcludePathAndAttributes()).ids(assetId));+ return find(new AssetQuery().select(loadComplete ? null : Select.selectExcludeAll()).ids(assetId));}/**@@ -569,7 +680,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// If username presentUser user = null;if (!TextUtil.isNullOrEmpty(userName)) {- user = identityService.getIdentityProvider().getUser(asset.getRealm(), userName);+ user = identityService.getIdentityProvider().getUserByUsername(asset.getRealm(), userName);if (user == null) {String msg = "User not found: " + userName;LOG.info(msg);diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 2187d99cc..26b53f250 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -250,7 +250,7 @@ public class ClientEventService implements ContainerService {.otherwise().to(ClientEventService.CLIENT_EVENT_TOPIC).endChoice()- .when(header(HEADER_CONNECTION_TYPE).isNull())+ .when(header(HEADER_CONNECTION_TYPE).isNull()) // Outbound message to clients.split(method(eventSubscriptions, "splitForSubscribers")).process(exchange -> {String sessionKey = getSessionKey(exchange);diff --git a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.javaindex f842f85ed..ff40aa6ac 100644--- a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java+++ b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java@@ -67,7 +67,7 @@ public class EventTypeConverters implements TypeConverters {@Converterpublic String writeTriggeredEventSubscription(TriggeredEventSubscription triggeredEventSubscription, Exchange exchange) throws Exception {- return SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription);+ return TriggeredEventSubscription.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription);}@Converterdiff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.javaindex 575fe9939..e566ed01a 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java@@ -48,7 +48,6 @@ import org.openremote.model.gateway.GatewayConnectionStatusEvent;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.TenantPredicate;import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.syslog.SyslogEvent;import java.util.*;import java.util.logging.Level;@@ -312,7 +311,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi// Force realm to be the one that this client is associated withquery.tenant(new TenantPredicate(connection.getLocalRealm()));List<Asset> assets = assetStorageService.findAll(readAssets.getAssetQuery());- sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(new AssetsEvent(readAssets.getName(), assets)));+ sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(new AssetsEvent(readAssets.getMessageId(), assets)));}}}diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.javaindex c17e041be..61ab77b8c 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java@@ -291,14 +291,14 @@ public class GatewayConnector {return;}- if (!expectedSyncResponseName.equalsIgnoreCase(e.getName())) {- LOG.info("Unexpected response from gateway so ignoring (expected=" + expectedSyncResponseName + ", actual =" + e.getName() + "): " + e);+ if (!expectedSyncResponseName.equalsIgnoreCase(e.getMessageId())) {+ LOG.info("Unexpected response from gateway so ignoring (expected=" + expectedSyncResponseName + ", actual =" + e.getMessageId() + "): " + e);return;}syncProcessorFuture.cancel(true);syncProcessorFuture = null;- boolean isInitialResponse = ASSET_READ_EVENT_NAME_INITIAL.equalsIgnoreCase(e.getName());+ boolean isInitialResponse = ASSET_READ_EVENT_NAME_INITIAL.equalsIgnoreCase(e.getMessageId());if (isInitialResponse) {diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.javaindex ce3ec7094..de674683f 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java@@ -516,12 +516,10 @@ public class GatewayService extends RouteBuilder implements ContainerService, AsclientRepresentation.setSecret(secret);LOG.info("Creating gateway keycloak client for gateway id: " + gateway.getId());+ clientRepresentation = identityProvider.createClient(gateway.getRealm(), clientRepresentation);- ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(gateway.getRealm()).clients();- Response response = clientsResource.create(clientRepresentation);- response.close();- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {- LOG.warning("Failed to create Keycloak client for gateway '" + gateway.getId() + "' Response=" + response.getStatus());+ if (clientRepresentation == null) {+ LOG.warning("Failed to create Keycloak client for gateway '" + gateway.getId());} else {gateway.getAttribute("clientId").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(clientId)));gateway.getAttribute("clientSecret").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(secret)));@@ -535,12 +533,9 @@ public class GatewayService extends RouteBuilder implements ContainerService, As}protected void updateGatewayClient(GatewayConnector connector, boolean enabled) {- final ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(connector.getRealm()).clients();- clientsResource.findByClientId(GATEWAY_CLIENT_ID_PREFIX + connector.gatewayId).stream().findFirst().ifPresent(- clientRepresentation -> {- clientRepresentation.setEnabled(enabled);- clientsResource.get(clientRepresentation.getId()).update(clientRepresentation);- });+ ClientRepresentation client = identityProvider.getClient(connector.getRealm(), GATEWAY_CLIENT_ID_PREFIX + connector.gatewayId);+ client.setEnabled(enabled);+ identityProvider.updateClient(connector.getRealm(), client);}protected void destroyGatewayClient(Asset gateway) {@@ -550,16 +545,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, Asreturn;}- final ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(gateway.getRealm()).clients();- clientsResource.findByClientId(id).stream().findFirst().ifPresent(- clientRepresentation -> {- clientsResource.get(clientRepresentation.getId()).remove();- });- }-- protected ClientRequestInfo getClientRequestInfo() {- String accessToken = identityProvider.getAdminAccessToken(null);- return new ClientRequestInfo(null, accessToken);+ identityProvider.deleteClient(gateway.getRealm(), id);}protected Consumer<Object> createConnectorMessageConsumer(String sessionId) {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.javaindex 60bdd681a..46bf3fe9a 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java@@ -20,17 +20,11 @@package org.openremote.manager.mqtt;import io.moquette.broker.security.IAuthenticator;-import org.keycloak.admin.client.resource.ClientResource;-import org.keycloak.admin.client.resource.ClientsResource;-import org.keycloak.admin.client.resource.RealmResource;import org.keycloak.representations.idm.ClientRepresentation;-import org.openremote.container.web.ClientRequestInfo;-import org.openremote.manager.security.ManagerIdentityProvider;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;+import org.openremote.model.security.Tenant;-import javax.ws.rs.NotFoundException;import java.nio.charset.StandardCharsets;-import java.util.List;import java.util.logging.Logger;public class KeycloakAuthenticator implements IAuthenticator {@@ -52,26 +46,22 @@ public class KeycloakAuthenticator implements IAuthenticator {realm = realm.substring(0, indexSplit);}- RealmResource realmResource = identityProvider.getRealms(getClientRequestInfo()).realm(realm);- if (realmResource == null) {+ Tenant tenant = identityProvider.getTenant(realm);+ if (tenant == null) {LOG.info("Realm not found");return false;}- List<ClientRepresentation> clientRepresentations = realmResource.clients().findByClientId(clientId);- if (clientRepresentations.isEmpty()) {+ ClientRepresentation client = identityProvider.getClient(realm, clientId);++ if (client == null) {LOG.info("Client not found");return false;}String suppliedClientSecret = new String(password, StandardCharsets.UTF_8);- String clientSecret = realmResource.clients().get(clientRepresentations.get(0).getId()).getSecret().getValue();+ String clientSecret = client.getSecret();return suppliedClientSecret.equals(clientSecret);}-- private ClientRequestInfo getClientRequestInfo() {- String accessToken = identityProvider.getAdminAccessToken(null);- return new ClientRequestInfo(null, accessToken);- }}diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.javaindex 2c8d94cae..f96d4183f 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java@@ -24,7 +24,6 @@ import org.openremote.container.Container;import org.openremote.container.security.AuthContext;import org.openremote.container.security.basic.BasicIdentityProvider;import org.openremote.container.security.basic.PasswordStorage;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.manager.persistence.ManagerPersistenceService;import org.openremote.model.event.shared.TenantFilter;import org.openremote.model.query.UserQuery;@@ -77,7 +76,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemenUser adminUser = new User();adminUser.setUsername(MASTER_REALM_ADMIN_USER);- createUser(null, MASTER_REALM, adminUser, adminPassword);+ createUser(MASTER_REALM, adminUser, adminPassword);}}@@ -87,7 +86,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}@Override- public User[] getUsers(ClientRequestInfo clientRequestInfo, String realm) {+ public User[] getUsers(String realm) {return getUsers(new UserQuery().tenant(new TenantPredicate(realm)));}@@ -102,23 +101,23 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}@Override- public User getUser(ClientRequestInfo clientRequestInfo, String realm, String userId) {+ public User getUser(String realm, String userId) {return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, realm, userId);}@Override- public User getUser(String realm, String username) {+ public User getUserByUsername(String realm, String username) {return ManagerIdentityProvider.getUserByUsernameFromDb(persistenceService, realm, username);}@Override- public void updateUser(ClientRequestInfo clientRequestInfo, String realm, String userId, User user) {+ public void updateUser(String realm, User user) {LOG.info("Updating user: " + user);persistenceService.doTransaction(em -> em.merge(user));}@Override- public void createUser(ClientRequestInfo clientRequestInfo, String realm, User user, String password) {+ public User createUser(String realm, User user, String password) {if (!realm.equals(MASTER_REALM)) {throw new UnsupportedOperationException("This provider does not support realms other than master");}@@ -128,6 +127,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}LOG.info("Creating user: " + user);+ user.setId(UUID.randomUUID().toString());persistenceService.doTransaction(em -> em.unwrap(Session.class).doWork(connection -> {String sql = "insert into PUBLIC.USER_ENTITY(ID, REALM_ID, USERNAME, PASSWORD, FIRST_NAME, LAST_NAME, EMAIL, ENABLED) values (?, ?, ?, ?, ?, ?, ?, ?)";try (PreparedStatement st = connection.prepareStatement(sql)) {@@ -142,10 +142,11 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemenst.executeUpdate();}}));+ return user;}@Override- public void deleteUser(ClientRequestInfo clientRequestInfo, String realm, String userId) {+ public void deleteUser(String realm, String userId) {LOG.info("Deleting user: " + userId);persistenceService.doTransaction(em -> {User user = em.find(User.class, userId);@@ -158,24 +159,24 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}@Override- public void resetPassword(ClientRequestInfo clientRequestInfo, String realm, String userId, Credential credential) {+ public void resetPassword(String realm, String userId, Credential credential) {throw new UnsupportedOperationException("This provider does not support password reset");}@Override- public Role[] getRoles(ClientRequestInfo clientRequestInfo, String realm, String userId) {+ public Role[] getRoles(String realm, String userId) {return ClientRole.ALL_ROLES.stream().map(role -> new Role(UUID.randomUUID().toString(), role, false, true)).toArray(Role[]::new);}@Override- public void updateRoles(ClientRequestInfo clientRequestInfo, String realm, String userId, Role[] roles) {+ public void updateRoles(String realm, String username, ClientRole[] roles) {throw new UnsupportedOperationException("This provider does not support updating roles");}@Override- public boolean isMasterRealmAdmin(ClientRequestInfo clientRequestInfo, String userId) {+ public boolean isMasterRealmAdmin(String userId) {return true;}@@ -200,17 +201,17 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen}@Override- public void updateTenant(ClientRequestInfo clientRequestInfo, String realm, Tenant tenant) {+ public void updateTenant(Tenant tenant) {throw new UnsupportedOperationException("This provider does not support modifying tenants");}@Override- public void createTenant(ClientRequestInfo clientRequestInfo, Tenant tenant, Container container) {+ public Tenant createTenant(Tenant tenant) {throw new UnsupportedOperationException("This provider does not support multiple tenants");}@Override- public void deleteTenant(ClientRequestInfo clientRequestInfo, String realm) {+ public void deleteTenant(String realm) {throw new UnsupportedOperationException("This provider does not support multiple tenants");}diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.javaindex c11b391bb..88c8fb448 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java@@ -19,11 +19,9 @@*/package org.openremote.manager.security;-import org.openremote.container.Container;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.AuthContext;import org.openremote.container.security.IdentityProvider;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.model.event.shared.TenantFilter;import org.openremote.model.query.UserQuery;import org.openremote.model.security.*;@@ -36,7 +34,7 @@ import java.util.stream.IntStream;import static org.openremote.model.Constants.MASTER_REALM;-// TODO: Remove use of ClientRequestInfo and normalise interface for Basic and Keycloak providers+// TODO: Normalise interface for Basic and Keycloak providers and add client CRUD/*** SPI for implementations used by {@link ManagerIdentityService}, provides CRUD of* {@link User} and {@link Tenant}.@@ -46,29 +44,29 @@ public interface ManagerIdentityProvider extends IdentityProvider {String SETUP_ADMIN_PASSWORD = "SETUP_ADMIN_PASSWORD";String SETUP_ADMIN_PASSWORD_DEFAULT = "secret";- User[] getUsers(ClientRequestInfo clientRequestInfo, String realm);+ User[] getUsers(String realm);User[] getUsers(List<String> userIds);User[] getUsers(UserQuery userQuery);- User getUser(ClientRequestInfo clientRequestInfo, String realm, String userId);+ User getUser(String realm, String userId);- User getUser(String realm, String username);+ User getUserByUsername(String realm, String username);- void updateUser(ClientRequestInfo clientRequestInfo, String realm, String userId, User user);+ void updateUser(String realm, User user);- void createUser(ClientRequestInfo clientRequestInfo, String realm, User user, String password);+ User createUser(String realm, User user, String password);- void deleteUser(ClientRequestInfo clientRequestInfo, String realm, String userId);+ void deleteUser(String realm, String userId);- void resetPassword(ClientRequestInfo clientRequestInfo, String realm, String userId, Credential credential);+ void resetPassword(String realm, String userId, Credential credential);- Role[] getRoles(ClientRequestInfo clientRequestInfo, String realm, String userId);+ Role[] getRoles(String realm, String userId);- void updateRoles(ClientRequestInfo clientRequestInfo, String realm, String userId, Role[] roles);+ void updateRoles(String realm, String username, ClientRole[] roles);- boolean isMasterRealmAdmin(ClientRequestInfo clientRequestInfo, String userId);+ boolean isMasterRealmAdmin(String userId);boolean isRestrictedUser(String userId);@@ -78,11 +76,11 @@ public interface ManagerIdentityProvider extends IdentityProvider {Tenant getTenant(String realm);- void updateTenant(ClientRequestInfo clientRequestInfo, String realm, Tenant tenant);+ void updateTenant(Tenant tenant);- void createTenant(ClientRequestInfo clientRequestInfo, Tenant tenant, Container container);+ Tenant createTenant(Tenant tenant);- void deleteTenant(ClientRequestInfo clientRequestInfo, String realm);+ void deleteTenant(String realm);boolean isTenantActiveAndAccessible(AuthContext authContext, Tenant tenant);diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex b96844875..5d47e29ad 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -19,12 +19,10 @@*/package org.openremote.manager.security;-import io.undertow.util.Headers;import org.apache.camel.ExchangePattern;-import org.keycloak.TokenVerifier;import org.keycloak.admin.client.resource.*;-import org.keycloak.common.VerificationException;-import org.keycloak.representations.AccessToken;+import org.keycloak.admin.client.resource.UserResource;+import org.keycloak.common.enums.SslRequired;import org.keycloak.representations.idm.*;import org.openremote.container.Container;import org.openremote.container.message.MessageBrokerService;@@ -52,6 +50,7 @@ import javax.ws.rs.core.UriBuilder;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;+import java.util.stream.Collectors;import static org.openremote.container.security.IdentityService.IDENTITY_NETWORK_HOST;import static org.openremote.container.security.IdentityService.IDENTITY_NETWORK_HOST_DEFAULT;@@ -64,9 +63,16 @@ import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_EMAIL_FROimport static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_EMAIL_FROM_KEYCLOAK_DEFAULT;import static org.openremote.model.Constants.*;+/**+ * All keycloak interaction is done through the admin-cli client; security is implemented downstream of here; anything+ * in the same process as this service has privileged access to keycloak.+ */public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider implements ManagerIdentityProvider {private static final Logger LOG = Logger.getLogger(ManagerKeycloakIdentityProvider.class.getName());+ public static final String REALM_KEYCLOAK_THEME_SUFFIX = "_REALM_KEYCLOAK_THEME";+ public static final String DEFAULT_REALM_KEYCLOAK_THEME = "DEFAULT_REALM_KEYCLOAK_THEME";+ public static final String DEFAULT_REALM_KEYCLOAK_THEME_DEFAULT = "openremote";final boolean devMode;final protected PersistenceService persistenceService;@@ -75,10 +81,12 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imfinal protected ClientEventService clientEventService;final protected ConsoleAppService consoleAppService;final protected String keycloakAdminPassword;+ final protected Container container;public ManagerKeycloakIdentityProvider(UriBuilder externalServerUri, Container container) {super(KEYCLOAK_CLIENT_ID, externalServerUri, container);+ this.container = container;this.keycloakAdminPassword = container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT);this.devMode = container.isDevMode();this.timerService = container.getService(TimerService.class);@@ -107,7 +115,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}@Override- public User[] getUsers(ClientRequestInfo clientRequestInfo, String realm) {+ public User[] getUsers(String realm) {return getUsers(new UserQuery().tenant(new TenantPredicate(realm)));}@@ -122,27 +130,32 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}@Override- public User getUser(ClientRequestInfo clientRequestInfo, String realm, String userId) {+ public User getUser(String realm, String userId) {return ManagerIdentityProvider.getUserByIdFromDb(persistenceService, realm, userId);}@Override- public User getUser(String realm, String username) {+ public User getUserByUsername(String realm, String username) {return ManagerIdentityProvider.getUserByUsernameFromDb(persistenceService, realm, username);}@Override- public void updateUser(ClientRequestInfo clientRequestInfo, String realm, String userId, User user) {- getRealms(clientRequestInfo)- .realm(realm).users().get(userId).update(- convert(Container.JSON, UserRepresentation.class, user)- );+ public void updateUser(String realm, User user) {+ // User only has a subset of user representation so overlay on actual user representation+ UserResource userResource = getRealms().realm(realm).users().get(user.getId());+ UserRepresentation userRepresentation = userResource.toRepresentation();+ userRepresentation.setUsername(user.getUsername());+ userRepresentation.setFirstName(user.getFirstName());+ userRepresentation.setLastName(user.getLastName());+ userRepresentation.setEmail(user.getEmail());+ userRepresentation.setEnabled(user.getEnabled());+ userResource.update(userRepresentation);}@Override- public void createUser(ClientRequestInfo clientRequestInfo, String realm, User user, String password) {- Response response = getRealms(clientRequestInfo)- .realm(realm).users().create(+ public User createUser(String realm, User user, String password) {+ RealmResource realmResource = getRealms().realm(realm);+ Response response = realmResource.users().create(convert(Container.JSON, UserRepresentation.class, user));if (!response.getStatusInfo().equals(Response.Status.CREATED)) {@@ -153,12 +166,23 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im);} else {response.close();+ user = getUserByUsername(realm, user.getUsername());+ if (user != null && !TextUtil.isNullOrEmpty(password)) {+ CredentialRepresentation credentials = new CredentialRepresentation();+ credentials.setType("password");+ credentials.setValue(password);+ credentials.setTemporary(false);+ realmResource.users().get(user.getId()).resetPassword(credentials);+ LOG.info("Created user '" + user.getUsername() + "' with password '" + password + "'");+ }}++ return user;}@Override- public void deleteUser(ClientRequestInfo clientRequestInfo, String realm, String userId) {- Response response = getRealms(clientRequestInfo)+ public void deleteUser(String realm, String userId) {+ Response response = getRealms().realm(realm).users().delete(userId);if (!response.getStatusInfo().equals(Response.Status.NO_CONTENT)) {throw new WebApplicationException(@@ -172,17 +196,18 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}@Override- public void resetPassword(ClientRequestInfo clientRequestInfo, String realm, String userId, Credential credential) {- getRealms(clientRequestInfo)+ public void resetPassword(String realm, String userId, Credential credential) {+ getRealms().realm(realm).users().get(userId).resetPassword(convert(Container.JSON, CredentialRepresentation.class, credential));}@Override- public Role[] getRoles(ClientRequestInfo clientRequestInfo, String realm, String userId) {- RoleMappingResource roleMappingResource = getRealms(clientRequestInfo).realm(realm).users().get(userId).roles();- ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();+ public Role[] getRoles(String realm, String userId) {+ RealmResource realmResource = getRealms().realm(realm);+ RoleMappingResource roleMappingResource = realmResource.users().get(userId).roles();+ ClientsResource clientsResource = realmResource.clients();String clientId = clientsResource.findByClientId(KEYCLOAK_CLIENT_ID).get(0).getId();RolesResource rolesResource = clientsResource.get(clientId).roles();@@ -211,36 +236,47 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}@Override- public void updateRoles(ClientRequestInfo clientRequestInfo, String realm, String userId, Role[] roles) {- RoleMappingResource roleMappingResource = getRealms(clientRequestInfo).realm(realm).users().get(userId).roles();- ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();+ public void updateRoles(String realm, String userId, ClientRole[] roles) {+ RealmResource realmResource = getRealms().realm(realm);+ UserRepresentation user = realmResource.users().get(userId).toRepresentation();- String clientId = clientsResource.findByClientId(KEYCLOAK_CLIENT_ID).get(0).getId();+ if (user == null) {+ throw new IllegalStateException("Multiple users with the same username found");+ }- List<RoleRepresentation> rolesToAdd = new ArrayList<>();- List<RoleRepresentation> rolesToRemove = new ArrayList<>();+ RoleMappingResource roleMappingResource = realmResource.users().get(user.getId()).roles();+ ClientRepresentation client = getClient(realm, KEYCLOAK_CLIENT_ID);- for (Role role : roles) {- RoleRepresentation roleRepresentation = new RoleRepresentation();- roleRepresentation.setId(role.getId());- roleRepresentation.setName(role.getName());- if (role.isAssigned()) {- rolesToAdd.add(roleRepresentation);- } else {- rolesToRemove.add(roleRepresentation);- }- }+ // Get all role mappings for user on this client and remove any no longer in the roles+ List<RoleRepresentation> clientMappedRoles = roleMappingResource.clientLevel(client.getId()).listAll();+ List<RoleRepresentation> availableRoles = roleMappingResource.clientLevel(client.getId()).listAvailable();++ // Get newly defined roles+ List<RoleRepresentation> addRoles = Arrays.stream(roles)+ .filter(cr -> clientMappedRoles.stream().noneMatch(r -> r.getName().equals(cr.getValue())))+ .map(cr -> availableRoles.stream().filter(r -> r.getName().equals(cr.getValue())).findFirst().orElse(null))+ .collect(Collectors.toList());- roleMappingResource.clientLevel(clientId).add(rolesToAdd);- roleMappingResource.clientLevel(clientId).remove(rolesToRemove);+ // Remove obsolete roles+ List<RoleRepresentation> removeRoles = clientMappedRoles.stream()+ .filter(r -> Arrays.stream(roles).noneMatch(cr -> cr.getValue().equals(r.getName())))+ .collect(Collectors.toList());++ if (!removeRoles.isEmpty()) {+ roleMappingResource.clientLevel(client.getId()).remove(removeRoles);+ }+ if (!addRoles.isEmpty()) {+ roleMappingResource.clientLevel(client.getId()).add(addRoles);+ }}@Override- public boolean isMasterRealmAdmin(ClientRequestInfo clientRequestInfo, String userId) {- List<UserRepresentation> adminUsers =- // TODO Why null here in getRealms()?- getRealms(null, clientRequestInfo.getAccessToken()).realm(MASTER_REALM)- .users().search(MASTER_REALM_ADMIN_USER, null, null);+ public boolean isMasterRealmAdmin(String userId) {+ List<UserRepresentation> adminUsers = getRealms()+ .realm(MASTER_REALM)+ .users()+ .search(MASTER_REALM_ADMIN_USER, null, null);+if (adminUsers.size() == 0) {throw new IllegalStateException("Can't load master realm admin user");} else if (adminUsers.size() > 1) {@@ -256,42 +292,130 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im@Overridepublic Tenant getTenant(String realm) {- return ManagerIdentityProvider.getTenantFromDb(persistenceService, realm);+ RealmRepresentation realmRepresentation = getRealms().realm(realm).toRepresentation();+ return convert(Container.JSON, Tenant.class, realmRepresentation);}@Override- public void updateTenant(ClientRequestInfo clientRequestInfo, String realm, Tenant tenant) {+ public void updateTenant(Tenant tenant) {LOG.fine("Update tenant: " + tenant);+ RealmsResource realmsResource = getRealms();- getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo))).realm(realm).update(- convert(Container.JSON, RealmRepresentation.class, tenant)- );+ // Find existing realm by ID as realm name could have been changed+ RealmRepresentation existing = realmsResource.findAll().stream().filter(r -> r.getId().equals(tenant.getId())).findFirst().orElse(null);+ if (existing == null) {+ throw new IllegalStateException("Tenant must already exist, ID does not match an existing tenant");+ }++ String realm = existing.getRealm();++ // Tenant only has a subset of realm representation so overlay on actual realm representation+ existing.setRealm(tenant.getRealm());+ existing.setDisplayName(tenant.getDisplayName());+ existing.setAccountTheme(tenant.getAccountTheme());+ existing.setAdminTheme(tenant.getAdminTheme());+ existing.setEmailTheme(tenant.getEmailTheme());+ existing.setLoginTheme(tenant.getLoginTheme());+ existing.setRememberMe(tenant.getRememberMe());+ existing.setEnabled(tenant.getEnabled());+ existing.setDuplicateEmailsAllowed(tenant.getDuplicateEmailsAllowed());+ existing.setResetPasswordAllowed(tenant.getResetPasswordAllowed());+ existing.setNotBefore(tenant.getNotBefore() != null ? tenant.getNotBefore().intValue() : null);+ configureRealm(existing);++ realmsResource.realm(realm).update(existing);publishModification(PersistenceEvent.Cause.UPDATE, tenant);}@Override- public void createTenant(ClientRequestInfo clientRequestInfo, Tenant tenant, Container container) {+ public Tenant createTenant(Tenant tenant) {LOG.fine("Create tenant: " + tenant);+ RealmsResource realmsResource = getRealms();RealmRepresentation realmRepresentation = convert(Container.JSON, RealmRepresentation.class, tenant);- // TODO This is not atomic, write compensation actions- getRealms(clientRequestInfo).create(realmRepresentation);- realmRepresentation = getRealms(clientRequestInfo).realm(tenant.getRealm()).toRepresentation();- configureRealm(realmRepresentation, container);- clientRequestInfo = new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo));- createClientApplication(clientRequestInfo, realmRepresentation.getRealm());+ realmsResource.create(realmRepresentation);+ realmRepresentation = realmsResource.realm(tenant.getRealm()).toRepresentation();+ configureRealm(realmRepresentation);+ createOpenRemoteClientApplication(realmRepresentation.getRealm());publishModification(PersistenceEvent.Cause.CREATE, tenant);+ return convert(Container.JSON, Tenant.class, realmRepresentation);}@Override- public void deleteTenant(ClientRequestInfo clientRequestInfo, String realm) {+ public void deleteTenant(String realm) {Tenant tenant = getTenant(realm);+if (tenant != null) {LOG.fine("Delete tenant: " + realm);- getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo))).realm(realm).remove();+ getRealms().realm(realm).remove();publishModification(PersistenceEvent.Cause.DELETE, tenant);}}+ // TODO: Provide an implementation agnostic client+ public ClientRepresentation getClient(String realm, String clientId) {+ ClientsResource clientsResource = getRealms().realm(realm).clients();+ List<ClientRepresentation> clients = clientsResource.findByClientId(clientId);+ if (clients.isEmpty()) {+ return null;+ }++ // Need to get secret separately for some reason+ ClientRepresentation client = clients.get(0);+ CredentialRepresentation credentialRepresentation = clientsResource.get(client.getId()).getSecret();+ if (credentialRepresentation != null) {+ client.setSecret(credentialRepresentation.getValue());+ }+ return client;+ }++ // TODO: Provide an implementation agnostic client+ public ClientRepresentation[] getClients(String realm) {+ return getRealms().realm(realm).clients().findAll().toArray(new ClientRepresentation[0]);+ }++ // TODO: Provide an implementation agnostic client+ public ClientRepresentation createClient(String realm, ClientRepresentation client) {+ ClientsResource clientsResource = getRealms().realm(realm).clients();+ Response response = clientsResource.create(client);+ response.close();+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {+ return null;+ }++ client = clientsResource.findByClientId(client.getClientId()).get(0);+ ClientResource clientResource = clientsResource.get(client.getId());+ addDefaultRoles(clientResource.roles());+ return client;+ }++ // TODO: Provide an implementation agnostic client+ public void updateClient(String realm, ClientRepresentation client) {+ getRealms().realm(realm).clients().get(client.getId()).update(client);+ }++ public void deleteClient(String realm, String clientId) {+ getRealms().realm(realm).clients().findByClientId(clientId).forEach(client ->+ deleteClient(realm, client));+ }++ public void deleteClient(String realm, ClientRepresentation client) {+ getRealms().realm(realm).clients().get(client.getId()).remove();+ }++ // TODO: This shouldn't really be exposed as identity provider shouldn't leak implementation+ public RealmsResource getRealms() {+ return super.getRealms(new ClientRequestInfo(null, getAdminAccessToken()));+ }++ public User getClientServiceUser(String realm, String clientId) {+ ClientRepresentation client = getClient(realm, clientId);+ if (client == null) {+ return null;+ }+ UserRepresentation user = getRealms().realm(realm).clients().get(client.getId()).getServiceAccountUser();+ return user != null ? convert(Container.JSON, User.class, user) : null;+ }+/*** @return <code>true</code> if the user is the superuser (admin) or if the user is authenticated* in the same realm as the tenant and the tenant is active.@@ -357,11 +481,33 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}return false;-}- public void configureRealm(RealmRepresentation realmRepresentation, Container container) {- configureRealm(realmRepresentation, Constants.ACCESS_TOKEN_LIFESPAN_SECONDS);+ public void configureRealm(RealmRepresentation realmRepresentation) {++ realmRepresentation.setAccessTokenLifespan(Constants.ACCESS_TOKEN_LIFESPAN_SECONDS);++ String themeName = getString(container.getConfig(), realmRepresentation.getRealm().toUpperCase(Locale.ROOT) + REALM_KEYCLOAK_THEME_SUFFIX, getString(container.getConfig(), DEFAULT_REALM_KEYCLOAK_THEME, DEFAULT_REALM_KEYCLOAK_THEME_DEFAULT));++ if(TextUtil.isNullOrEmpty(realmRepresentation.getLoginTheme())) {+ realmRepresentation.setLoginTheme(themeName);+ }+ if(TextUtil.isNullOrEmpty(realmRepresentation.getAccountTheme())) {+ realmRepresentation.setAccountTheme(themeName);+ }+ if(TextUtil.isNullOrEmpty(realmRepresentation.getEmailTheme())) {+ realmRepresentation.setEmailTheme(themeName);+ }++ realmRepresentation.setDisplayNameHtml(+ realmRepresentation.getDisplayName().replaceAll("[^A-Za-z0-9]", "")+ );+ realmRepresentation.setSsoSessionIdleTimeout(sessionTimeoutSeconds);+ realmRepresentation.setSsoSessionMaxLifespan(sessionMaxSeconds);+ realmRepresentation.setOfflineSessionIdleTimeout(sessionOfflineTimeoutSeconds);++ // Service-internal network (between manager and keycloak service containers) does not use SSL+ realmRepresentation.setSslRequired(SslRequired.NONE.toString());// Configure SMTPString host = container.getConfig().getOrDefault(SETUP_EMAIL_HOST, null);@@ -400,39 +546,18 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im}}- public void createClientApplication(ClientRequestInfo clientRequestInfo, String realm) {- ClientRepresentation client = createDefaultClientRepresentation(- realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode- );- createClientApplication(clientRequestInfo, realm, client);- }-- public void createClientApplication(ClientRequestInfo clientRequestInfo, String realm, ClientRepresentation client) {- ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();- clientsResource.create(client);- client = clientsResource.findByClientId(client.getClientId()).get(0);- ClientResource clientResource = clientsResource.get(client.getId());- addDefaultRoles(clientResource.roles());+ public void createOpenRemoteClientApplication(String realm) {+ createClient(realm, createDefaultClientRepresentation(realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode));}/*** Keycloak only allows realm CRUD using the {realm}-realm client or the admin-cli client so we need to ensure we* have a token for one of these realms; if we are creating a realm then that means using the admin-cli*/- public String getAdminAccessToken(ClientRequestInfo clientRequestInfo) {- try {- AccessToken token = clientRequestInfo != null ? TokenVerifier.create(clientRequestInfo.getAccessToken(), AccessToken.class).getToken() : null;-- if (token == null || !token.getIssuedFor().equals(ADMIN_CLI_CLIENT_ID)) {- return getKeycloak().getAccessToken(- MASTER_REALM, new PasswordAuthForm(ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)- ).getToken();- }- return clientRequestInfo.getAccessToken();- } catch (VerificationException e) {- LOG.log(Level.WARNING, "Failed to parse access token", e);- return clientRequestInfo.getAccessToken();- }+ protected String getAdminAccessToken() {+ return getKeycloak().getAccessToken(+ MASTER_REALM, new PasswordAuthForm(ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)+ ).getToken();}protected ClientRepresentation createDefaultClientRepresentation(String realm, String clientId, String appName, boolean devMode) {@@ -509,8 +634,10 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im);}- public String addLDAPConfiguration(ClientRequestInfo clientRequestInfo, String realm, ComponentRepresentation componentRepresentation) {- RealmResource realmResource = getRealms(clientRequestInfo)+ public String addLDAPConfiguration(String realm, ComponentRepresentation componentRepresentation) {+ ClientRequestInfo clientRequestInfo = new ClientRequestInfo(null, getAdminAccessToken());++ RealmResource realmResource = getRealms().realm(realm);Response response = realmResource.components().add(componentRepresentation);@@ -542,8 +669,9 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider imreturn componentRepresentation.getId();}- public String addLDAPMapper(ClientRequestInfo clientRequestInfo, String realm, ComponentRepresentation componentRepresentation) {- RealmResource realmResource = getRealms(clientRequestInfo)+ public String addLDAPMapper(String realm, ComponentRepresentation componentRepresentation) {+ ClientRequestInfo clientRequestInfo = new ClientRequestInfo(null, getAdminAccessToken());+ RealmResource realmResource = getRealms().realm(realm);Response response = realmResource.components().add(componentRepresentation);diff --git a/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.javaindex 6aced13de..5ff33bb31 100644--- a/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java@@ -19,9 +19,9 @@*/package org.openremote.manager.security;+import org.apache.http.HttpStatus;import org.openremote.container.Container;import org.openremote.container.timer.TimerService;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.manager.i18n.I18NService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.security.Tenant;@@ -95,10 +95,12 @@ public class TenantResourceImpl extends ManagerWebResource implements TenantReso}try {identityService.getIdentityProvider().updateTenant(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, tenant+ tenant);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());+ } catch (IllegalArgumentException ex) {+ throw new WebApplicationException(ex.getCause(), HttpStatus.SC_CONFLICT);} catch (Exception ex) {throw new WebApplicationException(ex);}@@ -111,7 +113,7 @@ public class TenantResourceImpl extends ManagerWebResource implements TenantReso}try {identityService.getIdentityProvider().createTenant(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), tenant, container+ tenant);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -137,7 +139,7 @@ public class TenantResourceImpl extends ManagerWebResource implements TenantReso}try {identityService.getIdentityProvider().deleteTenant(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm+ realm);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());diff --git a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.javaindex c9093cabb..954d4c39b 100644--- a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java@@ -20,26 +20,19 @@package org.openremote.manager.security;import org.openremote.container.timer.TimerService;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.manager.i18n.I18NService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.http.ConstraintViolation;import org.openremote.model.http.ConstraintViolationReport;import org.openremote.model.http.RequestParams;-import org.openremote.model.security.Credential;-import org.openremote.model.security.Role;-import org.openremote.model.security.User;-import org.openremote.model.security.UserResource;+import org.openremote.model.security.*;import javax.ws.rs.BeanParam;import javax.ws.rs.ClientErrorException;import javax.ws.rs.ForbiddenException;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.Response;-import java.util.ArrayList;-import java.util.List;-import java.util.Objects;-import java.util.ResourceBundle;+import java.util.*;import static javax.ws.rs.core.Response.Status.BAD_REQUEST;import static org.openremote.model.Constants.MASTER_REALM;@@ -55,7 +48,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcepublic User[] getAll(RequestParams requestParams, String realm) {try {return identityService.getIdentityProvider().getUsers(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm+ realm);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -72,7 +65,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcetry {return identityService.getIdentityProvider().getUser(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId+ realm, userId);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -102,7 +95,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource}try {identityService.getIdentityProvider().updateUser(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId, user+ realm, user);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -115,7 +108,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcepublic void create(RequestParams requestParams, String realm, User user) {try {identityService.getIdentityProvider().createUser(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, user,+ realm, user,null);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -139,7 +132,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource}try {identityService.getIdentityProvider().deleteUser(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId+ realm, userId);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -154,7 +147,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcepublic void resetPassword(@BeanParam RequestParams requestParams, String realm, String userId, Credential credential) {try {identityService.getIdentityProvider().resetPassword(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId, credential+ realm, userId, credential);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -171,7 +164,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcetry {return identityService.getIdentityProvider().getRoles(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId+ realm, userId);} catch (ClientErrorException ex) {throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());@@ -192,7 +185,12 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcepublic void updateRoles(@BeanParam RequestParams requestParams, String realm, String userId, Role[] roles) {try {identityService.getIdentityProvider().updateRoles(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), realm, userId, roles+ realm,+ userId,+ Arrays.stream(roles)+ .filter(Role::isAssigned)+ .map(r -> Arrays.stream(ClientRole.values()).filter(cr -> cr.getValue().equals(r.getName())).findFirst().orElse(null))+ .toArray(ClientRole[]::new));} catch (ClientErrorException ex) {ex.printStackTrace(System.out);@@ -207,7 +205,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcereturn null;if (!identityService.getIdentityProvider().isMasterRealmAdmin(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), userId+ userId)) return null;ResourceBundle validationMessages = getContainer().getService(I18NService.class).getValidationMessages();@@ -226,7 +224,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcereturn null;if (!identityService.getIdentityProvider().isMasterRealmAdmin(- new ClientRequestInfo(getClientRemoteAddress(), requestParams.getBearerAuth()), user.getId()+ user.getId())) return null;ResourceBundle validationMessages = getContainer().getService(I18NService.class).getValidationMessages();diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.javaindex 1f3107f15..6e9251a40 100644--- a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java@@ -19,34 +19,34 @@*/package org.openremote.manager.setup;-import org.keycloak.admin.client.resource.ClientsResource;-import org.keycloak.admin.client.resource.RealmResource;-import org.keycloak.admin.client.resource.UsersResource;-import org.keycloak.representations.idm.ClientRepresentation;import org.openremote.container.Container;-import org.openremote.container.security.PasswordAuthForm;-import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;--import static org.openremote.manager.security.ManagerIdentityProvider.SETUP_ADMIN_PASSWORD;-import static org.openremote.manager.security.ManagerIdentityProvider.SETUP_ADMIN_PASSWORD_DEFAULT;-import static org.openremote.model.Constants.MASTER_REALM;-import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;+import org.openremote.model.security.ClientRole;+import org.openremote.model.security.Tenant;+import org.openremote.model.security.User;public abstract class AbstractKeycloakSetup implements Setup {public static final String SETUP_EMAIL_FROM_KEYCLOAK = "SETUP_EMAIL_FROM_KEYCLOAK";public static final String SETUP_EMAIL_FROM_KEYCLOAK_DEFAULT = "no-reply@";+ public static final ClientRole[] PUBLIC_USER_ROLES = new ClientRole[] {+ ClientRole.READ_ASSETS,+ ClientRole.READ_MAP+ };+ public static final ClientRole[] REGULAR_USER_ROLES = new ClientRole[] {+ ClientRole.WRITE_USER,+ ClientRole.READ_MAP,+ ClientRole.READ_ASSETS,+ ClientRole.READ_RULES,+ ClientRole.WRITE_ASSETS,+ ClientRole.WRITE_RULES+ };final protected Container container;final protected ManagerIdentityService identityService;final protected ManagerKeycloakIdentityProvider keycloakProvider;final protected SetupService setupService;- protected String accessToken;- protected RealmResource masterRealmResource;- protected ClientsResource masterClientsResource;- protected UsersResource masterUsersResource;public AbstractKeycloakSetup(Container container) {this.container = container;@@ -59,23 +59,30 @@ public abstract class AbstractKeycloakSetup implements Setup {return keycloakProvider;}- @Override- public void onStart() throws Exception {- // Use direct access grant feature of Keycloak Admin CLI to get superuser access token- String keycloakAdminPassword = container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT);- this.accessToken = keycloakProvider.getKeycloak().getAccessToken(- MASTER_REALM, new PasswordAuthForm(KeycloakIdentityProvider.ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)- ).getToken();-- masterRealmResource = keycloakProvider.getRealms(accessToken).realm(MASTER_REALM);- masterClientsResource = masterRealmResource.clients();- masterUsersResource = masterRealmResource.users();+ protected Tenant createTenant(String realm, String displayName) {+ Tenant tenant = new Tenant();+ tenant.setRealm(realm);+ tenant.setDisplayName(displayName);+ tenant.setEnabled(true);+ tenant.setDuplicateEmailsAllowed(true);+ tenant = keycloakProvider.createTenant(tenant);+ return tenant;}- protected String getClientObjectId(ClientsResource clientsResource, String clientName) {- return clientsResource.findByClientId(clientName)- .stream()- .map(ClientRepresentation::getId)- .findFirst().orElseThrow(() -> new RuntimeException("Client object ID not found for client name: " + clientName));+ protected User createUser(String realm, String username, String password, String firstName, String lastName, String email, boolean enabled, ClientRole[] roles) {+ User user = new User();+ user.setUsername(username);+ user.setFirstName(firstName);+ user.setLastName(lastName);+ user.setEmail(email);+ user.setEnabled(enabled);+ user = keycloakProvider.createUser(realm, user, password);+ if (user == null) {+ return null;+ }+ if (roles != null && roles.length > 0) {+ keycloakProvider.updateRoles(realm, user.getId(), roles);+ }+ return user;}}diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakCleanSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakCleanSetup.javaindex 43118c02e..b5a885ded 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakCleanSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakCleanSetup.java@@ -19,18 +19,15 @@*/package org.openremote.manager.setup.builtin;-import org.keycloak.admin.client.resource.RealmsResource;-import org.keycloak.admin.client.resource.UserResource;-import org.keycloak.representations.idm.ClientRepresentation;-import org.keycloak.representations.idm.RealmRepresentation;import org.openremote.container.Container;-import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.setup.AbstractKeycloakSetup;-import java.util.List;+import java.util.Arrays;import java.util.logging.Logger;-import static org.openremote.model.Constants.*;+import static org.openremote.container.security.keycloak.KeycloakIdentityProvider.DEFAULT_CLIENTS;+import static org.openremote.model.Constants.MASTER_REALM;+import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;public class KeycloakCleanSetup extends AbstractKeycloakSetup {@@ -46,30 +43,26 @@ public class KeycloakCleanSetup extends AbstractKeycloakSetup {// Delete all realms that are not the master realmLOG.info("Deleting all non-master realms");- RealmsResource realmsResource = keycloakProvider.getRealms(accessToken);- List<RealmRepresentation> realms = realmsResource.findAll();- for (RealmRepresentation realmRepresentation : realms) {- if (!realmRepresentation.getRealm().equals(MASTER_REALM)) {- keycloakProvider.getRealms(accessToken).realm(realmRepresentation.getRealm()).remove();+ Arrays.stream(keycloakProvider.getTenants()).forEach(tenant -> {+ if (!tenant.getRealm().equals(MASTER_REALM)) {+ LOG.info("Deleting tenant: " + tenant);+ keycloakProvider.deleteTenant(tenant.getRealm());}- }+ });- // Find out if there is a client already present for this application, if so, delete it as well as any gateway clients- masterClientsResource.findAll().stream()- .filter(clientRepresentation -> clientRepresentation.getClientId().equals(KEYCLOAK_CLIENT_ID) || clientRepresentation.getClientId().startsWith(GatewayService.GATEWAY_CLIENT_ID_PREFIX))- .map(ClientRepresentation::getId)- .forEach(clientObjectId -> {- LOG.info("Deleting client: " + clientObjectId);- masterClientsResource.get(clientObjectId).remove();- });+ Arrays.stream(keycloakProvider.getUsers(MASTER_REALM)).forEach(user -> {+ if (!user.getUsername().equals(MASTER_REALM_ADMIN_USER)) {+ LOG.info("Deleting user: " + user);+ keycloakProvider.deleteUser(MASTER_REALM, user.getId());+ }+ });- // Find out if there are any users except the admin, delete them- masterUsersResource.search(null, null, null).stream()- .filter(userRepresentation -> !userRepresentation.getUsername().equals(MASTER_REALM_ADMIN_USER))- .map(userRepresentation -> {- LOG.info("Deleting user: " + userRepresentation);- return masterUsersResource.get(userRepresentation.getId());- })- .forEach(UserResource::remove);+ // Delete all non built in clients+ Arrays.stream(keycloakProvider.getClients(MASTER_REALM)).forEach(client -> {+ if (!DEFAULT_CLIENTS.contains(client.getClientId())) {+ LOG.info("Deleting client: " + client.getClientId());+ keycloakProvider.deleteClient(MASTER_REALM, client);+ }+ });}}diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.javaindex d6b664a53..37cb44dd7 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java@@ -19,26 +19,19 @@*/package org.openremote.manager.setup.builtin;-import org.keycloak.admin.client.resource.ClientResource;-import org.keycloak.admin.client.resource.ClientsResource;-import org.keycloak.admin.client.resource.RolesResource;-import org.keycloak.admin.client.resource.UsersResource;import org.keycloak.representations.idm.ClientRepresentation;-import org.keycloak.representations.idm.CredentialRepresentation;-import org.keycloak.representations.idm.UserRepresentation;import org.openremote.container.Container;import org.openremote.container.util.UniqueIdentifierGenerator;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.manager.mqtt.MqttBrokerService;import org.openremote.manager.setup.AbstractKeycloakSetup;+import org.openremote.model.Constants;import org.openremote.model.security.ClientRole;import org.openremote.model.security.Tenant;-import org.openremote.model.Constants;+import org.openremote.model.security.User;-import java.util.Arrays;import java.util.logging.Logger;-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;+import static org.openremote.model.Constants.MASTER_REALM;/*** We have the following demo users:@@ -75,170 +68,41 @@ public class KeycloakDemoSetup extends AbstractKeycloakSetup {// TenantsmasterTenant = identityService.getIdentityProvider().getTenant(Constants.MASTER_REALM);-- tenantBuilding = new Tenant();- tenantBuilding.setRealm("building");- tenantBuilding.setDisplayName("Building");- tenantBuilding.setEnabled(true);- keycloakProvider.createTenant(new ClientRequestInfo(null, accessToken), tenantBuilding, container);- tenantBuilding = keycloakProvider.getTenant(tenantBuilding.getRealm());-- tenantCity = new Tenant();- tenantCity.setRealm("smartcity");- tenantCity.setDisplayName("Smart City");- tenantCity.setEnabled(true);- keycloakProvider.createTenant(new ClientRequestInfo(null, accessToken), tenantCity, container);- tenantCity = keycloakProvider.getTenant(tenantCity.getRealm());+ tenantBuilding = createTenant("building", "Building");+ tenantCity = createTenant("smaercity", "Smart City");// Users- String masterClientObjectId = getClientObjectId(masterClientsResource, KEYCLOAK_CLIENT_ID);- RolesResource masterRolesResource = masterClientsResource.get(masterClientObjectId).roles();-- UserRepresentation testuser1 = new UserRepresentation();- testuser1.setUsername("testuser1");- testuser1.setFirstName("DemoMaster");- testuser1.setLastName("DemoLast");- testuser1.setEnabled(true);- masterUsersResource.create(testuser1);- testuser1 = masterUsersResource.search("testuser1", null, null, null, null, null).get(0);+ User testuser1 = createUser(MASTER_REALM, "testuser1", "testuser1", "DemoMaster", "DemoLast", null, true, REGULAR_USER_ROLES);this.testuser1Id = testuser1.getId();- CredentialRepresentation testuser1Credentials = new CredentialRepresentation();- testuser1Credentials.setType("password");- testuser1Credentials.setValue("testuser1");- testuser1Credentials.setTemporary(false);- masterUsersResource.get(testuser1.getId()).resetPassword(testuser1Credentials);- masterUsersResource.get(testuser1.getId()).roles().clientLevel(masterClientObjectId).add(Arrays.asList(- masterRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(),- masterRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(),- masterRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(),- masterRolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation(),- masterRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(),- masterRolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation()- ));- LOG.info("Added demo user '" + testuser1.getUsername() + "' with password '" + testuser1Credentials.getValue() + "'");-- UsersResource tenantBuildingUsersResource = keycloakProvider.getRealms(accessToken).realm(tenantBuilding.getRealm()).users();- ClientsResource tenantBuildingClientsResource = keycloakProvider.getRealms(accessToken).realm(tenantBuilding.getRealm()).clients();- String tenantBuildingClientObjectId = getClientObjectId(tenantBuildingClientsResource, KEYCLOAK_CLIENT_ID);- RolesResource tenantBuildingRolesResource = tenantBuildingClientsResource.get(tenantBuildingClientObjectId).roles();-- /**- * MQTT Client- */- ClientRepresentation mqttClientRepresentation = new ClientRepresentation();- String buildingMqttClientId = MqttBrokerService.MQTT_CLIENT_ID_PREFIX + UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm());- mqttClientRepresentation.setClientId(buildingMqttClientId);- mqttClientRepresentation.setName("MQTT");- mqttClientRepresentation.setStandardFlowEnabled(false);- mqttClientRepresentation.setImplicitFlowEnabled(false);- mqttClientRepresentation.setDirectAccessGrantsEnabled(false);- mqttClientRepresentation.setServiceAccountsEnabled(true);- mqttClientRepresentation.setSecret(UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm()));- keycloakProvider.createClientApplication(new ClientRequestInfo(null, keycloakProvider.getAdminAccessToken(null)), tenantBuilding.getRealm(), mqttClientRepresentation);-- ClientResource mqttResource = tenantBuildingClientsResource.get(getClientObjectId(tenantBuildingClientsResource, buildingMqttClientId));- UserRepresentation user = mqttResource.getServiceAccountUser();- tenantBuildingUsersResource.get(user.getId()).roles().clientLevel(tenantBuildingClientObjectId).add(Arrays.asList(- tenantBuildingRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation()- ));-- UserRepresentation testuser2 = new UserRepresentation();- testuser2.setUsername("testuser2");- testuser2.setFirstName("DemoA2");- testuser2.setLastName("DemoLast");- testuser2.setEmail("[email protected]");- testuser2.setEnabled(true);- tenantBuildingUsersResource.create(testuser2);- testuser2 = tenantBuildingUsersResource.search("testuser2", null, null, null, null, null).get(0);+ User testuser2 = createUser(tenantBuilding.getRealm(), "testuser2", "testuser2", "DemoA2", "DemoLast", "[email protected]", true, new ClientRole[] {+ ClientRole.WRITE_USER,+ ClientRole.READ_MAP,+ ClientRole.READ_ASSETS+ });this.testuser2Id = testuser2.getId();- CredentialRepresentation testuser2Credentials = new CredentialRepresentation();- testuser2Credentials.setType("password");- testuser2Credentials.setValue("testuser2");- testuser2Credentials.setTemporary(false);- tenantBuildingUsersResource.get(testuser2.getId()).resetPassword(testuser2Credentials);- tenantBuildingUsersResource.get(testuser2.getId()).roles().clientLevel(tenantBuildingClientObjectId).add(Arrays.asList(- tenantBuildingRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation()- ));- LOG.info("Added demo user '" + testuser2.getUsername() + "' with password '" + testuser2Credentials.getValue() + "'");-- UserRepresentation testuser3 = new UserRepresentation();- testuser3.setUsername("testuser3");- testuser3.setFirstName("DemoA3");- testuser3.setLastName("DemoLast");- testuser3.setEmail("[email protected]");- testuser3.setEnabled(true);- tenantBuildingUsersResource.create(testuser3);- testuser3 = tenantBuildingUsersResource.search("testuser3", null, null, null, null, null).get(0);+ User testuser3 = createUser(tenantBuilding.getRealm(), "testuser3", "testuser3", "DemoA3", "DemoLast", "[email protected]", true, REGULAR_USER_ROLES);this.testuser3Id = testuser3.getId();- CredentialRepresentation testuser3Credentials = new CredentialRepresentation();- testuser3Credentials.setType("password");- testuser3Credentials.setValue("testuser3");- testuser3Credentials.setTemporary(false);- tenantBuildingUsersResource.get(testuser3.getId()).resetPassword(testuser3Credentials);- tenantBuildingUsersResource.get(testuser3.getId()).roles().clientLevel(tenantBuildingClientObjectId).add(Arrays.asList(- tenantBuildingRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation()- ));- LOG.info("Added demo user '" + testuser3.getUsername() + "' with password '" + testuser3Credentials.getValue() + "'");-- UserRepresentation buildingUser = new UserRepresentation();- buildingUser.setUsername("building");- buildingUser.setFirstName("Building");- buildingUser.setLastName("User");- buildingUser.setEmail("[email protected]");- buildingUser.setEnabled(true);- tenantBuildingUsersResource.create(buildingUser);- buildingUser = tenantBuildingUsersResource.search("building", null, null, null, null, null).get(0);+ User buildingUser = createUser(tenantBuilding.getRealm(), "building", "building", "Building", "User", "[email protected]", true, REGULAR_USER_ROLES);this.buildingUserId = buildingUser.getId();- CredentialRepresentation buildingUserCredentials = new CredentialRepresentation();- buildingUserCredentials.setType("password");- buildingUserCredentials.setValue("building");- buildingUserCredentials.setTemporary(false);- tenantBuildingUsersResource.get(buildingUser.getId()).resetPassword(buildingUserCredentials);- tenantBuildingUsersResource.get(buildingUser.getId()).roles().clientLevel(tenantBuildingClientObjectId).add(Arrays.asList(- tenantBuildingRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(),- tenantBuildingRolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation()- ));- LOG.info("Added demo user '" + buildingUser.getUsername() + "' with password '" + buildingUserCredentials.getValue() + "'");-- UsersResource tenantCityUsersResource = keycloakProvider.getRealms(accessToken).realm("smartcity").users();- ClientsResource tenantCityClientsResource = keycloakProvider.getRealms(accessToken).realm("smartcity").clients();- String tenantCityClientObjectId = getClientObjectId(tenantCityClientsResource, KEYCLOAK_CLIENT_ID);- RolesResource tenantCityRolesResource = tenantCityClientsResource.get(tenantCityClientObjectId).roles();-- UserRepresentation smartCityUser = new UserRepresentation();- smartCityUser.setUsername("smartCity");- smartCityUser.setFirstName("Smart");- smartCityUser.setLastName("City");- smartCityUser.setEmail("[email protected]");- smartCityUser.setEnabled(true);- tenantCityUsersResource.create(smartCityUser);- smartCityUser = tenantCityUsersResource.search("smartCity", null, null, null, null, null).get(0);+ User smartCityUser = createUser(tenantCity.getRealm(), "smartcity", "smartycity", "Smart", "City", null, true, REGULAR_USER_ROLES);this.smartCityUserId = smartCityUser.getId();- CredentialRepresentation smartCityCredentials = new CredentialRepresentation();- smartCityCredentials.setType("password");- smartCityCredentials.setValue("smartCity");- smartCityCredentials.setTemporary(false);- tenantCityUsersResource.get(smartCityUser.getId()).resetPassword(smartCityCredentials);- tenantCityUsersResource.get(smartCityUser.getId()).roles().clientLevel(tenantCityClientObjectId).add(Arrays.asList(- tenantCityRolesResource.get(ClientRole.WRITE_USER.getValue()).toRepresentation(),- tenantCityRolesResource.get(ClientRole.READ_MAP.getValue()).toRepresentation(),- tenantCityRolesResource.get(ClientRole.READ_ASSETS.getValue()).toRepresentation(),- tenantCityRolesResource.get(ClientRole.WRITE_ASSETS.getValue()).toRepresentation(),- tenantCityRolesResource.get(ClientRole.READ_RULES.getValue()).toRepresentation(),- tenantCityRolesResource.get(ClientRole.WRITE_RULES.getValue()).toRepresentation()- ));- LOG.info("Added demo user '" + smartCityUser.getUsername() + "' with password '" + smartCityCredentials.getValue() + "'");++ /*+ * MQTT Client+ */+ ClientRepresentation mqttClient = new ClientRepresentation();+ String buildingMqttClientId = MqttBrokerService.MQTT_CLIENT_ID_PREFIX + UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm());+ mqttClient.setClientId(buildingMqttClientId);+ mqttClient.setName("MQTT");+ mqttClient.setStandardFlowEnabled(false);+ mqttClient.setImplicitFlowEnabled(false);+ mqttClient.setDirectAccessGrantsEnabled(false);+ mqttClient.setServiceAccountsEnabled(true);+ mqttClient.setSecret(UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm()));+ mqttClient = keycloakProvider.createClient(tenantBuilding.getRealm(), mqttClient);++ // Add asset RW roles to service user+ User serviceUser = keycloakProvider.getClientServiceUser(tenantBuilding.getRealm(), mqttClient.getClientId());+ keycloakProvider.updateRoles(tenantBuilding.getRealm(), serviceUser.getId(), new ClientRole[] {ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS});}}diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakInitSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakInitSetup.javaindex 86e4fbf7e..d344eaab9 100644--- a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakInitSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakInitSetup.java@@ -19,82 +19,46 @@*/package org.openremote.manager.setup.builtin;-import org.keycloak.admin.client.resource.ClientResource;-import org.keycloak.admin.client.resource.RolesResource;-import org.keycloak.representations.idm.RealmRepresentation;-import org.keycloak.representations.idm.RoleRepresentation;-import org.keycloak.representations.idm.UserRepresentation;import org.openremote.container.Container;-import org.openremote.container.web.ClientRequestInfo;import org.openremote.manager.setup.AbstractKeycloakSetup;import org.openremote.model.security.ClientRole;+import org.openremote.model.security.Tenant;+import org.openremote.model.security.User;-import java.util.Arrays;import java.util.logging.Logger;-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;+import static org.openremote.model.Constants.MASTER_REALM;import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;public class KeycloakInitSetup extends AbstractKeycloakSetup {private static final Logger LOG = Logger.getLogger(KeycloakInitSetup.class.getName());- protected static final String MASTER_REALM_KEYCLOAK_THEME = "MASTER_REALM_KEYCLOAK_THEME";-- protected String masterRealmKeycloakTheme;--public KeycloakInitSetup(Container container) {super(container);-- masterRealmKeycloakTheme = container.getConfig().getOrDefault(MASTER_REALM_KEYCLOAK_THEME, "openremote");}@Overridepublic void onStart() throws Exception {super.onStart();- // Configure the master realm- RealmRepresentation masterRealm = masterRealmResource.toRepresentation();-+ // Update the master realm which is auto created by Keycloak itself+ // This will cause the keycloak provider to configure it appropriately+ // e.g. Set SMTP server, theme, timeouts, etc.+ Tenant masterRealm = keycloakProvider.getTenant(MASTER_REALM);masterRealm.setDisplayName("Master");- masterRealm.setAccountTheme(masterRealmKeycloakTheme);- masterRealm.setEmailTheme(masterRealmKeycloakTheme);- masterRealm.setLoginTheme(masterRealmKeycloakTheme);-- // Set SMTP server, theme, timeouts, etc.- keycloakProvider.configureRealm(masterRealm, container);-- masterRealmResource.update(masterRealm);+ keycloakProvider.updateTenant(masterRealm);// Create our client application with its default roles- keycloakProvider.createClientApplication(new ClientRequestInfo(null, accessToken), masterRealm.getRealm());+ keycloakProvider.createOpenRemoteClientApplication(masterRealm.getRealm());- // Get the client application ID so we can assign roles to users at the client- // level (we can only check realm _or_ client application roles in @RolesAllowed!)- String clientObjectId = getClientObjectId(masterClientsResource, KEYCLOAK_CLIENT_ID);-- ClientResource clientResource = masterClientsResource.get(clientObjectId);- RolesResource rolesResource = clientResource.roles();+ // Update master user name+ User adminUser = keycloakProvider.getUserByUsername(MASTER_REALM, MASTER_REALM_ADMIN_USER);+ adminUser.setFirstName("System");+ adminUser.setLastName("Administrator");+ keycloakProvider.updateUser(MASTER_REALM, adminUser);// Give admin all roles on application client level- RoleRepresentation readRole = rolesResource.get(ClientRole.READ.getValue()).toRepresentation();- RoleRepresentation writeRole = rolesResource.get(ClientRole.WRITE.getValue()).toRepresentation();-- masterUsersResource.search(MASTER_REALM_ADMIN_USER, null, null, null, null, null)- .stream()- .map(userRepresentation -> masterUsersResource.get(userRepresentation.getId()))- .forEach(adminUser -> {- adminUser.roles().clientLevel(clientObjectId).add(Arrays.asList(- readRole,- writeRole- ));- LOG.info("Assigned all application roles to 'admin' user");- UserRepresentation adminRep = adminUser.toRepresentation();- adminRep.setFirstName("System");- adminRep.setLastName("Administrator");- adminUser.update(adminRep);- }- );+ keycloakProvider.updateRoles(MASTER_REALM, adminUser.getId(), new ClientRole[] {ClientRole.READ, ClientRole.WRITE});}}diff --git a/model/src/main/java/org/openremote/model/asset/AssetsEvent.java b/model/src/main/java/org/openremote/model/asset/AssetsEvent.javaindex 18077daf4..9f20d2516 100644--- a/model/src/main/java/org/openremote/model/asset/AssetsEvent.java+++ b/model/src/main/java/org/openremote/model/asset/AssetsEvent.java@@ -21,6 +21,7 @@ package org.openremote.model.asset;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;+import org.openremote.model.event.shared.EventIdentifier;import org.openremote.model.event.shared.SharedEvent;import java.util.List;@@ -29,19 +30,19 @@ import java.util.stream.Collectors;/*** This event is used in response to a {@link ReadAssetsEvent}.*/-public class AssetsEvent extends SharedEvent {+public class AssetsEvent extends SharedEvent implements EventIdentifier {- protected String name;+ protected String messageId;protected List<Asset> assets;@JsonCreator- public AssetsEvent(@JsonProperty("name") String name, @JsonProperty("assets") List<Asset> assets) {- this.name = name;+ public AssetsEvent(@JsonProperty("messageId") String messageId, @JsonProperty("assets") List<Asset> assets) {+ this.messageId = messageId;this.assets = assets;}- public String getName() {- return name;+ public String getMessageId() {+ return messageId;}public List<Asset> getAssets() {@@ -51,7 +52,7 @@ public class AssetsEvent extends SharedEvent {@Overridepublic String toString() {return AssetsEvent.class.getSimpleName() + "{" +- "name='" + name + '\'' ++ "messageId='" + messageId + '\'' +", assets=" + (assets == null ? "null" : assets.stream().map(Asset::getId).collect(Collectors.joining())) +'}';}diff --git a/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.javaindex 02d189fff..f5b79b3c7 100644--- a/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java@@ -21,6 +21,7 @@ package org.openremote.model.asset;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;+import org.openremote.model.event.shared.EventIdentifier;import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.query.AssetQuery;@@ -28,14 +29,14 @@ 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 EventIdentifier {- protected String name;+ protected String messageId;protected AssetQuery assetQuery;@JsonCreator- public ReadAssetsEvent(@JsonProperty("name") String name, @JsonProperty("assetQuery") AssetQuery assetQuery) {- this.name = name;+ public ReadAssetsEvent(@JsonProperty("messageId") String messageId, @JsonProperty("assetQuery") AssetQuery assetQuery) {+ this.messageId = messageId;this.assetQuery = assetQuery;}@@ -47,14 +48,14 @@ public class ReadAssetsEvent extends SharedEvent {return assetQuery;}- public String getName() {- return name;+ public String getMessageId() {+ return messageId;}@Overridepublic String toString() {return getClass().getSimpleName() + "{" +- "name='" + name + '\'' ++ "messageId='" + messageId + '\'' +"query='" + assetQuery + '\'' +'}';}diff --git a/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java b/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.javaindex f67bf24fc..08c3fb80d 100644--- a/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java+++ b/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java@@ -28,6 +28,7 @@ import java.util.List;public class TriggeredEventSubscription<T extends SharedEvent> {+ public static final String MESSAGE_PREFIX = "TRIGGERED:";protected List<T> events;protected String subscriptionId;diff --git a/model/src/main/java/org/openremote/model/event/shared/EventIdentifier.java b/model/src/main/java/org/openremote/model/event/shared/EventIdentifier.javanew file mode 100644index 000000000..a92766a93--- /dev/null+++ b/model/src/main/java/org/openremote/model/event/shared/EventIdentifier.java@@ -0,0 +1,32 @@+/*+ * Copyright 2020, 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.event.shared;++import com.fasterxml.jackson.annotation.JsonProperty;++/**+ * An interface for Events that 'expect' a response; {@link #getMessageId()} should return a unique string that+ * allows the response to be linked to the request. It is up to the backend to then use this message ID on the+ * reply and to also determine what the reply message should be.+ */+public interface EventIdentifier {+ @JsonProperty+ String getMessageId();+}diff --git a/model/src/main/java/org/openremote/model/security/Tenant.java b/model/src/main/java/org/openremote/model/security/Tenant.javaindex 9238b8dca..1d64ef934 100644--- a/model/src/main/java/org/openremote/model/security/Tenant.java+++ b/model/src/main/java/org/openremote/model/security/Tenant.java@@ -78,6 +78,9 @@ public class Tenant {@Transientprotected String emailTheme;+ @Transient+ protected Integer accessTokenLifespan;+@JsIgnorepublic Tenant() {this(null, null, null, null);@@ -200,6 +203,8 @@ public class Tenant {this.emailTheme = emailTheme;}++@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/test/src/main/groovy/org/openremote/test/ClientEventService.groovy b/test/src/main/groovy/org/openremote/test/ClientEventService.groovyindex 7187fa8d5..b6928f50a 100644--- a/test/src/main/groovy/org/openremote/test/ClientEventService.groovy+++ b/test/src/main/groovy/org/openremote/test/ClientEventService.groovy@@ -55,15 +55,19 @@ class ClientEventService implements EventService {data = data.substring(UnauthorizedEventSubscription.MESSAGE_PREFIX.length())UnauthorizedEventSubscription failure = objectMapper.readValue(data, UnauthorizedEventSubscription.class)eventBus.dispatch(new SubscriptionFailureEvent(failure.subscription.getEventType()))- } else if (data.startsWith(SharedEvent.MESSAGE_PREFIX)) {- data = data.substring(SharedEvent.MESSAGE_PREFIX.length())+ } else if (data.startsWith(TriggeredEventSubscription.MESSAGE_PREFIX)) {+ data = data.substring(TriggeredEventSubscription.MESSAGE_PREFIX.length())if (data.startsWith("{")) {TriggeredEventSubscription event = objectMapper.readValue(data, TriggeredEventSubscription.class)if (event.events != null) {event.events.forEach{ Event evt -> eventBus.dispatch(evt)}}- } else if (data.startsWith("[")) {+ }+ } else if (data.startsWith(SharedEvent.MESSAGE_PREFIX)) {+ data = data.substring(SharedEvent.MESSAGE_PREFIX.length())++ if (data.startsWith("[")) {// Handle array of eventsSharedEvent[] events = objectMapper.readValue(data, SharedEvent[].class)for (SharedEvent event : events) {diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovyindex 1aef5513e..896536554 100644--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy@@ -60,7 +60,6 @@ class GatewayTest extends Specification implements ManagerContainerTrait {def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)def identityProvider = container.getService(ManagerIdentityService.class).identityProvider as ManagerKeycloakIdentityProviderdef httpClientProtocol = container.getService(HttpClientProtocol.class)- def clientsResource = identityProvider.getRealms(new ClientRequestInfo(null, identityProvider.getAdminAccessToken(null))).realm(managerDemoSetup.realmBuildingTenant).clients()expect: "the container should be running and initialised"conditions.eventually {@@ -73,7 +72,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {then: "a keycloak client should have been created for this gateway"conditions.eventually {- def client = clientsResource.findByClientId(GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId()).get(0)+ def client = identityProvider.getClient(managerDemoSetup.realmBuildingTenant, GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId())assert client != null}@@ -134,7 +133,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert clientReceivedMessages.size() >= 1assert clientReceivedMessages[0].startsWith(SharedEvent.MESSAGE_PREFIX)def readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)- assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL+ assert readAssetsEvent.messageId == GatewayConnector.ASSET_READ_EVENT_NAME_INITIALassert readAssetsEvent.assetQuery != nullassert readAssetsEvent.assetQuery.select.excludeAttributesassert readAssetsEvent.assetQuery.select.excludePath@@ -255,7 +254,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert clientReceivedMessages.get(0).startsWith(SharedEvent.MESSAGE_PREFIX)assert clientReceivedMessages.get(0).contains("read-assets")readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)- assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"+ assert readAssetsEvent.messageId == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"assert readAssetsEvent.assetQuery != nullassert readAssetsEvent.assetQuery.select.excludePathassert readAssetsEvent.assetQuery.select.excludeParentInfo@@ -270,7 +269,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {sendAssets.addAll(agentAssets)sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).filter{!agentAssetIds.contains(it)}.map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))readAssetsReplyEvent = new AssetsEvent(- readAssetsEvent.name,+ readAssetsEvent.messageId,sendAssets)gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))@@ -324,7 +323,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert clientReceivedMessages.get(1).startsWith(SharedEvent.MESSAGE_PREFIX)assert clientReceivedMessages.get(1).contains("read-assets")readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[1].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)- assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + GatewayConnector.SYNC_ASSET_BATCH_SIZE+ assert readAssetsEvent.messageId == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + GatewayConnector.SYNC_ASSET_BATCH_SIZEassert readAssetsEvent.assetQuery != nullassert readAssetsEvent.assetQuery.select.excludePathassert readAssetsEvent.assetQuery.select.excludeParentInfo@@ -337,7 +336,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {sendAssets = []sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))readAssetsReplyEvent = new AssetsEvent(- readAssetsEvent.name,+ readAssetsEvent.messageId,sendAssets)gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))@@ -631,7 +630,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert clientReceivedMessages.get(0).startsWith(SharedEvent.MESSAGE_PREFIX)assert clientReceivedMessages.get(0).contains("read-assets")readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)- assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"+ assert readAssetsEvent.messageId == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"assert readAssetsEvent.assetQuery != nullassert readAssetsEvent.assetQuery.select.excludePathassert readAssetsEvent.assetQuery.select.excludeParentInfo@@ -681,7 +680,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {sendAssets.addAll(agentAssets)sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).filter{!agentAssetIds.contains(it) && it != removedAsset.id && it != building1Room5Asset.id}.map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))readAssetsReplyEvent = new AssetsEvent(- readAssetsEvent.name,+ readAssetsEvent.messageId,sendAssets)gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))@@ -692,7 +691,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {assert clientReceivedMessages.get(1).startsWith(SharedEvent.MESSAGE_PREFIX)assert clientReceivedMessages.get(1).contains("read-assets")readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[1].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)- assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + (GatewayConnector.SYNC_ASSET_BATCH_SIZE - 1)+ assert readAssetsEvent.messageId == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + (GatewayConnector.SYNC_ASSET_BATCH_SIZE - 1)assert readAssetsEvent.assetQuery != nullassert readAssetsEvent.assetQuery.select.excludePathassert readAssetsEvent.assetQuery.select.excludeParentInfo@@ -705,7 +704,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {sendAssets = []sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))readAssetsReplyEvent = new AssetsEvent(- readAssetsEvent.name,+ readAssetsEvent.messageId,sendAssets)gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))@@ -722,7 +721,6 @@ class GatewayTest extends Specification implements ManagerContainerTrait {when: "the gateway asset is deleted"deleted = assetStorageService.delete([gateway.id])- clientsResource = identityProvider.getRealms(new ClientRequestInfo(null, identityProvider.getAdminAccessToken(null))).realm(managerDemoSetup.realmBuildingTenant).clients()then: "all descendant assets should have been removed"conditions.eventually {@@ -732,7 +730,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {then: "the keycloak client should also be removed"assert deletedconditions.eventually {- assert clientsResource.findByClientId(GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId()).isEmpty()+ assert identityProvider.getClient(managerDemoSetup.realmBuildingTenant, GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId()) == null}cleanup: "the server should be stopped"@@ -750,7 +748,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {messageFromSharedEvent(_) >> {SharedEvent event ->if (event instanceof AssetsEvent) {- if (GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL.equals(event.getName())) {+ if (GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL.equals(event.getMessageId())) {// Store real IDs and map to a fake IDsevent.getAssets().forEach {String oldId = it.iddiff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovyindex e0412bf2b..83a9bbeb3 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy@@ -35,6 +35,7 @@ import org.openremote.model.Constantsimport org.openremote.model.asset.*import org.openremote.model.asset.agent.ConnectionStatusimport org.openremote.model.attribute.*+import org.openremote.model.event.TriggeredEventSubscriptionimport org.openremote.model.event.shared.EventSubscriptionimport org.openremote.model.event.shared.SharedEventimport org.openremote.model.query.AssetQuery@@ -201,7 +202,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContainew MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,Values.convertToValue([- new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)] as ValueFilter[], Container.JSON.writer()).orElse(null)),@@ -210,7 +211,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContainew MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,Values.convertToValue([- new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"targetTemperature\")].attributeState.value", true, false)] as ValueFilter[], Container.JSON.writer()).orElse(null)),@@ -229,7 +230,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContainew MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,Values.convertToValue([- new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)] as ValueFilter[], Container.JSON.writer()).orElse(null)),@@ -238,7 +239,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContainew MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,Values.convertToValue([- new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"co2Level\")].attributeState.value", true, false),] as ValueFilter[], Container.JSON.writer()).orElse(null)),@@ -269,7 +270,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContaiValues.create(19.5))assetProcessingService.sendAttributeEvent(attributeEvent)- then: "the linked targetTemperature attribute should contain this written value"+ then: "the linked targetTemperature attribute should contain this written value (it should have been written to the target temp attribute and then read back again)"conditions.eventually {asset = assetStorageService.find(asset.getId(), true)assert asset.getAttribute("readWriteTargetTemp").flatMap{it.getValueAsNumber()}.orElse(null) == 19.5d@@ -290,4 +291,4 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContaicleanup: "the server should be stopped"stopContainer(container)}-}\ No newline at end of file+}diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovyindex 262ebd00b..4b82c13e9 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy@@ -119,8 +119,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitthen: "the client receives the event"conditions.eventually {assert lastMessage != null- assert lastMessage.indexOf(SharedEvent.MESSAGE_PREFIX) == 0- def triggeredEvent = Container.JSON.readValue(lastMessage.substring(SharedEvent.MESSAGE_PREFIX.length()), TriggeredEventSubscription.class)+ assert lastMessage.indexOf(TriggeredEventSubscription.MESSAGE_PREFIX) == 0+ def triggeredEvent = Container.JSON.readValue(lastMessage.substring(TriggeredEventSubscription.MESSAGE_PREFIX.length()), TriggeredEventSubscription.class)assert triggeredEvent.subscriptionId == "1"assert triggeredEvent.events.size() == 1assert ((AttributeEvent)triggeredEvent.events[0]).entityId == managerDemoSetup.apartment1LivingroomIddiff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovyindex 52c8246db..f381f61f2 100644--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy@@ -1,6 +1,6 @@package org.openremote.test.rules-import org.openremote.container.web.ClientRequestInfo+import org.openremote.manager.rules.RulesFactsimport org.openremote.manager.rules.RulesLoopExceptionimport org.openremote.manager.rules.RulesService@@ -194,7 +194,7 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainerdef apartment3Engine = rulesService.assetEngines.get(managerDemoSetup.apartment3Id)def tenantBuildingTenant = keycloakDemoSetup.tenantBuildingtenantBuildingTenant.setEnabled(false)- identityService.getIdentityProvider().updateTenant(new ClientRequestInfo(null, accessToken), tenantBuildingTenant.getRealm(), tenantBuildingTenant)+ identityService.getIdentityProvider().updateTenant(tenantBuildingTenant)then: "the tenants rule engine should stop and all asset rule engines in this realm should also stop"conditions.eventually {@@ -223,7 +223,7 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainerwhen: "the disabled tenant is re-enabled"tenantBuildingTenant.setEnabled(true)- identityService.getIdentityProvider().updateTenant(new ClientRequestInfo(null, accessToken), tenantBuildingTenant.getRealm(), tenantBuildingTenant)+ identityService.getIdentityProvider().updateTenant(tenantBuildingTenant)then: "the tenants rule engine should start and all asset rule engines from this realm should also start"conditions.eventually {