Skip to content

armgit / core

Public Imported from GitHub
0

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

Updated to work with Keycloak 18+ (#762)

Rich Turnercfd4eee8be7f
diff --git a/README.md b/README.md
index a0503353a..e1578ff0c 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,7 @@ Username: admin
Password: secret
### Changing host and/or port
-The URL you use to access the system is important, the default is configured as `https://localhost` if you are using a VM or want to run on a different port then you will need to set the `OR_HOSTNAME` amd `OR_SSL_PORT` environment variables, so if for example you will be accessing using `https://192.168.1.1:8443` then use the following startup command:
+The URL you use to access the system is important, the default is configured as `https://localhost` if you are using a VM or want to run on a different port then you will need to set the `OR_HOSTNAME` and `OR_SSL_PORT` environment variables, so if for example you will be accessing using `https://192.168.1.1:8443` then use the following startup command:
BASH:
```
@@ -49,8 +49,6 @@ CMD:
cmd /C "set OR_HOSTNAME=192.168.1.1 && set OR_SSL_PORT=8443 && docker-compose -p openremote up -d"
```
-***NOTE: When chaning `OR_SSL_PORT` you will need to alter the `KEYCLOAK_FRONTEND_URL` in the docker-compose.yml file (see comments in the file for details)***
-
## What next
Try creating assets, agents, rules, users, realms, etc. using the Manager UI, please refer to the [wiki](https://github.com/openremote/openremote/wiki) for more information, some things to try:
diff --git a/deployment/keycloak/themes/README.md b/deployment/keycloak/themes/README.md
new file mode 100644
index 000000000..4ceda9a3a
--- /dev/null
+++ b/deployment/keycloak/themes/README.md
@@ -0,0 +1,8 @@
+## Custom Keycloak themes
+Add a directory for each custom theme (the directory name is the theme name) and add the required theme templates within; you can use the default [openremote theme](https://github.com/openremote/keycloak/tree/main/themes/openremote) as a template alternatively refer to the keycloak themes documentation.
+
+This themes directory can then be volume mapped into the keycloak container at `/deployment/keycloak/themes`.
+
+### Development
+To be able to see the custom theme in development just run the `dev-testing.yml` compose profile and uncomment the deployment volume mapping.s
+
diff --git a/docker-compose.yml b/docker-compose.yml
index 9cb6cdf74..172b7ce79 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -50,10 +50,10 @@ services:
volumes:
- ./deployment:/deployment
environment:
- KEYCLOAK_PASSWORD: ${OR_ADMIN_PASSWORD:-secret}
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
- # Use the following if OR_SSL_PORT is not the default 443
- #KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}:${OR_SSL_PORT:-443}/auth
+ KEYCLOAK_ADMIN_PASSWORD: ${OR_ADMIN_PASSWORD:-secret}
+ KC_HOSTNAME: ${OR_HOSTNAME:-localhost}
+ KC_HOSTNAME_PORT: ${OR_SSL_PORT:--1}
+
manager:
# privileged: true
@@ -74,11 +74,8 @@ services:
OR_EMAIL_ADMIN:
OR_HOSTNAME: ${OR_HOSTNAME:-localhost}
OR_ADDITIONAL_HOSTNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}
- OR_SSL_PORT: ${OR_SSL_PORT:-443}
+ OR_SSL_PORT: ${OR_SSL_PORT:--1}
OR_DEV_MODE: ${OR_DEV_MODE:-false}
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
- # Use the following if OR_SSL_PORT is not the default 443
- #KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}:${OR_SSL_PORT:-443}/auth
# The following variables will configure the demo
OR_FORECAST_SOLAR_API_KEY:
diff --git a/gradle.properties b/gradle.properties
index 80b9e2f7a..3d2959510 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -12,7 +12,7 @@ undertowVersion = 2.2.3.Final
resteasyVersion = 3.14.0.Final
resteasyVersionAsyncHttpServlet = 3.0.19.Final
jbossLoggingAnnotationsVersion = 2.0.2.Final
-keycloakVersion = 16.0.0
+keycloakVersion = 18.0.2
jacksonVersion = 2.13.1
groovyVersion = 3.0.9
groovySandboxVersion = 1.19
diff --git a/manager/Dockerfile b/manager/Dockerfile
index f11fc171b..356bcf102 100644
--- a/manager/Dockerfile
+++ b/manager/Dockerfile
@@ -11,7 +11,7 @@ ENV JAVA_TOOL_OPTIONS ${JAVA_TOOL_OPTIONS}
ENV TZ ${TZ:-Europe/Amsterdam}
ENV OR_ADMIN_PASSWORD ${OR_ADMIN_PASSWORD:-secret}
ENV OR_SETUP_TYPE ${OR_SETUP_TYPE}
-ENV OR_SSL_PORT ${OR_SSL_PORT:-443}
+ENV OR_SSL_PORT ${OR_SSL_PORT:--1}
ENV OR_HOSTNAME ${OR_HOSTNAME:-localhost}
ENV OR_EMAIL_HOST ${OR_EMAIL_HOST}
ENV OR_EMAIL_USER ${OR_EMAIL_USER}
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 94baa2ef7..0cd371228 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -21,6 +21,7 @@ package org.openremote.manager.security;
import io.undertow.util.Headers;
import org.apache.commons.io.IOUtils;
+import org.apache.http.client.utils.URIBuilder;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.*;
@@ -62,6 +63,7 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.io.InputStream;
+import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -74,8 +76,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
-import static org.openremote.container.util.MapAccess.getBoolean;
-import static org.openremote.container.util.MapAccess.getString;
+import static org.openremote.container.util.MapAccess.*;
import static org.openremote.model.Constants.*;
import static org.openremote.model.util.ValueUtil.convert;
@@ -94,7 +95,9 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
public static final String KEYCLOAK_DEFAULT_ROLES_PREFIX = "default-roles-";
public static final String KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED = "emailNotificationsDisabled";
public static final String KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED = "pushNotificationsDisabled";
- public static final String KEYCLOAK_FRONTEND_URL = "KEYCLOAK_FRONTEND_URL";
+ public static final String KC_HOSTNAME = "KC_HOSTNAME";
+ public static final String KC_HOSTNAME_PATH = "KC_HOSTNAME_PATH";
+ public static final String KC_HOSTNAME_PORT = "KC_HOSTNAME_PORT";
protected PersistenceService persistenceService;
protected AssetStorageService assetStorageService;
@@ -118,7 +121,24 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
setActiveCredentials(grant);
}
- frontendUrl = getString(container.getConfig(), KEYCLOAK_FRONTEND_URL, null);
+ String hostname = getString(container.getConfig(), Constants.OR_HOSTNAME, "localhost");
+ int port = getInteger(container.getConfig(), Constants.OR_SSL_PORT, 8080); // Should just be called port
+ String keycloakHostname = getString(container.getConfig(), KC_HOSTNAME, hostname);
+ int keycloakPort = getInteger(container.getConfig(), KC_HOSTNAME_PORT, port);
+ String keycloakHostnamePath = getString(container.getConfig(), KC_HOSTNAME_PATH, "auth");
+
+ URIBuilder uriBuilder = new URIBuilder().setHost(keycloakHostname).setPath(keycloakHostnamePath);
+ if (keycloakPort > 0) {
+ uriBuilder.setPort(keycloakPort);
+ }
+
+ try {
+ frontendUrl = uriBuilder.build().toString();
+ } catch (URISyntaxException e) {
+ LOG.log(Level.SEVERE, "Failed to build Keycloak host URI", e);
+ throw new RuntimeException(e);
+ }
+
keycloakAdminPassword = container.getConfig().getOrDefault(OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT);
timerService = container.getService(TimerService.class);
persistenceService = container.getService(PersistenceService.class);
@@ -130,7 +150,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
// Allow all external hostnames with wildcard and same host with wildcard
validRedirectUris = new ArrayList<>();
validRedirectUris.add("/*");
- validRedirectUris.addAll(WebService.getExternalHostnames(container).stream().map(hostname -> "https://" + hostname + "/*").toList());
+ validRedirectUris.addAll(WebService.getExternalHostnames(container).stream().map(host -> "https://" + host + "/*").toList());
}
@Override
diff --git a/model/build.gradle b/model/build.gradle
index 9608963ff..960506028 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -20,7 +20,6 @@ dependencies {
api "org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:$jaxrsSpecVersion"
api "org.keycloak:keycloak-core:$keycloakVersion"
- api "org.keycloak:keycloak-core:$keycloakVersion:sources"
api "org.geotools:gt-main:$geotoolsVersion"
api "com.jayway.jsonpath:json-path:$jaywayJsonPathVersion"
diff --git a/profile/demo.yml b/profile/demo.yml
index 62188bb35..fd30f77b1 100644
--- a/profile/demo.yml
+++ b/profile/demo.yml
@@ -73,8 +73,9 @@ services:
volumes:
- ./deployment:/deployment
environment:
- KEYCLOAK_PASSWORD: ${OR_ADMIN_PASSWORD?OR_ADMIN_PASSWORD must be set}
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
+ KEYCLOAK_ADMIN_PASSWORD: ${OR_ADMIN_PASSWORD?OR_ADMIN_PASSWORD must be set}
+ KC_HOSTNAME: ${OR_HOSTNAME:-localhost}
+ KC_HOSTNAME_PORT: ${OR_SSL_PORT:--1}
<<: *awslogs
manager:
@@ -94,11 +95,10 @@ services:
OR_EMAIL_X_HEADERS:
OR_EMAIL_FROM:
OR_EMAIL_ADMIN:
- OR_SSL_PORT: ${OR_SSL_PORT:-443}
+ OR_SSL_PORT: ${OR_SSL_PORT:--1}
OR_HOSTNAME: ${OR_HOSTNAME:-localhost}
OR_ADDITIONAL_HOSTNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}
OR_DEV_MODE: ${OR_DEV_MODE:-false}
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
# The following variables will configure the demo
OR_FORECAST_SOLAR_API_KEY:
OR_OPEN_WEATHER_API_APP_ID:
diff --git a/profile/deploy.yml b/profile/deploy.yml
index 1bd8253f7..9ce0913c5 100644
--- a/profile/deploy.yml
+++ b/profile/deploy.yml
@@ -61,7 +61,7 @@ services:
# private/externally generated certificates); this is an alternative to DOMAINNAME for multiple domains.
DOMAINNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}
- # USE A CUSTOM PROXY CONFIG - COPY FROM https://github.com/openremote/proxy/blob/main/haproxy.cfg
+ # USE A CUSTOM PROXY CONFIG - COPY FROM https://raw.githubusercontent.com/openremote/proxy/main/haproxy.cfg
HAPROXY_CONFIG: ${HAPROXY_CONFIG:-/etc/haproxy/haproxy.cfg}
# Email address for SSL certificate issuance and expiration notifications.
@@ -150,7 +150,7 @@ services:
OR_SETUP_TYPE: ${OR_SETUP_TYPE}
# The public HOSTNAME and SSL PORT of this manager
- OR_SSL_PORT: ${OR_SSL_PORT:-443}
+ OR_SSL_PORT: ${OR_SSL_PORT:--1}
OR_HOSTNAME: ${OR_HOSTNAME:-localhost}
# Additional hostnames that should be allowed to access the auth server
@@ -216,10 +216,10 @@ services:
OR_KEYCLOAK_HOST: ${OR_KEYCLOAK_HOST:-keycloak}
OR_KEYCLOAK_PORT: ${OR_KEYCLOAK_PORT:-8080}
- # The frontend URL of the Keycloak identity provider used by this instance (defaults to http:// https://OR_HOSTNAME:OR_SSL_PORT/auth)
- # This is used in server info endpoint to inform frontend where the identity provider is for this instance (frontend apps should
- # assume identity provider is on the same host as this instance if it is not set)
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
+ # Keycloak frontend host name, port and path (defaults: to OR_HOSTNAME:OR_SSL_PORT/auth)
+ KC_HOSTNAME:
+ KC_HOSTNAME_PORT:
+ KC_HOSTNAME_PATH:
# Location of built in web content.
OR_APP_DOCROOT: ${OR_APP_DOCROOT:-/opt/web}
@@ -322,33 +322,36 @@ services:
# Bearer tokens are only valid for minutes, and authentication fails if Keycloak drifts
TZ: ${TZ:-Europe/Amsterdam}
- # Switch to DEBUG to diagnose Keycloak problems; default:
- KEYCLOAK_LOGLEVEL: ${KEYCLOAK_LOGLEVEL:-INFO}
+ # Switch to debug to diagnose Keycloak problems; default:
+ KC_LOG_LEVEL: ${KC_LOG_LEVEL:-info}
# Configure how Keycloak connects to the database
- DB_VENDOR: ${OR_DB_VENDOR:-postgres}
- DB_ADDR: ${OR_DB_HOST:-postgresql}
- DB_PORT: ${OR_DB_PORT:-5432}
- DB_DATABASE: ${OR_DB_NAME:-openremote}
- DB_USER: ${OR_DB_USER:-postgres}
- DB_PASSWORD: ${OR_DB_PASSWORD:-postgres}
- DB_SCHEMA: ${OR_DB_SCHEMA:-public}
- KEYCLOAK_USER: ${KEYCLOAK_USER:-admin}
- KEYCLOAK_PASSWORD: ${OR_ADMIN_PASSWORD:-secret}
- PROXY_ADDRESS_FORWARDING: ${PROXY_ADDRESS_FORWARDING:-true}
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
- # Use the following if OR_SSL_PORT is not the default 443
- #KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}:${OR_SSL_PORT:-443}/auth
+ KC_DB_URL_HOST: ${KC_DB_URL_HOST:-postgresql}
+ KC_DB_URL_PORT: ${KC_DB_URL_PORT:-5432}
+ KC_DB_URL_DATABASE: ${KC_DB_URL_DATABASE:-openremote}
+ KC_DB_SCHEMA: ${KC_DB_SCHEMA:-public}
+ KC_DB_USERNAME: ${KC_DB_USERNAME:-postgres}
+ KC_DB_PASSWORD: ${KC_DB_PASSWORD:-postgres}
+ # Configure web server
+ KC_HOSTNAME: ${OR_HOSTNAME:-localhost}
+ KC_HOSTNAME_PORT: ${OR_SSL_PORT:--1}
+ KC_HOSTNAME_PATH: ${KC_HOSTNAME_PATH:-auth}
+ KC_HOSTNAME_ADMIN: ${KC_HOSTNAME_ADMIN:-}
+ KC_PROXY: ${KC_PROXY:-edge}
+
+ # Configure main user credentials (only on first startup)
+ KEYCLOAK_ADMIN: ${KEYCLOAK_ADMIN:-admin}
+ KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD:-secret}
+ # Custom startup options (see keycloak documentation)
+ KEYCLOAK_START_COMMAND: ${KEYCLOAK_START_COMMAND:-start}
+ KEYCLOAK_START_OPTS: ${KEYCLOAK_START_OPTS:-}
+ # Default themes to use
+ KEYCLOAK_DEFAULT_THEME: ${KEYCLOAK_DEFAULT_THEME:-openremote}
+ KEYCLOAK_ACCOUNT_THEME: ${KEYCLOAK_ACCOUNT_THEME:-openremote}
+ KEYCLOAK_WELCOME_THEME: ${KEYCLOAK_WELCOME_THEME:-keycloak}
# Custom JVM options (increase heap size if required)
- JAVA_OPTS: ${KEYCLOAK_JAVA_OPTS:--XX:CompressedClassSpaceSize=50m -XX:MaxMetaspaceSize=250m
- -XX:InitialCodeCacheSize=50m -XX:ReservedCodeCacheSize=50m
- -XX:MaxDirectMemorySize=25m
- -XX:NativeMemoryTracking=summary
- -XX:+ExitOnOutOfMemoryError
- -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/javadump.core.hprof
- -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true}
-
+ JAVA_OPTS: ${KEYCLOAK_JAVA_OPTS:-}
# PostgreSQL DBMS
postgresql:
diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.yml
index e54f83ce9..3a57cd6d2 100644
--- a/profile/dev-proxy.yml
+++ b/profile/dev-proxy.yml
@@ -23,28 +23,20 @@ services:
service: proxy
environment:
MANAGER_HOST: 'host.docker.internal'
- OR_KEYCLOAK_HOST: 'host.docker.internal'
- OR_KEYCLOAK_PORT: 8081
keycloak:
extends:
file: deploy.yml
service: keycloak
- volumes:
- # Map custom themes
- #- ../deployment:/deployment
- - ./disable-theme-cache.cli:/opt/jboss/startup-scripts/disable-theme-cache.cli
+ #volumes:
+ # Map custom themes
+ # - ../deployment:/deployment
# Access directly if needed on localhost
ports:
- "8081:8080"
depends_on:
postgresql:
condition: service_healthy
- environment:
- # Use manager dev mode reverse proxy to access keycloak so manager and keycloak hosts match
- KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}/auth
- # Use the following if OR_SSL_PORT is not the default 443
- #KEYCLOAK_FRONTEND_URL: https://${OR_HOSTNAME:-localhost}:${OR_SSL_PORT:-443}/auth
postgresql:
extends:
diff --git a/profile/dev-testing.yml b/profile/dev-testing.yml
index 1c3e72078..8181d0e40 100644
--- a/profile/dev-testing.yml
+++ b/profile/dev-testing.yml
@@ -12,19 +12,19 @@ services:
extends:
file: deploy.yml
service: keycloak
- volumes:
+ # volumes:
# Map custom themes
# - ../deployment:/deployment
- - ./disable-theme-cache.cli:/opt/jboss/startup-scripts/disable-theme-cache.cli
- # Access directly if needed on localhost
ports:
- "8081:8080"
depends_on:
postgresql:
condition: service_healthy
environment:
- # Use manager dev mode reverse proxy to access keycloak so manager and keycloak hosts match
- KEYCLOAK_FRONTEND_URL: http://${OR_HOSTNAME:-localhost}:8080/auth
+ KC_HOSTNAME_STRICT_HTTPS: 'false'
+ KC_HOSTNAME_PORT: ${KC_HOSTNAME_PORT:-8080}
+ # Prevent theme caching during dev
+ KEYCLOAK_START_OPTS: --spi-theme-static-max-age=-1 --spi-theme-cache-themes=false --spi-theme-cache-templates=false
postgresql:
extends:
diff --git a/profile/dev-ui.yml b/profile/dev-ui.yml
index 9d5214d7b..6d05533df 100644
--- a/profile/dev-ui.yml
+++ b/profile/dev-ui.yml
@@ -15,10 +15,9 @@ services:
extends:
file: deploy.yml
service: keycloak
- volumes:
- # Map custom themes
- #- ../deployment:/deployment
- - ./disable-theme-cache.cli:/opt/jboss/startup-scripts/disable-theme-cache.cli
+ #volumes:
+ # Map custom themes
+ # - ../deployment:/deployment
# Access directly if needed on localhost
ports:
- "8081:8080"
@@ -26,8 +25,9 @@ services:
postgresql:
condition: service_healthy
environment:
- # Use manager dev mode reverse proxy to access keycloak so manager and keycloak hosts match
- KEYCLOAK_FRONTEND_URL: http://${OR_HOSTNAME:-localhost}:8080/auth
+ KEYCLOAK_START_COMMAND: start-dev
+ # Prevent theme caching during dev
+ KEYCLOAK_START_OPTS: --spi-theme-static-max-age=-1 --spi-theme-cache-themes=false --spi-theme-cache-templates=false
postgresql:
extends:
@@ -52,6 +52,5 @@ services:
environment:
OR_SETUP_RUN_ON_RESTART: ${OR_SETUP_RUN_ON_RESTART:-true}
OR_DEV_MODE: ${OR_DEV_MODE:-true}
- KEYCLOAK_FRONTEND_URL:
ports:
- "8080:8080"
diff --git a/profile/disable-theme-cache.cli b/profile/disable-theme-cache.cli
deleted file mode 100644
index fde02b1be..000000000
--- a/profile/disable-theme-cache.cli
+++ /dev/null
@@ -1,5 +0,0 @@
-embed-server --std-out=echo --server-config=standalone-ha.xml
-/subsystem=keycloak-server/theme=defaults/:write-attribute(name=cacheThemes,value=false)
-/subsystem=keycloak-server/theme=defaults/:write-attribute(name=cacheTemplates,value=false)
-/subsystem=keycloak-server/theme=defaults/:write-attribute(name=staticMaxAge,value=-1)
-stop-embedded-server
diff --git a/project.gradle b/project.gradle
index 8a240fa00..1599cb84f 100644
--- a/project.gradle
+++ b/project.gradle
@@ -97,7 +97,7 @@ if (project == rootProject) {
envs = [
OR_SETUP_TYPE: "demo",
OR_WEBSERVER_LISTEN_HOST: "0.0.0.0",
- OR_SSL_PORT: "8443"
+ OR_SSL_PORT: "443"
]
}
"Empty"(Application) {
@@ -107,7 +107,7 @@ if (project == rootProject) {
moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.manager.main" : ".manager.main")
envs = [
OR_WEBSERVER_LISTEN_HOST: "0.0.0.0",
- OR_SSL_PORT: "8443"
+ OR_SSL_PORT: "443"
]
}
}