KEYCLOAK-12414 Remove the need to specify defaults in config file

This commit is contained in:
stianst 2020-05-12 15:42:02 +02:00 committed by Pedro Igor
parent 35f622f48e
commit b04932ede5
11 changed files with 73 additions and 183 deletions

View file

@ -88,7 +88,8 @@ public class Config {
@Override @Override
public String get(String key, String defaultValue) { public String get(String key, String defaultValue) {
return System.getProperty(prefix + key, defaultValue); String v = System.getProperty(prefix + key, defaultValue);
return v != null && !v.isEmpty() ? v : null;
} }
@Override @Override

View file

@ -52,4 +52,9 @@ public class JpaUserSessionPersisterProviderFactory implements UserSessionPersis
public String getId() { public String getId() {
return ID; return ID;
} }
@Override
public int order() {
return 100;
}
} }

View file

@ -111,7 +111,11 @@ public class QuarkusJpaConnectionProviderFactory implements JpaConnectionProvide
public void postInit(KeycloakSessionFactory factory) { public void postInit(KeycloakSessionFactory factory) {
this.factory = factory; this.factory = factory;
checkJtaEnabled(factory); checkJtaEnabled(factory);
}
@Override
public int order() {
return 100;
} }
protected void checkJtaEnabled(KeycloakSessionFactory factory) { protected void checkJtaEnabled(KeycloakSessionFactory factory) {

View file

@ -53,4 +53,9 @@ public class QuarkusJtaTransactionManagerLookup implements JtaTransactionManager
public String getId() { public String getId() {
return "quarkus"; return "quarkus";
} }
@Override
public int order() {
return 100;
}
} }

View file

@ -1,45 +1,8 @@
# Main
admin.realm = master
scheduled.interval = 900
# Theme
theme.staticMaxAge = 2592000
theme.cacheThemes = true
theme.cacheTemplates = true
#theme.dir = ${keycloak.home.dir}/themes
# Datasource # Datasource
datasource.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 datasource.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
datasource.driver = org.h2.Driver datasource.driver = org.h2.Driver
datasource.username = sa datasource.username = sa
datasource.password = keycloak datasource.password = keycloak
# SPIs
eventsListener.jboss-logging.success-level = debug
eventsListener.jboss-logging.error-level = warn
connectionsJpa.provider = quarkus
connectionsJpa.quarkus.initializeEmpty = true
connectionsJpa.quarkus.migrationStrategy = update
connectionsJpa.quarkus.showSql = false
connectionsJpa.quarkus.formatSql = true
connectionsJpa.quarkus.globalStatsInterval = -1
eventsStore.provider=jpa
realm.provider=jpa
user.provider=jpa
userFederatedStorage.provider=jpa
userSessionPersister.provider=jpa
authorizationPersister.provider=jpa
userCache.enabled=true
timer.provider=basic
hostname.provider = default
hostname.default.frontendUrl = ${keycloak.frontendUrl:} hostname.default.frontendUrl = ${keycloak.frontendUrl:}
hostname.default.forceBackendUrlToFrontendUrl = false
jta-lookup.provider = quarkus

View file

@ -34,9 +34,4 @@ public interface HostnameProviderFactory extends ProviderFactory<HostnameProvide
default void postInit(KeycloakSessionFactory factory) { default void postInit(KeycloakSessionFactory factory) {
} }
@Override
default int order() {
return 0;
}
} }

View file

