fix: adds a better error message if an spi argument lacks a value (#24826)

closes: #22260

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2023-11-20 05:03:45 -05:00 committed by GitHub
parent 1241bd2919
commit 73d5a2c553
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 8 deletions

View file

@ -41,7 +41,7 @@ import org.keycloak.Config;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler; import org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler;
import org.keycloak.quarkus.runtime.cli.NonCliPropertyException; import org.keycloak.quarkus.runtime.cli.PropertyException;
import org.keycloak.quarkus.runtime.cli.Picocli; import org.keycloak.quarkus.runtime.cli.Picocli;
import org.keycloak.common.Version; import org.keycloak.common.Version;
import org.keycloak.quarkus.runtime.cli.command.Start; import org.keycloak.quarkus.runtime.cli.command.Start;
@ -64,7 +64,16 @@ public class KeycloakMain implements QuarkusApplication {
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("kc.version", Version.VERSION); System.setProperty("kc.version", Version.VERSION);
List<String> cliArgs = Picocli.parseArgs(args); List<String> cliArgs = null;
try {
cliArgs = Picocli.parseArgs(args);
} catch (PropertyException e) {
ExecutionExceptionHandler errorHandler = new ExecutionExceptionHandler();
PrintWriter errStream = new PrintWriter(System.err, true);
errorHandler.error(errStream, e.getMessage(), null);
System.exit(ExitCode.USAGE);
return;
}
if (cliArgs.isEmpty()) { if (cliArgs.isEmpty()) {
cliArgs = new ArrayList<>(cliArgs); cliArgs = new ArrayList<>(cliArgs);
@ -83,7 +92,7 @@ public class KeycloakMain implements QuarkusApplication {
try { try {
Picocli.validateNonCliConfig(cliArgs, new Start(), new PrintWriter(System.out, true)); Picocli.validateNonCliConfig(cliArgs, new Start(), new PrintWriter(System.out, true));
} catch (NonCliPropertyException e) { } catch (PropertyException e) {
errorHandler.error(errStream, e.getMessage(), null); errorHandler.error(errStream, e.getMessage(), null);
System.exit(ExitCode.USAGE); System.exit(ExitCode.USAGE);
return; return;

View file

@ -42,7 +42,7 @@ public final class ExecutionExceptionHandler implements CommandLine.IExecutionEx
@Override @Override
public int handleExecutionException(Exception cause, CommandLine cmd, ParseResult parseResult) { public int handleExecutionException(Exception cause, CommandLine cmd, ParseResult parseResult) {
if (cause instanceof NonCliPropertyException) { if (cause instanceof PropertyException) {
PrintWriter writer = cmd.getErr(); PrintWriter writer = cmd.getErr();
writer.println(cmd.getColorScheme().errorText(cause.getMessage())); writer.println(cmd.getColorScheme().errorText(cause.getMessage()));
return ShortErrorMessageHandler.getInvalidInputExitCode(cause, cmd); return ShortErrorMessageHandler.getInvalidInputExitCode(cause, cmd);

View file

@ -297,7 +297,7 @@ public final class Picocli {
} }
if (!PropertyMapperParameterConsumer.isExpectedValue(mapper.getExpectedValues(), value)) { if (!PropertyMapperParameterConsumer.isExpectedValue(mapper.getExpectedValues(), value)) {
throw new NonCliPropertyException(PropertyMapperParameterConsumer.getErrorMessage(mapper.getFrom(), throw new PropertyException(PropertyMapperParameterConsumer.getErrorMessage(mapper.getFrom(),
value, mapper.getExpectedValues(), mapper.getExpectedValues()) + ". From ConfigSource " + configSource.getName()); value, mapper.getExpectedValues(), mapper.getExpectedValues()) + ". From ConfigSource " + configSource.getName());
} }
} }
@ -595,7 +595,7 @@ public final class Picocli {
cmd.getOut().println(message); cmd.getOut().println(message);
} }
public static List<String> parseArgs(String[] rawArgs) { public static List<String> parseArgs(String[] rawArgs) throws PropertyException {
if (rawArgs.length == 0) { if (rawArgs.length == 0) {
return List.of(); return List.of();
} }
@ -615,6 +615,12 @@ public final class Picocli {
iterator.remove(); iterator.remove();
if (!arg.contains(ARG_KEY_VALUE_SEPARATOR)) { if (!arg.contains(ARG_KEY_VALUE_SEPARATOR)) {
if (!iterator.hasNext()) {
if (arg.startsWith("--spi")) {
throw new PropertyException(String.format("spi argument %s requires a value.", arg));
}
return args;
}
String next = iterator.next(); String next = iterator.next();
if (!next.startsWith("--")) { if (!next.startsWith("--")) {

View file

@ -17,9 +17,9 @@
package org.keycloak.quarkus.runtime.cli; package org.keycloak.quarkus.runtime.cli;
public class NonCliPropertyException extends RuntimeException { public class PropertyException extends RuntimeException {
public NonCliPropertyException(String message) { public PropertyException(String message) {
super(message); super(message);
} }

View file

@ -44,6 +44,13 @@ public class StartCommandDistTest {
() -> "The Output:\n" + result.getOutput() + "doesn't contains the expected string."); () -> "The Output:\n" + result.getOutput() + "doesn't contains the expected string.");
} }
@Test
@Launch({ "start", "--spi-events-listener-jboss-logging-success-level" })
void failSpiArgMissingValue(LaunchResult result) {
assertTrue(result.getErrorOutput().contains("spi argument --spi-events-listener-jboss-logging-success-level requires a value"),
() -> "The Output:\n" + result.getErrorOutput() + "doesn't contains the expected string.");
}
@Test @Test
@Launch({ "--profile=dev", "start" }) @Launch({ "--profile=dev", "start" })
void failUsingDevProfile(LaunchResult result) { void failUsingDevProfile(LaunchResult result) {