Skip to content

armgit / core

Public Imported from GitHub
0

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

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.java
index 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 profile
public static final String KEYCLOAK_PORT = "KEYCLOAK_PORT";
public static final int KEYCLOAK_PORT_DEFAULT = 8081;
public static final String KEYCLOAK_CONNECT_TIMEOUT = "KEYCLOAK_CONNECT_TIMEOUT";
@@ -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.gradle
index 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/Dockerfile
index c9189bd2c..da81231a8 100644
--- a/manager/Dockerfile
+++ b/manager/Dockerfile
@@ -15,6 +15,7 @@ LABEL git-commit=$GIT_COMMIT
ENV JAVA_OPTS=${JAVA_OPTS}
ENV JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}
+ENV DEV_MODE false
ENV APP_DOCROOT /opt/web
ENV CUSTOM_APP_DOCROOT /deployment/manager/app
ENV LOGGING_CONFIG_FILE /deployment/manager/logging.properties
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 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 SSL
realmRepresentation.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 SMTP
String 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.java
index 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 roles
keycloakProvider.createOpenRemoteClientApplication(masterRealm.getRealm());
diff --git a/profile/deploy-no-build.yml b/profile/deploy-no-build.yml
index 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_PASSWORD
KEYCLOAK_USER: admin
PROXY_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.yml
index 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: 8080
MASTER_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_PASSWORD
KEYCLOAK_USER: admin
PROXY_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: 60s
retries: 30
- # Uncomment to support the IKEA TRÅDFRI protocol. USE AT YOUR OWN RISK.
- # ports:
- # - "8081:8080"
-
# PostgreSQL DBMS
postgresql:
diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.yml
index 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.yml
service: proxy
volumes:
# Store data either in host-mapped directory or a named Docker volume
# - ${DEPLOYMENT_DIRECTORY:-../deployment}/proxy:/deployment
- proxy-data:/deployment
environment:
- PROXY_BACKEND_HOST: 'host.docker.internal'
+ MANAGER_HOST: 'host.docker.internal'
keycloak:
extends:
- file: deploy-no-build.yml
+ file: deploy.yml
service: keycloak
volumes:
- ../deployment:/deployment
@@ -59,7 +59,7 @@ services:
postgresql:
extends:
- file: deploy-no-build.yml
+ file: deploy.yml
service: postgresql
# Access directly if needed on localhost
ports:
diff --git a/profile/dev-testing.yml b/profile/dev-testing.yml
index 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/Dockerfile
index 0a600f845..f45112675 100644
--- a/proxy/Dockerfile
+++ b/proxy/Dockerfile
@@ -27,6 +27,11 @@ ENV DOMAINNAME=${DOMAINNAME:-localhost}
ENV LOCAL_CERT_FILE=$LOCAL_CERT_FILE
ENV TERM xterm
ENV 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.cfg
index c8e52887f..3afb65563 100644
--- a/proxy/haproxy.cfg
+++ b/proxy/haproxy.cfg
@@ -80,10 +80,10 @@ listen mqtt
option tcplog
balance 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.groovy
index 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.groovy
index 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 ManagerContai
def 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.groovy
index 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 ManagerContainerTrait
def 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.js
index 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.js
index 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 = {