@ -34,11 +34,13 @@ import org.keycloak.provider.Spi;
import org.keycloak.services.resources.admin.permissions.AdminPermissions; import org.keycloak.services.resources.admin.permissions.AdminPermissions;
import org.keycloak.theme.DefaultThemeManagerFactory; import org.keycloak.theme.DefaultThemeManagerFactory;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -181,18 +183,34 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory, Pr
protected void checkProvider() { protected void checkProvider() {
for (Spi spi : spis) { for (Spi spi : spis) {
String provider = Config.getProvider(spi.getName()); String defaultProvider = Config.getProvider(spi.getName());
if (provider != null) { if (defaultProvider != null) {
this.provider.put(spi.getProviderClass(), provider); if (getProviderFactory(spi.getProviderClass(), defaultProvider) == null) {
if (getProviderFactory(spi.getProviderClass(), provider) == null) {
throw new RuntimeException("Failed to find provider " + provider + " for " + spi.getName()); throw new RuntimeException("Failed to find provider " + provider + " for " + spi.getName());
} }
} else { } else {
Map<String, ProviderFactory> factories = factoriesMap.get(spi.getProviderClass()); Map<String, ProviderFactory> factories = factoriesMap.get(spi.getProviderClass());
if (factories != null && factories.size() == 1) { if (factories != null && factories.size() == 1) {
provider = factories.values().iterator().next().getId(); defaultProvider = factories.values().iterator().next().getId();
this.provider.put(spi.getProviderClass(), provider);
} }
if (defaultProvider == null) {
Optional<ProviderFactory> highestPriority = factories.values().stream().max(Comparator.comparing(ProviderFactory::order));
if (highestPriority.isPresent() && highestPriority.get().order() > 0) {
defaultProvider = highestPriority.get().getId();
}
}
if (defaultProvider == null && factories.containsKey("default")) {
defaultProvider = "default";
}
}
if (defaultProvider != null) {
this.provider.put(spi.getProviderClass(), defaultProvider);
logger.debugv("Set default provider for {0} to {1}", spi.getName(), defaultProvider);
} else {
logger.debugv("No default provider for {0}", spi.getName());
} }
} }
} }
@ -257,69 +275,11 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory, Pr
return true; return true;
} }
protected void loadSPIs(ProviderManager pm, List<Spi> spiList) {
for (Spi spi : spiList) {
spis.add(spi);
Map<String, ProviderFactory> factories = new HashMap<String, ProviderFactory>();
factoriesMap.put(spi.getProviderClass(), factories);
String provider = Config.getProvider(spi.getName());
if (provider != null) {
this.provider.put(spi.getProviderClass(), provider);
ProviderFactory factory = pm.load(spi, provider);
if (factory == null) {
throw new RuntimeException("Failed to find provider " + provider + " for " + spi.getName());
}
Config.Scope scope = Config.scope(spi.getName(), provider);
factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) {
ServicesLogger.LOGGER.spiMayChange(factory.getId(), factory.getClass().getName(), spi.getName());
}
factories.put(factory.getId(), factory);
logger.debugv("Loaded SPI {0} (provider = {1})", spi.getName(), provider);
} else {
for (ProviderFactory factory : pm.load(spi)) {
Config.Scope scope = Config.scope(spi.getName(), factory.getId());
if (scope.getBoolean("enabled", true)) {
factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) {
ServicesLogger.LOGGER.spiMayChange(factory.getId(), factory.getClass().getName(), spi.getName());
}
factories.put(factory.getId(), factory);
} else {
logger.debugv("SPI {0} provider {1} disabled", spi.getName(), factory.getId());
}
}
if (factories.size() == 1) {
provider = factories.values().iterator().next().getId();
this.provider.put(spi.getProviderClass(), provider);
logger.debugv("Loaded SPI {0} (provider = {1})", spi.getName(), provider);
} else {
logger.debugv("Loaded SPI {0} (providers = {1})", spi.getName(), factories.keySet());
}
}
}
}
public KeycloakSession create() { public KeycloakSession create() {
KeycloakSession session = new DefaultKeycloakSession(this); KeycloakSession session = new DefaultKeycloakSession(this);
return session; return session;
} }
<T extends Provider> String getDefaultProvider(Class<T> clazz) {
return provider.get(clazz);
}
@Override @Override
public Set<Spi> getSpis() { public Set<Spi> getSpis() {
return spis; return spis;

View file

@ -268,7 +268,7 @@ public class KeycloakApplication extends Application {
} }
public static void setupScheduledTasks(final KeycloakSessionFactory sessionFactory) { public static void setupScheduledTasks(final KeycloakSessionFactory sessionFactory) {
long interval = Config.scope("scheduled").getLong("interval", 60L) * 1000; long interval = Config.scope("scheduled").getLong("interval", 900L) * 1000;
KeycloakSession session = sessionFactory.create(); KeycloakSession session = sessionFactory.create();
try { try {

View file

@ -36,7 +36,7 @@ public class CacheControlUtil {
public static CacheControl getDefaultCacheControl() { public static CacheControl getDefaultCacheControl() {
CacheControl cacheControl = new CacheControl(); CacheControl cacheControl = new CacheControl();
cacheControl.setNoTransform(false); cacheControl.setNoTransform(false);
Integer maxAge = Config.scope("theme").getInt("staticMaxAge"); Integer maxAge = Config.scope("theme").getInt("staticMaxAge", 2592000);
if (maxAge != null && maxAge > 0) { if (maxAge != null && maxAge > 0) {
cacheControl.setMaxAge(maxAge); cacheControl.setMaxAge(maxAge);
} else { } else {

View file

@ -88,7 +88,8 @@ public class JsonConfigProvider implements Config.ConfigProvider {
if (n == null) { if (n == null) {
return defaultValue; return defaultValue;
} }
return replaceProperties(n.textValue()); String v = replaceProperties(n.textValue());
return !v.isEmpty() ? v : defaultValue;
} }
@Override @Override
@ -126,7 +127,8 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue; return defaultValue;
} }
if (n.isTextual()) { if (n.isTextual()) {
return Integer.parseInt(replaceProperties(n.textValue())); String v = replaceProperties(n.textValue());
return !v.isEmpty() ? Integer.parseInt(v) : defaultValue;
} else { } else {
return n.intValue(); return n.intValue();
} }
@ -147,7 +149,8 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue; return defaultValue;
} }
if (n.isTextual()) { if (n.isTextual()) {
return Long.parseLong(replaceProperties(n.textValue())); String v = replaceProperties(n.textValue());
return !v.isEmpty() ? Long.parseLong(v) : defaultValue;
} else { } else {
return n.longValue(); return n.longValue();
} }
@ -168,7 +171,8 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue; return defaultValue;
} }
if (n.isTextual()) { if (n.isTextual()) {
return Boolean.parseBoolean(replaceProperties(n.textValue())); String v = replaceProperties(n.textValue());
return !v.isEmpty() ? Boolean.parseBoolean(v) : defaultValue;
} else { } else {
return n.booleanValue(); return n.booleanValue();
} }

View file

@ -1,77 +1,48 @@
{ {
"hostname": { "hostname": {
"provider": "${keycloak.hostname.provider:default}", "provider": "${keycloak.hostname.provider:}",
"default": { "default": {
"frontendUrl": "${keycloak.frontendUrl:}", "frontendUrl": "${keycloak.frontendUrl:}",
"adminUrl": "${keycloak.adminUrl:}", "adminUrl": "${keycloak.adminUrl:}",
"forceBackendUrlToFrontendUrl": "${keycloak.hostname.default.forceBackendUrlToFrontendUrl:false}" "forceBackendUrlToFrontendUrl": "${keycloak.hostname.default.forceBackendUrlToFrontendUrl:}"
} }
}, },
"admin": {
"realm": "master"
},
"eventsStore": { "eventsStore": {
"provider": "${keycloak.eventsStore.provider:jpa}" "provider": "${keycloak.eventsStore.provider:}"
},
"eventsListener": {
"jboss-logging" : {
"success-level": "debug",
"error-level": "warn"
}
}, },
"realm": { "realm": {
"provider": "${keycloak.realm.provider:jpa}" "provider": "${keycloak.realm.provider:}"
}, },
"user": { "user": {
"provider": "${keycloak.user.provider:jpa}" "provider": "${keycloak.user.provider:}"
}, },
"userFederatedStorage": { "userFederatedStorage": {
"provider": "${keycloak.userFederatedStorage.provider:jpa}" "provider": "${keycloak.userFederatedStorage.provider:}"
}, },
"userSessionPersister": { "userSessionPersister": {
"provider": "${keycloak.userSessionPersister.provider:jpa}" "provider": "${keycloak.userSessionPersister.provider:}"
}, },
"authorizationPersister": { "authorizationPersister": {
"provider": "${keycloak.authorization.provider:jpa}" "provider": "${keycloak.authorization.provider:}"
},
"userCache": {
"default" : {
"enabled": true
}
},
"timer": {
"provider": "basic"
}, },
"theme": { "theme": {
"staticMaxAge": "${keycloak.theme.staticMaxAge:2592000}", "staticMaxAge": "${keycloak.theme.staticMaxAge:}",
"cacheTemplates": "${keycloak.theme.cacheTemplates:true}", "cacheTemplates": "${keycloak.theme.cacheTemplates:}",
"cacheThemes": "${keycloak.theme.cacheThemes:true}", "cacheThemes": "${keycloak.theme.cacheThemes:}",
"folder": { "folder": {
"dir": "${keycloak.theme.dir}" "dir": "${keycloak.theme.dir}"
} }
}, },
"scheduled": {
"interval": 900
},
"connectionsHttpClient": {
"default": {}
},
"connectionsJpa": { "connectionsJpa": {
"default": { "default": {
"url": "${keycloak.connectionsJpa.url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1}", "url": "${keycloak.connectionsJpa.url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1}",
@ -79,17 +50,9 @@
"driverDialect": "${keycloak.connectionsJpa.driverDialect:}", "driverDialect": "${keycloak.connectionsJpa.driverDialect:}",
"user": "${keycloak.connectionsJpa.user:sa}", "user": "${keycloak.connectionsJpa.user:sa}",
"password": "${keycloak.connectionsJpa.password:}", "password": "${keycloak.connectionsJpa.password:}",
"initializeEmpty": true, "showSql": "${keycloak.connectionsJpa.showSql:}",
"migrationStrategy": "update", "formatSql": "${keycloak.connectionsJpa.formatSql:}",
"showSql": "${keycloak.connectionsJpa.showSql:false}", "globalStatsInterval": "${keycloak.connectionsJpa.globalStatsInterval:}"
"formatSql": "${keycloak.connectionsJpa.formatSql:true}",
"globalStatsInterval": "${keycloak.connectionsJpa.globalStatsInterval:-1}"
}
},
"realmCache": {
"default" : {
"enabled": true
} }
}, },
@ -98,13 +61,13 @@
"jgroupsUdpMcastAddr": "${keycloak.connectionsInfinispan.jgroupsUdpMcastAddr:234.56.78.90}", "jgroupsUdpMcastAddr": "${keycloak.connectionsInfinispan.jgroupsUdpMcastAddr:234.56.78.90}",
"nodeName": "${keycloak.connectionsInfinispan.nodeName,jboss.node.name:}", "nodeName": "${keycloak.connectionsInfinispan.nodeName,jboss.node.name:}",
"siteName": "${keycloak.connectionsInfinispan.siteName,jboss.site.name:}", "siteName": "${keycloak.connectionsInfinispan.siteName,jboss.site.name:}",
"clustered": "${keycloak.connectionsInfinispan.clustered:false}", "clustered": "${keycloak.connectionsInfinispan.clustered:}",
"async": "${keycloak.connectionsInfinispan.async:false}", "async": "${keycloak.connectionsInfinispan.async:}",
"sessionsOwners": "${keycloak.connectionsInfinispan.sessionsOwners:1}", "sessionsOwners": "${keycloak.connectionsInfinispan.sessionsOwners:}",
"l1Lifespan": "${keycloak.connectionsInfinispan.l1Lifespan:600000}", "l1Lifespan": "${keycloak.connectionsInfinispan.l1Lifespan:}",
"remoteStoreEnabled": "${keycloak.connectionsInfinispan.remoteStoreEnabled:false}", "remoteStoreEnabled": "${keycloak.connectionsInfinispan.remoteStoreEnabled:}",
"remoteStoreHost": "${keycloak.connectionsInfinispan.remoteStoreServer:localhost}", "remoteStoreHost": "${keycloak.connectionsInfinispan.remoteStoreServer:}",
"remoteStorePort": "${keycloak.connectionsInfinispan.remoteStorePort:11222}", "remoteStorePort": "${keycloak.connectionsInfinispan.remoteStorePort:}",
"hotrodProtocolVersion": "${keycloak.connectionsInfinispan.hotrodProtocolVersion}", "hotrodProtocolVersion": "${keycloak.connectionsInfinispan.hotrodProtocolVersion}",
"embedded": "${keycloak.connectionsInfinispan.embedded:true}" "embedded": "${keycloak.connectionsInfinispan.embedded:true}"
} }
@ -114,11 +77,7 @@
}, },
"jta-lookup": { "jta-lookup": {
"provider": "${keycloak.jta.lookup.provider:jboss}", "provider": "${keycloak.jta.lookup.provider:}"
"jboss" : {
"enabled": true
}
}, },
"login-protocol": { "login-protocol": {
@ -131,24 +90,18 @@
}, },
"x509cert-lookup": { "x509cert-lookup": {
"provider": "${keycloak.x509cert.lookup.provider:default}", "provider": "${keycloak.x509cert.lookup.provider:}",
"default": {
"enabled": true
},
"haproxy": { "haproxy": {
"enabled": true,
"sslClientCert": "x-ssl-client-cert", "sslClientCert": "x-ssl-client-cert",
"sslCertChainPrefix": "x-ssl-client-cert-chain", "sslCertChainPrefix": "x-ssl-client-cert-chain",
"certificateChainLength": 1 "certificateChainLength": 1
}, },
"apache": { "apache": {
"enabled": true,
"sslClientCert": "x-ssl-client-cert", "sslClientCert": "x-ssl-client-cert",
"sslCertChainPrefix": "x-ssl-client-cert-chain", "sslCertChainPrefix": "x-ssl-client-cert-chain",
"certificateChainLength": 1 "certificateChainLength": 1
}, },
"nginx": { "nginx": {
"enabled": true,
"sslClientCert": "x-ssl-client-cert", "sslClientCert": "x-ssl-client-cert",
"sslCertChainPrefix": "x-ssl-client-cert-chain", "sslCertChainPrefix": "x-ssl-client-cert-chain",
"certificateChainLength": 1 "certificateChainLength": 1