[KEYCLOAK-16116] - PersistedConfigSource for resolving build time properties

This commit is contained in:
Pedro Igor 2020-11-02 08:50:26 -03:00 committed by Marek Posolda
parent f0bdcdd204
commit 8d7195637d
7 changed files with 83 additions and 15 deletions

View file

@ -140,7 +140,7 @@ class KeycloakProcessor {
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
for (String name : KeycloakRecorder.getConfig().getPropertyNames()) { for (String name : KeycloakRecorder.getConfig().getPropertyNames()) {
if (isRuntimeProperty(name)) { if (isNotPersistentProperty(name)) {
continue; continue;
} }
@ -156,9 +156,9 @@ class KeycloakProcessor {
recorder.showConfig(); recorder.showConfig();
} }
private boolean isRuntimeProperty(String name) { private boolean isNotPersistentProperty(String name) {
// these properties are ignored from the build time properties as they are runtime-specific // these properties are ignored from the build time properties as they are runtime-specific
return "kc.home.dir".equals(name) || "kc.config.args".equals(name); return !name.startsWith("kc") || "kc.home.dir".equals(name) || "kc.config.args".equals(name);
} }
/** /**

View file

@ -18,6 +18,7 @@
package org.keycloak.cli; package org.keycloak.cli;
import static java.lang.Boolean.parseBoolean; import static java.lang.Boolean.parseBoolean;
import static org.keycloak.configuration.PropertyMappers.canonicalFormat;
import static org.keycloak.configuration.PropertyMappers.formatValue; import static org.keycloak.configuration.PropertyMappers.formatValue;
import static org.keycloak.util.Environment.getBuiltTimeProperty; import static org.keycloak.util.Environment.getBuiltTimeProperty;
import static org.keycloak.util.Environment.getConfig; import static org.keycloak.util.Environment.getConfig;
@ -30,6 +31,8 @@ import java.util.stream.Collectors;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import org.keycloak.configuration.MicroProfileConfigProvider; import org.keycloak.configuration.MicroProfileConfigProvider;
import org.keycloak.configuration.PropertyMappers;
import org.keycloak.quarkus.KeycloakRecorder;
import org.keycloak.util.Environment; import org.keycloak.util.Environment;
import io.smallrye.config.ConfigValue; import io.smallrye.config.ConfigValue;
@ -41,12 +44,21 @@ public final class ShowConfigCommand {
if (configArgs != null) { if (configArgs != null) {
Map<String, Set<String>> properties = getPropertiesByGroup(buildTimeProperties); Map<String, Set<String>> properties = getPropertiesByGroup(buildTimeProperties);
Set<String> uniqueNames = new HashSet<>();
String profile = getProfile(); String profile = getProfile();
System.out.printf("Current Profile: %s%n", profile == null ? "none" : profile); System.out.printf("Current Profile: %s%n", profile == null ? "none" : profile);
System.out.println("Runtime Configuration:"); System.out.println("Runtime Configuration:");
properties.get(MicroProfileConfigProvider.NS_KEYCLOAK).stream().sorted() properties.get(MicroProfileConfigProvider.NS_KEYCLOAK).stream().sorted()
.filter(name -> {
String canonicalFormat = canonicalFormat(name);
if (!canonicalFormat.equals(name)) {
return uniqueNames.add(canonicalFormat);
}
return uniqueNames.add(name);
})
.forEachOrdered(ShowConfigCommand::printProperty); .forEachOrdered(ShowConfigCommand::printProperty);
if (configArgs.equalsIgnoreCase("all")) { if (configArgs.equalsIgnoreCase("all")) {
@ -109,20 +121,19 @@ public final class ShowConfigCommand {
} }
private static void printProperty(String property) { private static void printProperty(String property) {
String value = getBuiltTimeProperty(property).orElse(null); String canonicalFormat = PropertyMappers.canonicalFormat(property);
ConfigValue configValue = KeycloakRecorder.getConfig().getConfigValue(canonicalFormat);
if (value != null && !"".equals(value.trim())) { if (configValue.getValue() == null) {
System.out.printf("\t%s = %s (persisted)%n", property, formatValue(property, value)); configValue = getConfig().getConfigValue(property);
}
if (configValue.getValue() == null) {
return; return;
} }
ConfigValue configValue = getConfig().getConfigValue(property); System.out.printf("\t%s = %s (%s)%n", configValue.getName(), formatValue(configValue.getName(), configValue.getValue()), configValue.getConfigSourceName());
if (configValue == null) {
return;
}
System.out.printf("\t%s = %s (%s)%n", property, formatValue(property, configValue.getValue()), configValue.getConfigSourceName());
} }
private static String groupProperties(String property) { private static String groupProperties(String property) {

View file

@ -50,7 +50,7 @@ public class ConfigArgsConfigSource extends PropertiesConfigSource {
ConfigArgsConfigSource() { ConfigArgsConfigSource() {
// higher priority over default Quarkus config sources // higher priority over default Quarkus config sources
super(parseArgument(), "cli", 500); super(parseArgument(), "CliConfigSource", 500);
} }
@Override @Override

View file

@ -51,6 +51,7 @@ public class KeycloakConfigSourceProvider implements ConfigSourceProvider {
} }
CONFIG_SOURCES.add(new ConfigArgsConfigSource()); CONFIG_SOURCES.add(new ConfigArgsConfigSource());
CONFIG_SOURCES.add(new PersistedConfigSource());
CONFIG_SOURCES.add(new SysPropConfigSource()); CONFIG_SOURCES.add(new SysPropConfigSource());
Path configFile = getConfigurationFile(); Path configFile = getConfigurationFile();

View file

@ -0,0 +1,52 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.configuration;
import java.util.Collections;
import java.util.Map;
import io.smallrye.config.common.AbstractConfigSource;
import org.keycloak.quarkus.KeycloakRecorder;
/**
* A {@link org.eclipse.microprofile.config.spi.ConfigSource} based on the configuration properties persisted into the server
* image.
*/
public class PersistedConfigSource extends AbstractConfigSource {
public PersistedConfigSource() {
super("PersistedConfigSource", 300);
}
@Override
public Map<String, String> getProperties() {
return Collections.emptyMap();
}
@Override
public String getValue(String propertyName) {
String canonicalFormat = PropertyMappers.canonicalFormat(propertyName);
String value = KeycloakRecorder.getBuiltTimeProperty(canonicalFormat);
if (value != null) {
return value;
}
return KeycloakRecorder.getBuiltTimeProperty(propertyName);
}
}

View file

@ -44,7 +44,7 @@ public class SysPropConfigSource implements ConfigSource {
} }
public String getName() { public String getName() {
return "System properties"; return "KcSysPropConfigSource";
} }
public int getOrdinal() { public int getOrdinal() {

View file

@ -67,6 +67,10 @@ public class KeycloakRecorder {
value = BUILD_TIME_PROPERTIES.get("%" + profile + "." + name); value = BUILD_TIME_PROPERTIES.get("%" + profile + "." + name);
} }
if (value == null) {
value = BUILD_TIME_PROPERTIES.get(PropertyMappers.toCLIFormat(name));
}
return value; return value;
} }