FFFF adding the ability to get the root configuration by shawkins · Pull Request #39123 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
22 changes: 17 additions & 5 deletions core/src/main/java/org/keycloak/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ public Set<String> getPropertyNames() {

}

/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public static interface Scope {
public static interface BaseScope {

String get(String key);

Expand All @@ -232,9 +229,24 @@ public static interface Scope {
Boolean getBoolean(String key);

Boolean getBoolean(String key, Boolean defaultValue);
}

Scope scope(String... scope);
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public static interface Scope extends BaseScope {

/**
* @deprecated to be removed
*/
@Deprecated
Set<String> getPropertyNames();

Scope scope(String... scope);

default BaseScope root() {
throw new UnsupportedOperationException("Not implemented");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.keycloak.quarkus.runtime.cli.Picocli.ARG_PREFIX;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

Expand All @@ -29,7 +28,6 @@
import io.smallrye.config.SmallRyeConfig;

import org.keycloak.config.Option;
import org.keycloak.quarkus.runtime.cli.Picocli;
import org.keycloak.utils.StringUtil;

import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX;
Expand Down Expand Up @@ -174,11 +172,17 @@ public static String toCliFormat(String key) {
}

public static String toDashCase(String key) {
if (key == null) {
return null;
}
StringBuilder sb = new StringBuilder(key.length());
boolean l = false;

for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (c == ',') {
c = '-'; // should not happen, but was allowed by the previous logic
}
if (l && Character.isUpperCase(c)) {
sb.append('-');
c = Character.toLowerCase(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@

import static org.keycloak.quarkus.runtime.configuration.Configuration.OPTION_PART_SEPARATOR;
import static org.keycloak.quarkus.runtime.configuration.Configuration.toDashCase;
import static org.keycloak.quarkus.runtime.configuration.Configuration.toEnvVarFormat;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.microprofile.config.ConfigProvider;

import org.keycloak.Config;
import org.keycloak.Config.BaseScope;

public class MicroProfileConfigProvider implements Config.ConfigProvider {

public static final String NS_KEYCLOAK = "kc";
public static final String NS_KEYCLOAK_PREFIX = NS_KEYCLOAK + ".";
public static final String SPI_PREFIX = NS_KEYCLOAK_PREFIX + "spi" + OPTION_PART_SEPARATOR;
public static final String NS_QUARKUS = "quarkus";
public static final String NS_QUARKUS_PREFIX = "quarkus" + ".";

Expand All @@ -58,21 +58,19 @@ public String getDefaultProvider(String spi) {

@Override
public Config.Scope scope(String... scope) {
return new MicroProfileScope(scope);
return new MicroProfileScope(SPI_PREFIX, scope);
}

public class MicroProfileScope implements Config.Scope {

private final String[] scope;
private final String prefix;

public MicroProfileScope(String... scopes) {
this.scope = scopes;
StringBuilder prefix = new StringBuilder(NS_KEYCLOAK_PREFIX).append("spi");
public MicroProfileScope(String prefix, String... scopes) {
StringBuilder prefixBuilder = new StringBuilder(prefix);
for (String scope : scopes) {
prefix.append(OPTION_PART_SEPARATOR).append(scope);
prefixBuilder.append(toDashCase(scope)).append(OPTION_PART_SEPARATOR);
}
this.prefix = prefix.toString();
this.prefix = prefixBuilder.toString();
}

@Override
Expand Down Expand Up @@ -122,22 +120,23 @@ public Boolean getBoolean(String key, Boolean defaultValue) {

@Override
public Config.Scope scope(String... scope) {
return new MicroProfileScope(ArrayUtils.addAll(this.scope, scope));
return new MicroProfileScope(prefix, scope);
}

@Override
public Set<String> getPropertyNames() {
return StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.filter(this::startWithPrefix)
.filter(key -> key.startsWith(prefix))
.collect(Collectors.toSet());
}

private <T> T getValue(String key, Class<T> clazz, T defaultValue) {
return config.getOptionalValue(toDashCase(prefix.concat(OPTION_PART_SEPARATOR).concat(key.replace('.', '-'))), clazz).orElse(defaultValue);
return config.getOptionalValue(prefix.concat(toDashCase(key)), clazz).orElse(defaultValue);
}

private boolean startWithPrefix(String key) {
return key.startsWith(toDashCase(prefix)) || key.startsWith(toDashCase(toEnvVarFormat(prefix)));
@Override
public BaseScope root() {
return new MicroProfileScope(NS_KEYCLOAK_PREFIX);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public void testCommandLineArguments() {
assertEquals("http://fromargs.unittest", initConfig("hostname", "default").get("frontendUrl"));
}

@Test
public void testRootConfig() {
ConfigArgsConfigSource.setCliArgs("--metrics-enabled=true");
assertTrue(initConfig("hostname", "default").root().getBoolean("metrics-enabled"));
}

@Test
public void testSpiConfigurationUsingCommandLineArguments() {
ConfigArgsConfigSource.setCliArgs("--spi-hostname-default-frontend-url=http://spifull.unittest");
Expand Down Expand Up @@ -204,7 +210,7 @@ public void testPropertyNamesFromConfig() {
System.getProperties().remove("kc.spi-client-registration-openid-connect-static-jwk-url");
putEnvVar("KC_SPI_CLIENT_REGISTRATION_OPENID_CONNECT_STATIC_JWK_URL", "http://c.jwk.url/from-env");
config = initConfig("client-registration", "openid-connect");
assertEquals(2, config.getPropertyNames().size()); // transformed name is coming from KcEnvVarConfigSource, raw env var name is coming from EnvVarConfigSource
assertEquals(1, config.getPropertyNames().size());
assertEquals("http://c.jwk.url/from-env", config.get("static-jwk-url"));
}

Expand Down
49C6
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 All @@ -17,6 +17,8 @@
package org.keycloak.component;

import java.util.Set;

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

/**
Expand Down Expand Up @@ -123,4 +125,9 @@ public ComponentModel getComponentModel() {
return componentConfig;
}

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

}
0