8000 fix: corrects cli arg stringification by shawkins · Pull Request #34156 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity. We're still setting the kc.config.args sys prop as before. Why we don't need to ignore it any longer? It might still be recognized as a config option, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must have at some point seen system properties as config properties. This and the check for build time properties seem to indicate that - however system properties are no longer picked up as config properties, so we don't need this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, right. Let's get this merged.

"kc.show.config", "kc.profile", "kc.quarkus-properties-enabled", "kc.home.dir");

@Parameters(
paramLabel = "filter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.keycloak.quarkus.runtime.configuration.Configuration.OPTION_PART_SEPARATOR_CHAR;
import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -29,6 +30,8 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.smallrye.config.ConfigValue;
import io.smallrye.config.PropertiesConfigSource;
Expand All @@ -41,9 +44,6 @@
* <p>A configuration source for mapping configuration arguments to their corresponding properties so that they can be recognized
* when building and running the server.
*
* <p>The mapping is based on the system property {@code kc.config.args}, where the value is a comma-separated list of
* the arguments passed during build or runtime. E.g: "--http-enabled=true,--http-port=8180,--database-vendor=postgres".
*
* <p>Each argument is going to be mapped to its corresponding configuration property by prefixing the key with the {@link MicroProfileConfigProvider#NS_KEYCLOAK} namespace.
*/
public class ConfigArgsConfigSource extends PropertiesConfigSource {
Expand All @@ -52,17 +52,17 @@ public class ConfigArgsConfigSource extends PropertiesConfigSource {

public static final Set<String> SHORT_OPTIONS_ACCEPTING_VALUE = Set.of(Main.PROFILE_SHORT_NAME, Main.CONFIG_FILE_SHORT_NAME);

public static final String CLI_ARGS = "kc.config.args";
private static final String CLI_ARGS = "kc.config.args";
public static final String NAME = "CliConfigSource";
private static final String ARG_SEPARATOR = ";;";
private static final Pattern ARG_KEY_VALUE_SPLIT = Pattern.compile("=");

protected ConfigArgsConfigSource() {
super(parseArguments(), NAME, 600);
}

public static void setCliArgs(String... args) {
System.setProperty(CLI_ARGS, String.join(ARG_SEPARATOR, args));
System.setProperty(CLI_ARGS,
Stream.of(args).map(arg -> arg.replaceAll(",", ",,")).collect(Collectors.joining(", ")));
}

/**
Expand All @@ -73,22 +73,36 @@ public static void setCliArgs(String... args) {
* @return the invoked command from the CLI, or empty List if not set.
*/
public static List<String> getAllCliArgs() {
if(System.getProperty(CLI_ARGS) == null) {
String args = System.getProperty(CLI_ARGS);
if(args == null) {
return Collections.emptyList();
}

return List.of(System.getProperty(CLI_ARGS).split(ARG_SEPARATOR));
}

private static String getRawConfigArgs() {
String args = System.getProperty(CLI_ARGS);

if (args != null) {
return args;

List<String> result = new ArrayList<String>();
boolean escaped = false;
StringBuilder arg = new StringBuilder();
for (int i = 0; i < args.length(); i++) {
char c = args.charAt(i);
if (c == ',') {
if (escaped) {
arg.append(c);
}
escaped = !escaped;
} else if (c == ' ') {
if (escaped) {
result.add(arg.toString());
arg.setLength(0);
escaped = false;
} else {
arg.append(c);
}
} else {
arg.append(c);
}
}

// make sure quarkus.args property is properly formatted
return String.join(ARG_SEPARATOR, System.getProperty("quarkus.args", "").split(" "));
result.add(arg.toString());

return result;
}

@Override
Expand All @@ -103,12 +117,6 @@ public ConfigValue getConfigValue(String propertyName) {
}

private static Map<String, String> parseArguments() {
String rawArgs = getRawConfigArgs();

if (rawArgs == null || "".equals(rawArgs.trim())) {
return Collections.emptyMap();
}

Map<String, String> properties = new HashMap<>();

parseConfigArgs(getAllCliArgs(), new BiConsumer<String, String>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static boolean isBuildTimeProperty(String name) {

return isBuildTimeProperty
&& !"kc.version".equals(name)
&& !ConfigArgsConfigSource.CLI_ARGS.equals(name)
&& !"kc.home.dir".equals(name)
&& !"kc.config.file".equals(name)
&& !org.keycloak.common.util.Environment.PROFILE.equals(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@ public void testParseArgsWithSpi() {
ConfigArgsConfigSource.parseConfigArgs(Arrays.asList("--spi-some-thing-enabled=value", "--spi-some-thing-else", "other-value"), (key, value) -> values.add(key+'='+value), ignored -> {});
assertEquals(Arrays.asList("--spi-some-thing-enabled=value", "--spi-some-thing-else=other-value"), values);
}

@Test
public void testArgEndingInSemiColon() {
ConfigArgsConfigSource.setCliArgs("--some=thing;", "--else=value");
assertEquals(Arrays.asList("--some=thing;", "--else=value"), ConfigArgsConfigSource.getAllCliArgs());
}

@Test
public void testArgCommas() {
ConfigArgsConfigSource.setCliArgs("--some=,,,", "--else=,");
assertEquals(Arrays.asList("--some=,,,", "--else=,"), ConfigArgsConfigSource.getAllCliArgs());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.After;
import org.keycloak.Config;
import org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource;
import org.ke 10000 ycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.KeycloakConfigSourceProvider;
import org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider;
Expand Down Expand Up @@ -111,6 +112,7 @@ public void onAfter() {

SmallRyeConfigProviderResolver.class.cast(ConfigProviderResolver.instance()).releaseConfig(ConfigProvider.getConfig());
PropertyMappers.reset();
ConfigArgsConfigSource.setCliArgs();
}

protected Config.Scope initConfig(String... scope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.keycloak.quarkus.runtime.Environment.isWindows;
import static org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource.CLI_ARGS;

import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -175,13 +174,13 @@ public void testPropertyNamesFromConfig() {
assertEquals(1, config.getPropertyNames().size());
assertEquals("JKS", config.get("type"));

System.getProperties().remove(CLI_ARGS);
ConfigArgsConfigSource.setCliArgs();
System.setProperty("kc.spi-client-registration-openid-connect-static-jwk-url", "http://c.jwk.url");
config = initConfig("client-registration", "openid-connect");
assertEquals(1, config.getPropertyNames().size());
assertEquals("http://c.jwk.url", config.get("static-jwk-url"));

System.getProperties().remove(CLI_ARGS);
ConfigArgsConfigSource.setCliArgs();
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");
Expand Down Expand Up @@ -388,7 +387,7 @@ public void testDatabaseDriverSetExplicitly() {
ConfigArgsConfigSource.setCliArgs("--db=mssql", "--db-url=jdbc:sqlserver://localhost/keycloak");
System.setProperty("kc.db-driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.setProperty("kc.transaction-xa-enabled", "false");
assertTrue(System.getProperty(CLI_ARGS, "").contains("mssql"));
assertTrue(ConfigArgsConfigSource.getAllCliArgs().contains("--db=mssql"));
SmallRyeConfig config = createConfig();
assertEquals("jdbc:sqlserver://localhost/keycloak", config.getConfigValue("quarkus.datasource.jdbc.url").getValue());
assertEquals("mssql", config.getConfigValue("quarkus.datasource.db-kind").getValue());
Expand All @@ -405,14 +404,14 @@ public void testDatabaseDialectSetExplicitly() {
@Test
public void testTransactionTypeChangesDriver() {
ConfigArgsConfigSource.setCliArgs("--db=mssql", "--transaction-xa-enabled=false");
assertTrue(System.getProperty(CLI_ARGS, "").contains("mssql"));
assertTrue(ConfigArgsConfigSource.getAllCliArgs().contains("--db=mssql"));

SmallRyeConfig jtaEnabledConfig = createConfig();
assertEquals("com.m 59F1 icrosoft.sqlserver.jdbc.SQLServerDriver", jtaEnabledConfig.getConfigValue("quarkus.datasource.jdbc.driver").getValue());
assertEquals("enabled", jtaEnabledConfig.getConfigValue("quarkus.datasource.jdbc.transactions").getValue());

ConfigArgsConfigSource.setCliArgs("--db=mssql", "--transaction-xa-enabled=true");
assertTrue(System.getProperty(CLI_ARGS, "").contains("mssql"));
assertTrue(ConfigArgsConfigSource.getAllCliArgs().contains("--db=mssql"));
SmallRyeConfig xaConfig = createConfig();

assertEquals("com.microsoft.sqlserver.jdbc.SQLServerXADataSource", xaConfig.getConfigValue("quarkus.datasource.jdbc.driver").getValue());
Expand Down
0