100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Improve handling of keycloak frontend URL (now set using REST API on each realm)
Rich Turnerd136ba747868
diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.javaindex 4d4774071..506585da0 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@@ -22,6 +22,10 @@ package org.openremote.container.security.keycloak;import com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;+import io.undertow.server.HttpHandler;+import io.undertow.server.handlers.ResponseCodeHandler;+import io.undertow.server.handlers.proxy.LoadBalancingProxyClient;+import io.undertow.server.handlers.proxy.ProxyHandler;import io.undertow.servlet.api.DeploymentInfo;import io.undertow.servlet.api.LoginConfig;import org.jboss.resteasy.client.jaxrs.ResteasyClient;@@ -36,6 +40,7 @@ import org.openremote.container.security.ClientCredentialsAuthForm;import org.openremote.container.security.IdentityProvider;import org.openremote.container.web.ClientRequestInfo;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;@@ -55,6 +60,7 @@ import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;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.REQUEST_HEADER_REALM;@@ -91,6 +97,7 @@ 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 identifierfinal protected String clientId;@@ -111,6 +118,8 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {protected LoadingCache<KeycloakRealmClient, KeycloakDeployment> keycloakDeploymentCache;// The configuration for the Keycloak servlet extension, looks up the client application per realmprotected KeycloakConfigResolver keycloakConfigResolver;+ // Optional reverse proxy that listens to 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 KeycloakIdentityProvider(String clientId) {this.clientId = clientId;@@ -151,7 +160,7 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {.scheme("http").host(getString(container.getConfig(), KEYCLOAK_HOST, KEYCLOAK_HOST_DEFAULT)).port(getInteger(container.getConfig(), KEYCLOAK_PORT, KEYCLOAK_PORT_DEFAULT))- .path("auth");+ .path(KEYCLOAK_AUTH_PATH);LOG.info("Keycloak service URL: " + keycloakServiceUri.build());@@ -198,6 +207,15 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {return keycloakDeployment;};+ if (container.isDevMode()) {+ authProxyHandler = ProxyHandler.builder()+ .setProxyClient(new LoadBalancingProxyClient().addHost(keycloakServiceUri.clone().replacePath("").build()))+ .setMaxRequestTime(getInteger(container.getConfig(), KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT))+ .setNext(ResponseCodeHandler.HANDLE_404)+ .setReuseXForwarded(true)+ .build();+ }+// TODO Not a great way to block startup while we wait for other services (Hystrix?)waitForKeycloak();LOG.info("Keycloak identity provider available: " + keycloakServiceUri.build());@@ -324,11 +342,6 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {keycloakRealmClient.realm, keycloakRealmClient.clientId);- // The auth-server-url in the adapter config must be reachable by this manager- adapterConfig.setAuthServerUrl(- keycloakServiceUri.clone().build().toString()- );-return KeycloakDeploymentBuilder.build(adapterConfig);}};@@ -340,6 +353,17 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {.build(loader);}+ protected void enableAuthProxy(WebService webService) {+ if (authProxyHandler == null)+ throw new IllegalStateException("Initialize this service first");++ LOG.info("Enabling auth reverse proxy (passing requests through to Keycloak) on web context: /" + KEYCLOAK_AUTH_PATH);+ webService.getRequestHandlers().add(0, pathStartsWithHandler(+ "Keycloak auth proxy",+ "/" + KEYCLOAK_AUTH_PATH,+ authProxyHandler));+ }+/*** There must be _some_ valid redirect URIs for the application or authentication will not be possible.*/diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 2659daecb..22d994d7c 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -100,6 +100,9 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im@Overridepublic void start(Container container) {super.start(container);+ if (container.isDevMode()) {+ enableAuthProxy(container.getService(WebService.class));+ }}@Override@@ -650,6 +653,14 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im// Service-internal network (between manager and keycloak service containers) does not use SSLrealmRepresentation.setSslRequired(SslRequired.NONE.toString());+ // Update the frontend URL to match the external URL of the system+ Map<String, String> attributes = realmRepresentation.getAttributes();+ if (attributes == null) {+ attributes = new HashMap<>();+ realmRepresentation.setAttributes(attributes);+ }+ attributes.put("frontendUrl", externalServerUri.clone().path(KEYCLOAK_AUTH_PATH).build().toString());+// Configure SMTPString host = container.getConfig().getOrDefault(SETUP_EMAIL_HOST, null);if (!TextUtil.isNullOrEmpty(host) && (realmRepresentation.getSmtpServer() == null || realmRepresentation.getSmtpServer().isEmpty())) {diff --git a/profile/deploy-no-build.yml b/profile/deploy-no-build.ymlindex 46454a346..4b2c36dc6 100644--- a/profile/deploy-no-build.yml+++ b/profile/deploy-no-build.yml@@ -273,7 +273,6 @@ services:DB_PASSWORD: CHANGE_ME_DB_USER_PASSWORDKEYCLOAK_USER: adminPROXY_ADDRESS_FORWARDING: 'true'- KEYCLOAK_FRONTEND_URL: 'https://${IDENTITY_NETWORK_HOST:-localhost}/auth'# CHANGE THIS PASSWORD BEFORE STARTING THE STACK! THIS IS ONLY FOR DEMO!KEYCLOAK_PASSWORD: ${SETUP_ADMIN_PASSWORD:-secret}diff --git a/profile/deploy.yml b/profile/deploy.ymlindex e8cb4b626..a6e03e22a 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -300,7 +300,6 @@ services:DB_PASSWORD: CHANGE_ME_DB_USER_PASSWORDKEYCLOAK_USER: adminPROXY_ADDRESS_FORWARDING: 'true'- KEYCLOAK_FRONTEND_URL: 'https://${IDENTITY_NETWORK_HOST:-localhost}/auth'# CHANGE THIS PASSWORD BEFORE STARTING THE STACK! THIS IS ONLY FOR DEMO!KEYCLOAK_PASSWORD: ${SETUP_ADMIN_PASSWORD:-secret}diff --git a/profile/dev-testing.yml b/profile/dev-testing.ymlindex 529fa579d..c1b0c5db4 100644--- a/profile/dev-testing.yml+++ b/profile/dev-testing.yml@@ -24,8 +24,6 @@ services:depends_on:postgresql:condition: service_healthy- environment:- KEYCLOAK_FRONTEND_URL: 'http://localhost:8081/auth'postgresql:extends:diff --git a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovyindex 021ef238a..0ba361065 100644--- a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy+++ b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy@@ -30,8 +30,8 @@ trait ManagerContainerTrait extends ContainerTrait {}[(WEBSERVER_LISTEN_PORT) : Integer.toString(serverPort),- (IDENTITY_NETWORK_WEBSERVER_PORT): Integer.toString(serverPort),(IDENTITY_NETWORK_HOST) : "127.0.0.1",+ (IDENTITY_NETWORK_WEBSERVER_PORT): "8081",(SETUP_TESTS) : "true",(SETUP_IMPORT_DEMO_ASSETS) : "true",(SETUP_IMPORT_DEMO_USERS) : "true",diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovyindex a961300fb..a2c5c426e 100644--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy@@ -109,7 +109,7 @@ class GatewayTest extends Specification implements ManagerContainerTrait {def gatewayClient = new WebsocketIoClient<String>(new URIBuilder("ws://127.0.0.1:$serverPort/websocket/events?Auth-Realm=$managerTestSetup.realmBuildingTenant").build(),null,- new OAuthClientCredentialsGrant("http://127.0.0.1:$serverPort/auth/realms/$managerTestSetup.realmBuildingTenant/protocol/openid-connect/token",+ new OAuthClientCredentialsGrant("http://127.0.0.1:8081/auth/realms/$managerTestSetup.realmBuildingTenant/protocol/openid-connect/token",gateway.getClientId().orElse(""),gateway.getClientSecret().orElse(""),null).setBasicAuthHeader(true))diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovyindex 207dfb046..691aa5687 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy@@ -144,7 +144,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContaidef agent = new WebsocketClientAgent("Test agent").setRealm(Constants.MASTER_REALM).setConnectUri("ws://127.0.0.1:$serverPort/websocket/events?Auth-Realm=master")- .setOAuthGrant(new OAuthPasswordGrant("http://127.0.0.1:$serverPort/auth/realms/master/protocol/openid-connect/token",+ .setOAuthGrant(new OAuthPasswordGrant("http://127.0.0.1:8081/auth/realms/master/protocol/openid-connect/token",KEYCLOAK_CLIENT_ID,null,null,diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovyindex 3d272e0f6..2ca5c8e19 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy@@ -87,7 +87,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitdef client = new WebsocketIoClient<String>(new URIBuilder("ws://127.0.0.1:$serverPort/websocket/events?Auth-Realm=master").build(),null,- new OAuthPasswordGrant("http://127.0.0.1:$serverPort/auth/realms/master/protocol/openid-connect/token",+ new OAuthPasswordGrant("http://127.0.0.1:8081/auth/realms/master/protocol/openid-connect/token",KEYCLOAK_CLIENT_ID,null,null,