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
Upgraded Keycloak
Christian Bauercb45b73d1282
diff --git a/container/src/main/java/org/openremote/container/security/ClientInstall.java b/container/src/main/java/org/openremote/container/security/ClientInstall.javadeleted file mode 100644index 5e5491959..000000000--- a/container/src/main/java/org/openremote/container/security/ClientInstall.java+++ /dev/null@@ -1,107 +0,0 @@-/*- * Copyright 2016, 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.container.security;--import com.fasterxml.jackson.annotation.JsonIgnore;-import com.fasterxml.jackson.annotation.JsonProperty;--import java.security.PublicKey;--public class ClientInstall {-- protected String realm;-- @JsonProperty("resource")- protected String clientId;-- @JsonProperty("realm-public-key")- protected String publicKeyPEM;-- @JsonProperty("auth-server-url")- protected String authServerUrl;-- @JsonProperty("ssl-required")- protected String sslRequired;-- @JsonProperty("public-client")- protected boolean publicClient;-- @JsonIgnore- protected PublicKey publicKey;-- public ClientInstall() {- }-- public String getRealm() {- return realm;- }-- public void setRealm(String realm) {- this.realm = realm;- }-- public String getClientId() {- return clientId;- }-- public void setClientId(String clientId) {- this.clientId = clientId;- }-- public String getPublicKeyPEM() {- return publicKeyPEM;- }-- public void setPublicKeyPEM(String publicKeyPEM) {- this.publicKeyPEM = publicKeyPEM;- }-- public String getAuthServerUrl() {- return authServerUrl;- }-- public void setAuthServerUrl(String authServerUrl) {- this.authServerUrl = authServerUrl;- }-- public String getSslRequired() {- return sslRequired;- }-- public void setSslRequired(String sslRequired) {- this.sslRequired = sslRequired;- }-- public boolean isPublicClient() {- return publicClient;- }-- public void setPublicClient(boolean publicClient) {- this.publicClient = publicClient;- }-- public PublicKey getPublicKey() {- return publicKey;- }-- public void setPublicKey(PublicKey publicKey) {- this.publicKey = publicKey;- }-}diff --git a/container/src/main/java/org/openremote/container/security/ClientRealm.java b/container/src/main/java/org/openremote/container/security/ClientRealm.javadeleted file mode 100644index 8f6bb80ae..000000000--- a/container/src/main/java/org/openremote/container/security/ClientRealm.java+++ /dev/null@@ -1,63 +0,0 @@-/*- * Copyright 2016, 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.container.security;--import org.keycloak.adapters.KeycloakDeployment;--public class ClientRealm {-- public static class Key {- public final String realm;- public final String clientId;-- public Key(String realm, String clientId) {- this.realm = realm;- this.clientId = clientId;- }-- @Override- public boolean equals(Object o) {- if (this == o) return true;- if (o == null || getClass() != o.getClass()) return false;-- Key that = (Key) o;-- if (!realm.equals(that.realm)) return false;- return clientId.equals(that.clientId);-- }-- @Override- public int hashCode() {- int result = realm.hashCode();- result = 31 * result + clientId.hashCode();- return result;- }- }-- final public ClientInstall clientInstall;- final public KeycloakDeployment keycloakDeployment;-- public ClientRealm(ClientInstall clientInstall, KeycloakDeployment keycloakDeployment) {- this.clientInstall = clientInstall;- this.keycloakDeployment = keycloakDeployment;- }--}diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.javaindex 29b72edea..8eaa3ccb7 100644--- a/container/src/main/java/org/openremote/container/security/IdentityService.java+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java@@ -29,12 +29,13 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;import org.keycloak.adapters.KeycloakConfigResolver;import org.keycloak.adapters.KeycloakDeployment;+import org.keycloak.adapters.KeycloakDeploymentBuilder;import org.keycloak.admin.client.resource.RealmsResource;import org.keycloak.admin.client.resource.ServerInfoResource;import org.keycloak.common.enums.SslRequired;-import org.keycloak.common.util.PemUtils;import org.keycloak.representations.adapters.config.AdapterConfig;import org.keycloak.representations.idm.ClientRepresentation;+import org.keycloak.representations.idm.PublishedRealmRepresentation;import org.keycloak.representations.idm.RealmRepresentation;import org.openremote.container.Container;import org.openremote.container.ContainerService;@@ -52,7 +53,6 @@ import javax.ws.rs.core.UriBuilder;import java.net.URI;import java.util.ArrayList;import java.util.List;-import java.util.Locale;import java.util.concurrent.TimeUnit;import java.util.logging.Level;import java.util.logging.Logger;@@ -102,8 +102,8 @@ public class IdentityService implements ContainerService {// The client we use to access Keycloakprotected Client httpClient;- // Cache realms so we don't have to access Keycloak for every token validation- protected LoadingCache<ClientRealm.Key, ClientRealm> clientApplicationCache;+ // 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 realmprotected KeycloakConfigResolver keycloakConfigResolver;@@ -172,7 +172,7 @@ public class IdentityService implements ContainerService {httpClient = WebClient.registerDefaults(container, clientBuilder).build();- clientApplicationCache = createClientApplicationCache();+ keycloakDeploymentCache = createKeycloakDeploymentCache();keycloakConfigResolver = request -> {// The realm we authenticate against must be available as a request header@@ -181,12 +181,12 @@ public class IdentityService implements ContainerService {LOG.fine("No realm in request, no authentication will be attempted: " + request.getURI());return notAuthenticatedKeycloakDeployment;}- ClientRealm clientRealm = getClientRealm(realm, getClientId());- if (clientRealm == null) {- LOG.fine("No client application configured for realm, no authentication will be attempted: " + request.getURI());+ KeycloakDeployment keycloakDeployment = getKeycloakDeployment(realm, getClientId());+ if (keycloakDeployment == null) {+ LOG.fine("No Keycloak deployment available for realm, no authentication will be attempted: " + request.getURI());return notAuthenticatedKeycloakDeployment;}- return clientRealm.keycloakDeployment;+ return keycloakDeployment;};authProxyHandler = new ProxyHandler(@@ -194,10 +194,6 @@ public class IdentityService implements ContainerService {getInteger(container.getConfig(), KEYCLOAK_REQUEST_TIMEOUT, KEYCLOAK_REQUEST_TIMEOUT_DEFAULT),ResponseCodeHandler.HANDLE_404);-- container.getService(WebService.class).getApiSingletons().add(- new IdentityResource(this)- );}public String getClientId() {@@ -289,9 +285,9 @@ public class IdentityService implements ContainerService {).toBlocking().singleOrDefault(false);}- public ClientRealm getClientRealm(String realm, String clientId) {+ public KeycloakDeployment getKeycloakDeployment(String realm, String clientId) {try {- return clientApplicationCache.get(new ClientRealm.Key(realm, clientId));+ return keycloakDeploymentCache.get(new KeycloakRealmClient(realm, clientId));} catch (Exception ex) {if (ex.getCause() != null && ex.getCause() instanceof NotFoundException) {LOG.fine("Client '" + clientId + "' for realm '" + realm + "' not found on identity provider");@@ -307,46 +303,27 @@ public class IdentityService implements ContainerService {}}- protected LoadingCache<ClientRealm.Key, ClientRealm> createClientApplicationCache() {- CacheLoader<ClientRealm.Key, ClientRealm> loader =- new CacheLoader<ClientRealm.Key, ClientRealm>() {- public ClientRealm load(ClientRealm.Key key) {- LOG.fine("Loading client '" + key.clientId + "' for realm '" + key.realm + "'");+ 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 + "'");- // The client install contains the details we need to verify access tokens- ClientInstall clientInstall = getKeycloak().getClientInstall(key.realm, key.clientId);+ AdapterConfig adapterConfig = getKeycloak().getAdapterConfig(+ keycloakRealmClient.realm, keycloakRealmClient.clientId+ );- // Make the public key usable for token verification- try {- clientInstall.setPublicKey(PemUtils.decodePublicKey(clientInstall.getPublicKeyPEM()));- } catch (Exception ex) {- throw new RuntimeException("Error decoding public key PEM for realm: " + clientInstall.getRealm(), ex);- }+ // Get the public key for token verification+ PublishedRealmRepresentation realmRepresentation = getKeycloak().getPublishedRealm(keycloakRealmClient.realm);+ adapterConfig.setRealmKey(realmRepresentation.getPublicKeyPem());// Must rewrite the auth-server URL to our external host and port, which// we reverse-proxy back to Keycloak. This is the issuer baked into a token.- clientInstall.setAuthServerUrl(externalAuthServerUri.toString());-- // Some more bloated infrastructure needed, this is for the Keycloak container adapter which- // does the automatic token verification. As you can see, it's duplicating ClientInstall.- // TODO: Some of the options should be configurable, e.g. CORS and NotBefore- KeycloakDeployment keycloakDeployment = new KeycloakDeployment();-- keycloakDeployment.setRealm(clientInstall.getRealm());- keycloakDeployment.setRealmKey(clientInstall.getPublicKey());- keycloakDeployment.setResourceName(clientInstall.getClientId());- keycloakDeployment.setUseResourceRoleMappings(true);- keycloakDeployment.setSslRequired(SslRequired.valueOf(clientInstall.getSslRequired().toUpperCase(Locale.ROOT)));- keycloakDeployment.setBearerOnly(true);-- AdapterConfig adapterConfig = new AdapterConfig();- adapterConfig.setResource(keycloakDeployment.getResourceName());- adapterConfig.setAuthServerUrl(clientInstall.getAuthServerUrl());- keycloakDeployment.setAuthServerBaseUrl(adapterConfig);-- return new ClientRealm(- clientInstall, keycloakDeployment- );+ adapterConfig.setAuthServerUrl(externalAuthServerUri.toString());++ // TODO: Some other options should be configurable, e.g. CORS and NotBefore++ return KeycloakDeploymentBuilder.build(adapterConfig);}};diff --git a/container/src/main/java/org/openremote/container/security/IdentityResource.java b/container/src/main/java/org/openremote/container/security/KeycloakRealmClient.javasimilarity index 52%rename from container/src/main/java/org/openremote/container/security/IdentityResource.javarename to container/src/main/java/org/openremote/container/security/KeycloakRealmClient.javaindex 88649fb45..bd6e194f4 100644--- a/container/src/main/java/org/openremote/container/security/IdentityResource.java+++ b/container/src/main/java/org/openremote/container/security/KeycloakRealmClient.java@@ -19,29 +19,33 @@*/package org.openremote.container.security;-import org.openremote.container.web.WebResource;+public class KeycloakRealmClient {-import javax.ws.rs.*;-import javax.ws.rs.core.MediaType;-import javax.ws.rs.core.Response;+ public final String realm;+ public final String clientId;-@Path("identity")-public class IdentityResource extends WebResource {+ public KeycloakRealmClient(String realm, String clientId) {+ this.realm = realm;+ this.clientId = clientId;+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (o == null || getClass() != o.getClass()) return false;- final protected IdentityService identityService;+ KeycloakRealmClient that = (KeycloakRealmClient) o;++ if (!realm.equals(that.realm)) return false;+ return clientId.equals(that.clientId);- public IdentityResource(IdentityService identityService) {- this.identityService = identityService;}- @GET- @Path("install/{clientId}")- @Produces(MediaType.APPLICATION_JSON)- public ClientInstall getClientInstall(@PathParam("clientId") String clientId) {- ClientRealm clientRealm =- identityService.getClientRealm(getRealm(), clientId);- if (clientRealm == null)- throw new WebApplicationException(Response.Status.NOT_FOUND);- return clientRealm.clientInstall;+ @Override+ public int hashCode() {+ int result = realm.hashCode();+ result = 31 * result + clientId.hashCode();+ return result;}+}diff --git a/container/src/main/java/org/openremote/container/security/KeycloakResource.java b/container/src/main/java/org/openremote/container/security/KeycloakResource.javaindex 58ccf8da7..3eb80b86d 100644--- a/container/src/main/java/org/openremote/container/security/KeycloakResource.java+++ b/container/src/main/java/org/openremote/container/security/KeycloakResource.java@@ -20,6 +20,8 @@package org.openremote.container.security;import org.keycloak.representations.AccessTokenResponse;+import org.keycloak.representations.adapters.config.AdapterConfig;+import org.keycloak.representations.idm.PublishedRealmRepresentation;import javax.ws.rs.*;import javax.ws.rs.core.Response;@@ -41,9 +43,14 @@ public interface KeycloakResource {@Produces(APPLICATION_JSON)AccessTokenResponse getAccessToken(@PathParam("realm") String realm, @BeanParam AuthForm authForm);+ @GET+ @Path("realms/{realm}")+ @Produces(APPLICATION_JSON)+ PublishedRealmRepresentation getPublishedRealm(@PathParam("realm") String realm);+@GET@Path("realms/{realm}/clients-registrations/install/{clientId}")@Produces(APPLICATION_JSON)- ClientInstall getClientInstall(@PathParam("realm") String realm, @PathParam("clientId") String clientId);+ AdapterConfig getAdapterConfig(@PathParam("realm") String realm, @PathParam("clientId") String clientId);}\ No newline at end of filediff --git a/gradle.properties b/gradle.propertiesindex 45cfdaf85..2b304c860 100644--- a/gradle.properties+++ b/gradle.properties@@ -10,7 +10,7 @@ guavaVersion = 19.0undertowVersion = 1.3.23.Final# TODO https://issues.jboss.org/browse/RESTEASY-1599resteasyVersion = 3.0.19.Final-keycloakVersion = 2.1.0.Final+keycloakVersion = 2.5.4.FinalgwtVersion = 2.8.0ginVersion = 2.1.2gwtJacksonVersion = 0.14.1diff --git a/keycloak/Dockerfile b/keycloak/Dockerfileindex 3263f71e3..8f5931cd9 100644--- a/keycloak/Dockerfile+++ b/keycloak/Dockerfile@@ -1,4 +1,4 @@-FROM jboss/keycloak:2.1.0.Final+FROM jboss/keycloak:2.5.4.FinalADD changeDatabase.xsl /opt/jboss/keycloak/RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml; java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml; rm /opt/jboss/keycloak/changeDatabase.xsl@@ -11,6 +11,8 @@ RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configADD theme /opt/jboss/keycloak/themes/openremote+RUN rm /opt/jboss/keycloak/*.xsl+USER rootADD wait-for-it.sh /opt/wait-for-it.shRUN chmod +x /opt/wait-for-it.shdiff --git a/keycloak/Dockerfile-dev b/keycloak/Dockerfile-devindex e56605fec..95c9b6df6 100644--- a/keycloak/Dockerfile-dev+++ b/keycloak/Dockerfile-dev@@ -1,4 +1,4 @@-FROM jboss/keycloak:2.1.0.Final+FROM jboss/keycloak:2.5.4.FinalADD changeDatabase.xsl /opt/jboss/keycloak/RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml; java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml -xsl:/opt/jboss/keycloak/changeDatabase.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone-ha.xml; rm /opt/jboss/keycloak/changeDatabase.xsl@@ -9,8 +9,10 @@ ADD module.xml /opt/jboss/keycloak/modules/system/layers/base/org/postgresql/jdbADD setProxyForwarding.xsl /opt/jboss/keycloak/RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/setProxyForwarding.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml-ADD keycloak-server-json-dev.jq /opt/jboss/keycloak/config.jq-RUN INPUT=`cat /opt/jboss/keycloak/standalone/configuration/keycloak-server.json` && echo $INPUT | jq -f /opt/jboss/keycloak/config.jq > /opt/jboss/keycloak/standalone/configuration/keycloak-server.json+ADD devModeConfig.xsl /opt/jboss/keycloak/+RUN java -jar /usr/share/java/saxon.jar -s:/opt/jboss/keycloak/standalone/configuration/standalone.xml -xsl:/opt/jboss/keycloak/devModeConfig.xsl -o:/opt/jboss/keycloak/standalone/configuration/standalone.xml++RUN rm /opt/jboss/keycloak/*.xslUSER rootADD wait-for-it.sh /opt/wait-for-it.shdiff --git a/keycloak/devModeConfig.xsl b/keycloak/devModeConfig.xslnew file mode 100644index 000000000..b3ebd2dce--- /dev/null+++ b/keycloak/devModeConfig.xsl@@ -0,0 +1,29 @@+<?xml version="1.0" encoding="utf-8"?>+<xsl:stylesheet version="2.0"+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"+ xmlns:keycloak="urn:jboss:domain:keycloak-server:1.1">++ <xsl:output method="xml" indent="yes"/>++ <xsl:template match="//keycloak:staticMaxAge/text()">-1</xsl:template>+ <xsl:template match="//keycloak:cacheThemes/text()">false</xsl:template>+ <xsl:template match="//keycloak:cacheTemplates/text()">false</xsl:template>++<!--+ <staticMaxAge>2592000</staticMaxAge>+ <cacheThemes>true</cacheThemes>+ <cacheTemplates>true</cacheTemplates>+ <dir>${jboss.home.dir}/themes</dir>++.theme.staticMaxAge |= -1 |+.theme.cacheTemplates |=false |+.theme.cacheThemes |= false+-->++ <xsl:template match="@*|node()">+ <xsl:copy>+ <xsl:apply-templates select="@*|node()"/>+ </xsl:copy>+ </xsl:template>++</xsl:stylesheet>\ No newline at end of filediff --git a/keycloak/keycloak-server-json-dev.jq b/keycloak/keycloak-server-json-dev.jqdeleted file mode 100644index 9c0e8b4df..000000000--- a/keycloak/keycloak-server-json-dev.jq+++ /dev/null@@ -1,3 +0,0 @@-.theme.staticMaxAge |= -1 |-.theme.cacheTemplates |=false |-.theme.cacheThemes |= false\ No newline at end of file