Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/java/org/keycloak/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public Set<String> getPropertyNames() {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Scope root() {
return new SystemPropertiesScope("keycloak.");
}

}

/**
Expand Down Expand Up @@ -243,5 +248,13 @@ public static interface Scope {
*/
@Deprecated
Set<String> getPropertyNames();

/**
* Root {@link Scope} for global options. The key format should match exactly what
* is expected to appear in the main configuration file - e.g. metrics-enabled, db, etc.
*
* @return a {@link Scope} with access to global configuration properties.
*/
Scope root();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class DefaultCacheEmbeddedConfigProviderFactory implements CacheEmbeddedC

// Configuration
public static final String CONFIG = "configFile";
private static final String METRICS = "metricsEnabled";
public static final String TRACING = "tracingEnabled";
private static final String HISTOGRAMS = "metricsHistogramsEnabled";
public static final String STACK = "stack";
Expand Down Expand Up @@ -119,7 +118,6 @@ public List<ProviderConfigProperty> getConfigMetadata() {
var builder = ProviderConfigurationBuilder.create();
Util.copyFromOption(builder, CONFIG, "file", ProviderConfigProperty.STRING_TYPE, CachingOptions.CACHE_CONFIG_FILE, false);
Util.copyFromOption(builder, HISTOGRAMS, "enabled", ProviderConfigProperty.BOOLEAN_TYPE, CachingOptions.CACHE_METRICS_HISTOGRAMS_ENABLED, false);
Util.copyFromOption(builder, METRICS, "enabled", ProviderConfigProperty.BOOLEAN_TYPE, MetricsOptions.INFINISPAN_METRICS_ENABLED, false);
Stream.concat(Arrays.stream(LOCAL_CACHE_NAMES), Arrays.stream(CLUSTERED_MAX_COUNT_CACHES))
.forEach(name -> Util.copyFromOption(builder, CacheConfigurator.maxCountConfigKey(name), "max-count", ProviderConfigProperty.INTEGER_TYPE, CachingOptions.maxCountOption(name), false));
createTopologyProperties(builder);
Expand Down Expand Up @@ -219,7 +217,7 @@ private static void singleSiteConfiguration(Config.Scope config, ConfigurationBu

private static void configureMetrics(Config.Scope keycloakConfig, ConfigurationBuilderHolder holder) {
//metrics are disabled by default (check MetricsOptions class)
if (keycloakConfig.getBoolean(METRICS, Boolean.FALSE)) {
if (keycloakConfig.root().getBoolean(MetricsOptions.METRICS_ENABLED.getKey(), Boolean.FALSE)) {
logger.debug("Enabling Infinispan metrics");
var builder = holder.getGlobalConfigurationBuilder();
builder.addModule(MicrometerMeterRegisterConfigurationBuilder.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,4 @@ public class MetricsOptions {
.defaultValue(Boolean.FALSE)
.build();

public static final Option<Boolean> PASSWORD_VALIDATION_COUNTER_ENABLED = new OptionBuilder<>("metrics-password-validation-counter-enabled", Boolean.class)
.category(OptionCategory.METRICS)
.description("If the server should publish counter metric for number of password validations performed.")
.defaultValue(Boolean.TRUE)
.hidden() // This is intended to be enabled all the time when global metrics are enabled, therefore this option is hidden
.build();

public static final Option<Boolean> INFINISPAN_METRICS_ENABLED = new OptionBuilder<>("infinispan-metrics-enabled", Boolean.class)
.category(OptionCategory.METRICS)
.description("If Infinispan metrics should be collected and exposed.")
.defaultValue(Boolean.FALSE)
.hidden() // Intentional, Infinispan metrics are enabled when '--metrics-enabled' are enabled.
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.microprofile.config.ConfigProvider;

import org.keycloak.Config;
import org.keycloak.Config.Scope;

public class MicroProfileConfigProvider implements Config.ConfigProvider {

Expand Down Expand Up @@ -132,11 +133,19 @@ public Set<String> getPropertyNames() {
}

private <T> T getValue(String key, Class<T> clazz, T defaultValue) {
if (NS_KEYCLOAK_PREFIX.equals(separatorPrefix)) {
return config.getOptionalValue(separatorPrefix.concat(key), clazz).orElse(defaultValue);
}
String dashCase = toDashCase(key);
return config.getOptionalValue(separatorPrefix.concat(dashCase), clazz)
.or(() -> config.getOptionalValue(prefix.concat(dashCase), clazz)).orElse(defaultValue);
}

@Override
public Scope root() {
return new MicroProfileScope(NS_KEYCLOAK_PREFIX);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ public static PropertyMapper<?>[] getMetricsPropertyMappers() {
return new PropertyMapper[] {
fromOption(MetricsOptions.METRICS_ENABLED)
.to("quarkus.micrometer.enabled")
.build(),
fromOption(MetricsOptions.PASSWORD_VALIDATION_COUNTER_ENABLED)
.to("kc.spi-credential--keycloak-password--metrics-enabled")
.isEnabled(MetricsPropertyMappers::metricsEnabled, "metrics are enabled")
.build(),
fromOption(MetricsOptions.INFINISPAN_METRICS_ENABLED)
.mapFrom(MetricsOptions.METRICS_ENABLED)
.to("kc.spi-cache-embedded--default--metrics-enabled")
.isEnabled(MetricsPropertyMappers::metricsEnabled, "metrics are enabled")
.build()
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ public void testCamelCase() {
putEnvVar("KC_SPI_CAMEL_CASE_SCOPE_CAMEL_CASE_PROP", "foobar");
initConfig();
String value = Config.scope("camelCaseScope").get("camelCaseProp");
assertEquals(value, "foobar");
assertEquals("foobar", value);

// root should be at kc - users are not expected to obtain spi options this way
value = Config.scope().root().get("spi-camel-case-scope-camel-case-prop");
assertEquals("foobar", value);
}

@Test
public void testCamelCaseNewFormat() {
putEnvVar("KC_SPI_CAMEL_CASE_SCOPE__CAMEL_CASE_PROP", "foobar");
initConfig();
String value = Config.scope("camelCaseScope").get("camelCaseProp");
assertEquals(value, "foobar");
assertEquals("foobar", value);

// root should be at kc - users are not expected to obtain spi options this way
value = Config.scope().root().get("spi-camel-case-scope--camel-case-prop");
assertEquals("foobar", value);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright 2021 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.
Expand Down Expand Up @@ -123,4 +123,9 @@ public ComponentModel getComponentModel() {
return componentConfig;
}

@Override
public Scope root() {
return this.origScope.root();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface ProviderFactory<T extends Provider> {
T create(KeycloakSession session);

/**
* Only called once when the factory is first created. This config is pulled from keycloak_server.json
* Only called once when the factory is first created.
*
* @param config
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import org.keycloak.Config;
import org.keycloak.config.MetricsOptions;
import org.keycloak.models.KeycloakSession;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.provider.ProviderConfigurationBuilder;
Expand Down Expand Up @@ -61,7 +62,7 @@ public PasswordCredentialProvider create(KeycloakSession session) {

@Override
public void init(Config.Scope config) {
metricsEnabled = config.getBoolean("metrics-enabled", false);
metricsEnabled = config.root().getBoolean(MetricsOptions.METRICS_ENABLED.getKey(), false);
if (metricsEnabled) {
meterProvider = Counter.builder(LOGIN_PASSWORD_VERIFY_METER_NAME)
.description(LOGIN_PASSWORD_VERIFY_METER_DESCRIPTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.util.Set;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;

import org.keycloak.Config;
import org.keycloak.Config.Scope;
import org.keycloak.common.util.StringPropertyReplacer;
import org.keycloak.common.util.SystemEnvProperties;

Expand Down Expand Up @@ -192,6 +195,11 @@ public Set<String> getPropertyNames() {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Scope root() {
return new JsonScope(NullNode.getInstance());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.keycloak.Config;
import org.keycloak.Config.Scope;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resteasy.ResteasyKeycloakSession;
import org.keycloak.services.resteasy.ResteasyKeycloakSessionFactory;
Expand Down Expand Up @@ -136,6 +137,11 @@ public Config.Scope scope(String... scope) {
public Set<String> getPropertyNames() {
throw new UnsupportedOperationException("not implemented");
}

@Override
public Scope root() {
throw new UnsupportedOperationException("not implemented");
}
}

}