Properties mapped in our property mappers are overriden (#37265)

Fixes #36927

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
This commit is contained in:
Martin Bartoš 2025-02-17 16:39:51 +00:00 committed by GitHub
parent 764ca50fc4
commit 61e48f1cd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 43 deletions

View File

@ -7,12 +7,14 @@ import static org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper.
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.stream.Stream;
import io.quarkus.runtime.configuration.MemorySizeConverter;
import io.smallrye.config.ConfigValue;
import org.jboss.logmanager.LogContext;
import org.keycloak.config.LoggingOptions;
import org.keycloak.config.Option;
@ -29,10 +31,13 @@ public final class LoggingPropertyMappers {
private static final String SYSLOG_ENABLED_MSG = "Syslog is activated";
private static final String DEFAULT_ROOT_LOG_LEVEL = toLevel(LoggingOptions.LOG_LEVEL.getDefaultValue().orElseThrow().get(0)).getName();
private static List<CategoryLevel> rootLogLevels;
private LoggingPropertyMappers() {
}
public static PropertyMapper<?>[] getMappers() {
rootLogLevels = null; // reset the cached root log level and categories
PropertyMapper<?>[] defaultMappers = new PropertyMapper[]{
fromOption(LoggingOptions.LOG)
.paramLabel("<handler>")
@ -254,7 +259,7 @@ public final class LoggingPropertyMappers {
}
private static String resolveRootLogLevel(String value, ConfigSourceInterceptorContext configSourceInterceptorContext) {
for (CategoryLevel categoryLevel : parseLogLevels(value)) {
for (CategoryLevel categoryLevel : parseRootLogLevel(value)) {
if (categoryLevel.category == null) {
return categoryLevel.levelName;
}
@ -263,7 +268,7 @@ public final class LoggingPropertyMappers {
}
private static Set<String> getConfiguredLogCategories(Set<String> categories) {
for (CategoryLevel categoryLevel : parseLogLevels(Configuration.getKcConfigValue("log-level").getValue())) {
for (CategoryLevel categoryLevel : parseRootLogLevel(null)) {
if (categoryLevel.category != null) {
categories.add(categoryLevel.category);
}
@ -281,24 +286,33 @@ public final class LoggingPropertyMappers {
private static String resolveCategoryLogLevelFromParentLogLevelOption(String category, String parentLogLevelValue, ConfigSourceInterceptorContext context) {
String rootLevel = DEFAULT_ROOT_LOG_LEVEL;
for (CategoryLevel categoryLevel : parseLogLevels(parentLogLevelValue)) {
for (CategoryLevel categoryLevel : parseRootLogLevel(parentLogLevelValue)) {
if (category.equals(categoryLevel.category)) {
return categoryLevel.levelName;
} else if (categoryLevel.category == null) {
rootLevel = categoryLevel.levelName;
}
}
return rootLevel;
// If KC property is not set and the 'log-level' does not override the specific category, use value from Quarkus or properties files
return Optional.ofNullable(context.proceed("quarkus.log.category.\"" + category + "\".level"))
.map(ConfigValue::getValue)
.map(level -> !level.equals("inherit") ? level : null)
.orElse(rootLevel);
}
private static List<CategoryLevel> parseLogLevels(String value) {
if (value == null) {
return List.of();
}
private static List<CategoryLevel> parseRootLogLevel(String values) {
if (rootLogLevels == null) {
var value = values != null ? values : Configuration.getConfigValue(LoggingOptions.LOG_LEVEL).getValue();
if (value == null) {
return List.of(); // if no value is present, we do not cache the result
}
return Stream.of(value.split(","))
.map(LoggingPropertyMappers::validateLogLevel)
.toList();
rootLogLevels = Stream.of(value.split(","))
.map(LoggingPropertyMappers::validateLogLevel)
.toList();
}
return rootLogLevels;
}
private static String resolveLogOutput(String value, ConfigSourceInterceptorContext context) {

View File

@ -531,35 +531,4 @@ public class ConfigurationTest extends AbstractConfigurationTest {
assertEquals(Integer.toString(maxCount), config.getConfigValue(prop).getValue());
}
}
@Test
public void testWildcardCliOptionCanBeMappedToQuarkusOption() {
ConfigArgsConfigSource.setCliArgs("--log-level-org.keycloak=trace");
SmallRyeConfig config = createConfig();
assertEquals("TRACE", config.getConfigValue("quarkus.log.category.\"org.keycloak\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"foo.bar\".level").getValue());
}
@Test
public void testWildcardEnvVarOptionCanBeMappedToQuarkusOption() {
putEnvVar("KC_LOG_LEVEL_IO_QUARKUS", "trace");
SmallRyeConfig config = createConfig();
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"org.keycloak\".level").getValue());
assertEquals("TRACE", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"foo.bar\".level").getValue());
}
@Test
public void testWildcardOptionFromConfigFile() {
putEnvVar("SOME_CATEGORY_LOG_LEVEL", "debug");
SmallRyeConfig config = createConfig();
assertEquals("DEBUG", config.getConfigValue("quarkus.log.category.\"io.k8s\".level").getValue());
}
@Test
public void testWildcardPropertiesDontMatchEnvVarsFormat() {
SmallRyeConfig config = createConfig();
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
}
}

View File

@ -255,4 +255,35 @@ public class LoggingConfigurationTest extends AbstractConfigurationTest {
"quarkus.log.syslog.json.log-format", "ecs"
));
}
@Test
public void testWildcardCliOptionCanBeMappedToQuarkusOption() {
ConfigArgsConfigSource.setCliArgs("--log-level-org.keycloak=trace");
SmallRyeConfig config = createConfig();
assertEquals("TRACE", config.getConfigValue("quarkus.log.category.\"org.keycloak\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"foo.bar\".level").getValue());
}
@Test
public void testWildcardEnvVarOptionCanBeMappedToQuarkusOption() {
putEnvVar("KC_LOG_LEVEL_IO_QUARKUS", "trace");
SmallRyeConfig config = createConfig();
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"org.keycloak\".level").getValue());
assertEquals("TRACE", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"foo.bar\".level").getValue());
}
@Test
public void testWildcardOptionFromConfigFile() {
putEnvVar("SOME_CATEGORY_LOG_LEVEL", "debug");
SmallRyeConfig config = createConfig();
assertEquals("DEBUG", config.getConfigValue("quarkus.log.category.\"io.k8s\".level").getValue());
}
@Test
public void testWildcardPropertiesDontMatchEnvVarsFormat() {
SmallRyeConfig config = createConfig();
assertEquals("INFO", config.getConfigValue("quarkus.log.category.\"io.quarkus\".level").getValue());
}
}

View File

@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.CLIResult;
import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.utils.KeycloakDistribution;
@ -57,7 +58,10 @@ public class MetricsDistTest {
@Test
@Launch({ "start-dev", "--metrics-enabled=true" })
void testMetricsEndpoint() {
void testMetricsEndpoint(CLIResult cliResult) {
// See https://github.com/keycloak/keycloak/issues/36927
cliResult.assertNoMessage("A MeterFilter is being configured after a Meter has been registered to this registry.");
// Send one request to populate some of the HTTP metrics that are not available on an instance on startup
when().get("/metrics").then()
.statusCode(200);