Fixes wrong values shown for configkeys (e.g. http-enabled) in show-config when in dev-mode.

also removes unnecessary internal and self-referencing values from output

Closes #9525

Co-authored-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
Dominik Guhr 2022-06-10 16:34:17 +02:00 committed by Pedro Igor
parent 0fef4305b6
commit 9df0d9a5c4
6 changed files with 57 additions and 26 deletions

View file

@ -34,6 +34,7 @@ import java.util.stream.Collectors;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ProfileManager;
import org.apache.commons.lang3.SystemUtils;
import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource;
public final class Environment {
@ -43,8 +44,8 @@ public final class Environment {
public static final String DATA_PATH = "/data";
public static final String DEFAULT_THEMES_PATH = "/themes";
public static final String DEV_PROFILE_VALUE = "dev";
public static final String PROD_PROFILE_VALUE = "prod";
public static final String LAUNCH_MODE = "kc.launch.mode";
private Environment() {}
public static Boolean isRebuild() {
@ -116,7 +117,7 @@ public final class Environment {
public static String getCurrentOrPersistedProfile() {
String profile = getProfile();
if(profile == null) {
profile = getConfig().getRawValue(PROFILE);
profile = PersistedConfigSource.getInstance().getValue(PROFILE);
}
return profile;
}

View file

@ -18,12 +18,15 @@
package org.keycloak.quarkus.runtime.cli.command;
import static org.keycloak.quarkus.runtime.Environment.getCurrentOrPersistedProfile;
import static org.keycloak.quarkus.runtime.Environment.setProfile;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getConfigValue;
import static org.keycloak.quarkus.runtime.configuration.Configuration.getPropertyNames;
import static org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers.formatValue;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
@ -45,6 +48,9 @@ import picocli.CommandLine.Parameters;
public final class ShowConfig extends AbstractCommand implements Runnable {
public static final String NAME = "show-config";
private static final List<String> ignoredPropertyKeys = List.of(
"kc.config.args", "kc.show.config", "kc.profile", "kc.quarkus-properties-enabled", "kc.home.dir");
@Parameters(
paramLabel = "filter",
defaultValue = "none",
@ -55,22 +61,20 @@ public final class ShowConfig extends AbstractCommand implements Runnable {
public void run() {
System.setProperty("kc.show.config", filter);
String configArgs = System.getProperty("kc.show.config");
String profile = Optional.ofNullable(getCurrentOrPersistedProfile()).orElse(Environment.PROD_PROFILE_VALUE);
setProfile(profile);
if (configArgs != null) {
Map<String, Set<String>> properties = getPropertiesByGroup();
String profile = getCurrentOrPersistedProfile();
Map<String, Set<String>> properties = getPropertiesByGroup();
printRunTimeConfig(properties, profile);
printRunTimeConfig(properties, profile);
if (configArgs.equalsIgnoreCase("all")) {
spec.commandLine().getOut().println("Quarkus Configuration:");
properties.get(MicroProfileConfigProvider.NS_QUARKUS).stream().sorted()
.forEachOrdered(this::printProperty);
}
if (configArgs.equalsIgnoreCase("all")) {
spec.commandLine().getOut().println("Quarkus Configuration:");
properties.get(MicroProfileConfigProvider.NS_QUARKUS).stream().sorted()
.forEachOrdered(this::printProperty);
}
if (!Boolean.getBoolean("kc.show.config.runtime")) {
Quarkus.asyncExit(0);
}
if (!Boolean.getBoolean("kc.show.config.runtime")) {
Quarkus.asyncExit(0);
}
}
@ -79,7 +83,7 @@ public final class ShowConfig extends AbstractCommand implements Runnable {
spec.commandLine().getOut().printf("Current Mode: %s%n", Environment.getKeycloakModeFromProfile(profile));
spec.commandLine().getOut().println("Runtime Configuration:");
spec.commandLine().getOut().println("Current Configuration:");
properties.get(MicroProfileConfigProvider.NS_KEYCLOAK).stream().sorted()
.filter(uniqueNames::add)
@ -151,8 +155,9 @@ public final class ShowConfig extends AbstractCommand implements Runnable {
}
private static boolean filterByGroup(String property) {
return property.startsWith(MicroProfileConfigProvider.NS_KEYCLOAK)
return (property.startsWith(MicroProfileConfigProvider.NS_KEYCLOAK)
|| property.startsWith(MicroProfileConfigProvider.NS_QUARKUS)
|| property.startsWith("%");
|| property.startsWith("%"))
&& !ignoredPropertyKeys.contains(property);
}
}

View file

@ -39,8 +39,6 @@ import org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers;
*/
public class PropertyMappingInterceptor implements ConfigSourceInterceptor {
private final boolean isQuarkusPropertiesEnabled = QuarkusPropertiesConfigSource.isQuarkusPropertiesEnabled();
@Override
public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) {
ConfigValue value = PropertyMappers.getValue(context, name);

View file

@ -60,10 +60,6 @@ public final class QuarkusPropertiesConfigSource extends AbstractLocationConfigS
return NAME.equals(value.getConfigSourceName());
}
public static boolean isQuarkusPropertiesEnabled() {
return parseBoolean(getRawPersistedProperty(QUARKUS_PROPERTY_ENABLED).orElse(Boolean.FALSE.toString()));
}
public static Path getConfigurationFile() {
String homeDir = Environment.getHomeDir();

View file

@ -35,14 +35,14 @@ public class ShowConfigCommandTest {
@Launch({ ShowConfig.NAME })
void testShowConfigCommandShowsRuntimeConfig(LaunchResult result) {
Assertions.assertTrue(result.getOutput()
.contains("Runtime Configuration"));
.contains("Current Configuration"));
}
@Test
@Launch({ ShowConfig.NAME, "all" })
void testShowConfigCommandWithAllShowsAllProfiles(LaunchResult result) {
Assertions.assertTrue(result.getOutput()
.contains("Runtime Configuration"));
.contains("Current Configuration"));
Assertions.assertTrue(result.getOutput()
.contains("Quarkus Configuration"));
}

View file

@ -0,0 +1,31 @@
package org.keycloak.it.cli.dist;
import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.RawDistOnly;
import org.keycloak.it.utils.KeycloakDistribution;
@DistributionTest
public class ShowConfigCommandDistTest {
@Test
@RawDistOnly(reason = "Containers are immutable")
void testShowConfigPicksUpRightConfigDependingOnCurrentMode(KeycloakDistribution distribution) {
CLIResult initialResult = distribution.run("show-config");
initialResult.assertMessage("Current Mode: production");
initialResult.assertMessage("kc.http-enabled = false");
distribution.run("start-dev");
CLIResult devModeResult = distribution.run("show-config");
devModeResult.assertMessage("Current Mode: development");
devModeResult.assertMessage("kc.http-enabled = true");
distribution.run("build");
CLIResult resetResult = distribution.run("show-config");
resetResult.assertMessage("Current Mode: production");
resetResult.assertMessage("kc.http-enabled = false");
}
}