Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Refactor and cleanup of MQTT broker, also introduced service users and reloadable keycloak adapter config and removed ClientEventAgent

Rich Turner7be213252022
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 35db4fada..264716abf 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -83,7 +83,6 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent
protected ScheduledExecutorService executorService;
protected ProtocolAssetService assetService;
protected ProtocolPredictedAssetService predictedAssetService;
- protected ProtocolClientEventService protocolClientEventService;
protected T agent;
public AbstractProtocol(T agent) {
@@ -96,7 +95,6 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent
executorService = container.getExecutorService();
assetService = container.getService(ProtocolAssetService.class);
predictedAssetService = container.getService(ProtocolPredictedAssetService.class);
- protocolClientEventService = container.getService(ProtocolClientEventService.class);
messageBrokerContext = container.getService(MessageBrokerService.class).getContext();
withLock(getProtocolName() + "::start", () -> {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java
deleted file mode 100644
index 54657ef42..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * 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.agent.protocol;
-
-import org.apache.camel.Exchange;
-import org.openremote.agent.protocol.event.ClientEventAgent;
-import org.openremote.model.ContainerService;
-import org.openremote.container.security.AuthContext;
-import org.openremote.container.web.ConnectionConstants;
-import org.openremote.model.Constants;
-import org.openremote.model.asset.agent.Protocol;
-import org.openremote.model.security.ClientRole;
-
-import java.util.Arrays;
-import java.util.function.Consumer;
-
-import static org.apache.camel.builder.Builder.header;
-
-/**
- * Interface for {@link Protocol}s to add/remove Camel {@link Exchange} interceptors for specific clients and to
- * also add/remove clients.
- *
- */
-public interface ProtocolClientEventService extends ContainerService {
-
- String CLIENT_ID_PREFIX = "ClientEvent-";
- String HEADER_ACCESS_RESTRICTED = ProtocolClientEventService.class.getName() + ".HEADER_ACCESS_RESTRICTED";
- String HEADER_CONNECTION_TYPE = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE";
- String HEADER_CONNECTION_TYPE_WEBSOCKET = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_WEBSOCKET";
- String HEADER_CONNECTION_TYPE_MQTT = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_MQTT";
- String HEADER_REQUEST_RESPONSE_MESSAGE_ID = ProtocolClientEventService.class.getName() + ".HEADER_REQUEST_RESPONSE_MESSAGE_ID";
-
- class ClientCredentials {
- String realm;
- ClientRole[] roles;
- String clientId;
- String secret;
-
- public ClientCredentials(String realm, ClientRole[] roles, String clientId, String secret) {
- this.realm = realm;
- this.roles = roles;
- this.clientId = clientId;
- this.secret = secret;
- }
-
- public String getRealm() {
- return realm;
- }
-
- public void setRealm(String realm) {
- this.realm = realm;
- }
-
- public ClientRole[] getRoles() {
- return roles;
- }
-
- public void setRoles(ClientRole[] roles) {
- this.roles = roles;
- }
-
- public String getClientId() {
- return clientId;
- }
-
- public void setClientId(String clientId) {
- this.clientId = clientId;
- }
-
- public String getSecret() {
- return secret;
- }
-
- public void setSecret(String secret) {
- this.secret = secret;
- }
-
- @Override
- public String toString() {
- return ClientCredentials.class.getSimpleName() + "{" +
- "realm='" + realm + '\'' +
- ", roles=" + Arrays.toString(roles) +
- ", clientId='" + clientId + '\'' +
- ", secret=" + secret +
- '}';
- }
- }
-
- void addExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) throws RuntimeException;
-
- void removeExchangeInterceptor(Consumer<Exchange> exchangeInterceptor);
-
- void closeSession(String sessionKey);
-
- void sendToSession(String sessionKey, Object data);
-
- void addClientCredentials(ClientCredentials clientCredentials) throws RuntimeException;
-
- void removeClientCredentials(String realm, String clientId);
-
- /**
- * Method to stop further processing of the exchange
- */
- static void stopMessage(Exchange exchange) {
- exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
- }
-
- static boolean isInbound(Exchange exchange) {
- return header(HEADER_CONNECTION_TYPE).isNotNull().matches(exchange);
- }
-
- static String getSessionKey(Exchange exchange) {
- return exchange.getIn().getHeader(ConnectionConstants.SESSION_KEY, String.class);
- }
-
- static String getClientId(Exchange exchange) {
- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
- if(authContext != null) {
- return authContext.getClientId();
- }
- return null;
- }
-
- static String getClientId(String suffix) {
- return CLIENT_ID_PREFIX + suffix;
- }
-}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventAgent.java b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventAgent.java
deleted file mode 100644
index d04916f43..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventAgent.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * 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.agent.protocol.event;
-
-import org.openremote.model.asset.agent.Agent;
-import org.openremote.model.asset.agent.AgentDescriptor;
-import org.openremote.model.asset.agent.AgentLink;
-import org.openremote.model.attribute.MetaItem;
-import org.openremote.model.security.ClientRole;
-import org.openremote.model.value.AttributeDescriptor;
-import org.openremote.model.value.ValueDescriptor;
-import org.openremote.model.value.ValueType;
-
-import javax.persistence.Entity;
-import java.util.Optional;
-
-import static org.openremote.model.value.MetaItemType.READ_ONLY;
-
-@Entity
-public class ClientEventAgent extends Agent<ClientEventAgent, ClientEventProtocol, AgentLink.Default> {
-
- public static final AttributeDescriptor<String> CLIENT_SECRET = new AttributeDescriptor<>("clientSecret", ValueType.UUID);
- public static final AttributeDescriptor<String> CLIENT_ID = new AttributeDescriptor<>("clientId", ValueType.TEXT, new MetaItem<>(READ_ONLY));
- public static final AttributeDescriptor<Boolean> WRITE = new AttributeDescriptor<>("write", ValueType.BOOLEAN);
- public static final AttributeDescriptor<Boolean> READ = new AttributeDescriptor<>("read", ValueType.BOOLEAN);
-
- public static final AgentDescriptor<ClientEventAgent, ClientEventProtocol, AgentLink.Default> DESCRIPTOR =new AgentDescriptor<>(
- ClientEventAgent.class, ClientEventProtocol.class, AgentLink.Default.class
- );
-
- /**
- * For use by hydrators (i.e. JPA/Jackson)
- */
- protected ClientEventAgent() {
- }
-
-
- public ClientEventAgent(String name) {
- super(name);
- }
-
- @Override
- public ClientEventProtocol getProtocolInstance() {
- return new ClientEventProtocol(this);
- }
-
- public Optional<String> getClientSecret() {
- return getAttributes().getValue(CLIENT_SECRET);
- }
-
- public ClientEventAgent setClientSecret(String value) {
- getAttributes().getOrCreate(CLIENT_SECRET).setValue(value);
- return this;
- }
-
- public Optional<String> getClientId() {
- return getAttributes().getValue(CLIENT_ID);
- }
-
- public ClientEventAgent setClientId(String value) {
- getAttributes().getOrCreate(CLIENT_ID).setValue(value);
- return this;
- }
-
- public Optional<Boolean> isRead() {
- return getAttributes().getValue(READ);
- }
-
- public ClientEventAgent setRead(Boolean value) {
- getAttributes().getOrCreate(READ).setValue(value);
- return this;
- }
-
- public Optional<Boolean> isWrite() {
- return getAttributes().getValue(WRITE);
- }
-
- public ClientEventAgent setWrite(Boolean value) {
- getAttributes().getOrCreate(WRITE).setValue(value);
- return this;
- }
-}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
deleted file mode 100644
index ea2e44510..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * 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.agent.protocol.event;
-
-import org.apache.camel.Exchange;
-import org.openremote.agent.protocol.AbstractProtocol;
-import org.openremote.agent.protocol.ProtocolClientEventService;
-import org.openremote.container.web.ConnectionConstants;
-import org.openremote.model.Container;
-import org.openremote.model.asset.agent.AgentLink;
-import org.openremote.model.asset.agent.ConnectionStatus;
-import org.openremote.model.attribute.Attribute;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.attribute.AttributeState;
-import org.openremote.model.security.ClientRole;
-import org.openremote.model.syslog.SyslogCategory;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.UUID;
-import java.util.logging.Logger;
-
-import static org.apache.camel.builder.Builder.body;
-import static org.apache.camel.builder.Builder.header;
-import static org.apache.camel.builder.PredicateBuilder.or;
-import static org.openremote.agent.protocol.ProtocolClientEventService.*;
-import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
-
-/**
- * This protocol is for pub-sub devices to connect to the manager client event broker (MQTT or websocket);
- * attributes then linked to this protocol can be written to/read by the devices. The protocol handles the
- * creation of a Keycloak client that allows client secret 'authentication' for machines therefore this protocol
- * requires the Keycloak identity provider.
- */
-public class ClientEventProtocol extends AbstractProtocol<ClientEventAgent, AgentLink.Default> {
-
- public static final String PROTOCOL_DISPLAY_NAME = "Client Event";
- protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, ClientEventProtocol.class);
-
- public ClientEventProtocol(ClientEventAgent agent) {
- super(agent);
- }
-
- public static String getClientIdSuffix(ClientEventAgent agent) {
- return agent.getId().substring(12);
- }
-
- @Override
- public void doStart(Container container) throws Exception {
- LOG.info("Creating client credentials for: " + this);
-
- protocolClientEventService.addExchangeInterceptor(this::onMessageIntercept);
- String clientId = getClientId(getClientIdSuffix(agent));
- String clientSecret = agent.getClientSecret().orElse(UUID.randomUUID().toString());
- List<ClientRole> roles = new ArrayList<>();
-
- if (agent.isRead().orElse(false)) {
- roles.add(ClientRole.READ_ASSETS);
- }
- if (agent.isWrite().orElse(false)) {
- roles.add(ClientRole.WRITE_ASSETS);
- roles.add(ClientRole.WRITE_ATTRIBUTES);
- }
-
- ProtocolClientEventService.ClientCredentials clientCredentials =
- new ProtocolClientEventService.ClientCredentials(
- agent.getRealm(),
- roles.toArray(new ClientRole[0]),
- clientId,
- clientSecret
- );
-
- protocolClientEventService.addClientCredentials(clientCredentials);
- setConnectionStatus(ConnectionStatus.CONNECTED);
- updateAgentAttribute(new AttributeState(agent.getId(), ClientEventAgent.CLIENT_ID.getName(), clientId));
- updateAgentAttribute(new AttributeState(agent.getId(), ClientEventAgent.CLIENT_SECRET.getName(), clientSecret));
- }
-
- @Override
- protected void doStop(Container container) throws Exception {
- LOG.info("Removing client credentials for: " + this);
- protocolClientEventService.removeExchangeInterceptor(this::onMessageIntercept);
- protocolClientEventService.removeClientCredentials(agent.getRealm(), getClientId(getClientIdSuffix(agent)));
- }
-
- @Override
- protected void doLinkAttribute(String assetId, Attribute<?> attribute, AgentLink.Default agentLink) throws RuntimeException {
- // Nothing to do here
- }
-
- @Override
- protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, AgentLink.Default agentLink) {
- // Nothing to do here
- }
-
- @Override
- protected void doLinkedAttributeWrite(Attribute<?> attribute, AgentLink.Default agentLink, AttributeEvent event, Object processedValue) {
- // Nothing to do here
- }
-
- @Override
- public String getProtocolName() {
- return PROTOCOL_DISPLAY_NAME;
- }
-
- @Override
- public String getProtocolInstanceUri() {
- return "clientEvent://" + agent.getId();
- }
-
- protected void onMessageIntercept(Exchange exchange) {
- String clientId = ProtocolClientEventService.getClientId(exchange);
-
- if (!isThisClient(clientId)) {
- return;
- }
-
- if (header(ConnectionConstants.SESSION_OPEN).matches(exchange)) {
- LOG.info("Client connected: " + this);
- return;
- }
-
- if (or(header(ConnectionConstants.SESSION_CLOSE), header(ConnectionConstants.SESSION_CLOSE_ERROR)).matches(exchange)) {
- LOG.info("Client disconnected: " + this);
- return;
- }
-
- if (isInbound(exchange)) {
- if (body().isInstanceOf(AttributeEvent.class).matches(exchange)) {
- // Inbound attribute event is essentially a protocol sensor update
-
- AttributeEvent attributeEvent = exchange.getIn().getBody(AttributeEvent.class);
- Attribute<?> linkedAttribute = getLinkedAttributes().get(attributeEvent.getAttributeRef());
-
- if (linkedAttribute == null) {
- LOG.info("Message received from a client for an unlinked attribute, so ignoring");
- stopMessage(exchange);
- return;
- }
-
- // TODO: Can do some processing of the incoming value here
- updateLinkedAttribute(new AttributeState(attributeEvent.getAttributeRef(), attributeEvent.getValue().orElse(null)));
- stopMessage(exchange);
- }
- }
- }
-
- protected boolean isThisClient(String clientId) {
- return Objects.equals(getClientId(getClientIdSuffix(agent)), clientId);
- }
-}
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.java
index 338f27d10..75acea43a 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
@@ -36,22 +36,25 @@ import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.adapters.KeycloakDeploymentBuilder;
import org.keycloak.admin.client.resource.RealmsResource;
import org.keycloak.representations.adapters.config.AdapterConfig;
-import org.openremote.container.security.ClientCredentialsAuthForm;
import org.openremote.container.security.IdentityProvider;
-import org.openremote.container.web.ClientRequestInfo;
+import org.openremote.container.web.OAuthFilter;
import org.openremote.container.web.WebClient;
import org.openremote.container.web.WebService;
import org.openremote.container.web.WebTargetBuilder;
import org.openremote.model.Container;
-import org.openremote.model.auth.OAuthPasswordGrant;
+import org.openremote.model.auth.OAuthGrant;
-import javax.ws.rs.HttpMethod;
import javax.ws.rs.NotFoundException;
+import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
+import java.net.URI;
import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -61,7 +64,7 @@ import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
import static org.openremote.container.util.MapAccess.*;
import static org.openremote.container.web.WebClient.getTarget;
import static org.openremote.container.web.WebService.pathStartsWithHandler;
-import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;
+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
import static org.openremote.model.Constants.REQUEST_HEADER_REALM;
public abstract class KeycloakIdentityProvider implements IdentityProvider {
@@ -97,10 +100,9 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
public static final String IDENTITY_NETWORK_HOST_DEFAULT = "localhost";
public static final String IDENTITY_NETWORK_WEBSERVER_PORT = "IDENTITY_NETWORK_WEBSERVER_PORT";
public static final int IDENTITY_NETWORK_WEBSERVER_PORT_DEFAULT = 8080;
+
public static final String KEYCLOAK_AUTH_PATH = "auth";
private static final Logger LOG = Logger.getLogger(KeycloakIdentityProvider.class.getName());
- // Each realm in Keycloak has a client application with this identifier
- final protected String clientId;
// The externally visible address of this installation
protected UriBuilder externalServerUri;
// The (internal) URI where Keycloak can be found
@@ -114,15 +116,22 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
// The client we use to access Keycloak
protected ResteasyClient httpClient;
protected ResteasyWebTarget keycloakTarget;
+ protected ConcurrentLinkedQueue<RealmsResource> realmsResourcePool = new ConcurrentLinkedQueue<>();
// Cache Keycloak deployment per realm/client so we don't have to access Keycloak for every token validation
protected LoadingCache<KeycloakRealmClient, KeycloakDeployment> keycloakDeploymentCache;
- // The configuration for the Keycloak servlet extension, looks up the client application per realm
+ // The configuration for the Keycloak servlet extension, looks up the openremote client application per realm
protected KeycloakConfigResolver keycloakConfigResolver;
// Optional reverse proxy that listens to KEYCLOAK_AUTH_PATH and forwards requests to Keycloak (used in dev mode to allow same url to be used for manager and keycloak) - handled by proxy in production
protected HttpHandler authProxyHandler;
+ protected OAuthGrant oAuthGrant;
- protected KeycloakIdentityProvider(String clientId) {
- this.clientId = clientId;
+ /**
+ * The supplied {@link OAuthGrant} will be used to authenticate with keycloak so we can programmatically make changes.
+ * It must be credentials for the master realm for a user with `admin` role so that they can perform CRUD on realms,
+ * clients and users.
+ */
+ protected KeycloakIdentityProvider(OAuthGrant oAuthGrant) {
+ this.oAuthGrant = oAuthGrant;
}
@Override
@@ -179,16 +188,8 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
);
httpClient = WebClient.registerDefaults(clientBuilder).build();
- WebTargetBuilder targetBuilder = new WebTargetBuilder(httpClient, keycloakServiceUri.build());
- targetBuilder.setOAuthAuthentication(new OAuthPasswordGrant(
- keycloakServiceUri.clone().path("/realms/master/protocol/openid-connect/token").build().toString(),
- ADMIN_CLI_CLIENT_ID,
- null,
- "openid",
- MASTER_REALM_ADMIN_USER,
- container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)
- ));
- keycloakTarget = targetBuilder.build();
+ // Generate the keycloak proxy for
+ updateCredentials(oAuthGrant);
keycloakDeploymentCache = createKeycloakDeploymentCache();
@@ -199,7 +200,7 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
LOG.finer("No realm in request, no authentication will be attempted: " + request.getURI());
return notAuthenticatedKeycloakDeployment;
}
- KeycloakDeployment keycloakDeployment = getKeycloakDeployment(realm, clientId);
+ KeycloakDeployment keycloakDeployment = getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID);
if (keycloakDeployment == null) {
LOG.fine("No Keycloak deployment available for realm, no authentication will be attempted: " + request.getURI());
return notAuthenticatedKeycloakDeployment;
@@ -231,6 +232,19 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
httpClient.close();
}
+ /**
+ * Update the credentials used to interact with keycloak; the token endpoint will be overwritten with this instances
+ * keycloak server URI and for the master realm.
+ */
+ public void updateCredentials(OAuthGrant grant) {
+ this.oAuthGrant = grant;
+ // Force token endpoint to master realm as this is the realm we need to be in to do keycloak CRUD
+ oAuthGrant.setTokenEndpointUri(getTokenUri("master").toString());
+ WebTargetBuilder targetBuilder = new WebTargetBuilder(httpClient, keycloakServiceUri.build());
+ targetBuilder.setOAuthAuthentication(grant);
+ keycloakTarget = targetBuilder.build();
+ }
+
@Override
public void secureDeployment(DeploymentInfo deploymentInfo) {
LoginConfig loginConfig = new LoginConfig(SimpleKeycloakServletExtension.AUTH_MECHANISM, "OpenRemote");
@@ -242,40 +256,25 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
return keycloakTarget.proxy(KeycloakResource.class);
}
- public String getAccessToken(String realm, String username, String secret) {
- return getTarget(httpClient, keycloakServiceUri.build(), null, null, null)
- .proxy(KeycloakResource.class)
- .getAccessToken(realm, new ClientCredentialsAuthForm(username, secret))
- .getToken();
- }
-
//There is a bug in {@link org.keycloak.admin.client.resource.UserStorageProviderResource#syncUsers} which misses the componentId as parameter
- protected Response syncUsers(ClientRequestInfo clientRequestInfo, String componentId, String realm, String action) {
- return getTarget(httpClient,
- keycloakServiceUri.build(),
- clientRequestInfo.getAccessToken(),
- clientRequestInfo.getRemoteAddress(),
- clientRequestInfo.getRemoteAddress() != null ? externalServerUri.build() : null)
- .path("admin")
- .path("realms")
- .path(realm)
- .path("user-storage")
- .path(componentId)
- .path("sync")
- .queryParam("action", action)
- .request()
- .build(HttpMethod.POST)
- .invoke();
+ protected void syncUsers(String componentId, String realm, String action) {
+ getRealms(realmsResource -> {
+ realmsResource.realm(realm).userStorage().syncUsers(componentId, action);
+ return null;
+ });
}
- /**
- * 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() {
- return keycloakTarget.proxy(RealmsResource.class);
+ final protected <T> T getRealms(Function<RealmsResource, T> consumer) {
+ RealmsResource realmsResource;
+
+ if ((realmsResource = realmsResourcePool.poll()) == null) {
+ realmsResource = keycloakTarget.proxy(RealmsResource.class);
+ }
+ try {
+ return consumer.apply(realmsResource);
+ } finally {
+ realmsResourcePool.offer(realmsResource);
+ }
}
protected void waitForKeycloak() {
@@ -330,16 +329,39 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
}
}
+ public URI getTokenUri(String realm) {
+ return keycloakServiceUri.clone().path("realms").path(realm).path("protocol/openid-connect/token").build();
+ }
+
+ /**
+ * Convenience method for generating access tokens from a given OAuth compliant server
+ */
+ public Supplier<String> getAccessTokenSupplier(OAuthGrant grant) {
+ WebTarget authTarget = httpClient.target(grant.getTokenEndpointUri());
+ OAuthFilter oAuthFilter = new OAuthFilter(authTarget, grant);
+ return () -> {
+ try {
+ return oAuthFilter.getAccessToken();
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Failed to get OAuth access token using grant: " + grant, e);
+ }
+ return null;
+ };
+ }
+
protected LoadingCache<KeycloakRealmClient, KeycloakDeployment> createKeycloakDeploymentCache() {
CacheLoader<KeycloakRealmClient, KeycloakDeployment> loader =
new CacheLoader<KeycloakRealmClient, KeycloakDeployment>() {
public KeycloakDeployment load(KeycloakRealmClient keycloakRealmClient) {
LOG.fine("Loading adapter config for client '" + keycloakRealmClient.clientId + "' in realm '" + keycloakRealmClient.realm + "'");
+ // Using authenticated client here doesn't seem to work
+ //KeycloakResource keycloak = getKeycloak();
KeycloakResource keycloak = getTarget(httpClient, keycloakServiceUri.build(), null, null, null).proxy(KeycloakResource.class);
+ // Can't get adapter for client in another realm
AdapterConfig adapterConfig = keycloak.getAdapterConfig(
- keycloakRealmClient.realm, keycloakRealmClient.clientId
+ keycloakRealmClient.realm, KEYCLOAK_CLIENT_ID//keycloakRealmClient.clientId
);
// The auth-server-url in the adapter config must be reachable by this manager it will be the frontend URL by default
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index faf3dcc62..5b0f2fc9a 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -23,7 +23,6 @@ import com.vladmihalcea.hibernate.type.array.StringArrayType;
import org.apache.camel.builder.RouteBuilder;
import org.hibernate.Session;
import org.hibernate.jdbc.AbstractReturningWork;
-import org.openremote.agent.protocol.ProtocolClientEventService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceEvent;
import org.openremote.container.persistence.PersistenceService;
@@ -84,7 +83,6 @@ 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;
-import static org.openremote.manager.event.ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID;
import static org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes;
import static org.openremote.model.query.AssetQuery.*;
import static org.openremote.model.query.AssetQuery.Access.PRIVATE;
@@ -377,7 +375,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
}
boolean isAttributeRead = !TextUtil.isNullOrEmpty(attributeName);
- String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
+ String sessionKey = ClientEventService.getSessionKey(exchange);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
// Superuser can get all, User must have role
@@ -394,7 +392,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
.access(access));
if (asset != null) {
- String messageId = exchange.getIn().getHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);
+ String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);
Object response = null;
if (isAttributeRead) {
@@ -419,7 +417,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
ReadAssetsEvent readAssets = exchange.getIn().getBody(ReadAssetsEvent.class);
AssetQuery query = readAssets.getAssetQuery();
- String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
+ String sessionKey = ClientEventService.getSessionKey(exchange);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
// Superuser can get all, User must have role
@@ -439,7 +437,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
List<Asset<?>> assets = findAll(query);
- String messageId = exchange.getIn().getHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);
+ String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);
if (isNullOrEmpty(messageId)) {
clientEventService.sendToSession(sessionKey, new AssetsEvent(assets));
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
index 3c7e73834..c42c181ad 100644
--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
@@ -21,9 +21,6 @@ package org.openremote.manager.event;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
-import org.keycloak.representations.idm.ClientRepresentation;
-import org.openremote.agent.protocol.ProtocolClientEventService;
-import org.openremote.model.Container;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
@@ -31,14 +28,12 @@ import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.mqtt.MqttBrokerService;
import org.openremote.manager.security.ManagerIdentityService;
-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.Constants;
+import org.openremote.model.Container;
+import org.openremote.model.ContainerService;
import org.openremote.model.event.shared.*;
-import org.openremote.model.security.ClientRole;
-import org.openremote.model.security.User;
import org.openremote.model.syslog.SyslogEvent;
-import org.openremote.model.util.TextUtil;
import javax.websocket.CloseReason;
import javax.websocket.Session;
@@ -48,10 +43,9 @@ import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.apache.camel.builder.Builder.header;
import static org.apache.camel.builder.PredicateBuilder.or;
import static org.openremote.container.web.ConnectionConstants.SESSION;
-import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
/**
* Receives and publishes messages, handles the client/server event bus.
@@ -95,7 +89,7 @@ import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
* </p></dd>
* </dl>
*/
-public class ClientEventService implements ProtocolClientEventService {
+public class ClientEventService implements ContainerService {
protected static class SessionInfo {
String connectionType;
@@ -108,6 +102,11 @@ public class ClientEventService implements ProtocolClientEventService {
}
public static final int PRIORITY = ManagerWebService.PRIORITY - 200;
+ public static final String HEADER_ACCESS_RESTRICTED = ClientEventService.class.getName() + ".HEADER_ACCESS_RESTRICTED";
+ public static final String HEADER_CONNECTION_TYPE = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE";
+ public static final String HEADER_CONNECTION_TYPE_WEBSOCKET = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_WEBSOCKET";
+ public static final String HEADER_CONNECTION_TYPE_MQTT = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_MQTT";
+ public static final String HEADER_REQUEST_RESPONSE_MESSAGE_ID = ClientEventService.class.getName() + ".HEADER_REQUEST_RESPONSE_MESSAGE_ID";
private static final Logger LOG = Logger.getLogger(ClientEventService.class.getName());
public static final String WEBSOCKET_EVENTS = "events";
protected static final String INTERNAL_SESSION_KEY = "ClientEventServiceInternal";
@@ -128,6 +127,29 @@ public class ClientEventService implements ProtocolClientEventService {
protected Set<EventSubscription<?>> pendingInternalSubscriptions;
protected boolean stopped;
+ /**
+ * Method to stop further processing of the exchange
+ */
+ public static void stopMessage(Exchange exchange) {
+ exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
+ }
+
+ public static boolean isInbound(Exchange exchange) {
+ return header(HEADER_CONNECTION_TYPE).isNotNull().matches(exchange);
+ }
+
+ public static String getSessionKey(Exchange exchange) {
+ return exchange.getIn().getHeader(ConnectionConstants.SESSION_KEY, String.class);
+ }
+
+ public static String getClientId(Exchange exchange) {
+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
+ if(authContext != null) {
+ return authContext.getClientId();
+ }
+ return null;
+ }
+
@Override
public int getPriority() {
return PRIORITY;
@@ -304,74 +326,14 @@ public class ClientEventService implements ProtocolClientEventService {
stopped = true;
}
- @Override
public void addExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) throws RuntimeException {
exchangeInterceptors.add(exchangeInterceptor);
}
- @Override
public void removeExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) {
exchangeInterceptors.remove(exchangeInterceptor);
}
- @Override
- public void addClientCredentials(ClientCredentials clientCredentials) throws RuntimeException {
- if (!(identityService.getIdentityProvider() instanceof ManagerKeycloakIdentityProvider)) {
- throw new IllegalStateException("Cannot add client credentials if not using Keycloak identity provider");
- }
-
- if (TextUtil.isNullOrEmpty(clientCredentials.getRealm())
- || TextUtil.isNullOrEmpty(clientCredentials.getClientId())
- || TextUtil.isNullOrEmpty(clientCredentials.getSecret())) {
- throw new IllegalArgumentException("Invalid client credentials realm, client ID and secret must be specified");
- }
-
- ManagerKeycloakIdentityProvider keycloakIdentityProvider = (ManagerKeycloakIdentityProvider)identityService.getIdentityProvider();
-
- // Check if exists already
- ClientRepresentation clientRepresentation = keycloakIdentityProvider.getClient(clientCredentials.getRealm(), clientCredentials.getClientId());
- boolean isNew = clientRepresentation == null;
-
- if (clientRepresentation == null) {
- clientRepresentation = new ClientRepresentation();
- clientRepresentation.setStandardFlowEnabled(false);
- clientRepresentation.setImplicitFlowEnabled(false);
- clientRepresentation.setDirectAccessGrantsEnabled(false);
- clientRepresentation.setServiceAccountsEnabled(true);
- clientRepresentation.setClientAuthenticatorType("client-secret");
- clientRepresentation.setClientId(clientCredentials.getClientId());
- }
-
- clientRepresentation.setSecret(clientCredentials.getSecret());
- LOG.info("Creating/updating client event service client credentials: " + clientCredentials);
- if (isNew) {
- keycloakIdentityProvider.createClient(clientCredentials.getRealm(), clientRepresentation);
- } else {
- keycloakIdentityProvider.updateClient(clientCredentials.getRealm(), clientRepresentation);
- }
-
- User serviceUser = keycloakIdentityProvider.getClientServiceUser(clientCredentials.getRealm(), clientCredentials.getClientId());
- if (clientCredentials.getRoles() != null && clientCredentials.getRoles().length > 0) {
- keycloakIdentityProvider.updateUserRoles(clientCredentials.getRealm(), serviceUser.getId(), clientCredentials.getClientId(), Arrays.stream(clientCredentials.getRoles()).filter(Objects::nonNull).map(ClientRole::getValue).toArray(String[]::new));
- }
- }
-
- @Override
- public void removeClientCredentials(String realm, String clientId) throws RuntimeException {
- if (!(identityService.getIdentityProvider() instanceof ManagerKeycloakIdentityProvider)) {
- throw new IllegalStateException("Cannot remove client credentials if not using Keycloak identity provider");
- }
-
- if (TextUtil.isNullOrEmpty(realm)
- || TextUtil.isNullOrEmpty(clientId)) {
- throw new IllegalArgumentException("Invalid client credentials realm and client ID must be specified");
- }
-
- ManagerKeycloakIdentityProvider keycloakIdentityProvider = (ManagerKeycloakIdentityProvider)identityService.getIdentityProvider();
- LOG.info("Deleting client event service client: realm=" + realm + ", client ID=" + clientId);
- keycloakIdentityProvider.deleteClient(realm, clientId);
- }
-
public void addSubscriptionAuthorizer(EventSubscriptionAuthorizer authorizer) {
this.eventSubscriptionAuthorizers.add(authorizer);
}
diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
index 59918b18e..96a39a8ae 100644
--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
@@ -33,8 +33,6 @@ import org.openremote.model.util.TextUtil;
import java.util.*;
import java.util.logging.Logger;
-import static org.openremote.manager.event.ClientEventService.HEADER_ACCESS_RESTRICTED;
-
/**
* Manages subscriptions to events for WebSocket sessions.
*/
@@ -68,7 +66,7 @@ public class EventSubscriptions {
}
public void cancelByType(String eventType) {
- removeIf(sessionSubscription -> sessionSubscription.subscription.getEventType().equals(eventType));
+ removeIf(sessionSubscription -> sessionSubscription.subscriptionId == null && sessionSubscription.subscription.getEventType().equals(eventType));
}
public void cancelById(String subscriptionId) {
@@ -153,7 +151,7 @@ public class EventSubscriptions {
if (event == null)
return messageList;
- boolean accessibleForRestrictedUsers = exchange.getIn().getHeader(HEADER_ACCESS_RESTRICTED, false, Boolean.class);
+ boolean accessibleForRestrictedUsers = exchange.getIn().getHeader(ClientEventService.HEADER_ACCESS_RESTRICTED, false, Boolean.class);
Set<Map.Entry<String, SessionSubscriptions>> sessionSubscriptionsSet;
synchronized (this.sessionSubscriptionIdMap) {
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
index 1235f823d..37c304ab5 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
@@ -22,7 +22,6 @@ package org.openremote.manager.gateway;
import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
-import org.openremote.agent.protocol.ProtocolClientEventService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceEvent;
import org.openremote.container.web.ConnectionConstants;
@@ -48,6 +47,7 @@ import org.openremote.model.gateway.GatewayDisconnectEvent;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.rules.Ruleset;
import org.openremote.model.security.Tenant;
+import org.openremote.model.security.User;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
@@ -66,8 +66,6 @@ import static org.apache.camel.builder.PredicateBuilder.and;
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.ClientCredentials;
-import static org.openremote.manager.event.ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID;
import static org.openremote.manager.gateway.GatewayConnector.mapAssetId;
import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
@@ -262,7 +260,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
}
protected void onMessageIntercept(Exchange exchange) {
- String clientId = ProtocolClientEventService.getClientId(exchange);
+ String clientId = ClientEventService.getClientId(exchange);
if (!isGatewayClientId(clientId)) {
return;
@@ -270,7 +268,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
if (header(ConnectionConstants.SESSION_OPEN).matches(exchange)) {
Session session = exchange.getIn().getHeader(ConnectionConstants.SESSION, Session.class);
- String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
+ String sessionKey = ClientEventService.getSessionKey(exchange);
processGatewayConnected(clientId, sessionKey, session);
return;
}
@@ -281,10 +279,10 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
}
// Inbound shared events
- if (and(ProtocolClientEventService::isInbound, body().isInstanceOf(SharedEvent.class)).matches(exchange)) {
- ProtocolClientEventService.stopMessage(exchange);
+ if (and(ClientEventService::isInbound, body().isInstanceOf(SharedEvent.class)).matches(exchange)) {
+ ClientEventService.stopMessage(exchange);
String gatewayId = getGatewayIdFromClientId(clientId);
- onGatewayClientEventReceived(gatewayId, exchange.getIn().getHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));
+ onGatewayClientEventReceived(gatewayId, exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));
}
}
@@ -592,7 +590,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
String secret = gateway.getClientSecret().orElseGet(() -> UUID.randomUUID().toString());
try {
- clientEventService.addClientCredentials(new ClientCredentials(gateway.getRealm(), null, clientId, secret));
+ identityProvider.createUpdateUser(gateway.getRealm(), new User().setServiceAccount(true).setUsername(clientId), secret);
if (!clientId.equals(gateway.getClientId().orElse(null)) || !secret.equals(gateway.getClientSecret().orElse(null))) {
gateway.setClientId(clientId);
@@ -616,12 +614,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
LOG.warning("Cannot find gateway keycloak client ID so cannot remove keycloak client for gateway: " + gateway.getId());
return;
}
-
- try {
- clientEventService.removeClientCredentials(gateway.getRealm(), id);
- } catch (Exception e) {
- LOG.warning("Failed to delete client for gateway '" + gateway.getId());
- }
+ identityProvider.deleteClient(gateway.getRealm(), id);
}
protected Consumer<Object> createConnectorMessageConsumer(String sessionId) {
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java
deleted file mode 100644
index b19842932..000000000
--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java
+++ /dev/null
@@ -1,329 +0,0 @@
-/*
- * Copyright 2017, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.manager.mqtt;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import io.moquette.interception.AbstractInterceptHandler;
-import io.moquette.interception.messages.*;
-import org.keycloak.adapters.rotation.AdapterTokenVerifier;
-import org.keycloak.common.VerificationException;
-import org.keycloak.representations.AccessToken;
-import org.openremote.container.message.MessageBrokerService;
-import org.openremote.container.security.keycloak.AccessTokenAuthContext;
-import org.openremote.container.util.UniqueIdentifierGenerator;
-import org.openremote.container.web.ConnectionConstants;
-import org.openremote.manager.event.ClientEventService;
-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
-import org.openremote.model.Constants;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetFilter;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.attribute.AttributeRef;
-import org.openremote.model.event.shared.CancelEventSubscription;
-import org.openremote.model.event.shared.EventSubscription;
-import org.openremote.model.syslog.SyslogCategory;
-import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.Values;
-
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import static org.openremote.manager.mqtt.MqttBrokerService.*;
-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
-import static org.openremote.model.syslog.SyslogCategory.API;
-
-public class EventInterceptHandler extends AbstractInterceptHandler {
-
- private static final Logger LOG = SyslogCategory.getLogger(API, EventInterceptHandler.class);
-
- protected final ManagerKeycloakIdentityProvider identityProvider;
- protected final MessageBrokerService messageBrokerService;
- protected final Map<String, MqttConnection> mqttConnectionMap;
-
- public EventInterceptHandler(ManagerKeycloakIdentityProvider managerKeycloakIdentityProvider,
- MessageBrokerService messageBrokerService,
- Map<String, MqttConnection> mqttConnectionMap) {
-
- this.identityProvider = managerKeycloakIdentityProvider;
- this.messageBrokerService = messageBrokerService;
- this.mqttConnectionMap = mqttConnectionMap;
- }
-
- @Override
- public String getID() {
- return UniqueIdentifierGenerator.generateId(EventInterceptHandler.class.getName());
- }
-
- @Override
- public Class<?>[] getInterceptedMessageTypes() {
- return new Class[]{
- InterceptConnectMessage.class,
- InterceptDisconnectMessage.class,
- InterceptConnectionLostMessage.class,
- InterceptSubscribeMessage.class,
- InterceptUnsubscribeMessage.class,
- InterceptPublishMessage.class
- };
- }
-
- @Override
- public void onConnect(InterceptConnectMessage interceptConnectMessage) {
- MqttConnection connection = new MqttConnection(interceptConnectMessage.getClientID(), interceptConnectMessage.getUsername(), interceptConnectMessage.getPassword());
- mqttConnectionMap.put(connection.clientId, connection);
-
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_OPEN, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
- }
-
- @Override
- public void onDisconnect(InterceptDisconnectMessage interceptDisconnectMessage) {
- MqttConnection connection = mqttConnectionMap.remove(interceptDisconnectMessage.getClientID());
- if (connection != null) {
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_CLOSE, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
- }
- }
-
- @Override
- public void onConnectionLost(InterceptConnectionLostMessage interceptConnectionLostMessage) {
- MqttConnection connection = mqttConnectionMap.remove(interceptConnectionLostMessage.getClientID());
- if (connection != null) {
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
-
- }
- LOG.info("Connection lost for client: " + interceptConnectionLostMessage.getClientID());
- }
-
- @Override
- public void onSubscribe(InterceptSubscribeMessage interceptSubscribeMessage) {
- MqttConnection connection = mqttConnectionMap.get(interceptSubscribeMessage.getClientID());
- if (connection != null) {
- String[] topicParts = interceptSubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);
- if (topicParts.length > 1) {
- boolean isAttributeTopic = topicParts[0].equals(ATTRIBUTE_TOPIC);
- boolean isValueSubscription = false;
-
- String secondTopicLevel = topicParts[1];
- String assetId = null;
- if (!secondTopicLevel.equals(SINGLE_LEVEL_WILDCARD) && !secondTopicLevel.equals(MULTI_LEVEL_WILDCARD)) {
- assetId = secondTopicLevel;
- }
-
- if (topicParts[topicParts.length - 1].equals(ATTRIBUTE_VALUE_TOPIC)) {
- isValueSubscription = true;
- }
- String subscriptionId;
- if (isValueSubscription) {
- subscriptionId = connection.attributeValueSubscriptions.remove(interceptSubscribeMessage.getTopicFilter());
- } else {
- subscriptionId = connection.assetSubscriptions.remove(interceptSubscribeMessage.getTopicFilter());
- }
- if (TextUtil.isNullOrEmpty(subscriptionId)) {
- subscriptionId = String.valueOf(connection.getNextSubscriptionId());
- }
-
- EventSubscription<?> subscription;
- if (isAttributeTopic) {
- AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm);
- int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);
-
- if (assetId != null) {
- int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);
- if (multiLevelIndex == -1) {
- if (singleLevelIndex == -1) { //attribute/assetId/
- attributeAssetFilter.setAssetIds(assetId);
- } else {
- attributeAssetFilter.setParentIds(assetId);
- }
- if (topicParts.length > 2) { //attribute/assetId/attributeName
- if (singleLevelIndex == -1) { //attribute/assetId/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[2]);
- } else if (singleLevelIndex == 2 && topicParts.length > 3 && !topicParts[3].equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[3]);
- } // else attribute/assetId/+ which should return all attributes
- }
- } else if (multiLevelIndex == 2) { //attribute/assetId/#
- attributeAssetFilter.setParentIds(assetId);
- }
- } else {
- if (singleLevelIndex == 1) { //attribute/+
- if (topicParts.length > 2) { //attribute/+/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[2]);
- }
- }
- }
-
- subscription = new EventSubscription<>(
- AttributeEvent.class,
- attributeAssetFilter,
- subscriptionId
- );
- } else {
- AssetFilter<AssetEvent> assetFilter = new AssetFilter<AssetEvent>().setRealm(connection.realm);
- if (assetId != null) {
- int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);
- int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);
-
- if (multiLevelIndex == -1) {
- assetFilter.setAssetIds(assetId);
- if (singleLevelIndex == 2) { //asset/assetId/+
- assetFilter.setParentIds(assetId);
- } else if (singleLevelIndex == -1) { //asset/assetId
- assetFilter.setAssetIds(assetId);
- }
- } else if (multiLevelIndex == 2) { //asset/assetId/#
- assetFilter.setParentIds(assetId);
- }
- }
-
- subscription = new EventSubscription<>(
- AssetEvent.class,
- assetFilter,
- subscriptionId
- );
- }
-
- if (isValueSubscription) {
- connection.attributeValueSubscriptions.put(interceptSubscribeMessage.getTopicFilter(), subscription.getSubscriptionId());
- } else {
- connection.assetSubscriptions.put(interceptSubscribeMessage.getTopicFilter(), subscription.getSubscriptionId());
- }
-
- Map<String, Object> headers = prepareHeaders(connection);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
- } else {
- LOG.warning("Couldn't process message for topic: " + interceptSubscribeMessage.getTopicFilter());
- }
- } else {
- LOG.info("No connection found for clientId: " + interceptSubscribeMessage.getClientID());
- }
- }
-
- @Override
- public void onUnsubscribe(InterceptUnsubscribeMessage interceptUnsubscribeMessage) {
- MqttConnection connection = mqttConnectionMap.get(interceptUnsubscribeMessage.getClientID());
- if (connection != null) {
- String[] topicParts = interceptUnsubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);
- if (topicParts.length > 1) {
- String subscriptionId;
-
- boolean isValueSubscription = topicParts[topicParts.length - 1].equals(ATTRIBUTE_VALUE_TOPIC);
- if (isValueSubscription) {
- subscriptionId = connection.attributeValueSubscriptions.remove(interceptUnsubscribeMessage.getTopicFilter());
- } else {
- subscriptionId = connection.assetSubscriptions.remove(interceptUnsubscribeMessage.getTopicFilter());
- }
- if (subscriptionId != null) {
- Map<String, Object> headers = prepareHeaders(connection);
- CancelEventSubscription cancelEventSubscription;
- if (topicParts[0].equals(ATTRIBUTE_TOPIC)) {
- cancelEventSubscription = new CancelEventSubscription(AttributeEvent.class, subscriptionId);
- } else {
- cancelEventSubscription = new CancelEventSubscription(AssetEvent.class, subscriptionId);
- }
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
- }
- } else {
- LOG.warning("Couldn't process message for topic: " + interceptUnsubscribeMessage.getTopicFilter());
- }
- } else {
- LOG.info("No connection found for clientId: " + interceptUnsubscribeMessage.getClientID());
- }
- }
-
- @Override
- public void onPublish(InterceptPublishMessage msg) {
- MqttConnection connection = mqttConnectionMap.get(msg.getClientID());
- if (connection != null) {
- String[] topicParts = msg.getTopicName().split(TOPIC_SEPARATOR);
- if (topicParts.length > 1) {
- String assetId = topicParts[1];
- AttributeRef attributeRef = null;
- if (topicParts.length > 2) { //attribute specific
- attributeRef = new AttributeRef(assetId, topicParts[2]);
- }
- if (attributeRef == null) {
- String payloadContent = msg.getPayload().toString(Charset.defaultCharset());
- Values.parse(payloadContent).flatMap(Values::asJSONObject).ifPresent(objectValue -> {
- Map<String, Object> headers = prepareHeaders(connection);
- Map.Entry<String, JsonNode> firstElem = objectValue.fields().next();
- AttributeEvent attributeEvent = new AttributeEvent(assetId, firstElem.getKey(), firstElem.getValue());
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
- });
- } else {
- String payloadContent = msg.getPayload().toString(Charset.defaultCharset());
- Object value = null;
- if (Character.isLetter(payloadContent.charAt(0))) {
- if (payloadContent.equals(Boolean.TRUE.toString())) {
- value = true;
- }
- if (payloadContent.equals(Boolean.FALSE.toString())) {
- value = false;
- }
- payloadContent = '"' + payloadContent + '"';
- }
- if (value == null) {
- value = Values.parse(payloadContent);
- }
- if (value == null) {
- value = payloadContent;
- }
-
- Map<String, Object> headers = prepareHeaders(connection);
- AttributeEvent attributeEvent = new AttributeEvent(assetId, attributeRef.getName(), value);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
- }
- } else {
- LOG.warning("Couldn't process message for topic: " + msg.getTopicName());
- }
- } else {
- LOG.info("No connection found for clientId: " + msg.getClientID());
- }
- }
-
- private Map<String, Object> prepareHeaders(MqttConnection connection) {
- Map<String, Object> headers = new HashMap<>();
- headers.put(ConnectionConstants.SESSION_KEY, connection.clientId);
- headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
- try {
- AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
- headers.put(Constants.AUTH_CONTEXT, new AccessTokenAuthContext(connection.realm, accessToken));
- } catch (VerificationException e) {
- String suppliedClientSecret = new String(connection.password, StandardCharsets.UTF_8);
- connection.accessToken = identityProvider.getAccessToken(connection.realm, connection.username, suppliedClientSecret);
- try {
- AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
- headers.put(Constants.AUTH_CONTEXT, new AccessTokenAuthContext(connection.realm, accessToken));
- } catch (VerificationException verificationException) {
- LOG.log(Level.WARNING, "Couldn't verify token", verificationException);
- }
- }
- return headers;
- }
-}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java
index c697ef1d6..b80fd1101 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthenticator.java
@@ -24,8 +24,10 @@ import org.keycloak.representations.idm.ClientRepresentation;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.security.Tenant;
import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
import java.nio.charset.StandardCharsets;
+import java.util.Map;
import java.util.logging.Logger;
import static org.openremote.model.syslog.SyslogCategory.API;
@@ -34,37 +36,48 @@ public class KeycloakAuthenticator implements IAuthenticator {
private static final Logger LOG = SyslogCategory.getLogger(API, KeycloakAuthenticator.class);
- public static final String MQTT_CLIENT_ID_SEPARATOR = "_";
+ protected final ManagerKeycloakIdentityProvider identityProvider;
+ protected final Map<String, MqttConnection> sessionIdConnectionMap;
- final ManagerKeycloakIdentityProvider identityProvider;
-
- public KeycloakAuthenticator(ManagerKeycloakIdentityProvider managerIdentityProvider) {
+ public KeycloakAuthenticator(ManagerKeycloakIdentityProvider managerIdentityProvider, Map<String, MqttConnection> sessionIdConnectionMap) {
identityProvider = managerIdentityProvider;
+ this.sessionIdConnectionMap = sessionIdConnectionMap;
}
@Override
- public boolean checkValid(String realm, String clientId, byte[] password) {
- int indexSplit = realm.indexOf(MQTT_CLIENT_ID_SEPARATOR);
- if (indexSplit > 0) {
- realm = realm.substring(0, indexSplit);
+ public boolean checkValid(String clientId, String username, byte[] password) {
+ String[] realmAndClientId = username.split(":");
+ String realm = realmAndClientId[0];
+ username = realmAndClientId[1]; // This is OAuth clientId
+ String suppliedClientSecret = new String(password, StandardCharsets.UTF_8);
+
+ if (TextUtil.isNullOrEmpty(realm)
+ || TextUtil.isNullOrEmpty(username)
+ || TextUtil.isNullOrEmpty(suppliedClientSecret)) {
+ LOG.warning("Realm, client ID and/or client secret missing");
+ return false;
}
- Tenant tenant = identityProvider.getTenant(realm);
- if (tenant == null) {
- LOG.warning("Realm not found");
+ if (sessionIdConnectionMap.containsKey(clientId)) {
+ LOG.warning("Client already connected");
return false;
}
- ClientRepresentation client = identityProvider.getClient(realm, clientId);
+ Tenant tenant = identityProvider.getTenant(realm);
- if (client == null) {
- LOG.warning("Client not found");
+ if (tenant == null || !tenant.getEnabled()) {
+ LOG.warning("Realm not found or is inactive: " + realm);
return false;
}
- String suppliedClientSecret = new String(password, StandardCharsets.UTF_8);
- String clientSecret = client.getSecret();
+ ClientRepresentation clientRepresentation = identityProvider.getClient(realm, username);
+
+ if (clientRepresentation == null || TextUtil.isNullOrEmpty(clientRepresentation.getSecret())) {
+ LOG.warning("Client not found or doesn't support client credentials grant type");
+ return false;
+ }
+ String clientSecret = clientRepresentation.getSecret();
return suppliedClientSecret.equals(clientSecret);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java
deleted file mode 100644
index 4a09ea311..000000000
--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java
+++ /dev/null
@@ -1,195 +0,0 @@
-package org.openremote.manager.mqtt;
-
-import io.moquette.broker.security.IAuthorizatorPolicy;
-import io.moquette.broker.subscriptions.Token;
-import io.moquette.broker.subscriptions.Topic;
-
-import org.keycloak.adapters.rotation.AdapterTokenVerifier;
-import org.keycloak.common.VerificationException;
-import org.keycloak.representations.AccessToken;
-import org.openremote.container.security.AuthContext;
-import org.openremote.container.security.keycloak.AccessTokenAuthContext;
-import org.openremote.manager.asset.AssetStorageService;
-import org.openremote.manager.event.ClientEventService;
-import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetFilter;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.event.shared.EventSubscription;
-import org.openremote.model.event.shared.TenantFilter;
-import org.openremote.model.security.ClientRole;
-import org.openremote.model.syslog.SyslogCategory;
-
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import static org.openremote.manager.mqtt.MqttBrokerService.*;
-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
-import static org.openremote.model.syslog.SyslogCategory.API;
-
-public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {
-
- private static final Logger LOG = SyslogCategory.getLogger(API, KeycloakAuthorizatorPolicy.class);
-
- protected final ManagerKeycloakIdentityProvider identityProvider;
- protected final AssetStorageService assetStorageService;
- protected final ClientEventService clientEventService;
- protected final Map<String, MqttConnection> mqttConnectionMap;
-
- public KeycloakAuthorizatorPolicy(ManagerKeycloakIdentityProvider identityProvider,
- AssetStorageService assetStorageService, ClientEventService clientEventService,
- Map<String, MqttConnection> mqttConnectionMap) {
- this.identityProvider = identityProvider;
- this.assetStorageService = assetStorageService;
- this.clientEventService = clientEventService;
- this.mqttConnectionMap = mqttConnectionMap;
- }
-
- @Override
- public boolean canWrite(Topic topic, String username, String clientId) {
- return verifyRights(topic, username, clientId, ClientRole.WRITE_ASSETS);
- }
-
- @Override
- public boolean canRead(Topic topic, String username, String clientId) {
- return verifyRights(topic, username, clientId, ClientRole.READ_ASSETS);
- }
-
- protected boolean verifyRights(Topic topic, String username, String clientId, ClientRole... roles) {
- MqttConnection connection = mqttConnectionMap.get(clientId);
- if (connection == null) {
- LOG.warning("No connection found for clientId: " + clientId);
- return false;
- }
-
- if (!connection.username.equals(username)) {
- LOG.warning("Username mismatch");
- return false;
- }
-
- if (topic.isEmpty() || topic.getTokens().size() < 2) {
- LOG.warning("Topic may not be empty and should have the following format: asset/... or attribute/...");
- return false;
- }
-
- boolean isAttributeTopic = topic.headToken().toString().equals(ATTRIBUTE_TOPIC);
-
- if (!topic.headToken().toString().equals(ASSET_TOPIC) && !isAttributeTopic) {
- LOG.warning("Topic should start with 'asset' or 'attribute'");
- return false;
- }
-
- Token token = topic.getTokens().get(1);
- Asset<?> asset = null;
- if (!token.toString().equals(SINGLE_LEVEL_WILDCARD) && !token.toString().equals(MULTI_LEVEL_WILDCARD)) {
- asset = assetStorageService.find(token.toString());
- if (asset == null) {
- LOG.warning("Asset not found");
- return false;
- }
- }
-
- if (isAttributeTopic && topic.getTokens().size() > 2) {
- token = topic.getTokens().get(2);
- if (!token.toString().equals(SINGLE_LEVEL_WILDCARD) && !token.toString().equals(MULTI_LEVEL_WILDCARD)) {
- if (asset == null || !asset.getAttribute(token.toString()).isPresent()) {
- LOG.warning("Attribute not found on asset");
- return false;
- }
- }
- }
-
- AccessToken accessToken;
- try {
- accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
- } catch (VerificationException e) {
- String suppliedClientSecret = new String(connection.password, StandardCharsets.UTF_8);
- connection.accessToken = identityProvider.getAccessToken(connection.realm, connection.username, suppliedClientSecret);
- try {
- accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
- } catch (VerificationException verificationException) {
- LOG.log(Level.WARNING, "Couldn't verify token", verificationException);
- return false;
- }
- }
-
- AuthContext authContext = new AccessTokenAuthContext(connection.realm, accessToken);
- if (Arrays.asList(roles).contains(ClientRole.WRITE_ASSETS)) { //write
- return identityProvider.canSubscribeWith(authContext, new TenantFilter(connection.realm), roles);
- } else { // read
- String[] topicParts = topic.getTokens().stream().map(Token::toString).toArray(String[]::new);
-
- if (isAttributeTopic) {
- AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm);
-
- int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);
-
- if (asset != null) {
- int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);
- if (multiLevelIndex == -1) {
- if (singleLevelIndex == -1) { //attribute/assetId/
- attributeAssetFilter.setAssetIds(asset.getId());
- } else {
- attributeAssetFilter.setParentIds(asset.getId());
- }
- if (topicParts.length > 2) { //attribute/assetId/attributeName
- if (singleLevelIndex == -1) { //attribute/assetId/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[2]);
- } else if (singleLevelIndex == 2 && topicParts.length > 3 && !topicParts[3].equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[3]);
- } // else attribute/assetId/+ which should return all attributes
- }
- } else if (multiLevelIndex == 2) { //attribute/assetId/#
- attributeAssetFilter.setParentIds(asset.getParentId());
- }
- } else {
- if (singleLevelIndex == 1) { //attribute/+
- if (topicParts.length > 2) { //attribute/+/attributeName
- attributeAssetFilter.setAttributeNames(topicParts[2]);
- } else {
- LOG.warning("Using single level must be followed with attribute name when not using assetId");
- return false;
- }
- }
- }
-
- EventSubscription<AttributeEvent> subscription = new EventSubscription<>(
- AttributeEvent.class,
- attributeAssetFilter
- );
- return clientEventService.authorizeEventSubscription(authContext, subscription);
- } else {
- AssetFilter<AssetEvent> assetFilter = new AssetFilter<AssetEvent>().setRealm(connection.realm);
- if (asset != null) {
- int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);
- int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);
-
- if (multiLevelIndex == -1) {
- assetFilter.setAssetIds(asset.getId());
- if (singleLevelIndex == 2) { //asset/assetId/+
- assetFilter.setParentIds(asset.getId());
- } else if (singleLevelIndex == -1) { //asset/assetId
- assetFilter.setAssetIds(asset.getId());
- } else {
- LOG.warning("Using single level can be only used after assetId for asset events");
- return false;
- }
- } else if (multiLevelIndex == 2) { //asset/assetId/#
- assetFilter.setParentIds(asset.getParentId());
- }
- }
-
- EventSubscription<AssetEvent> subscription = new EventSubscription<>(
- AssetEvent.class,
- assetFilter
- );
- return clientEventService.authorizeEventSubscription(authContext, subscription);
- }
- }
- }
-}
-
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java
new file mode 100644
index 000000000..4fe4fd902
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2021, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.mqtt;
+
+import io.moquette.broker.subscriptions.Topic;
+import io.moquette.interception.messages.*;
+import org.openremote.container.security.AuthContext;
+
+/**
+ * This allows custom handlers to be injected into the {@link MqttBrokerService} so a topic(s) can be handled in a
+ * custom way.
+ */
+public interface MQTTCustomHandler {
+
+ /**
+ * Provides a name to identify this custom handler for logging purposes etc.
+ */
+ String getName();
+
+ /**
+ * Should this handler be called to handle this topic; general security checks are performed before this handler
+ * is called so this handler can focus on the required custom business logic. This handler should return null
+ * if it doesn't want to allow/prevent the pub/sub.
+ */
+ Boolean shouldIntercept(AuthContext authContext, MqttConnection connection, Topic topic, boolean isWrite);
+
+ /**
+ * Will be called when any client connects
+ */
+ void onConnect(MqttConnection connection, InterceptConnectMessage msg);
+
+ /**
+ * Will be called when any client disconnects
+ */
+ void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg);
+
+ /**
+ * Will be called when any client loses connection
+ */
+ void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg);
+
+ /**
+ * Will be called for a specific topic if {@link #shouldIntercept} returned true.
+ */
+ void onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);
+
+ /**
+ * Will be called for a specific topic if {@link #shouldIntercept} returned true.
+ */
+ void onUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg);
+
+ /**
+ * Will be called for a specific topic if {@link #shouldIntercept} returned true.
+ */
+ void onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);
+}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
index 2a2e30a9d..3a118fc66 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -19,38 +19,44 @@
*/
package org.openremote.manager.mqtt;
-import com.fasterxml.jackson.core.JsonProcessingException;
import io.moquette.BrokerConstants;
import io.moquette.broker.Server;
import io.moquette.broker.config.MemoryConfig;
-import io.moquette.broker.security.IAuthorizatorPolicy;
+import io.moquette.broker.subscriptions.Topic;
import io.moquette.interception.InterceptHandler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-import io.netty.handler.codec.mqtt.*;
+import io.netty.handler.codec.mqtt.MqttMessageBuilders;
+import io.netty.handler.codec.mqtt.MqttPublishMessage;
+import io.netty.handler.codec.mqtt.MqttQoS;
import org.apache.camel.builder.RouteBuilder;
-import org.openremote.model.Container;
-import org.openremote.model.ContainerService;
import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.security.AuthContext;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Container;
+import org.openremote.model.ContainerService;
import org.openremote.model.asset.AssetEvent;
+import org.openremote.model.asset.AssetFilter;
import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.event.Event;
import org.openremote.model.event.TriggeredEventSubscription;
+import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.Values;
import java.nio.charset.Charset;
import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.openremote.container.util.MapAccess.getInteger;
import static org.openremote.container.util.MapAccess.getString;
-import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
+import static org.openremote.manager.event.ClientEventService.getSessionKey;
import static org.openremote.model.syslog.SyslogCategory.API;
public class MqttBrokerService implements ContainerService {
@@ -64,7 +70,6 @@ public class MqttBrokerService implements ContainerService {
public static final String ASSET_TOPIC = "asset";
public static final String ATTRIBUTE_TOPIC = "attribute";
- public static final String TOPIC_SEPARATOR = "/";
public static final String ATTRIBUTE_VALUE_TOPIC = "value";
public static final String SINGLE_LEVEL_WILDCARD = "+";
public static final String MULTI_LEVEL_WILDCARD = "#";
@@ -72,9 +77,11 @@ public class MqttBrokerService implements ContainerService {
protected ManagerKeycloakIdentityProvider identityProvider;
protected ClientEventService clientEventService;
protected MessageBrokerService messageBrokerService;
- protected ORAuthorizatorPolicy mainAuthorizatorPolicy;
-
- protected Map<String, MqttConnection> mqttConnectionMap;
+ // Moquette doesn't provide any session id so cannot have multiple connections per realm-clientId combo
+ protected Map<String, MqttConnection> sessionIdConnectionMap = new HashMap<>();
+ protected final Set<MQTTCustomHandler> customHandlers = new CopyOnWriteArraySet<>();
+ // This is not ideal (it'll keep filling with topics) but Moquette SPI is crappy - no association between Authoriser and Interceptor
+ protected final Map<String, MQTTCustomHandler> topicCustomHandlerMap = new ConcurrentHashMap<>();
protected boolean active;
protected String host;
@@ -91,9 +98,6 @@ public class MqttBrokerService implements ContainerService {
host = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST);
port = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT);
- mainAuthorizatorPolicy = new ORAuthorizatorPolicy();
- mqttConnectionMap = new HashMap<>();
-
clientEventService = container.getService(ClientEventService.class);
ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
@@ -119,43 +123,14 @@ public class MqttBrokerService implements ContainerService {
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
@SuppressWarnings("unchecked")
- TriggeredEventSubscription<?> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);
- triggeredEventSubscription.getEvents()
- .forEach(event -> {
- if (event.getEventType().equals(Event.getEventType(AssetEvent.class))) {
- AssetEvent assetEvent = (AssetEvent) event;
- MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);
- if (mqttConnection != null) {
- Optional<String> topic = mqttConnection.assetSubscriptions.entrySet()
- .stream()
- .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))
- .map(Map.Entry::getKey)
- .findFirst();
-
- topic.ifPresent(topicValue -> sendAssetEvent(sessionKey, topicValue, assetEvent));
- }
- } else {
- AttributeEvent attributeEvent = (AttributeEvent) event;
- MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);
- if (mqttConnection != null) {
- Optional<String> topic = mqttConnection.assetSubscriptions.entrySet()
- .stream()
- .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))
- .map(Map.Entry::getKey)
- .findFirst();
-
- topic.ifPresent(topicValue -> sendAttributeEvent(sessionKey, topicValue, attributeEvent));
-
- topic = mqttConnection.attributeValueSubscriptions.entrySet()
- .stream()
- .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))
- .map(Map.Entry::getKey)
- .findFirst();
-
- topic.ifPresent(topicValue -> sendAttributeValue(sessionKey, topicValue, attributeEvent));
- }
- }
- });
+ TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);
+ MqttConnection connection = sessionIdConnectionMap.get(sessionKey);
+
+ if (connection == null) {
+ return;
+ }
+
+ onSubscriptionTriggered(connection, triggeredEventSubscription);
})
.end();
}
@@ -168,12 +143,10 @@ public class MqttBrokerService implements ContainerService {
properties.setProperty(BrokerConstants.HOST_PROPERTY_NAME, host);
properties.setProperty(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(port));
properties.setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, String.valueOf(false));
- List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new EventInterceptHandler(identityProvider, messageBrokerService, mqttConnectionMap));
+ List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, sessionIdConnectionMap));
AssetStorageService assetStorageService = container.getService(AssetStorageService.class);
- mainAuthorizatorPolicy.addAuthorizatorPolicy(new KeycloakAuthorizatorPolicy(identityProvider, assetStorageService, clientEventService, mqttConnectionMap));
-
- mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, new KeycloakAuthenticator(identityProvider), mainAuthorizatorPolicy);
+ mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, new KeycloakAuthenticator(identityProvider, sessionIdConnectionMap), new ORAuthorizatorPolicy(identityProvider, this, assetStorageService, clientEventService));
LOG.fine("Started MQTT broker");
}
@@ -183,55 +156,154 @@ public class MqttBrokerService implements ContainerService {
LOG.fine("Stopped MQTT broker");
}
- protected void sendAssetEvent(String clientId, String topic, AssetEvent assetEvent) {
+ public void addCustomHandler(MQTTCustomHandler customHandler) {
+ customHandlers.add(customHandler);
+ }
+
+ public void removeCustomHandler(MQTTCustomHandler customHandler) {
+ customHandlers.remove(customHandler);
+ topicCustomHandlerMap.values().removeIf(h -> h == customHandler);
+ }
+
+ public void sendToSession(String sessionId, String topic, Object data) {
try {
- ByteBuf payload = Unpooled.copiedBuffer(Values.JSON.writeValueAsString(assetEvent), Charset.defaultCharset());
+ ByteBuf payload = Unpooled.copiedBuffer(Values.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());
MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
- .qos(MqttQoS.AT_MOST_ONCE)
- .topicName(topic)
- .payload(payload)
- .build();
+ .qos(MqttQoS.AT_MOST_ONCE)
+ .topicName(topic)
+ .payload(payload)
+ .build();
- mqttBroker.internalPublish(publishMessage, clientId);
- } catch (JsonProcessingException e) {
+ mqttBroker.internalPublish(publishMessage, sessionId);
+ } catch (Exception e) {
LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
}
}
- protected void sendAttributeEvent(String clientId, String topic, AttributeEvent attributeEvent) {
- try {
- ByteBuf payload = Unpooled.copiedBuffer(Values.JSON.writeValueAsString(attributeEvent), Charset.defaultCharset());
- MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
- .qos(MqttQoS.AT_MOST_ONCE)
- .topicName(topic)
- .payload(payload)
- .build();
+ public static boolean isAttributeTopic(List<String> tokens) {
+ return tokens.get(0).equals(ATTRIBUTE_TOPIC);
+ }
- mqttBroker.internalPublish(publishMessage, clientId);
- } catch (JsonProcessingException e) {
- LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
- }
+ public static boolean isAssetTopic(List<String> tokens) {
+ return tokens.get(0).equals(ASSET_TOPIC);
}
- public void sendAttributeValue(String clientId, String topic, AttributeEvent attributeEvent) {
- ByteBuf payload = Unpooled.copiedBuffer(Values.asJSON(attributeEvent.getValue()).orElse(""), Charset.defaultCharset());
+ public static AssetFilter<?> buildAssetFilter(MqttConnection connection, List<String> topicTokens) {
+ if (topicTokens == null || topicTokens.isEmpty()) {
+ return null;
+ }
- MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
- .qos(MqttQoS.AT_MOST_ONCE)
- .topicName(topic)
- .payload(payload)
- .build();
+ boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
+ String realm = connection.getRealm();
+ List<String> assetIds = new ArrayList<>();
+ List<String> parentIds = new ArrayList<>();
+ List<String> attributeNames = new ArrayList<>();
+
+ String assetId = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(1)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(1)) ? null : topicTokens.get(1);
+
+ if (isAttributeTopic) {
+ int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);
+
+ if (assetId != null) {
+ int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);
+ if (multiLevelIndex == -1) {
+ if (singleLevelIndex == -1) { //attribute/assetId/
+ assetIds.add(assetId);
+ } else {
+ parentIds.add(assetId);
+ }
+ if (topicTokens.size() > 2) { //attribute/assetId/attributeName
+ if (singleLevelIndex == -1) { //attribute/assetId/attributeName
+ attributeNames.add(topicTokens.get(2));
+ } else if (singleLevelIndex == 2 && topicTokens.size() > 3 && !topicTokens.get(3).equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName
+ attributeNames.add(topicTokens.get(3));
+ } // else attribute/assetId/+ which should return all attributes
+ }
+ } else if (multiLevelIndex == 2) { //attribute/assetId/#
+ parentIds.add(assetId);
+ }
+ } else {
+ if (singleLevelIndex == 1) { //attribute/+
+ if (topicTokens.size() > 2) { //attribute/+/attributeName
+ attributeNames.add(topicTokens.get(2));
+ }
+ }
+ }
- mqttBroker.internalPublish(publishMessage, clientId);
+ } else if (assetId != null) {
+ int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);
+ int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);
+
+ if (multiLevelIndex == -1) {
+ assetIds.add(assetId);
+ if (singleLevelIndex == 2) { //asset/assetId/+
+ parentIds.add(assetId);
+ } else if (singleLevelIndex == -1) { //asset/assetId
+ assetIds.add(assetId);
+ }
+ } else if (multiLevelIndex == 2) { //asset/assetId/#
+ parentIds.add(assetId);
+ }
+ }
+
+ AssetFilter<?> assetFilter = new AssetFilter<>().setRealm(realm);
+ if (!assetIds.isEmpty()) {
+ assetFilter.setAssetIds(assetIds.toArray(new String[0]));
+ }
+ if (!parentIds.isEmpty()) {
+ assetFilter.setParentIds(parentIds.toArray(new String[0]));
+ }
+ if (!attributeNames.isEmpty()) {
+ assetFilter.setAttributeNames(attributeNames.toArray(new String[0]));
+ }
+ return assetFilter;
+ }
+
+ protected void onSubscriptionTriggered(MqttConnection connection, TriggeredEventSubscription<SharedEvent> triggeredEventSubscription) {
+ triggeredEventSubscription.getEvents()
+ .forEach(event -> {
+ Consumer<SharedEvent> eventConsumer = connection.subscriptionHandlerMap.get(triggeredEventSubscription.getSubscriptionId());
+ if (eventConsumer != null) {
+ eventConsumer.accept(event);
+ }
+ });
+ }
+
+ protected Consumer<SharedEvent> getEventConsumer(MqttConnection connection, String topic, boolean isValueSubscription) {
+ return ev -> {
+ if (isValueSubscription) {
+ if (ev instanceof AttributeEvent) {
+ AttributeEvent attributeEvent = (AttributeEvent) ev;
+ sendToSession(connection.getSessionId(), topic, attributeEvent.getValue().orElse(null));
+ }
+ } else {
+ if (ev instanceof AttributeEvent || ev instanceof AssetEvent) {
+ sendToSession(connection.getSessionId(), topic, ev);
+ }
+ }
+ };
}
- public MqttBrokerService addInterceptHandler(InterceptHandler interceptHandler) {
- mqttBroker.addInterceptHandler(interceptHandler);
- return this;
+ protected Boolean customHandlerAuthorises(AuthContext authContext, MqttConnection connection, Topic topic, boolean isWrite) {
+ // See if a custom handler wants to handle this topic
+ for (MQTTCustomHandler handler : customHandlers) {
+ Boolean result = handler.shouldIntercept(authContext, connection, topic, isWrite);
+ if (result != null) {
+ topicCustomHandlerMap.put(topic.toString(), handler);
+ LOG.info("Custom handler intercepted request: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ return result;
+ }
+ }
+
+ return null;
}
- public void addAuthorizerPolicy(IAuthorizatorPolicy authorizatorPolicy) {
- mainAuthorizatorPolicy.addAuthorizatorPolicy(authorizatorPolicy);
+ /**
+ * Looks for a custom handler that authorised this topic and if found it return the {@link InterceptHandler}
+ * otherwise returns null.
+ */
+ protected MQTTCustomHandler getCustomInterceptHandler(String topic) {
+ return topicCustomHandlerMap.get(topic);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
index c4535c0d3..d5e2abd1a 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -1,79 +1,89 @@
package org.openremote.manager.mqtt;
-import org.openremote.model.attribute.AttributeRef;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.auth.OAuthClientCredentialsGrant;
+import org.openremote.model.auth.OAuthGrant;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.util.TextUtil;
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.logging.Logger;
-import static org.openremote.manager.mqtt.KeycloakAuthenticator.MQTT_CLIENT_ID_SEPARATOR;
-
+/**
+ * Handles access token generation and tracks all subscriptions for the connection
+ */
public class MqttConnection {
+ protected static final Logger LOG = Logger.getLogger(MqttConnection.class.getSimpleName());
protected final String realm;
- protected final String clientId;
- protected final String username;
- protected final byte[] password;
- protected final Map<String, String> assetSubscriptions;
- protected final Map<String, String> attributeValueSubscriptions;
- protected String accessToken;
- protected int subscriptionId;
-
- public MqttConnection(String clientId, String username, byte[] password) {
- int indexSplit = clientId.indexOf(MQTT_CLIENT_ID_SEPARATOR);
- if (indexSplit > 0) {
- realm = clientId.substring(0, indexSplit);
- } else {
- realm = clientId;
- }
- this.clientId = clientId;
+ protected final String username; // This is OAuth clientId
+ protected final String password;
+ protected final Map<String, Consumer<SharedEvent>> subscriptionHandlerMap = new HashMap<>();
+ protected final String sessionId;
+ protected Supplier<String> tokenSupplier;
+
+ public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String sessionId, String realm, String username, String password) {
+ this.realm = realm;
this.username = username;
this.password = password;
- this.assetSubscriptions = new HashMap<>();
- attributeValueSubscriptions = new HashMap<>();
- this.subscriptionId = 0;
- }
+ this.sessionId = sessionId;
+ String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
- public int getNextSubscriptionId() {
- return ++subscriptionId;
- }
+ if (!TextUtil.isNullOrEmpty(realm)
+ && !TextUtil.isNullOrEmpty(username)
+ && !TextUtil.isNullOrEmpty(password)) {
+ OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);
+ tokenSupplier = identityProvider.getAccessTokenSupplier(grant);
+ } else {
+ LOG.info("Invalid credentials provided, MQTT connection is not valid: " + this);
+ }
+ }
public String getRealm() {
return this.realm;
}
-
- public String getClientId() {
- return this.clientId;
- }
-
-
public String getUsername() {
return this.username;
}
-
- public byte[] getPassword() {
+ public String getPassword() {
return this.password;
}
-
- public Map<String, String> getAssetSubscriptions() {
- return this.assetSubscriptions;
+ public Map<String, Consumer<SharedEvent>> getSubscriptionHandlerMap() {
+ return this.subscriptionHandlerMap;
}
-
- public Map<String, String> getAttributeValueSubscriptions() {
- return this.attributeValueSubscriptions;
+ /**
+ * Doesn't mean that credentials are valid just that correct info is set
+ */
+ public boolean isValid() {
+ return tokenSupplier != null;
}
-
public String getAccessToken() {
- return this.accessToken;
+ if (!isValid()) {
+ return null;
+ }
+
+ return tokenSupplier.get();
}
+ public String getSessionId() {
+ return sessionId;
+ }
- public int getSubscriptionId() {
- return this.subscriptionId;
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName() + "{" +
+ "realm='" + realm + '\'' +
+ ", username='" + username + '\'' +
+ ", sessionId='" + sessionId + '\'' +
+ '}';
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
index 422833465..2b523d801 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
@@ -1,28 +1,192 @@
package org.openremote.manager.mqtt;
-import java.util.Collection;
-import java.util.concurrent.CopyOnWriteArraySet;
-
import io.moquette.broker.security.IAuthorizatorPolicy;
+import io.moquette.broker.subscriptions.Token;
import io.moquette.broker.subscriptions.Topic;
+import org.keycloak.adapters.rotation.AdapterTokenVerifier;
+import org.keycloak.common.VerificationException;
+import org.keycloak.representations.AccessToken;
+import org.openremote.container.security.AuthContext;
+import org.openremote.container.security.keycloak.AccessTokenAuthContext;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.AssetEvent;
+import org.openremote.model.asset.AssetFilter;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.event.shared.EventSubscription;
+import org.openremote.model.security.ClientRole;
+import org.openremote.model.syslog.SyslogCategory;
+
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.openremote.manager.mqtt.MqttBrokerService.*;
+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
+import static org.openremote.model.syslog.SyslogCategory.API;
public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
- final protected Collection<IAuthorizatorPolicy> authorizatorPolicies = new CopyOnWriteArraySet<>();
+ private static final Logger LOG = SyslogCategory.getLogger(API, ORAuthorizatorPolicy.class);
- @Override
- public boolean canRead(Topic topic, String username, String clientId) {
- return authorizatorPolicies.stream()
- .anyMatch(authorizer -> authorizer.canRead(topic, username, clientId));
+ protected final ManagerKeycloakIdentityProvider identityProvider;
+ protected final AssetStorageService assetStorageService;
+ protected final ClientEventService clientEventService;
+ protected final MqttBrokerService brokerService;
+
+ public ORAuthorizatorPolicy(ManagerKeycloakIdentityProvider identityProvider,
+ MqttBrokerService brokerService,
+ AssetStorageService assetStorageService,
+ ClientEventService clientEventService) {
+ this.identityProvider = identityProvider;
+ this.brokerService = brokerService;
+ this.assetStorageService = assetStorageService;
+ this.clientEventService = clientEventService;
}
@Override
public boolean canWrite(Topic topic, String username, String clientId) {
- return authorizatorPolicies.stream()
- .anyMatch(authorizer -> authorizer.canWrite(topic, username, clientId));
+ String[] realmAndUsername = username.split(":");
+ String realm = realmAndUsername[0];
+ username = realmAndUsername[1];
+ return verifyRights(topic, clientId, realm, username, true);
+ }
+
+ @Override
+ public boolean canRead(Topic topic, String username, String clientId) {
+ String[] realmAndUsername = username.split(":");
+ String realm = realmAndUsername[0];
+ username = realmAndUsername[1];
+ return verifyRights(topic, clientId, realm, username, false);
}
- public void addAuthorizatorPolicy(IAuthorizatorPolicy authorizer) {
- this.authorizatorPolicies.add(authorizer);
- }
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ protected boolean verifyRights(Topic topic, String sessionId, String realm, String username, boolean isWrite) {
+ MqttConnection connection = brokerService.sessionIdConnectionMap.get(sessionId);
+ AuthContext authContext;
+
+ if (connection == null) {
+ LOG.warning("No connection found: sessionId=" + sessionId);
+ return false;
+ }
+
+ try {
+ AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.getAccessToken(), identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
+ authContext = accessToken != null ? new AccessTokenAuthContext(connection.realm, accessToken) : null;
+ } catch (VerificationException e) {
+ LOG.log(Level.INFO, "Couldn't verify token: " + connection, e);
+ return false;
+ }
+
+ if (authContext == null) {
+ LOG.info("Failed to get auth context for connection: " + connection);
+ return false;
+ }
+
+ Boolean result = brokerService.customHandlerAuthorises(authContext, connection, topic, isWrite);
+ if (result != null) {
+ return result;
+ }
+
+ List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
+
+ if (topicTokens.size() < 2) {
+ LOG.info("Topic may not be empty and should have the following format: //asset/... or //attribute/...");
+ return false;
+ }
+
+ boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
+ boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
+
+ if (!isAssetTopic && !isAttributeTopic) {
+ LOG.info("Topic should start with 'asset' or 'attribute': " + connection);
+ return false;
+ }
+
+ String assetId = topicTokens.get(1);
+
+ if (!SINGLE_LEVEL_WILDCARD.equals(assetId) && !MULTI_LEVEL_WILDCARD.equals(assetId)) {
+ Asset<?> asset = assetStorageService.find(assetId);
+
+ if (asset == null) {
+ LOG.info("Asset not found for topic '" + topic + "': " + connection);
+ return false;
+ }
+
+ if (isAttributeTopic && topicTokens.size() > 2
+ && !(SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)))) {
+ String attributeName = topicTokens.get(2);
+
+ if (!asset.hasAttribute(attributeName)) {
+ LOG.info("Asset attribute not found for topic '" + topic + "': " + connection);
+ return false;
+ }
+ }
+ }
+
+ if (isWrite) {
+
+ if (topicTokens.contains(SINGLE_LEVEL_WILDCARD) || topicTokens.contains(MULTI_LEVEL_WILDCARD)) {
+ LOG.info("Write does not support wildcards in topic: " + connection);
+ return false;
+ }
+
+ // Check we have the correct write role
+ if (isAssetTopic && !authContext.hasResourceRole(ClientRole.WRITE_ASSETS.getValue(), username)) {
+ LOG.info("Write request to asset topic but user doesn't have required role: " + connection);
+ return false;
+ } else if (isAttributeTopic && !authContext.hasResourceRole(ClientRole.WRITE_ATTRIBUTES.getValue(), username)) {
+ LOG.info("Write request to attribute topic but user doesn't have required role: " + connection);
+ return false;
+ }
+
+ // Check if this is a restricted user
+ boolean isRestrictedUser = identityProvider.isRestrictedUser(authContext.getUserId());
+
+ if (isAssetTopic) {
+ if (topicTokens.size() > 1) {
+ LOG.info("Write request to asset topic shouldn't contain any topic parts: " + connection);
+ return false;
+ }
+
+ if (isRestrictedUser) {
+ LOG.info("Restricted users cannot write assets: " + connection);
+ return false;
+ }
+ } else {
+ // Must be of form //attribute/assetId/attributeName or //attribute/assetId/attributeName/value
+ if (topicTokens.size() == 3 || (topicTokens.size() == 4 && ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(3)))) {
+ LOG.info("Write request to attribute topic should contain asset ID and attribute name only: " + connection);
+ return false;
+ }
+
+ // Security of attribute write will be handled by the asset processing service so don't need to do anything here
+ }
+ } else { // read
+
+ // Build filter for the topic and verify that the filter is OK for given auth context
+ AssetFilter filter = buildAssetFilter(connection, topicTokens);
+ Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
+
+ if (filter == null) {
+ LOG.info("Invalid event filter generated for topic '" + topic + "': " + connection);
+ return false;
+ }
+ EventSubscription subscription = new EventSubscription(
+ subscriptionClass,
+ filter
+ );
+
+ if (!clientEventService.authorizeEventSubscription(authContext, subscription)) {
+ LOG.info("Generated event filter was not authorised for topic '" + topic + "': " + connection);
+ return false;
+ }
+ }
+
+ return true;
+ }
}
+
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
new file mode 100644
index 000000000..71b250a40
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
@@ -0,0 +1,302 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.mqtt;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.api.client.util.Charsets;
+import io.moquette.broker.subscriptions.Token;
+import io.moquette.broker.subscriptions.Topic;
+import io.moquette.interception.AbstractInterceptHandler;
+import io.moquette.interception.messages.*;
+import org.keycloak.adapters.rotation.AdapterTokenVerifier;
+import org.keycloak.common.VerificationException;
+import org.keycloak.representations.AccessToken;
+import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.security.keycloak.AccessTokenAuthContext;
+import org.openremote.container.web.ConnectionConstants;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Constants;
+import org.openremote.model.asset.AssetEvent;
+import org.openremote.model.asset.AssetFilter;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.event.shared.CancelEventSubscription;
+import org.openremote.model.event.shared.EventSubscription;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.value.Values;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.openremote.manager.mqtt.MqttBrokerService.*;
+import static org.openremote.model.syslog.SyslogCategory.API;
+
+public class ORInterceptHandler extends AbstractInterceptHandler {
+
+ private static final Logger LOG = SyslogCategory.getLogger(API, ORInterceptHandler.class);
+ protected final MqttBrokerService brokerService;
+ protected final MessageBrokerService messageBrokerService;
+ protected final ManagerKeycloakIdentityProvider identityProvider;
+ protected final Map<String, MqttConnection> sessionIdConnectionMap;
+
+ public ORInterceptHandler(MqttBrokerService brokerService,
+ ManagerKeycloakIdentityProvider identityProvider,
+ MessageBrokerService messageBrokerService,
+ Map<String, MqttConnection> sessionIdConnectionMap) {
+ this.brokerService = brokerService;
+ this.identityProvider = identityProvider;
+ this.messageBrokerService = messageBrokerService;
+ this.sessionIdConnectionMap = sessionIdConnectionMap;
+ }
+
+ @Override
+ public String getID() {
+ return ORInterceptHandler.class.getName();
+ }
+
+ @Override
+ public Class<?>[] getInterceptedMessageTypes() {
+ return new Class[]{
+ InterceptConnectMessage.class,
+ InterceptDisconnectMessage.class,
+ InterceptConnectionLostMessage.class,
+ InterceptSubscribeMessage.class,
+ InterceptUnsubscribeMessage.class,
+ InterceptPublishMessage.class
+ };
+ }
+
+ @Override
+ public void onConnect(InterceptConnectMessage msg) {
+ String[] realmAndUsername = msg.getUsername().split(":");
+ String realm = realmAndUsername[0];
+ String username = realmAndUsername[1];
+ String password = msg.getPassword() != null ? new String(msg.getPassword(), Charsets.UTF_8) : null;
+ MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password);
+
+ sessionIdConnectionMap.put(connection.getSessionId(), connection);
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_OPEN, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.info("Connected: " + connection);
+
+ // No topic info here (not sure what willTopic is??) so just notify all custom handlers
+ brokerService.customHandlers.forEach(customHandler -> customHandler.onConnect(connection, msg));
+ }
+
+ @Override
+ public void onDisconnect(InterceptDisconnectMessage msg) {
+ MqttConnection connection = sessionIdConnectionMap.remove(msg.getClientID());
+
+ if (connection != null) {
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_CLOSE, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.info("Connection closed: " + connection);
+
+ // No topic info here so just notify all custom handlers
+ brokerService.customHandlers.forEach(customHandler -> customHandler.onDisconnect(connection, msg));
+ }
+ }
+
+ @Override
+ public void onConnectionLost(InterceptConnectionLostMessage msg) {
+ MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+
+ if (connection != null) {
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.info("Connection lost: " + connection);
+
+ // No topic info here so just notify all custom handlers
+ brokerService.customHandlers.forEach(customHandler -> customHandler.onConnectionLost(connection, msg));
+ }
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Override
+ public void onSubscribe(InterceptSubscribeMessage msg) {
+
+ String[] realmAndUsername = msg.getUsername().split(":");
+ String realm = realmAndUsername[0];
+ String username = realmAndUsername[1];
+ MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+
+ if (connection == null) {
+ LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());
+ return;
+ }
+
+ Topic topic = Topic.asTopic(msg.getTopicFilter());
+
+ MQTTCustomHandler customHandler;
+ if ((customHandler = brokerService.getCustomInterceptHandler(msg.getTopicFilter())) != null) {
+ customHandler.onSubscribe(connection, topic, msg);
+ return;
+ }
+
+ List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
+ boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
+ boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
+ boolean isValueSubscription = isAttributeTopic && topic.toString().endsWith(ATTRIBUTE_VALUE_TOPIC);
+ String subscriptionId = msg.getTopicFilter(); // Use topic as subscription ID
+
+ AssetFilter filter = buildAssetFilter(connection, topicTokens);
+ Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
+
+ if (filter == null) {
+ LOG.info("Invalid event filter generated for topic '" + topic + "': " + connection);
+ return;
+ }
+
+ EventSubscription subscription = new EventSubscription(
+ subscriptionClass,
+ filter,
+ subscriptionId
+ );
+
+ Consumer<SharedEvent> eventConsumer = brokerService.getEventConsumer(connection, subscriptionId, isValueSubscription);
+ connection.subscriptionHandlerMap.put(subscriptionId, eventConsumer);
+ Map<String, Object> headers = prepareHeaders(connection);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override
+ public void onUnsubscribe(InterceptUnsubscribeMessage msg) {
+
+ String[] realmAndUsername = msg.getUsername().split(":");
+ String realm = realmAndUsername[0];
+ String username = realmAndUsername[1];
+ MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+
+ if (connection == null) {
+ LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());
+ return;
+ }
+
+ Topic topic = Topic.asTopic(msg.getTopicFilter());
+
+ MQTTCustomHandler customHandler;
+ if ((customHandler = brokerService.getCustomInterceptHandler(topic.toString())) != null) {
+ customHandler.onUnsubscribe(connection, topic, msg);
+ return;
+ }
+
+ String subscriptionId = topic.toString();
+ Consumer<? extends SharedEvent> eventConsumer = connection.subscriptionHandlerMap.remove(subscriptionId);
+
+ if (eventConsumer != null) {
+ boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
+ CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
+ }
+ }
+
+ @Override
+ public void onPublish(InterceptPublishMessage msg) {
+ String[] realmAndUsername = msg.getUsername().split(":");
+ String realm = realmAndUsername[0];
+ String username = realmAndUsername[1];
+ MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+
+ if (connection == null) {
+ LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());
+ return;
+ }
+
+ Topic topic = Topic.asTopic(msg.getTopicName());
+
+ MQTTCustomHandler customHandler;
+ if ((customHandler = brokerService.getCustomInterceptHandler(topic.toString())) != null) {
+ customHandler.onPublish(connection, topic, msg);
+ return;
+ }
+
+ List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
+
+ if (topicTokens.size() > 1) {
+ String assetId = topicTokens.get(1);
+ AttributeRef attributeRef = null;
+ if (topicTokens.size() > 2) { //attribute specific
+ attributeRef = new AttributeRef(assetId, topicTokens.get(2));
+ }
+ if (attributeRef == null) {
+ String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);
+ Values.parse(payloadContent).flatMap(Values::asJSONObject).ifPresent(objectValue -> {
+ Map<String, Object> headers = prepareHeaders(connection);
+ Map.Entry<String, JsonNode> firstElem = objectValue.fields().next();
+ AttributeEvent attributeEvent = new AttributeEvent(assetId, firstElem.getKey(), firstElem.getValue());
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
+ });
+ } else {
+ String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);
+ Object value = null;
+ if (Character.isLetter(payloadContent.charAt(0))) {
+ if (payloadContent.equals(Boolean.TRUE.toString())) {
+ value = true;
+ }
+ if (payloadContent.equals(Boolean.FALSE.toString())) {
+ value = false;
+ }
+ payloadContent = '"' + payloadContent + '"';
+ }
+ if (value == null) {
+ value = Values.parse(payloadContent);
+ }
+ if (value == null) {
+ value = payloadContent;
+ }
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ AttributeEvent attributeEvent = new AttributeEvent(assetId, attributeRef.getName(), value);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
+ }
+ } else {
+ LOG.warning("Couldn't process message for topic: " + topic);
+ }
+ }
+
+ protected Map<String, Object> prepareHeaders(MqttConnection connection) {
+ Map<String, Object> headers = new HashMap<>();
+ headers.put(ConnectionConstants.SESSION_KEY, connection.getSessionId());
+ headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
+
+ try {
+ AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.getAccessToken(), identityProvider.getKeycloakDeployment(connection.realm, connection.getUsername()));
+ headers.put(Constants.AUTH_CONTEXT, new AccessTokenAuthContext(connection.realm, accessToken));
+ } catch (VerificationException e) {
+ LOG.log(Level.WARNING, "Couldn't verify token", e);
+ }
+ return headers;
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java
index 2a92d2e3e..e2aa1cdf1 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerBasicIdentityProvider.java
@@ -76,7 +76,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen
User adminUser = new User();
adminUser.setUsername(MASTER_REALM_ADMIN_USER);
- createUser(MASTER_REALM, adminUser, adminPassword);
+ createUpdateUser(MASTER_REALM, adminUser, adminPassword);
}
}
@@ -111,13 +111,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen
}
@Override
- public void updateUser(String realm, User user) {
- LOG.info("Updating user: " + user);
- persistenceService.doTransaction(em -> em.merge(user));
- }
-
- @Override
- public User createUser(String realm, User user, String password) {
+ public User createUpdateUser(String realm, User user, String password) {
if (!realm.equals(MASTER_REALM)) {
throw new UnsupportedOperationException("This provider does not support realms other than master");
}
@@ -129,7 +123,9 @@ 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 (?, ?, ?, ?, ?, ?, ?, ?)";
+ String sql = "insert into PUBLIC.USER_ENTITY(ID, REALM_ID, USERNAME, PASSWORD, FIRST_NAME, LAST_NAME, EMAIL, ENABLED) values (?, ?, ?, ?, ?, ?, ?, ?)" +
+ "ON CONFLICT (ID) DO UPDATE " +
+ "SET username = excluded.username, password = excluded.pasword, first_name = excluded.first_name, last_name = excluded.last_name, email = excluded.email, enabled = excluded.enabled";
try (PreparedStatement st = connection.prepareStatement(sql)) {
st.setString(1, UUID.randomUUID().toString());
st.setString(2, MASTER_REALM); // For master REALM NAME and ID are the same
@@ -174,19 +170,19 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen
}
@Override
- public void updateRoles(String realm, String client, Role[] roles) {
+ public void updateClientRoles(String realm, String client, Role[] roles) {
throw new UnsupportedOperationException("This provider does not support updating roles");
}
@Override
- public Role[] getUserRoles(String realm, String userId) {
+ public Role[] getUserRoles(String realm, String userId, String client) {
return ClientRole.ALL_ROLES.stream()
.map(role -> new Role(UUID.randomUUID().toString(), role, false, true, null))
.toArray(Role[]::new);
}
@Override
- public void updateUserRoles(String realm, String username, String client, String... roles) {
+ public void updateUserRoles(String realm, String userId, String client, String... roles) {
throw new UnsupportedOperationException("This provider does not support updating user roles");
}
@@ -246,7 +242,7 @@ public class ManagerBasicIdentityProvider extends BasicIdentityProvider implemen
}
@Override
- public boolean canSubscribeWith(AuthContext auth, TenantFilter filter, ClientRole... requiredRoles) {
+ public boolean canSubscribeWith(AuthContext auth, TenantFilter<?> filter, ClientRole... requiredRoles) {
// TODO Doesn't really respect the description of the interface
return auth.isSuperUser();
}
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java
index 3375111ff..ef22a4803 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java
@@ -51,9 +51,7 @@ public interface ManagerIdentityProvider extends IdentityProvider {
User getUserByUsername(String realm, String username);
- void updateUser(String realm, User user);
-
- User createUser(String realm, User user, String password);
+ User createUpdateUser(String realm, User user, String password);
void deleteUser(String realm, String userId);
@@ -61,11 +59,11 @@ public interface ManagerIdentityProvider extends IdentityProvider {
Role[] getRoles(String realm, String client);
- void updateRoles(String realm, String client, Role[] roles);
+ void updateClientRoles(String realm, String client, Role[] roles);
- Role[] getUserRoles(String realm, String userId);
+ Role[] getUserRoles(String realm, String userId, String client);
- void updateUserRoles(String realm, String username, String client, String...roles);
+ void updateUserRoles(String realm, String userId, String client, String...roles);
boolean isMasterRealmAdmin(String userId);
@@ -95,7 +93,7 @@ public interface ManagerIdentityProvider extends IdentityProvider {
*
* @return <code>true</code> if the authenticated party can subscribe to events with the given filter.
*/
- boolean canSubscribeWith(AuthContext auth, TenantFilter filter, ClientRole... requiredRoles);
+ boolean canSubscribeWith(AuthContext auth, TenantFilter<?> filter, ClientRole... requiredRoles);
/*
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java
index 5404c61d9..39503939d 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityService.java
@@ -19,19 +19,37 @@
*/
package org.openremote.manager.security;
-import org.openremote.model.Container;
+import org.apache.commons.io.IOUtils;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.IdentityService;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.web.ManagerWebService;
+import org.openremote.model.Container;
+import org.openremote.model.auth.OAuthGrant;
+import org.openremote.model.auth.OAuthPasswordGrant;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.Values;
import javax.persistence.EntityManager;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Locale;
import java.util.logging.Logger;
+import static org.openremote.container.security.IdentityProvider.SETUP_ADMIN_PASSWORD;
+import static org.openremote.container.security.IdentityProvider.SETUP_ADMIN_PASSWORD_DEFAULT;
+import static org.openremote.container.security.keycloak.KeycloakIdentityProvider.ADMIN_CLI_CLIENT_ID;
+import static org.openremote.container.util.MapAccess.getString;
+import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;
+
public class ManagerIdentityService extends IdentityService {
private static final Logger LOG = Logger.getLogger(ManagerIdentityService.class.getName());
+ public static final String KEYCLOAK_GRANT_FILE = "KEYCLOAK_GRANT_FILE";
+ public static final String KEYCLOAK_GRANT_FILE_DEFAULT = "/deployment/manager/keycloak.json";
protected ManagerIdentityProvider identityProvider;
protected PersistenceService persistenceService;
@@ -59,7 +77,39 @@ public class ManagerIdentityService extends IdentityService {
switch (identityProviderType.toLowerCase(Locale.ROOT)) {
case "keycloak":
LOG.info("Enabling Keycloak identity provider");
- this.identityProvider = new ManagerKeycloakIdentityProvider();
+
+ // Try and load grant from file
+ String grantFile = getString(container.getConfig(), KEYCLOAK_GRANT_FILE, KEYCLOAK_GRANT_FILE_DEFAULT);
+ Path grantPath = TextUtil.isNullOrEmpty(grantFile) ? null : Paths.get(grantFile);
+ OAuthGrant grant = null;
+
+ if (grantPath != null && Files.isReadable(grantPath)) {
+ LOG.info("Loading KEYCLOAK_GRANT_FILE: " + grantFile);
+
+ try (InputStream is = Files.newInputStream(grantPath)) {
+ String grantJson = IOUtils.toString(is, StandardCharsets.UTF_8);
+ grant = Values.parse(grantJson, OAuthGrant.class).orElseGet(() -> {
+ LOG.info("Failed to load KEYCLOAK_GRANT_FILE: " + grantFile);
+ return null;
+ });
+ } catch (Exception ex) {
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ if (grant == null) {
+ // Fallback to using admin-cli client
+ grant = new OAuthPasswordGrant(
+ "dummy", // This will be replaced with the correct endpoint URI
+ ADMIN_CLI_CLIENT_ID,
+ null,
+ "openid",
+ MASTER_REALM_ADMIN_USER,
+ container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)
+ );
+ }
+
+ this.identityProvider = new ManagerKeycloakIdentityProvider(grant);
break;
case "basic":
LOG.info("Enabling basic identity provider");
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
index ef87b4be5..564f993eb 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.security;
+import io.undertow.util.Headers;
import org.apache.camel.ExchangePattern;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.*;
@@ -28,15 +29,14 @@ import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceEvent;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
-import org.openremote.container.security.PasswordAuthForm;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.web.ClientRequestInfo;
import org.openremote.container.web.WebService;
import org.openremote.manager.apps.ConsoleAppService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.model.Constants;
import org.openremote.model.Container;
+import org.openremote.model.auth.OAuthGrant;
import org.openremote.model.event.shared.TenantFilter;
import org.openremote.model.query.UserQuery;
import org.openremote.model.query.filter.TenantPredicate;
@@ -44,7 +44,7 @@ import org.openremote.model.security.*;
import org.openremote.model.util.TextUtil;
import javax.ws.rs.BadRequestException;
-import javax.ws.rs.NotFoundException;
+import javax.ws.rs.NotAllowedException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
@@ -81,8 +81,8 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
protected String keycloakAdminPassword;
protected Container container;
- public ManagerKeycloakIdentityProvider() {
- super(KEYCLOAK_CLIENT_ID);
+ public ManagerKeycloakIdentityProvider(OAuthGrant grant) {
+ super(grant);
}
@Override
@@ -138,186 +138,277 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
@Override
- 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);
- }
+ public User createUpdateUser(String realm, final User user, String passwordSecret) throws WebApplicationException {
+ return getRealms(realmsResource -> {
- @Override
- public User createUser(String realm, User user, String password) {
- RealmResource realmResource = getRealms().realm(realm);
- Response response = realmResource.users().create(
- convert(user, UserRepresentation.class)
- );
- response.close();
- if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
- throw new WebApplicationException(
- Response.status(response.getStatus())
- .entity(response.getEntity())
- .build()
- );
- } else {
- user = getUserByUsername(realm, user.getUsername().toLowerCase());//Username is stored in lowercase in Keycloak
- 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 + "'");
+ if (user.getUsername() == null) {
+ throw new BadRequestException("Attempt to create/update user but no username provided: User=" + user);
}
- }
- return user;
+ boolean isUpdate = false;
+ User existingUser = user.getId() != null ? getUser(realm, user.getId()) : getUserByUsername(realm, user.getUsername());
+ ClientRepresentation clientRepresentation = null;
+ UserRepresentation userRepresentation = null;
+
+ if (existingUser == null && user.isServiceAccount()) {
+ // Could be a service user
+ clientRepresentation = realmsResource.realm(realm)
+ .clients()
+ .findByClientId(user.getUsername()).stream().findFirst().orElse(null);
+
+ userRepresentation = clientRepresentation != null ? realmsResource.realm(realm)
+ .clients()
+ .get(user.getUsername()).getServiceAccountUser() : null;
+
+ if (userRepresentation != null) {
+ existingUser = convert(userRepresentation, User.class);
+ existingUser.setServiceAccount(true);
+ } else if (clientRepresentation != null) {
+ String msg = "Attempt to update/creat service user but a regular client with same client ID as this username already exists: User=" + user;
+ LOG.info(msg);
+ throw new NotAllowedException(msg);
+ }
+ }
+
+ if (existingUser != null && user.getId() != null && !existingUser.getId().equals(user.getId())) {
+ String msg = "Attempt to update user but retrieved user ID doesn't match supplied so ignoring: User=" + user;
+ LOG.info(msg);
+ throw new BadRequestException(msg);
+ }
+
+ if (existingUser != null) {
+ isUpdate = true;
+
+ if (existingUser.isServiceAccount() != user.isServiceAccount()) {
+ String msg = "Attempt to update user service account flag not allowed: User=" + user;
+ LOG.info(msg);
+ throw new NotAllowedException(msg);
+ }
+
+ if (existingUser.isServiceAccount() && !existingUser.getUsername().equals(user.getUsername())) {
+ String msg = "Attempt to update username of service user not allowed: User=" + user;
+ LOG.info(msg);
+ throw new NotAllowedException(msg);
+ }
+ }
+
+ // For service users we don't actually create the user - keycloak does that when the client is created
+ if (isUpdate) {
+
+ if (user.isServiceAccount()) {
+
+ if (passwordSecret != null) {
+ if (clientRepresentation == null) {
+ clientRepresentation = realmsResource.realm(realm)
+ .clients()
+ .get(user.getUsername()).toRepresentation();
+ }
+
+ if (clientRepresentation == null) {
+ String msg = "Attempt to update service user secret failed to find client: User=" + user;
+ LOG.info(msg);
+ throw new BadRequestException(msg);
+ }
+ clientRepresentation.setSecret(passwordSecret);
+ createUpdateClient(realm, clientRepresentation);
+ } else {
+ String msg = "Attempt to update service user but only secret can be udpated and no new value provided: User=" + user;
+ LOG.info(msg);
+ throw new BadRequestException(msg);
+ }
+
+ } else {
+
+ // User only has a subset of user representation so overlay on actual user representation
+ UserResource userResource = realmsResource.realm(realm).users().get(user.getId());
+ 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);
+
+ }
+ } else {
+
+ if (user.isServiceAccount()) {
+
+ // Just create client with service account and user will be generated
+ clientRepresentation = new ClientRepresentation();
+ clientRepresentation.setStandardFlowEnabled(false);
+ clientRepresentation.setImplicitFlowEnabled(false);
+ clientRepresentation.setDirectAccessGrantsEnabled(false);
+ clientRepresentation.setServiceAccountsEnabled(true);
+ clientRepresentation.setClientAuthenticatorType("client-secret");
+ clientRepresentation.setClientId(user.getUsername());
+ clientRepresentation.setSecret(passwordSecret);
+ clientRepresentation = createUpdateClient(realm, clientRepresentation);
+ userRepresentation = realmsResource.realm(realm)
+ .clients()
+ .get(clientRepresentation.getId()).getServiceAccountUser();
+ } else {
+
+ userRepresentation = convert(user, UserRepresentation.class);
+ RealmResource realmResource = realmsResource.realm(realm);
+ Response response = realmResource.users().create(userRepresentation);
+ String location = response.getHeaderString(Headers.LOCATION_STRING);
+ response.close();
+ if (!response.getStatusInfo().equals(Response.Status.CREATED) || TextUtil.isNullOrEmpty(location)) {
+ throw new BadRequestException("Failed to create user: User=" + user);
+ }
+ String[] locationArr = location.split("/");
+ String userId = locationArr.length > 0 ? locationArr[locationArr.length-1] : null;
+ userRepresentation = realmResource.users().get(userId).toRepresentation();
+
+ if (passwordSecret != null) {
+ CredentialRepresentation credentials = new CredentialRepresentation();
+ credentials.setType("password");
+ credentials.setValue(passwordSecret);
+ credentials.setTemporary(false);
+ realmResource.users().get(userRepresentation.getId()).resetPassword(credentials);
+ }
+
+ }
+ }
+
+ User updatedUser = convert(userRepresentation, User.class);
+ if (updatedUser != null) {
+ user.setServiceAccount(userRepresentation.getUsername().startsWith(User.SERVICE_ACCOUNT_PREFIX));
+ updatedUser.setRealm(realm);
+ }
+ return updatedUser;
+ });
}
@Override
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(
- Response.status(response.getStatus())
- .entity(response.getEntity())
- .build()
- );
- } else {
+ getRealms(realmsResource -> {
+ Response response = realmsResource.realm(realm).users().delete(userId);
response.close();
- }
+ if (!response.getStatusInfo().equals(Response.Status.NO_CONTENT)) {
+ throw new IllegalStateException("Failed to delete user: " + userId);
+ }
+ return null;
+ });
}
@Override
public void resetPassword(String realm, String userId, Credential credential) {
- getRealms()
- .realm(realm).users().get(userId).resetPassword(
- convert(credential, CredentialRepresentation.class)
- );
+ getRealms(realmsResource -> {
+ realmsResource.realm(realm).users().get(userId).resetPassword(
+ convert(credential, CredentialRepresentation.class)
+ );
+ return null;
+ });
}
@Override
public Role[] getRoles(String realm, String client) {
-
- RealmResource realmResource = getRealms().realm(realm);
- ClientsResource clientsResource = realmResource.clients();
- String clientId;
-
- if (TextUtil.isNullOrEmpty(client)) {
- clientId = clientsResource.findByClientId(KEYCLOAK_CLIENT_ID).get(0).getId();
- } else {
+ return getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ ClientsResource clientsResource = realmResource.clients();
ClientRepresentation clientRepresentation = getClient(realm, client);
if (clientRepresentation == null) {
- throw new NotFoundException("Cannot find specified client: " + client);
+ throw new IllegalStateException("Cannot find specified client: " + client);
}
- clientId = clientsResource.findByClientId(clientRepresentation.getClientId()).get(0).getId();
- }
-
- ClientResource clientResource = realmResource.clients().get(clientId);
- List<RoleRepresentation> clientRoles = clientResource.roles().list();
- List<Role> roles = new ArrayList<>();
+ ClientResource clientResource = clientsResource.get(clientRepresentation.getId());
+ List<RoleRepresentation> clientRoles = clientResource.roles().list();
+ List<Role> roles = new ArrayList<>();
- for (RoleRepresentation clientRole : clientRoles) {
- String[] composites = clientRole.isComposite() ? realmResource.rolesById().getClientRoleComposites(clientRole.getId(), clientId).stream().map(RoleRepresentation::getId).toArray(String[]::new) : null;
- roles.add(new Role(clientRole.getId(), clientRole.getName(), clientRole.isComposite(), null, composites).setDescription(clientRole.getDescription()));
- }
+ for (RoleRepresentation clientRole : clientRoles) {
+ String[] composites = clientRole.isComposite() ? realmResource.rolesById().getClientRoleComposites(clientRole.getId(), clientRepresentation.getId()).stream().map(RoleRepresentation::getId).toArray(String[]::new) : null;
+ roles.add(new Role(clientRole.getId(), clientRole.getName(), clientRole.isComposite(), null, composites).setDescription(clientRole.getDescription()));
+ }
- return roles.toArray(new Role[0]);
+ return roles.toArray(new Role[0]);
+ });
}
@Override
- public void updateRoles(String realm, String client, Role[] roles) {
-
- RealmResource realmResource = getRealms().realm(realm);
- ClientsResource clientsResource = realmResource.clients();
- String clientId;
+ public void updateClientRoles(String realm, String clientId, Role[] roles) {
- if (TextUtil.isNullOrEmpty(client)) {
- clientId = clientsResource.findByClientId(KEYCLOAK_CLIENT_ID).get(0).getId();
- } else {
- ClientRepresentation clientRepresentation = getClient(realm, client);
+ getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ ClientsResource clientsResource = realmResource.clients();
+ ClientRepresentation clientRepresentation = getClient(realm, clientId);
if (clientRepresentation == null) {
- throw new NotFoundException("Cannot find specified client: " + client);
+ throw new IllegalStateException("Cannot find specified client: " + clientId);
}
- clientId = clientsResource.findByClientId(clientRepresentation.getClientId()).get(0).getId();
- }
+ ClientResource clientResource = clientsResource.get(clientRepresentation.getId());
+ List<RoleRepresentation> existingRoles = new ArrayList<>(clientResource.roles().list());
+
+ List<RoleRepresentation> removedRoles = existingRoles.stream()
+ .filter(existingRole -> Arrays.stream(roles).noneMatch(r -> existingRole.getId().equals(r.getId())))
+ .collect(Collectors.toList());
+
+ removedRoles.forEach(removedRole -> {
+ realmResource.rolesById().deleteRole(removedRole.getId());
+ existingRoles.remove(removedRole);
+ });
+
+ Arrays.stream(roles).forEach(role -> {
+
+ RoleRepresentation existingRole;
+ boolean compositesModified = false;
+ Set<RoleRepresentation> existingComposites = new HashSet<>();
+ Set<RoleRepresentation> requestedComposites = new HashSet<>();
+
+ if (role.getId() == null) {
+ existingRole = saveClientRole(realmResource, clientResource, role, null);
+ existingRoles.add(existingRole);
+ compositesModified = role.getCompositeRoleIds() != null && role.getCompositeRoleIds().length > 0;
+ if (compositesModified) {
+ requestedComposites.addAll(Arrays.stream(role.getCompositeRoleIds())
+ .map(id -> existingRoles.stream().filter(er -> er.getId().equals(id)).findFirst().orElse(null))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet()));
+ }
+ } else {
+ existingRole = existingRoles.stream().filter(r -> r.getId().equals(role.getId())).findFirst().orElseThrow(() -> new IllegalStateException("One or more supplied roles have an ID that doesn't exist"));
- ClientResource clientResource = realmResource.clients().get(clientId);
- List<RoleRepresentation> existingRoles = new ArrayList<>(clientResource.roles().list());
+ boolean isComposite = role.isComposite() && role.getCompositeRoleIds() != null && role.getCompositeRoleIds().length > 0;
- List<RoleRepresentation> removedRoles = existingRoles.stream()
- .filter(existingRole -> Arrays.stream(roles).noneMatch(r -> existingRole.getId().equals(r.getId())))
- .collect(Collectors.toList());
+ boolean rolePropertiesModified = !Objects.equals(existingRole.getName(), role.getName())
+ || !Objects.equals(existingRole.getDescription(), role.getDescription());
- removedRoles.forEach(removedRole -> {
- realmResource.rolesById().deleteRole(removedRole.getId());
- existingRoles.remove(removedRole);
- });
+ if (isComposite || existingRole.isComposite()) {
+ existingComposites.addAll(Optional.ofNullable(realmResource.rolesById().getClientRoleComposites(existingRole.getId(), clientRepresentation.getId())).orElse(new HashSet<>()));
+ requestedComposites.addAll(Arrays.stream(role.getCompositeRoleIds())
+ .map(id -> existingRoles.stream().filter(er -> er.getId().equals(id)).findFirst().orElse(null))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet()));
- Arrays.stream(roles).forEach(role -> {
+ if (requestedComposites.size() != role.getCompositeRoleIds().length) {
+ throw new IllegalStateException("One or more composite roles contain an invalid role ID");
+ }
- RoleRepresentation existingRole;
- boolean compositesModified = false;
- Set<RoleRepresentation> existingComposites = new HashSet<>();
- Set<RoleRepresentation> requestedComposites = new HashSet<>();
+ compositesModified = !Objects.equals(existingComposites, requestedComposites);
+ }
- if (role.getId() == null) {
- existingRole = saveRole(realmResource, clientResource, role, null);
- existingRoles.add(existingRole);
- compositesModified = role.getCompositeRoleIds() != null && role.getCompositeRoleIds().length > 0;
- if (compositesModified) {
- requestedComposites.addAll(Arrays.stream(role.getCompositeRoleIds())
- .map(id -> existingRoles.stream().filter(er -> er.getId().equals(id)).findFirst().orElse(null))
- .filter(Objects::nonNull)
- .collect(Collectors.toSet()));
+ if (rolePropertiesModified) {
+ // Merge the role property changes
+ saveClientRole(realmResource, clientResource, role, existingRole);
+ }
}
- } else {
- existingRole = existingRoles.stream().filter(r -> r.getId().equals(role.getId())).findFirst().orElseThrow(() -> new BadRequestException("One or more supplied roles have an ID that doesn't exist"));
-
- boolean isComposite = role.isComposite() && role.getCompositeRoleIds() != null && role.getCompositeRoleIds().length > 0;
- boolean rolePropertiesModified = !Objects.equals(existingRole.getName(), role.getName())
- || !Objects.equals(existingRole.getDescription(), role.getDescription());
-
- if (isComposite || existingRole.isComposite()) {
- existingComposites.addAll(Optional.ofNullable(realmResource.rolesById().getClientRoleComposites(existingRole.getId(), clientId)).orElse(new HashSet<>()));
- requestedComposites.addAll(Arrays.stream(role.getCompositeRoleIds())
- .map(id -> existingRoles.stream().filter(er -> er.getId().equals(id)).findFirst().orElse(null))
- .filter(Objects::nonNull)
- .collect(Collectors.toSet()));
-
- if (requestedComposites.size() != role.getCompositeRoleIds().length) {
- throw new BadRequestException("One or more composite roles contain an invalid role ID");
+ if (compositesModified) {
+ List<RoleRepresentation> removed = existingComposites.stream().filter(existing -> !requestedComposites.contains(existing)).collect(Collectors.toList());
+ List<RoleRepresentation> added = requestedComposites.stream().filter(existing -> !existingComposites.contains(existing)).collect(Collectors.toList());
+ if (!removed.isEmpty()) {
+ realmResource.rolesById().deleteComposites(existingRole.getId(), removed);
+ }
+ if (!added.isEmpty()) {
+ realmResource.rolesById().addComposites(existingRole.getId(), added);
}
-
- compositesModified = !Objects.equals(existingComposites, requestedComposites);
}
+ });
- if (rolePropertiesModified) {
- // Merge the role property changes
- saveRole(realmResource, clientResource, role, existingRole);
- }
- }
-
- if (compositesModified) {
- List<RoleRepresentation> removed = existingComposites.stream().filter(existing -> !requestedComposites.contains(existing)).collect(Collectors.toList());
- List<RoleRepresentation> added = requestedComposites.stream().filter(existing -> !existingComposites.contains(existing)).collect(Collectors.toList());
- if (!removed.isEmpty()) {
- realmResource.rolesById().deleteComposites(existingRole.getId(), removed);
- }
- if (!added.isEmpty()) {
- realmResource.rolesById().addComposites(existingRole.getId(), added);
- }
- }
+ return null;
});
}
- protected RoleRepresentation saveRole(RealmResource realmResource, ClientResource clientResource, Role role, RoleRepresentation representation) {
+ protected RoleRepresentation saveClientRole(RealmResource realmResource, ClientResource clientResource, Role role, RoleRepresentation representation) {
if (representation == null) {
representation = new RoleRepresentation();
}
@@ -334,80 +425,86 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
@Override
- public Role[] getUserRoles(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();
-
- List<RoleRepresentation> allRoles = rolesResource.list();
- List<RoleRepresentation> effectiveRoles = roleMappingResource.clientLevel(clientId).listEffective();
-
- List<Role> roles = new ArrayList<>();
- for (RoleRepresentation roleRepresentation : allRoles) {
- boolean isAssigned = false;
+ public Role[] getUserRoles(String realm, String userId, String client) {
+ return getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ RoleMappingResource roleMappingResource = realmResource.users().get(userId).roles();
+ ClientsResource clientsResource = realmResource.clients();
+ String clientId = clientsResource.findByClientId(client).get(0).getId();
+ RolesResource rolesResource = clientsResource.get(clientId).roles();
+
+ List<RoleRepresentation> allRoles = rolesResource.list();
+ List<RoleRepresentation> effectiveRoles = roleMappingResource.clientLevel(clientId).listEffective();
+
+ List<Role> roles = new ArrayList<>();
+ for (RoleRepresentation roleRepresentation : allRoles) {
+ boolean isAssigned = false;
+
+ for (RoleRepresentation effectiveRole : effectiveRoles) {
+ if (effectiveRole.getId().equals(roleRepresentation.getId()))
+ isAssigned = true;
+ }
- for (RoleRepresentation effectiveRole : effectiveRoles) {
- if (effectiveRole.getId().equals(roleRepresentation.getId()))
- isAssigned = true;
+ roles.add(new Role(
+ roleRepresentation.getId(),
+ roleRepresentation.getName(),
+ roleRepresentation.isComposite(),
+ isAssigned,
+ null)
+ .setDescription(roleRepresentation.getDescription()));
}
- roles.add(new Role(
- roleRepresentation.getId(),
- roleRepresentation.getName(),
- roleRepresentation.isComposite(),
- isAssigned,
- null)
- .setDescription(roleRepresentation.getDescription()));
- }
-
- return roles.toArray(new Role[0]);
+ return roles.toArray(new Role[0]);
+ });
}
@Override
- public void updateUserRoles(String realm, String userId, String client, String... roles) {
- RealmResource realmResource = getRealms().realm(realm);
- UserRepresentation user = realmResource.users().get(userId).toRepresentation();
+ public void updateUserRoles(String realm, String userId, String client, String...roles) {
+ getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ UserRepresentation user = realmResource.users().get(userId).toRepresentation();
- if (user == null) {
- throw new IllegalStateException("Multiple users with the same username found");
- }
+ if (user == null) {
+ throw new IllegalStateException("Multiple users with the same username found");
+ }
- RoleMappingResource roleMappingResource = realmResource.users().get(user.getId()).roles();
- ClientRepresentation clientRepresentation = getClient(realm, client);
+ RoleMappingResource roleMappingResource = realmResource.users().get(user.getId()).roles();
+ ClientRepresentation clientRepresentation = getClient(realm, client);
- // Get all role mappings for user on this client and remove any no longer in the roles
- List<RoleRepresentation> clientMappedRoles = roleMappingResource.clientLevel(clientRepresentation.getId()).listAll();
- List<RoleRepresentation> availableRoles = roleMappingResource.clientLevel(clientRepresentation.getId()).listAvailable();
+ // Get all role mappings for user on this client and remove any no longer in the roles
+ List<RoleRepresentation> clientMappedRoles = roleMappingResource.clientLevel(clientRepresentation.getId()).listAll();
+ List<RoleRepresentation> availableRoles = roleMappingResource.clientLevel(clientRepresentation.getId()).listAvailable();
- // Get newly defined roles
- List<RoleRepresentation> addRoles = roles == null ? Collections.emptyList() : Arrays.stream(roles)
- .filter(cr -> clientMappedRoles.stream().noneMatch(r -> r.getName().equals(cr)))
- .map(cr -> availableRoles.stream().filter(r -> r.getName().equals(cr)).findFirst().orElse(null))
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
+ // Get newly defined roles
+ List<RoleRepresentation> addRoles = roles == null ? Collections.emptyList() : Arrays.stream(roles)
+ .filter(cr -> clientMappedRoles.stream().noneMatch(r -> r.getName().equals(cr)))
+ .map(cr -> availableRoles.stream().filter(r -> r.getName().equals(cr)).findFirst().orElse(null))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
- // Remove obsolete roles
- List<RoleRepresentation> removeRoles = roles == null ? clientMappedRoles : clientMappedRoles.stream()
- .filter(r -> Arrays.stream(roles).noneMatch(cr -> cr.equals(r.getName())))
- .collect(Collectors.toList());
+ // Remove obsolete roles
+ List<RoleRepresentation> removeRoles = roles == null ? clientMappedRoles : clientMappedRoles.stream()
+ .filter(r -> Arrays.stream(roles).noneMatch(cr -> cr.equals(r.getName())))
+ .collect(Collectors.toList());
- if (!removeRoles.isEmpty()) {
- roleMappingResource.clientLevel(clientRepresentation.getId()).remove(removeRoles);
- }
- if (!addRoles.isEmpty()) {
- roleMappingResource.clientLevel(clientRepresentation.getId()).add(addRoles);
- }
+ if (!removeRoles.isEmpty()) {
+ roleMappingResource.clientLevel(clientRepresentation.getId()).remove(removeRoles);
+ }
+ if (!addRoles.isEmpty()) {
+ roleMappingResource.clientLevel(clientRepresentation.getId()).add(addRoles);
+ }
+
+ return null;
+ });
}
@Override
public boolean isMasterRealmAdmin(String userId) {
- List<UserRepresentation> adminUsers = getRealms()
- .realm(MASTER_REALM)
- .users()
- .search(MASTER_REALM_ADMIN_USER, null, null);
+ List<UserRepresentation> adminUsers = getRealms(realmsResource ->
+ realmsResource.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");
@@ -425,7 +522,8 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
@Override
public Tenant getTenant(String realm) {
try {
- RealmRepresentation realmRepresentation = getRealms().realm(realm).toRepresentation();
+ RealmRepresentation realmRepresentation = getRealms(realmsResource ->
+ realmsResource.realm(realm).toRepresentation());
return convert(realmRepresentation, Tenant.class);
} catch (Exception ex) {
LOG.log(Level.INFO, "Failed to get tenant for realm: " + realm, ex);
@@ -436,49 +534,54 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
@Override
public void updateTenant(Tenant tenant) {
LOG.fine("Update tenant: " + tenant);
- RealmsResource realmsResource = getRealms();
-
- // 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);
+ getRealms(realmsResource -> {
+ // 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");
+ }
- realmsResource.realm(realm).update(existing);
- publishModification(PersistenceEvent.Cause.UPDATE, 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);
+ return null;
+ });
}
@Override
public Tenant createTenant(Tenant tenant) {
LOG.fine("Create tenant: " + tenant);
- RealmsResource realmsResource = getRealms();
- RealmRepresentation realmRepresentation = convert(tenant, RealmRepresentation.class);
- realmsResource.create(realmRepresentation);
- RealmResource realmResource = realmsResource.realm(tenant.getRealm());
-
- realmRepresentation = realmResource.toRepresentation();
- // Need a committed realmRepresentation to update the security
- configureRealm(realmRepresentation);
- realmResource.update(realmRepresentation);
- createOpenRemoteClientApplication(realmRepresentation.getRealm());
- publishModification(PersistenceEvent.Cause.CREATE, tenant);
- return convert(realmRepresentation, Tenant.class);
+ return getRealms(realmsResource -> {
+ RealmRepresentation realmRepresentation = convert(tenant, RealmRepresentation.class);
+ realmsResource.create(realmRepresentation);
+ RealmResource realmResource = realmsResource.realm(tenant.getRealm());
+
+ realmRepresentation = realmResource.toRepresentation();
+ // Need a committed realmRepresentation to update the security
+ configureRealm(realmRepresentation);
+ realmResource.update(realmRepresentation);
+
+ // Auto create the standard openremote client
+ ClientRepresentation clientRepresentation = generateOpenRemoteClientRepresentation();
+ createUpdateClient(tenant.getRealm(), clientRepresentation);
+ publishModification(PersistenceEvent.Cause.CREATE, tenant);
+ return convert(realmRepresentation, Tenant.class);
+ });
}
@Override
@@ -487,69 +590,135 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
if (tenant != null) {
LOG.fine("Delete tenant: " + realm);
- getRealms().realm(realm).remove();
+ getRealms(realmsResource -> {
+ realmsResource.realm(realm).remove();
+ return null;
+ });
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;
- }
+ public ClientRepresentation generateOpenRemoteClientRepresentation() {
+ ClientRepresentation client = new ClientRepresentation();
+ client.setClientId(KEYCLOAK_CLIENT_ID);
+ client.setName("OpenRemote");
+ client.setPublicClient(true);
- // 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());
+ if (container.isDevMode()) {
+ // We need direct access for integration tests
+ LOG.info("### Allowing direct access grants for client id '" + client.getClientId() + "', this must NOT be used in production! ###");
+ client.setDirectAccessGrantsEnabled(true);
+
+ // Allow any web origin (this will add CORS headers to token requests etc.)
+ client.setWebOrigins(Collections.singletonList("*"));
+ client.setRedirectUris(Collections.singletonList("*"));
+ } else {
+ // TODO: Decide how clients should be handled
+ client.setWebOrigins(Collections.singletonList("+"));
+ client.setRedirectUris(Collections.singletonList("/*"));
+// List<String> redirectUris = new ArrayList<>();
+// try {
+// for (String consoleName : consoleAppService.getInstalled()) {
+// addClientRedirectUris(consoleName, redirectUris, devMode);
+// }
+// } catch (Exception exception) {
+// LOG.log(Level.WARNING, exception.getMessage(), exception);
+// addClientRedirectUris(realm, redirectUris, devMode);
+// }
+//
+// client.setRedirectUris(redirectUris);
}
+
return client;
}
+ // TODO: Provide an implementation agnostic client
+ public ClientRepresentation getClient(String realm, String clientId) {
+ return getRealms(realmsResource -> {
+ ClientsResource clientsResource = realmsResource.realm(realm).clients();
+ List<ClientRepresentation> clients = clientsResource.findByClientId(clientId);
+
+ if (clients.isEmpty()) {
+ return null;
+ }
+
+ ClientRepresentation client = clients.get(0);
+ if ("client-secret".equals(client.getClientAuthenticatorType())) {
+ // Need to get secret separately for some reason
+ 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]);
+ return getRealms(realmsResource -> realmsResource.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;
+ public ClientRepresentation createUpdateClient(String realm, ClientRepresentation client) {
+
+ if (client == null || client.getClientId() == null) {
+ throw new IllegalArgumentException("Client is null or clientId is missing");
}
- client = clientsResource.findByClientId(client.getClientId()).get(0);
- ClientResource clientResource = clientsResource.get(client.getId());
- addDefaultRoles(clientResource.roles());
- return client;
- }
+ boolean isUpdate = !TextUtil.isNullOrEmpty(client.getId());
- // TODO: Provide an implementation agnostic client
- public void updateClient(String realm, ClientRepresentation client) {
- getRealms().realm(realm).clients().get(client.getId()).update(client);
+ if (!isUpdate) {
+ // Check if client exists
+ ClientRepresentation clientRepresentation = getClient(realm, client.getClientId());
+ if (clientRepresentation != null) {
+ client.setId(clientRepresentation.getId());
+ isUpdate = true;
+ }
+ }
+
+ boolean finalIsUpdate = isUpdate;
+
+ return getRealms(realmsResource -> {
+ ClientsResource clientsResource = realmsResource.realm(realm).clients();
+ if (finalIsUpdate) {
+ clientsResource.get(client.getId()).update(client);
+ } else {
+ Response response = clientsResource.create(client);
+ response.close();
+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
+ return null;
+ }
+
+ ClientRepresentation newClient = clientsResource.findByClientId(client.getClientId()).get(0);
+ ClientResource clientResource = clientsResource.get(newClient.getId());
+ addDefaultRoles(clientResource.roles());
+ return newClient;
+ }
+ return null;
+ });
}
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();
- }
+ if (TextUtil.isNullOrEmpty(realm)
+ || TextUtil.isNullOrEmpty(clientId)) {
+ throw new IllegalArgumentException("Invalid client credentials realm and client ID must be specified");
+ }
+
+ getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
- public User getClientServiceUser(String realm, String clientId) {
- ClientRepresentation client = getClient(realm, clientId);
- if (client == null) {
+ if (realmResource == null) {
+ LOG.info("Invalid realm provided for deleteClient call: " + realm);
+ return null;
+ }
+
+ LOG.info("Deleting client: realm=" + realm + ", client ID=" + clientId);
+ ClientsResource clients = realmResource.clients();
+ clients.findByClientId(clientId).forEach(client -> clients.get(client.getId()).remove());
return null;
- }
- UserRepresentation user = getRealms().realm(realm).clients().get(client.getId()).getServiceAccountUser();
- return user != null ? convert(user, User.class) : null;
+ });
}
/**
@@ -588,7 +757,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
@Override
- public boolean canSubscribeWith(AuthContext auth, TenantFilter filter, ClientRole... requiredRoles) {
+ public boolean canSubscribeWith(AuthContext auth, TenantFilter<?> filter, ClientRole... requiredRoles) {
// Superuser can always subscribe
if (auth.isSuperUser())
return true;
@@ -682,55 +851,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
}
- public void createOpenRemoteClientApplication(String realm) {
- createClient(realm, createDefaultClientRepresentation(realm, KEYCLOAK_CLIENT_ID, "OpenRemote", container.isDevMode()));
- }
-
- /**
- * 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
- */
- 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) {
- ClientRepresentation client = new ClientRepresentation();
-
- client.setClientId(clientId);
- client.setName(appName);
- client.setPublicClient(true);
-
- if (devMode) {
- // We need direct access for integration tests
- LOG.info("### Allowing direct access grants for client app '" + appName + "', this must NOT be used in production! ###");
- client.setDirectAccessGrantsEnabled(true);
-
- // Allow any web origin (this will add CORS headers to token requests etc.)
- client.setWebOrigins(Collections.singletonList("*"));
- client.setRedirectUris(Collections.singletonList("*"));
- } else {
- // TODO: Decide how clients should be handled
- client.setWebOrigins(Collections.singletonList("+"));
- client.setRedirectUris(Collections.singletonList("/*"));
-// List<String> redirectUris = new ArrayList<>();
-// try {
-// for (String consoleName : consoleAppService.getInstalled()) {
-// addClientRedirectUris(consoleName, redirectUris, devMode);
-// }
-// } catch (Exception exception) {
-// LOG.log(Level.WARNING, exception.getMessage(), exception);
-// addClientRedirectUris(realm, redirectUris, devMode);
-// }
-//
-// client.setRedirectUris(redirectUris);
- }
-
- return client;
- }
-
protected void addDefaultRoles(RolesResource rolesResource) {
for (ClientRole clientRole : ClientRole.values()) {
@@ -748,12 +868,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
}
- public static User convertUser(String realm, UserRepresentation userRepresentation) {
- User user = convert(userRepresentation, User.class);
- user.setRealm(realm);
- return user;
- }
-
protected void publishModification(PersistenceEvent.Cause cause, Tenant tenant) {
// Fire persistence event although we don't use database for Tenant CUD but call Keycloak API
PersistenceEvent<?> persistenceEvent = new PersistenceEvent<>(cause, tenant, new String[0], null);
@@ -770,62 +884,42 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
public String addLDAPConfiguration(String realm, ComponentRepresentation componentRepresentation) {
- ClientRequestInfo clientRequestInfo = new ClientRequestInfo(null, getAdminAccessToken());
-
- RealmResource realmResource = getRealms()
- .realm(realm);
- Response response = realmResource.components().add(componentRepresentation);
- if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
- throw new WebApplicationException(
- Response.status(response.getStatus())
- .entity(response.getEntity())
- .build()
- );
- } else {
+ return getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ Response response = realmResource.components().add(componentRepresentation);
response.close();
- componentRepresentation = realmResource.components()
- .query(componentRepresentation.getParentId(),
- componentRepresentation.getProviderType(),
- componentRepresentation.getName()).get(0);
- response = syncUsers(clientRequestInfo, componentRepresentation.getId(), realm, "triggerFullSync");
-
- if (!response.getStatusInfo().equals(Response.Status.OK)) {
- throw new WebApplicationException(
- Response.status(response.getStatus())
- .entity(response.getEntity())
- .build()
- );
+ if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
+ throw new IllegalStateException("Failed to add LDAP configuration");
} else {
- response.close();
+ ComponentRepresentation newComponentRepresentation = realmResource.components()
+ .query(componentRepresentation.getParentId(),
+ componentRepresentation.getProviderType(),
+ componentRepresentation.getName()).get(0);
+ syncUsers(newComponentRepresentation.getId(), realm, "triggerFullSync");
+ return newComponentRepresentation.getId();
}
- }
- return componentRepresentation.getId();
+ });
}
public String addLDAPMapper(String realm, ComponentRepresentation componentRepresentation) {
- ClientRequestInfo clientRequestInfo = new ClientRequestInfo(null, getAdminAccessToken());
- RealmResource realmResource = getRealms()
- .realm(realm);
- Response response = realmResource.components().add(componentRepresentation);
-
- if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
- throw new WebApplicationException(
- Response.status(response.getStatus())
- .entity(response.getEntity())
- .build()
- );
- } else {
+ return getRealms(realmsResource -> {
+ RealmResource realmResource = realmsResource.realm(realm);
+ Response response = realmResource.components().add(componentRepresentation);
response.close();
- componentRepresentation = realmResource.components()
- .query(componentRepresentation.getParentId(),
- componentRepresentation.getProviderType(),
- componentRepresentation.getName()).get(0);
- realmResource.userStorage().syncMapperData(componentRepresentation.getParentId(), componentRepresentation.getId(), "fedToKeycloak");
- }
- return componentRepresentation.getId();
+ if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
+ throw new IllegalStateException("Failed to add LDAP mapper");
+ } else {
+ ComponentRepresentation newComponentRepresentation = realmResource.components()
+ .query(componentRepresentation.getParentId(),
+ componentRepresentation.getProviderType(),
+ componentRepresentation.getName()).get(0);
+ realmResource.userStorage().syncMapperData(newComponentRepresentation.getParentId(), newComponentRepresentation.getId(), "fedToKeycloak");
+ return newComponentRepresentation.getId();
+ }
+ });
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java
index 0321652d3..daa0c9807 100644
--- a/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/security/TenantResourceImpl.java
@@ -99,9 +99,7 @@ public class TenantResourceImpl extends ManagerWebResource implements TenantReso
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
try {
- identityService.getIdentityProvider().createTenant(
- tenant
- );
+ identityService.getIdentityProvider().createTenant(tenant);
} catch (ClientErrorException ex) {
throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());
} catch (Exception ex) {
diff --git a/manager/src/main/java/org/openremote/manager/security/UserConfiguration.java b/manager/src/main/java/org/openremote/manager/security/UserConfiguration.java
index 029011af3..e64eafe6e 100644
--- a/manager/src/main/java/org/openremote/manager/security/UserConfiguration.java
+++ b/manager/src/main/java/org/openremote/manager/security/UserConfiguration.java
@@ -46,16 +46,18 @@ public class UserConfiguration {
return userId;
}
- public void setUserId(String userId) {
+ public UserConfiguration setUserId(String userId) {
this.userId = userId;
+ return this;
}
public boolean isRestricted() {
return restricted;
}
- public void setRestricted(boolean restricted) {
+ public UserConfiguration setRestricted(boolean restricted) {
this.restricted = restricted;
+ return this;
}
@Override
@@ -65,4 +67,4 @@ public class UserConfiguration {
", restricted=" + restricted +
'}';
}
-}
\ No newline at end of file
+}
diff --git a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java
index 0588efbd5..8c6be92da 100644
--- a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java
@@ -22,7 +22,6 @@ package org.openremote.manager.security;
import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.web.ManagerWebResource;
-import org.openremote.model.Constants;
import org.openremote.model.http.RequestParams;
import org.openremote.model.security.*;
@@ -104,9 +103,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
throwIfIllegalMasterAdminUserMutation(requestParams, realm, user);
try {
- identityService.getIdentityProvider().updateUser(
- realm, user
- );
+ identityService.getIdentityProvider().createUpdateUser(realm, user, null);
} catch (ClientErrorException ex) {
throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());
} catch (Exception ex) {
@@ -117,7 +114,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
@Override
public User create(RequestParams requestParams, String realm, User user) {
try {
- return identityService.getIdentityProvider().createUser(
+ return identityService.getIdentityProvider().createUpdateUser(
realm, user,
null);
} catch (ClientErrorException ex) {
@@ -175,7 +172,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
try {
return identityService.getIdentityProvider().getUserRoles(
- realm, userId
+ realm, userId, KEYCLOAK_CLIENT_ID
);
} catch (ClientErrorException ex) {
throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());
@@ -208,7 +205,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
try {
return identityService.getIdentityProvider().getRoles(
realm,
- null);
+ KEYCLOAK_CLIENT_ID);
} catch (ClientErrorException ex) {
throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());
} catch (Exception ex) {
@@ -219,7 +216,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
@Override
public void updateRoles(RequestParams requestParams, String realm, Role[] roles) {
try {
- identityService.getIdentityProvider().updateRoles(
+ identityService.getIdentityProvider().updateClientRoles(
realm,
KEYCLOAK_CLIENT_ID,
roles);
@@ -227,7 +224,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource
ex.printStackTrace(System.out);
throw new WebApplicationException(ex.getCause(), ex.getResponse().getStatus());
} catch (Exception ex) {
- throw new WebApplicationException(ex);
+ throw new NotFoundException(ex);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
index 29e3bf8df..e2a53283e 100644
--- a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
@@ -19,9 +19,9 @@
*/
package org.openremote.manager.setup;
-import org.openremote.model.Container;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Container;
import org.openremote.model.security.ClientRole;
import org.openremote.model.security.Tenant;
import org.openremote.model.security.User;
@@ -76,14 +76,6 @@ public abstract class AbstractKeycloakSetup implements Setup {
return tenant;
}
- protected Tenant createTenant(String realm, String displayName) {
- return createTenant(realm, displayName, false);
- }
-
- protected Tenant createTenant(Tenant tenant) {
- return keycloakProvider.createTenant(tenant);
- }
-
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);
@@ -91,7 +83,7 @@ public abstract class AbstractKeycloakSetup implements Setup {
user.setLastName(lastName);
user.setEmail(email);
user.setEnabled(enabled);
- user = keycloakProvider.createUser(realm, user, password);
+ user = keycloakProvider.createUpdateUser(realm, user, password);
if (user == null) {
return null;
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java b/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
index 5bcf0c843..10cdb3a4a 100644
--- a/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
@@ -60,7 +60,7 @@ public class KeycloakCleanSetup extends AbstractKeycloakSetup {
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);
+ keycloakProvider.deleteClient(MASTER_REALM, client.getClientId());
}
});
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
index 96fa9f913..fd986931a 100644
--- a/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
@@ -47,16 +47,18 @@ public class KeycloakInitSetup extends AbstractKeycloakSetup {
masterRealm.setDisplayName("Master");
keycloakProvider.updateTenant(masterRealm);
- // Create our client application with its default roles
- keycloakProvider.createOpenRemoteClientApplication(masterRealm.getRealm());
+ // Create our client application with its default roles in the master realm
+ keycloakProvider.createUpdateClient(masterRealm.getRealm(), keycloakProvider.generateOpenRemoteClientRepresentation());
// 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);
+ keycloakProvider.createUpdateUser(MASTER_REALM, adminUser, null);
// Give admin all roles on application client level
keycloakProvider.updateUserRoles(MASTER_REALM, adminUser.getId(), KEYCLOAK_CLIENT_ID, ClientRole.READ.getValue(), ClientRole.WRITE.getValue());
+
+
}
}
diff --git a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
index b007a8452..1b99091d0 100644
--- a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
+++ b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
@@ -42,7 +42,7 @@ import java.util.stream.Collectors;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;
-import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
+import static org.openremote.manager.event.ClientEventService.getSessionKey;
/**
* Connects the client/UI to the {@link SimulatorProtocol}.
diff --git a/model/src/main/java/org/openremote/model/auth/OAuthGrant.java b/model/src/main/java/org/openremote/model/auth/OAuthGrant.java
index 493ec2c8e..e1178f51f 100644
--- a/model/src/main/java/org/openremote/model/auth/OAuthGrant.java
+++ b/model/src/main/java/org/openremote/model/auth/OAuthGrant.java
@@ -82,6 +82,11 @@ public abstract class OAuthGrant implements Serializable {
return tokenEndpointUri;
}
+ public OAuthGrant setTokenEndpointUri(String tokenEndpointUri) {
+ this.tokenEndpointUri = tokenEndpointUri;
+ return this;
+ }
+
public String getGrantType() {
return grantType;
}
diff --git a/model/src/main/java/org/openremote/model/rules/AssetState.java b/model/src/main/java/org/openremote/model/rules/AssetState.java
index 0494403d9..ad8ee1404 100644
--- a/model/src/main/java/org/openremote/model/rules/AssetState.java
+++ b/model/src/main/java/org/openremote/model/rules/AssetState.java
@@ -19,7 +19,6 @@
*/
package org.openremote.model.rules;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@@ -27,7 +26,10 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.MetaMap;
-import org.openremote.model.value.*;
+import org.openremote.model.value.MetaHolder;
+import org.openremote.model.value.NameValueHolder;
+import org.openremote.model.value.ValueDescriptor;
+import org.openremote.model.value.Values;
import java.util.Date;
import java.util.Objects;
diff --git a/model/src/main/java/org/openremote/model/security/Tenant.java b/model/src/main/java/org/openremote/model/security/Tenant.java
index 39ea4aa13..03665f69e 100644
--- a/model/src/main/java/org/openremote/model/security/Tenant.java
+++ b/model/src/main/java/org/openremote/model/security/Tenant.java
@@ -103,8 +103,9 @@ public class Tenant {
return realm;
}
- public void setRealm(String realm) {
+ public Tenant setRealm(String realm) {
this.realm = realm;
+ return this;
}
@NotNull(message = "{Tenant.displayName.NotNull}")
@@ -113,48 +114,54 @@ public class Tenant {
return displayName;
}
- public void setDisplayName(String displayName) {
+ public Tenant setDisplayName(String displayName) {
this.displayName = displayName;
+ return this;
}
public Boolean getEnabled() {
return enabled;
}
- public void setEnabled(Boolean enabled) {
+ public Tenant setEnabled(Boolean enabled) {
this.enabled = enabled;
+ return this;
}
public Double getNotBefore() {
return notBefore;
}
- public void setNotBefore(Double notBefore) {
+ public Tenant setNotBefore(Double notBefore) {
this.notBefore = notBefore;
+ return this;
}
public Boolean getResetPasswordAllowed() {
return resetPasswordAllowed;
}
- public void setResetPasswordAllowed(Boolean resetPasswordAllowed) {
+ public Tenant setResetPasswordAllowed(Boolean resetPasswordAllowed) {
this.resetPasswordAllowed = resetPasswordAllowed;
+ return this;
}
public Boolean getDuplicateEmailsAllowed() {
return duplicateEmailsAllowed;
}
- public void setDuplicateEmailsAllowed(Boolean duplicateEmailsAllowed) {
+ public Tenant setDuplicateEmailsAllowed(Boolean duplicateEmailsAllowed) {
this.duplicateEmailsAllowed = duplicateEmailsAllowed;
+ return this;
}
public Boolean getRememberMe() {
return rememberMe;
}
- public void setRememberMe(Boolean rememberMe) {
+ public Tenant setRememberMe(Boolean rememberMe) {
this.rememberMe = rememberMe;
+ return this;
}
public boolean isActive(double currentTimeMillis) {
@@ -166,32 +173,36 @@ public class Tenant {
return loginTheme;
}
- public void setLoginTheme(String loginTheme) {
+ public Tenant setLoginTheme(String loginTheme) {
this.loginTheme = loginTheme;
+ return this;
}
public String getAccountTheme() {
return accountTheme;
}
- public void setAccountTheme(String accountTheme) {
+ public Tenant setAccountTheme(String accountTheme) {
this.accountTheme = accountTheme;
+ return this;
}
public String getAdminTheme() {
return adminTheme;
}
- public void setAdminTheme(String adminTheme) {
+ public Tenant setAdminTheme(String adminTheme) {
this.adminTheme = adminTheme;
+ return this;
}
public String getEmailTheme() {
return emailTheme;
}
- public void setEmailTheme(String emailTheme) {
+ public Tenant setEmailTheme(String emailTheme) {
this.emailTheme = emailTheme;
+ return this;
}
@Override
diff --git a/model/src/main/java/org/openremote/model/security/User.java b/model/src/main/java/org/openremote/model/security/User.java
index 0a804f047..c89bb4e74 100644
--- a/model/src/main/java/org/openremote/model/security/User.java
+++ b/model/src/main/java/org/openremote/model/security/User.java
@@ -19,12 +19,16 @@
*/
package org.openremote.model.security;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSetter;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Subselect;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
+import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
@@ -36,6 +40,7 @@ import javax.validation.constraints.Size;
@Entity
@Subselect("select * from PUBLIC.USER_ENTITY") // Map this immutable to an SQL view, don't use/create table
public class User {
+ public static final String SERVICE_ACCOUNT_PREFIX = "service-account-";
@Formula("(select r.NAME from PUBLIC.REALM r where r.ID = REALM_ID)")
protected String realm;
@@ -46,6 +51,7 @@ public class User {
@Id
protected String id;
+ @JsonIgnore
@Column(name = "USERNAME")
protected String username;
@@ -61,6 +67,9 @@ public class User {
@Column(name = "ENABLED")
protected Boolean enabled;
+ @Transient
+ protected boolean serviceAccount;
+
public User() {
}
@@ -94,15 +103,37 @@ public class User {
@NotNull(message = "{User.username.NotNull}")
@Size(min = 3, max = 255, message = "{User.username.Size}")
@Pattern(regexp = "[a-zA-Z0-9-_]+", message = "{User.username.Pattern}")
+ @JsonProperty
public String getUsername() {
- return username;
+ return serviceAccount ? username.replace(SERVICE_ACCOUNT_PREFIX, "") : username;
}
+ @JsonSetter("username")
public User setUsername(String username) {
+ if (this.serviceAccount && !username.startsWith(SERVICE_ACCOUNT_PREFIX)) {
+ username = SERVICE_ACCOUNT_PREFIX + username;
+ }
this.username = username;
return this;
}
+ public boolean isServiceAccount() {
+ return serviceAccount;
+ }
+
+ public User setServiceAccount(boolean serviceAccount) {
+ if (serviceAccount == this.serviceAccount) {
+ return this;
+ }
+
+ this.serviceAccount = serviceAccount;
+
+ if (username != null) {
+ username = serviceAccount ? SERVICE_ACCOUNT_PREFIX + username.replace(SERVICE_ACCOUNT_PREFIX, "") : username.replace(SERVICE_ACCOUNT_PREFIX, "");
+ }
+ return this;
+ }
+
@Size(min = 0, max = 127, message = "{User.firstName.Size}")
public String getFirstName() {
return firstName;
diff --git a/test/src/main/groovy/org/openremote/test/MqttClient.groovy b/test/src/main/groovy/org/openremote/test/MqttClient.groovy
index f90d8252d..6e2288f50 100644
--- a/test/src/main/groovy/org/openremote/test/MqttClient.groovy
+++ b/test/src/main/groovy/org/openremote/test/MqttClient.groovy
@@ -15,6 +15,7 @@ import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioSocketChannel
import io.netty.util.ReferenceCountUtil
+import org.openremote.model.asset.agent.ConnectionStatus
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Semaphore
@@ -80,9 +81,13 @@ class RawClient {
// Start the client.
m_channel = b.connect(host, port).sync().channel()
+
+ m_channel.closeFuture().addListener({
+ this.connected = false
+ })
+
this.connected = true
} catch (Exception ex) {
- LOG.error("Error received in client setup", ex)
workerGroup.shutdownGracefully()
}
}
@@ -91,11 +96,8 @@ class RawClient {
return new RawClient(host, port)
}
- RawClient isConnected() {
- if (!this.connected) {
- throw new IllegalStateException("Can't connect the client")
- }
- return this
+ boolean isConnected() {
+ return this.connected
}
RawClient write(int... bytes) {
diff --git a/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java b/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
index f929afc0d..eb38caa4b 100644
--- a/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
+++ b/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
@@ -19,27 +19,18 @@
*/
package org.openremote.test.setup;
-import org.keycloak.representations.idm.ClientRepresentation;
-import org.openremote.agent.protocol.ProtocolClientEventService;
-import org.openremote.manager.event.ClientEventService;
-import org.openremote.model.Container;
import org.openremote.container.util.UniqueIdentifierGenerator;
-import org.openremote.manager.mqtt.MqttBrokerService;
import org.openremote.manager.setup.AbstractKeycloakSetup;
import org.openremote.model.Constants;
+import org.openremote.model.Container;
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.List;
-import java.util.UUID;
import java.util.logging.Logger;
-import java.util.stream.Collectors;
+import java.util.stream.Stream;
-import static org.openremote.agent.protocol.ProtocolClientEventService.getClientId;
-import static org.openremote.container.util.MapAccess.getString;
-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
import static org.openremote.model.Constants.MASTER_REALM;
/**
@@ -67,8 +58,9 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {
public Tenant tenantBuilding;
public Tenant energyTenant;
public Tenant tenantCity;
- public static final String clientEventClientId = getClientId("test");
- public static final String clientEventClientSecret = UniqueIdentifierGenerator.generateId(getClientId("test"));
+ public User serviceUser;
+ public static final String serviceUserId = "test";
+ public static final String serviceUserSecret = UniqueIdentifierGenerator.generateId("test");
public KeycloakTestSetup(Container container) {
super(container);
@@ -80,9 +72,9 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {
// Tenants
masterTenant = identityService.getIdentityProvider().getTenant(Constants.MASTER_REALM);
- tenantBuilding = createTenant("building", "Building");
- tenantCity = createTenant("smartcity", "Smart City");
- energyTenant = createTenant("energy", "Energy Test");
+ tenantBuilding = createTenant("building", "Building", true);
+ tenantCity = createTenant("smartcity", "Smart City", true);
+ energyTenant = createTenant("energy", "Energy Test", true);
// Don't allow demo users to write assets
ClientRole[] demoUserRoles = Arrays.stream(REGULAR_USER_ROLES)
@@ -114,15 +106,15 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {
/*
* Client event keycloak client
*/
- ProtocolClientEventService.ClientCredentials clientCredentials =
- new ProtocolClientEventService.ClientCredentials(
- tenantBuilding.getRealm(),
- new ClientRole[] {ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES},
- clientEventClientId,
- clientEventClientSecret
- );
-
- ClientEventService clientEventService = container.getService(ClientEventService.class);
- clientEventService.addClientCredentials(clientCredentials);
+ serviceUser = new User()
+ .setServiceAccount(true)
+ .setUsername(serviceUserId);
+ serviceUser = keycloakProvider.createUpdateUser(tenantBuilding.getRealm(), serviceUser, serviceUserSecret);
+ keycloakProvider.updateUserRoles(
+ tenantBuilding.getRealm(),
+ serviceUser.getId(),
+ serviceUserId,
+ Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES).map(ClientRole::getValue).toArray(String[]::new)
+ );
}
}
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index d08b6ee84..fd9047468 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -7,26 +7,23 @@ import org.openremote.container.util.UniqueIdentifierGenerator
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.mqtt.MqttBrokerService
+import org.openremote.manager.mqtt.MqttConnection
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.AssetEvent
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
-import org.openremote.test.setup.KeycloakTestSetup
-import org.openremote.test.setup.ManagerTestSetup
import org.openremote.model.attribute.AttributeEvent
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import org.openremote.test.RawClient
-import spock.lang.Shared
+import org.openremote.test.setup.KeycloakTestSetup
+import org.openremote.test.setup.ManagerTestSetup
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
-import static org.openremote.container.util.MapAccess.*
-import static org.openremote.manager.mqtt.KeycloakAuthenticator.MQTT_CLIENT_ID_SEPARATOR
-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOST
-import static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORT
-import static org.openremote.manager.mqtt.MqttBrokerService.MULTI_LEVEL_WILDCARD
-import static org.openremote.manager.mqtt.MqttBrokerService.TOPIC_SEPARATOR
+import static org.openremote.container.util.MapAccess.getInteger
+import static org.openremote.container.util.MapAccess.getString
+import static org.openremote.manager.mqtt.MqttBrokerService.*
import static org.openremote.model.value.ValueType.TEXT
class MqttBrokerTest extends Specification implements ManagerContainerTrait {
@@ -38,17 +35,21 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def mqttBrokerServiceAttributeValueCalls = 0
def spyMqttBrokerService = Spy(MqttBrokerService) {
- sendAssetEvent(_ as String, _ as String, _ as AssetEvent) >> {
- clientId, topic, assetEvent ->
- mqttBrokerServiceAssetEventCalls++
- }
- sendAttributeEvent(_ as String, _ as String, _ as AttributeEvent) >> {
- clientId, topic, attributeEvent ->
- mqttBrokerServiceAttributeEventCalls++
- }
- sendAttributeValue(_ as String, _ as String, _ as AttributeEvent) >> {
- clientId, topic, attributeEvent ->
- mqttBrokerServiceAttributeValueCalls++
+ getEventConsumer(_ as MqttConnection, _ as String, _ as Boolean) >> {
+ connection, topic, isValueSubscription ->
+ return {
+ if (isValueSubscription) {
+ if (it instanceof AttributeEvent) {
+ mqttBrokerServiceAttributeValueCalls++
+ }
+ } else {
+ if (it instanceof AttributeEvent) {
+ mqttBrokerServiceAttributeEventCalls++
+ } else if (it instanceof AssetEvent) {
+ mqttBrokerServiceAssetEventCalls++
+ }
+ }
+ }
}
}
@@ -57,20 +58,22 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
services.replaceAll { it instanceof MqttBrokerService ? spyMqttBrokerService : it }
def container = startContainer(defaultConfig(), services)
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
def mqttBrokerService = container.getService(MqttBrokerService.class)
def assetStorageService = container.getService(AssetStorageService.class)
def agentService = container.getService(AgentService.class)
- def mqttClientId = managerTestSetup.realmBuildingTenant + MQTT_CLIENT_ID_SEPARATOR + UniqueIdentifierGenerator.generateId()
- def clientId = KeycloakTestSetup.clientEventClientId
- def clientSecret = KeycloakTestSetup.clientEventClientSecret
+ def mqttClientId = UniqueIdentifierGenerator.generateId()
+ def username = keycloakTestSetup.tenantBuilding.realm + ":" + KeycloakTestSetup.serviceUserId // realm and OAuth client id
+ def password = KeycloakTestSetup.serviceUserSecret
- def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST);
- def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT);
+ def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)
+ def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)
- when: "a mqtt client connects with wrong credentials"
- def payloadLength = "client1".size() + clientId.size() + clientSecret.size()
+ when: "a mqtt client connects with wrong realm credentials"
+ def wrongUsername = "master:" + KeycloakTestSetup.serviceUserId
+ def payloadLength = mqttClientId.size() + wrongUsername.size() + password.size()
def remainingLength = 16 + payloadLength
- def client = RawClient.connect(mqttHost, mqttPort).isConnected()
+ def client = RawClient.connect(mqttHost, mqttPort)
// CONNECT
.write(0x10) // MQTT Control Packet type(1)
.write(remainingLength.byteValue()) // Remaining Length
@@ -90,23 +93,24 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
.write(0x00, 0x00) // Keep Alive
// Payload
- .write(0x00, managerTestSetup.realmBuildingTenant.size().byteValue()) // Client Identifier Length
- .write(managerTestSetup.realmBuildingTenant) // Client Identifier
- .write(0x00, clientId.size().byteValue())
- .write(clientId)
- .write(0x00, clientSecret.size().byteValue())
- .write(clientSecret)
+ .write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
+ .write(mqttClientId) // Client Identifier
+ .write(0x00, wrongUsername.size().byteValue())
+ .write(wrongUsername)
+ .write(0x00, password.size().byteValue())
+ .write(password)
.flush()
then: "no connection should be made"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.size() == 0
+ assert !client.isConnected()
+ assert mqttBrokerService.sessionIdConnectionMap.size() == 0
}
when: "a mqtt client connects"
- payloadLength = mqttClientId.size() + clientId.size() + clientSecret.size()
+ payloadLength = mqttClientId.size() + username.size() + password.size()
remainingLength = 16 + payloadLength
- client = RawClient.connect(mqttHost, mqttPort).isConnected()
+ client = RawClient.connect(mqttHost, mqttPort)
// CONNECT
.write(0x10) // MQTT Control Packet type(1)
.write(remainingLength.byteValue()) // Remaining Length
@@ -128,15 +132,15 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
// Payload
.write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
.write(mqttClientId) // Client Identifier
- .write(0x00, clientId.size().byteValue())
- .write(clientId)
- .write(0x00, clientSecret.size().byteValue())
- .write(clientSecret)
+ .write(0x00, username.size().byteValue())
+ .write(username)
+ .write(0x00, password.size().byteValue())
+ .write(password)
.flush()
then: "mqtt connection should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId) != null
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId) != null
}
when: "a mqtt client subscribes to an asset in another realm"
@@ -157,7 +161,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes to an non existing asset"
@@ -178,7 +182,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes to an asset"
@@ -199,7 +203,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
when: "An asset attribute changed the client is subscribed on"
@@ -277,7 +281,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "Another asset attribute changed without any subscriptions"
@@ -307,7 +311,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should be created"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.containsKey(topic)
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
when: "that attribute changed"
@@ -346,11 +350,11 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes with multilevel on asset"
- topic = "attribute/" + managerTestSetup.apartment1Id + TOPIC_SEPARATOR + MULTI_LEVEL_WILDCARD
+ topic = "attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -367,7 +371,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
when: "An child asset attribute changed the client is subscribed on"
@@ -397,7 +401,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should be created"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).attributeValueSubscriptions.containsKey(topic)
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
when: "that attribute changed"
@@ -436,7 +441,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).attributeValueSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
when: " a client disconnects"
@@ -448,13 +453,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "no connection should be left"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.size() == 0
}
when: "a mqtt client reconnects"
- payloadLength = mqttClientId.size() + clientId.size() + clientSecret.size()
+ payloadLength = mqttClientId.size() + username.size() + password.size()
remainingLength = 16 + payloadLength
- client = RawClient.connect(mqttHost, mqttPort).isConnected()
+ client = RawClient.connect(mqttHost, mqttPort)
// CONNECT
.write(0x10) // MQTT Control Packet type(1)
.write(remainingLength.byteValue()) // Remaining Length
@@ -476,15 +481,15 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
// Payload
.write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
.write(mqttClientId) // Client Identifier
- .write(0x00, clientId.size().byteValue())
- .write(clientId)
- .write(0x00, clientSecret.size().byteValue())
- .write(clientSecret)
+ .write(0x00, username.size().byteValue())
+ .write(username)
+ .write(0x00, password.size().byteValue())
+ .write(password)
.flush()
then: "mqtt connection should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId) != null
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId) != null
}
when: "a mqtt client subscribes to an asset in another realm"
@@ -505,7 +510,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes to an non existing asset"
@@ -526,7 +531,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes to an asset"
@@ -547,7 +552,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A subscription should exist"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
when: "An asset is updated with a new attribute"
@@ -561,7 +566,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client subscribes for child assets of an asset"
- topic = "asset/" + managerTestSetup.apartment1Id + TOPIC_SEPARATOR + MULTI_LEVEL_WILDCARD
+ topic = "asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
client
@@ -578,7 +583,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
and: "a second subscription is in place"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 2
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
}
and: "a asset is added as child to the subcribed asset"
@@ -610,7 +615,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "Only one subscription should be left"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1
+ assert mqttBrokerService.sessionIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
when: "MQTT client disconnects"
@@ -622,7 +627,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "no connection should be left"
conditions.eventually {
- assert mqttBrokerService.mqttConnectionMap.size() == 0
+ assert mqttBrokerService.sessionIdConnectionMap.size() == 0
}
}
}