Fixes showing sensitive values like passwords and irritating error msg when key used is unknown

Only first unknown option is shown for now. May be iterated in the future.

Closes #10050

Closes #10051
This commit is contained in:
Dominik Guhr 2022-03-22 10:46:42 +01:00 committed by Pedro Igor
parent 0941a4709e
commit a5c3d035b9
4 changed files with 45 additions and 4 deletions

View file

@ -4,7 +4,6 @@ import picocli.CommandLine;
import picocli.CommandLine.IParameterExceptionHandler;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.UnmatchedArgumentException;
import picocli.CommandLine.Help;
import picocli.CommandLine.Model.CommandSpec;
import java.io.PrintWriter;
@ -14,8 +13,22 @@ public class ShortErrorMessageHandler implements IParameterExceptionHandler {
public int handleParseException(ParameterException ex, String[] args) {
CommandLine cmd = ex.getCommandLine();
PrintWriter writer = cmd.getErr();
String errorMessage = ex.getMessage();
writer.println(cmd.getColorScheme().errorText(ex.getMessage()));
if (ex instanceof UnmatchedArgumentException) {
UnmatchedArgumentException uae = (UnmatchedArgumentException) ex;
String[] unmatched = getUnmatchedPartsByOptionSeparator(uae,"=");
String original = uae.getUnmatched().get(0);
if (unmatched[0].equals(original)) {
unmatched = getUnmatchedPartsByOptionSeparator(uae," ");
}
errorMessage = "Unknown option: '" + unmatched[0] + "'";
}
writer.println(cmd.getColorScheme().errorText(errorMessage));
UnmatchedArgumentException.printSuggestions(ex, writer);
CommandSpec spec = cmd.getCommandSpec();
@ -25,4 +38,8 @@ public class ShortErrorMessageHandler implements IParameterExceptionHandler {
? cmd.getExitCodeExceptionMapper().getExitCode(ex)
: spec.exitCodeOnInvalidInput();
}
private String[] getUnmatchedPartsByOptionSeparator(UnmatchedArgumentException uae, String separator) {
return uae.getUnmatched().get(0).split(separator);
}
}

View file

@ -47,4 +47,28 @@ public class OptionValidationTest {
assertEquals("Unknown option: '--nosuch'\n" +
"Try 'kc.sh build --help' for more information on the available options.", result.getErrorOutput());
}
@Test
@Launch({"start", "--db-pasword mytestpw"})
public void failUnknownOptionWhitespaceSeparatorNotShowingValue(LaunchResult result) {
assertEquals("Unknown option: '--db-pasword'\n" +
"Possible solutions: --db-username, --db-url-host, --db-pool-min-size, --db-password, --db-url-properties, --db-url-database, --db-schema, --db-pool-max-size, --db-pool-initial-size, --db-url\n" +
"Try 'kc.sh start --help' for more information on the available options.", result.getErrorOutput());
}
@Test
@Launch({"start", "--db-pasword=mytestpw"})
public void failUnknownOptionEqualsSeparatorNotShowingValue(LaunchResult result) {
assertEquals("Unknown option: '--db-pasword'\n" +
"Possible solutions: --db-username, --db-url-host, --db-pool-min-size, --db-password, --db-url-properties, --db-url-database, --db-schema, --db-pool-max-size, --db-pool-initial-size, --db-url\n" +
"Try 'kc.sh start --help' for more information on the available options.", result.getErrorOutput());
}
@Test
@Launch({"start", "--db-username=foobar","--db-pasword=mytestpw", "--foobar=barfoo"})
public void failWithFirstOptionOnMultipleUnknownOptions(LaunchResult result) {
assertEquals("Unknown option: '--db-pasword'\n" +
"Possible solutions: --db-username, --db-url-host, --db-pool-min-size, --db-password, --db-url-properties, --db-url-database, --db-schema, --db-pool-max-size, --db-pool-initial-size, --db-url\n" +
"Try 'kc.sh start --help' for more information on the available options.", result.getErrorOutput());
}
}

View file

@ -54,6 +54,6 @@ public class StartCommandTest {
@Launch({ "-v", "start", "--db=dev-mem" })
void failBuildPropertyNotAvailable(LaunchResult result) {
CLIResult cliResult = (CLIResult) result;
cliResult.assertError("Unknown option: '--db=dev-mem'");
cliResult.assertError("Unknown option: '--db'");
}
}

View file

@ -59,6 +59,6 @@ class BuildCommandDistTest {
@Launch({ "build", "--db=postgres", "--db-username=myuser", "--db-password=mypassword", "--http-enabled=true" })
void testFailRuntimeOptions(LaunchResult result) {
CLIResult cliResult = (CLIResult) result;
cliResult.assertError("Unknown options: '--db-username=myuser', '--db-password=mypassword', '--http-enabled=true'");
cliResult.assertError("Unknown option: '--db-username'");
}
}