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 robustness of keycloak for all deployment scenarios
Rich Turner845c75d8a9f4
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 506585da0..4f1353608 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@@ -78,7 +78,7 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {"security-admin-console");public static final String KEYCLOAK_HOST = "KEYCLOAK_HOST";- public static final String KEYCLOAK_HOST_DEFAULT = "localhost";+ public static final String KEYCLOAK_HOST_DEFAULT = "127.0.0.1"; // Bug in keycloak default hostname provider means localhost causes problems with dev-proxy profilepublic static final String KEYCLOAK_PORT = "KEYCLOAK_PORT";public static final int KEYCLOAK_PORT_DEFAULT = 8081;public static final String KEYCLOAK_CONNECT_TIMEOUT = "KEYCLOAK_CONNECT_TIMEOUT";@@ -342,6 +342,11 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {keycloakRealmClient.realm, keycloakRealmClient.clientId);+ // The auth-server-url in the adapter config must be reachable by this manager it will be the frontend URL by default+ adapterConfig.setAuthServerUrl(+ keycloakServiceUri.clone().build().toString()+ );+return KeycloakDeploymentBuilder.build(adapterConfig);}};diff --git a/deployment/build.gradle b/deployment/build.gradleindex d730f190b..8148810aa 100644--- a/deployment/build.gradle+++ b/deployment/build.gradle@@ -9,7 +9,7 @@ task installDist(type: Copy) {into "${buildDir}/image"from(projectDir) {- exclude "build.gradle", "Dockerfile", "build", "**/*.mbtiles", "src"+ exclude "build.gradle", "Dockerfile", "build", "src"}into("manager/extensions") {diff --git a/manager/Dockerfile b/manager/Dockerfileindex c9189bd2c..da81231a8 100644--- a/manager/Dockerfile+++ b/manager/Dockerfile@@ -15,6 +15,7 @@ LABEL git-commit=$GIT_COMMITENV JAVA_OPTS=${JAVA_OPTS}ENV JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}+ENV DEV_MODE falseENV APP_DOCROOT /opt/webENV CUSTOM_APP_DOCROOT /deployment/manager/appENV LOGGING_CONFIG_FILE /deployment/manager/logging.propertiesdiff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.javaindex 22d994d7c..5322cef5e 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java@@ -653,14 +653,6 @@ 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/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.javaindex 96fa9f913..71c4ee1a8 100644--- a/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java@@ -47,6 +47,9 @@ public class KeycloakInitSetup extends AbstractKeycloakSetup {masterRealm.setDisplayName("Master");keycloakProvider.updateTenant(masterRealm);+ // Check that frontend URL has been updated before we proceed; we can see this in+ Thread.sleep(10000);+// Create our client application with its default roleskeycloakProvider.createOpenRemoteClientApplication(masterRealm.getRealm());diff --git a/profile/deploy-no-build.yml b/profile/deploy-no-build.ymlindex 4b2c36dc6..d65b82568 100644--- a/profile/deploy-no-build.yml+++ b/profile/deploy-no-build.yml@@ -56,8 +56,6 @@ services:# Available levels are: emerg, alert, crit, err, warning, notice, info, debug# PROXY_LOGLEVEL: 'notice'- # Uncomment to support the IKEA TRÅDFRI protocol. USE AT YOUR OWN RISK.- # PROXY_BACKEND_HOST: 127.0.0.1# Run proxy, obtain and automatically renew SSL certificates if DOMAINNAME is not localhost. Use# the command 'help' to get a list of all options.@@ -273,6 +271,7 @@ 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 a6e03e22a..b899b6b31 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -61,9 +61,6 @@ services:# Available levels are: emerg, alert, crit, err, warning, notice, info, debug# PROXY_LOGLEVEL: 'notice'- # Uncomment to support the IKEA TRÅDFRI protocol. USE AT YOUR OWN RISK.- # PROXY_BACKEND_HOST: 127.0.0.1-# Run proxy, obtain and automatically renew SSL certificates if DOMAINNAME is not localhost. Use# the command 'help' to get a list of all options.command: run@@ -174,12 +171,6 @@ services:KEYCLOAK_PORT: 8080MASTER_REALM_KEYCLOAK_THEME: ${MASTER_REALM_KEYCLOAK_THEME:-openremote}- # Uncomment to support the IKEA TRÅDFRI protocol. USE AT YOUR OWN RISK.- # Also remove (or comment) the already existing variables with the same name.- # KEYCLOAK_HOST: 127.0.0.1- # KEYCLOAK_PORT: 8081- # DATABASE_CONNECTION_URL: jdbc:postgresql://localhost/openremote-# The service-internal webservice interface binding, bind to all interfaces# when running in container. Default is 127.0.0.1.WEBSERVER_LISTEN_HOST: 0.0.0.0@@ -300,6 +291,7 @@ 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}@@ -325,10 +317,6 @@ services:start_period: 60sretries: 30- # Uncomment to support the IKEA TRÅDFRI protocol. USE AT YOUR OWN RISK.- # ports:- # - "8081:8080"-# PostgreSQL DBMSpostgresql:diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.ymlindex 48ed2d83e..b706f5b65 100644--- a/profile/dev-proxy.yml+++ b/profile/dev-proxy.yml@@ -33,18 +33,18 @@ services:proxy:extends:- file: deploy-no-build.yml+ file: deploy.ymlservice: proxyvolumes:# Store data either in host-mapped directory or a named Docker volume# - ${DEPLOYMENT_DIRECTORY:-../deployment}/proxy:/deployment- proxy-data:/deploymentenvironment:- PROXY_BACKEND_HOST: 'host.docker.internal'+ MANAGER_HOST: 'host.docker.internal'keycloak:extends:- file: deploy-no-build.yml+ file: deploy.ymlservice: keycloakvolumes:- ../deployment:/deployment@@ -59,7 +59,7 @@ services:postgresql:extends:- file: deploy-no-build.yml+ file: deploy.ymlservice: postgresql# Access directly if needed on localhostports:diff --git a/profile/dev-testing.yml b/profile/dev-testing.ymlindex c1b0c5db4..9b71e16f9 100644--- a/profile/dev-testing.yml+++ b/profile/dev-testing.yml@@ -24,6 +24,9 @@ services:depends_on:postgresql:condition: service_healthy+ environment:+ # Use manager dev mode reverse proxy to access keycloak+ KEYCLOAK_FRONTEND_URL: 'http://localhost:8080/auth'postgresql:extends:diff --git a/proxy/Dockerfile b/proxy/Dockerfileindex 0a600f845..f45112675 100644--- a/proxy/Dockerfile+++ b/proxy/Dockerfile@@ -27,6 +27,11 @@ ENV DOMAINNAME=${DOMAINNAME:-localhost}ENV LOCAL_CERT_FILE=$LOCAL_CERT_FILEENV TERM xtermENV PROXY_LOGLEVEL notice+ENV MANAGER_HOST=${MANAGER_HOST:-manager}+ENV MANAGER_WEB_PORT=${MANAGER_WEB_PORT:-8080}+ENV MANAGER_MQTT_PORT=${MANAGER_MQTT_PORT:-1883}+ENV KEYCLOAK_HOST=${KEYCLOAK_HOST:-keycloak}+ENV KEYCLOAK_PORT=${KEYCLOAK_PORT:-8080}RUN mkdir /tmp/lua && cd /tmp/lua \&& curl -sSL https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz -o lua.tar.gz \diff --git a/proxy/haproxy.cfg b/proxy/haproxy.cfgindex c8e52887f..3afb65563 100644--- a/proxy/haproxy.cfg+++ b/proxy/haproxy.cfg@@ -80,10 +80,10 @@ listen mqttoption tcplogbalance leastconn- server proxy_backend_host "manager:1883"+ server proxy_backend_host "${MANAGER_HOST}":"${MANAGER_MQTT_PORT}"backend manager_backend- server proxy_backend_host "manager:8080"+ server proxy_backend_host "${MANAGER_HOST}":"${MANAGER_WEB_PORT}"backend keycloak_backend- server proxy_backend_host "keycloak:8080"+ server proxy_backend_host "${KEYCLOAK_HOST}":"${KEYCLOAK_PORT}"diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovyindex a2c5c426e..a961300fb 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:8081/auth/realms/$managerTestSetup.realmBuildingTenant/protocol/openid-connect/token",+ new OAuthClientCredentialsGrant("http://127.0.0.1:$serverPort/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 691aa5687..207dfb046 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:8081/auth/realms/master/protocol/openid-connect/token",+ .setOAuthGrant(new OAuthPasswordGrant("http://127.0.0.1:$serverPort/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 2ca5c8e19..3d272e0f6 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:8081/auth/realms/master/protocol/openid-connect/token",+ new OAuthPasswordGrant("http://127.0.0.1:$serverPort/auth/realms/master/protocol/openid-connect/token",KEYCLOAK_CLIENT_ID,null,null,diff --git a/ui/app/manager/webpack.config.js b/ui/app/manager/webpack.config.jsindex fc2692911..cc8f51c25 100644--- a/ui/app/manager/webpack.config.js+++ b/ui/app/manager/webpack.config.js@@ -6,8 +6,9 @@ module.exports = (env, argv) => {const customConfigDir = env.config;const managerUrl = env.managerUrl;+ const keycloakUrl = env.keycloakUrl;const IS_DEV_SERVER = process.argv.find(arg => arg.includes("serve"));- const config = util.getAppConfig(argv.mode, IS_DEV_SERVER, __dirname, managerUrl);+ const config = util.getAppConfig(argv.mode, IS_DEV_SERVER, __dirname, managerUrl, keycloakUrl);if (IS_DEV_SERVER && customConfigDir) {console.log("CUSTOM_CONFIG_DIR: " + customConfigDir);diff --git a/ui/util/webpack.util.js b/ui/util/webpack.util.jsindex 4fcbbd978..387895d7e 100644--- a/ui/util/webpack.util.js+++ b/ui/util/webpack.util.js@@ -7,7 +7,7 @@ const HtmlWebpackPlugin = require("html-webpack-plugin");function getAppConfig(mode, isDevServer, dirname, managerUrl, keycloakUrl) {const production = mode === "production";managerUrl = managerUrl || (production ? undefined : "http://localhost:8080");- keycloakUrl = keycloakUrl || (production ? undefined : "http://localhost:8081/auth");+ keycloakUrl = keycloakUrl || (production ? undefined : "http://localhost:8080/auth");const OUTPUT_PATH = isDevServer ? 'src' : 'dist';const config = {