From b04932ede56993d36de70768c41d4c946c4c79e0 Mon Sep 17 00:00:00 2001 From: stianst Date: Tue, 12 May 2020 15:42:02 +0200 Subject: [PATCH] KEYCLOAK-12414 Remove the need to specify defaults in config file --- core/src/main/java/org/keycloak/Config.java | 3 +- ...paUserSessionPersisterProviderFactory.java | 5 + .../QuarkusJpaConnectionProviderFactory.java | 4 + .../QuarkusJtaTransactionManagerLookup.java | 5 + .../resources/META-INF/keycloak.properties | 37 -------- .../urls/HostnameProviderFactory.java | 5 - .../DefaultKeycloakSessionFactory.java | 88 +++++------------- .../resources/KeycloakApplication.java | 2 +- .../services/util/CacheControlUtil.java | 2 +- .../services/util/JsonConfigProvider.java | 12 ++- .../resources/META-INF/keycloak-server.json | 93 +++++-------------- 11 files changed, 73 insertions(+), 183 deletions(-) diff --git a/core/src/main/java/org/keycloak/Config.java b/core/src/main/java/org/keycloak/Config.java index 06240065a5..d0edf4516e 100755 --- a/core/src/main/java/org/keycloak/Config.java +++ b/core/src/main/java/org/keycloak/Config.java @@ -88,7 +88,8 @@ public class Config { @Override 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 diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProviderFactory.java b/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProviderFactory.java index e12223df35..5613b2da7d 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProviderFactory.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProviderFactory.java @@ -52,4 +52,9 @@ public class JpaUserSessionPersisterProviderFactory implements UserSessionPersis public String getId() { return ID; } + + @Override + public int order() { + return 100; + } } diff --git a/quarkus/extensions/src/main/java/org/keycloak/connections/jpa/QuarkusJpaConnectionProviderFactory.java b/quarkus/extensions/src/main/java/org/keycloak/connections/jpa/QuarkusJpaConnectionProviderFactory.java index 7a912dbb53..17d0bc7d8e 100644 --- a/quarkus/extensions/src/main/java/org/keycloak/connections/jpa/QuarkusJpaConnectionProviderFactory.java +++ b/quarkus/extensions/src/main/java/org/keycloak/connections/jpa/QuarkusJpaConnectionProviderFactory.java @@ -111,7 +111,11 @@ public class QuarkusJpaConnectionProviderFactory implements JpaConnectionProvide public void postInit(KeycloakSessionFactory factory) { this.factory = factory; checkJtaEnabled(factory); + } + @Override + public int order() { + return 100; } protected void checkJtaEnabled(KeycloakSessionFactory factory) { diff --git a/quarkus/extensions/src/main/java/org/keycloak/transaction/QuarkusJtaTransactionManagerLookup.java b/quarkus/extensions/src/main/java/org/keycloak/transaction/QuarkusJtaTransactionManagerLookup.java index b9714b7747..9eb6fbd2b8 100644 --- a/quarkus/extensions/src/main/java/org/keycloak/transaction/QuarkusJtaTransactionManagerLookup.java +++ b/quarkus/extensions/src/main/java/org/keycloak/transaction/QuarkusJtaTransactionManagerLookup.java @@ -53,4 +53,9 @@ public class QuarkusJtaTransactionManagerLookup implements JtaTransactionManager public String getId() { return "quarkus"; } + + @Override + public int order() { + return 100; + } } diff --git a/quarkus/server/src/main/resources/META-INF/keycloak.properties b/quarkus/server/src/main/resources/META-INF/keycloak.properties index c91fbb9b34..282a6372fd 100644 --- a/quarkus/server/src/main/resources/META-INF/keycloak.properties +++ b/quarkus/server/src/main/resources/META-INF/keycloak.properties @@ -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.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 datasource.driver = org.h2.Driver datasource.username = sa 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.forceBackendUrlToFrontendUrl = false -jta-lookup.provider = quarkus diff --git a/server-spi/src/main/java/org/keycloak/urls/HostnameProviderFactory.java b/server-spi/src/main/java/org/keycloak/urls/HostnameProviderFactory.java index 73b4a2d6f2..954f69edca 100644 --- a/server-spi/src/main/java/org/keycloak/urls/HostnameProviderFactory.java +++ b/server-spi/src/main/java/org/keycloak/urls/HostnameProviderFactory.java @@ -34,9 +34,4 @@ public interface HostnameProviderFactory extends ProviderFactory factories = factoriesMap.get(spi.getProviderClass()); if (factories != null && factories.size() == 1) { - provider = factories.values().iterator().next().getId(); - this.provider.put(spi.getProviderClass(), provider); + defaultProvider = factories.values().iterator().next().getId(); } + + if (defaultProvider == null) { + Optional 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; } - protected void loadSPIs(ProviderManager pm, List spiList) { - for (Spi spi : spiList) { - spis.add(spi); - - Map factories = new HashMap(); - 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() { KeycloakSession session = new DefaultKeycloakSession(this); return session; } - String getDefaultProvider(Class clazz) { - return provider.get(clazz); - } - @Override public Set getSpis() { return spis; diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java index 2913e69636..f0e8570b76 100644 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -268,7 +268,7 @@ public class KeycloakApplication extends Application { } 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(); try { diff --git a/services/src/main/java/org/keycloak/services/util/CacheControlUtil.java b/services/src/main/java/org/keycloak/services/util/CacheControlUtil.java index b32fce76e1..e9bfb54744 100755 --- a/services/src/main/java/org/keycloak/services/util/CacheControlUtil.java +++ b/services/src/main/java/org/keycloak/services/util/CacheControlUtil.java @@ -36,7 +36,7 @@ public class CacheControlUtil { public static CacheControl getDefaultCacheControl() { CacheControl cacheControl = new CacheControl(); cacheControl.setNoTransform(false); - Integer maxAge = Config.scope("theme").getInt("staticMaxAge"); + Integer maxAge = Config.scope("theme").getInt("staticMaxAge", 2592000); if (maxAge != null && maxAge > 0) { cacheControl.setMaxAge(maxAge); } else { diff --git a/services/src/main/java/org/keycloak/services/util/JsonConfigProvider.java b/services/src/main/java/org/keycloak/services/util/JsonConfigProvider.java index 0550d5390f..bd3335f955 100755 --- a/services/src/main/java/org/keycloak/services/util/JsonConfigProvider.java +++ b/services/src/main/java/org/keycloak/services/util/JsonConfigProvider.java @@ -88,7 +88,8 @@ public class JsonConfigProvider implements Config.ConfigProvider { if (n == null) { return defaultValue; } - return replaceProperties(n.textValue()); + String v = replaceProperties(n.textValue()); + return !v.isEmpty() ? v : defaultValue; } @Override @@ -126,7 +127,8 @@ public class JsonConfigProvider implements Config.ConfigProvider { return defaultValue; } if (n.isTextual()) { - return Integer.parseInt(replaceProperties(n.textValue())); + String v = replaceProperties(n.textValue()); + return !v.isEmpty() ? Integer.parseInt(v) : defaultValue; } else { return n.intValue(); } @@ -147,7 +149,8 @@ public class JsonConfigProvider implements Config.ConfigProvider { return defaultValue; } if (n.isTextual()) { - return Long.parseLong(replaceProperties(n.textValue())); + String v = replaceProperties(n.textValue()); + return !v.isEmpty() ? Long.parseLong(v) : defaultValue; } else { return n.longValue(); } @@ -168,7 +171,8 @@ public class JsonConfigProvider implements Config.ConfigProvider { return defaultValue; } if (n.isTextual()) { - return Boolean.parseBoolean(replaceProperties(n.textValue())); + String v = replaceProperties(n.textValue()); + return !v.isEmpty() ? Boolean.parseBoolean(v) : defaultValue; } else { return n.booleanValue(); } diff --git a/testsuite/utils/src/main/resources/META-INF/keycloak-server.json b/testsuite/utils/src/main/resources/META-INF/keycloak-server.json index f84c7d0c0e..91e50672cb 100755 --- a/testsuite/utils/src/main/resources/META-INF/keycloak-server.json +++ b/testsuite/utils/src/main/resources/META-INF/keycloak-server.json @@ -1,77 +1,48 @@ { "hostname": { - "provider": "${keycloak.hostname.provider:default}", + "provider": "${keycloak.hostname.provider:}", "default": { "frontendUrl": "${keycloak.frontendUrl:}", "adminUrl": "${keycloak.adminUrl:}", - "forceBackendUrlToFrontendUrl": "${keycloak.hostname.default.forceBackendUrlToFrontendUrl:false}" + "forceBackendUrlToFrontendUrl": "${keycloak.hostname.default.forceBackendUrlToFrontendUrl:}" } }, - "admin": { - "realm": "master" - }, - "eventsStore": { - "provider": "${keycloak.eventsStore.provider:jpa}" - }, - - "eventsListener": { - "jboss-logging" : { - "success-level": "debug", - "error-level": "warn" - } + "provider": "${keycloak.eventsStore.provider:}" }, "realm": { - "provider": "${keycloak.realm.provider:jpa}" + "provider": "${keycloak.realm.provider:}" }, "user": { - "provider": "${keycloak.user.provider:jpa}" + "provider": "${keycloak.user.provider:}" }, "userFederatedStorage": { - "provider": "${keycloak.userFederatedStorage.provider:jpa}" + "provider": "${keycloak.userFederatedStorage.provider:}" }, "userSessionPersister": { - "provider": "${keycloak.userSessionPersister.provider:jpa}" + "provider": "${keycloak.userSessionPersister.provider:}" }, "authorizationPersister": { - "provider": "${keycloak.authorization.provider:jpa}" - }, - - "userCache": { - "default" : { - "enabled": true - } - }, - - "timer": { - "provider": "basic" + "provider": "${keycloak.authorization.provider:}" }, "theme": { - "staticMaxAge": "${keycloak.theme.staticMaxAge:2592000}", - "cacheTemplates": "${keycloak.theme.cacheTemplates:true}", - "cacheThemes": "${keycloak.theme.cacheThemes:true}", + "staticMaxAge": "${keycloak.theme.staticMaxAge:}", + "cacheTemplates": "${keycloak.theme.cacheTemplates:}", + "cacheThemes": "${keycloak.theme.cacheThemes:}", "folder": { "dir": "${keycloak.theme.dir}" } }, - "scheduled": { - "interval": 900 - }, - - "connectionsHttpClient": { - "default": {} - }, - "connectionsJpa": { "default": { "url": "${keycloak.connectionsJpa.url:jdbc:h2:mem:test;DB_CLOSE_DELAY=-1}", @@ -79,17 +50,9 @@ "driverDialect": "${keycloak.connectionsJpa.driverDialect:}", "user": "${keycloak.connectionsJpa.user:sa}", "password": "${keycloak.connectionsJpa.password:}", - "initializeEmpty": true, - "migrationStrategy": "update", - "showSql": "${keycloak.connectionsJpa.showSql:false}", - "formatSql": "${keycloak.connectionsJpa.formatSql:true}", - "globalStatsInterval": "${keycloak.connectionsJpa.globalStatsInterval:-1}" - } - }, - - "realmCache": { - "default" : { - "enabled": true + "showSql": "${keycloak.connectionsJpa.showSql:}", + "formatSql": "${keycloak.connectionsJpa.formatSql:}", + "globalStatsInterval": "${keycloak.connectionsJpa.globalStatsInterval:}" } }, @@ -98,13 +61,13 @@ "jgroupsUdpMcastAddr": "${keycloak.connectionsInfinispan.jgroupsUdpMcastAddr:234.56.78.90}", "nodeName": "${keycloak.connectionsInfinispan.nodeName,jboss.node.name:}", "siteName": "${keycloak.connectionsInfinispan.siteName,jboss.site.name:}", - "clustered": "${keycloak.connectionsInfinispan.clustered:false}", - "async": "${keycloak.connectionsInfinispan.async:false}", - "sessionsOwners": "${keycloak.connectionsInfinispan.sessionsOwners:1}", - "l1Lifespan": "${keycloak.connectionsInfinispan.l1Lifespan:600000}", - "remoteStoreEnabled": "${keycloak.connectionsInfinispan.remoteStoreEnabled:false}", - "remoteStoreHost": "${keycloak.connectionsInfinispan.remoteStoreServer:localhost}", - "remoteStorePort": "${keycloak.connectionsInfinispan.remoteStorePort:11222}", + "clustered": "${keycloak.connectionsInfinispan.clustered:}", + "async": "${keycloak.connectionsInfinispan.async:}", + "sessionsOwners": "${keycloak.connectionsInfinispan.sessionsOwners:}", + "l1Lifespan": "${keycloak.connectionsInfinispan.l1Lifespan:}", + "remoteStoreEnabled": "${keycloak.connectionsInfinispan.remoteStoreEnabled:}", + "remoteStoreHost": "${keycloak.connectionsInfinispan.remoteStoreServer:}", + "remoteStorePort": "${keycloak.connectionsInfinispan.remoteStorePort:}", "hotrodProtocolVersion": "${keycloak.connectionsInfinispan.hotrodProtocolVersion}", "embedded": "${keycloak.connectionsInfinispan.embedded:true}" } @@ -114,11 +77,7 @@ }, "jta-lookup": { - "provider": "${keycloak.jta.lookup.provider:jboss}", - "jboss" : { - "enabled": true - } - + "provider": "${keycloak.jta.lookup.provider:}" }, "login-protocol": { @@ -131,24 +90,18 @@ }, "x509cert-lookup": { - "provider": "${keycloak.x509cert.lookup.provider:default}", - "default": { - "enabled": true - }, + "provider": "${keycloak.x509cert.lookup.provider:}", "haproxy": { - "enabled": true, "sslClientCert": "x-ssl-client-cert", "sslCertChainPrefix": "x-ssl-client-cert-chain", "certificateChainLength": 1 }, "apache": { - "enabled": true, "sslClientCert": "x-ssl-client-cert", "sslCertChainPrefix": "x-ssl-client-cert-chain", "certificateChainLength": 1 }, "nginx": { - "enabled": true, "sslClientCert": "x-ssl-client-cert", "sslCertChainPrefix": "x-ssl-client-cert-chain", "certificateChainLength": 1