From 7ea1e3840d5d9466936184a6cd94e8b1179088ef Mon Sep 17 00:00:00 2001 From: Dimantchick Date: Sat, 2 Sep 2023 11:58:37 +0700 Subject: [PATCH 1/2] Fix keycloak-server.json don't use ENV vars in docker container --- .../auth/config/RegularJsonConfigProviderFactory.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java index 1e955fc38..a4081588b 100644 --- a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java +++ b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java @@ -3,5 +3,8 @@ import org.keycloak.services.util.JsonConfigProviderFactory; public class RegularJsonConfigProviderFactory extends JsonConfigProviderFactory { - + @Override + protected Properties getProperties() { + return new SystemEnvProperties(System.getenv()); + } } From 48fbaec3fb1c888a3cd7e87e93e7605ca556fe88 Mon Sep 17 00:00:00 2001 From: "dmitrii.reshetinskii" Date: Mon, 11 Sep 2023 17:28:55 +0700 Subject: [PATCH 2/2] ENV variables support in realm.json. Realm not importing if already exists (if use external DB). --- .../config/EmbeddedKeycloakApplication.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java index 28adb912b..2a5850ccc 100644 --- a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java +++ b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java @@ -1,10 +1,14 @@ package com.baeldung.auth.config; +import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; import org.keycloak.Config; +import org.keycloak.common.util.StringPropertyReplacer; +import org.keycloak.common.util.SystemEnvProperties; import org.keycloak.exportimport.ExportImportManager; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.RealmModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.RealmManager; @@ -65,12 +69,18 @@ private void createBaeldungRealm() { session.getTransactionManager().begin(); RealmManager manager = new RealmManager(session); - Resource lessonRealmImportFile = new ClassPathResource(keycloakServerProperties.getRealmImportFile()); - - manager.importRealm( - JsonSerialization.readValue(lessonRealmImportFile.getInputStream(), RealmRepresentation.class)); - - session.getTransactionManager().commit(); + RealmModel baeldung = manager.getRealmByName("baeldung"); + if (baeldung == null) { // maybe already exist realm if using external DB, skip importing. + Resource lessonRealmImportFile = new ClassPathResource(keycloakServerProperties.getRealmImportFile()); + // Read realm to string + String stringRealm = lessonRealmImportFile.getContentAsString(StandardCharsets.UTF_8); + // replace ${name} to ENV values in realm + String realmWithEnv = StringPropertyReplacer.replaceProperties(stringRealm, new SystemEnvProperties(System.getenv())); + RealmRepresentation realmRepresentation = JsonSerialization.readValue(realmWithEnv, RealmRepresentation.class); + manager.importRealm( + realmRepresentation); + session.getTransactionManager().commit(); + } } catch (Exception ex) { LOG.warn("Failed to import Realm json file: {}", ex.getMessage()); session.getTransactionManager().rollback();