Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keycloak-server.json don't use ENV vars in docker container #430

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import org.keycloak.services.util.JsonConfigProviderFactory;

public class RegularJsonConfigProviderFactory extends JsonConfigProviderFactory {

@Override
protected Properties getProperties() {
return new SystemEnvProperties(System.getenv());
}
}