KEYCLOAK-1289 Support environment variables in keycloak.json
This commit is contained in:
1 parent
f3f4b8dee7
commit
a0368908aa
3 files changed
+32
-10
No files matched your search
@@ -0,0 +1,25 @@
|
|||||||
|
package org.keycloak.util;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||||
|
*/
|
||||||
|
public class SystemEnvProperties extends Properties {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getProperty(String key) {
|
||||||
|
if (key.startsWith("env.")) {
|
||||||
|
return System.getenv().get(key.substring(4));
|
||||||
|
} else {
|
||||||
|
return System.getProperty(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getProperty(String key, String defaultValue) {
|
||||||
|
String value = getProperty(key);
|
||||||
|
return value != null ? value : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ package org.keycloak.util;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.codehaus.jackson.JsonParser;
|
import org.codehaus.jackson.JsonParser;
|
||||||
import org.codehaus.jackson.io.IOContext;
|
import org.codehaus.jackson.io.IOContext;
|
||||||
@@ -16,6 +18,8 @@ import org.codehaus.jackson.util.JsonParserDelegate;
|
|||||||
*/
|
*/
|
||||||
public class SystemPropertiesJsonParserFactory extends MappingJsonFactory {
|
public class SystemPropertiesJsonParserFactory extends MappingJsonFactory {
|
||||||
|
|
||||||
|
private static final Properties properties = new SystemEnvProperties();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
|
protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
|
||||||
JsonParser delegate = super._createJsonParser(data, offset, len, ctxt);
|
JsonParser delegate = super._createJsonParser(data, offset, len, ctxt);
|
||||||
@@ -34,8 +38,6 @@ public class SystemPropertiesJsonParserFactory extends MappingJsonFactory {
|
|||||||
return new SystemPropertiesAwareJsonParser(delegate);
|
return new SystemPropertiesAwareJsonParser(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class SystemPropertiesAwareJsonParser extends JsonParserDelegate {
|
public static class SystemPropertiesAwareJsonParser extends JsonParserDelegate {
|
||||||
|
|
||||||
public SystemPropertiesAwareJsonParser(JsonParser d) {
|
public SystemPropertiesAwareJsonParser(JsonParser d) {
|
||||||
@@ -45,7 +47,7 @@ public class SystemPropertiesJsonParserFactory extends MappingJsonFactory {
|
|||||||
@Override
|
@Override
|
||||||
public String getText() throws IOException {
|
public String getText() throws IOException {
|
||||||
String orig = super.getText();
|
String orig = super.getText();
|
||||||
return StringPropertyReplacer.replaceProperties(orig);
|
return StringPropertyReplacer.replaceProperties(orig, properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@ import org.keycloak.services.scheduled.ScheduledTaskRunner;
|
|||||||
import org.keycloak.services.util.JsonConfigProvider;
|
import org.keycloak.services.util.JsonConfigProvider;
|
||||||
import org.keycloak.timer.TimerProvider;
|
import org.keycloak.timer.TimerProvider;
|
||||||
import org.keycloak.util.JsonSerialization;
|
import org.keycloak.util.JsonSerialization;
|
||||||
|
import org.keycloak.util.SystemEnvProperties;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.ws.rs.core.Application;
|
import javax.ws.rs.core.Application;
|
||||||
@@ -139,14 +140,8 @@ public class KeycloakApplication extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new SystemEnvProperties();
|
||||||
properties.putAll(System.getProperties());
|
|
||||||
for(Map.Entry<String, String> e : System.getenv().entrySet()) {
|
|
||||||
properties.put("env." + e.getKey(), e.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
Config.init(new JsonConfigProvider(node, properties));
|
Config.init(new JsonConfigProvider(node, properties));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
log.warn("Config 'keycloak-server.json' not found");
|
log.warn("Config 'keycloak-server.json' not found");
|
||||||
|
|||||||
Reference in new issue
Block a user