[KEYCLOAK-16116] - PersistedConfigSource for resolving build time properties
This commit is contained in:
parent
f0bdcdd204
commit
8d7195637d
7 changed files with 83 additions and 15 deletions
|
@ -140,7 +140,7 @@ class KeycloakProcessor {
|
|||
Map<String, String> properties = new HashMap<>();
|
||||
|
||||
for (String name : KeycloakRecorder.getConfig().getPropertyNames()) {
|
||||
if (isRuntimeProperty(name)) {
|
||||
if (isNotPersistentProperty(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -156,9 +156,9 @@ class KeycloakProcessor {
|
|||
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
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.keycloak.cli;
|
||||
|
||||
import static java.lang.Boolean.parseBoolean;
|
||||
import static org.keycloak.configuration.PropertyMappers.canonicalFormat;
|
||||
import static org.keycloak.configuration.PropertyMappers.formatValue;
|
||||
import static org.keycloak.util.Environment.getBuiltTimeProperty;
|
||||
import static org.keycloak.util.Environment.getConfig;
|
||||
|
@ -30,6 +31,8 @@ import java.util.stream.Collectors;
|
|||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.keycloak.configuration.MicroProfileConfigProvider;
|
||||
import org.keycloak.configuration.PropertyMappers;
|
||||
import org.keycloak.quarkus.KeycloakRecorder;
|
||||
import org.keycloak.util.Environment;
|
||||
|
||||
import io.smallrye.config.ConfigValue;
|
||||
|
@ -41,12 +44,21 @@ public final class ShowConfigCommand {
|
|||
|
||||
if (configArgs != null) {
|
||||
Map<String, Set<String>> properties = getPropertiesByGroup(buildTimeProperties);
|
||||
Set<String> uniqueNames = new HashSet<>();
|
||||
String profile = getProfile();
|
||||
|
||||
System.out.printf("Current Profile: %s%n", profile == null ? "none" : profile);
|
||||
|
||||
System.out.println("Runtime Configuration:");
|
||||
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);
|
||||
|
||||
if (configArgs.equalsIgnoreCase("all")) {
|
||||
|
@ -109,20 +121,19 @@ public final class ShowConfigCommand {
|
|||
}
|
||||
|
||||
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())) {
|
||||
System.out.printf("\t%s = %s (persisted)%n", property, formatValue(property, value));
|
||||
if (configValue.getValue() == null) {
|
||||
configValue = getConfig().getConfigValue(property);
|
||||
}
|
||||
|
||||
|
||||
if (configValue.getValue() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigValue configValue = getConfig().getConfigValue(property);
|
||||
|
||||
if (configValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.printf("\t%s = %s (%s)%n", property, formatValue(property, configValue.getValue()), configValue.getConfigSourceName());
|
||||
System.out.printf("\t%s = %s (%s)%n", configValue.getName(), formatValue(configValue.getName(), configValue.getValue()), configValue.getConfigSourceName());
|
||||
}
|
||||
|
||||
private static String groupProperties(String property) {
|
||||
|
|
|
@ -50,7 +50,7 @@ public class ConfigArgsConfigSource extends PropertiesConfigSource {
|
|||
|
||||
ConfigArgsConfigSource() {
|
||||
// higher priority over default Quarkus config sources
|
||||
super(parseArgument(), "cli", 500);
|
||||
super(parseArgument(), "CliConfigSource", 500);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -51,6 +51,7 @@ public class KeycloakConfigSourceProvider implements ConfigSourceProvider {
|
|||
}
|
||||
|
||||
CONFIG_SOURCES.add(new ConfigArgsConfigSource());
|
||||
CONFIG_SOURCES.add(new PersistedConfigSource());
|
||||
CONFIG_SOURCES.add(new SysPropConfigSource());
|
||||
|
||||
Path configFile = getConfigurationFile();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -44,7 +44,7 @@ public class SysPropConfigSource implements ConfigSource {
|
|||
}
|
||||
|
||||
public String getName() {
|
||||
return "System properties";
|
||||
return "KcSysPropConfigSource";
|
||||
}
|
||||
|
||||
public int getOrdinal() {
|
||||
|
|
|
@ -67,6 +67,10 @@ public class KeycloakRecorder {
|
|||
value = BUILD_TIME_PROPERTIES.get("%" + profile + "." + name);
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
value = BUILD_TIME_PROPERTIES.get(PropertyMappers.toCLIFormat(name));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue