Provide a clearer error message on keystore add (#39327)

When trying to add a setting to the keystore with an upper case name, we
reject with an unclear error message. This commit makes that error
message much clearer.
This commit is contained in:
Jason Tedor 2019-02-27 08:10:02 -05:00
parent 6c5bf3ac13
commit 55e98f08d8
No known key found for this signature in database
GPG Key ID: FA89F05560F16BC5
2 changed files with 14 additions and 2 deletions

View File

@ -91,8 +91,8 @@ class AddStringKeyStoreCommand extends EnvironmentAwareCommand {
try {
keystore.setString(setting, value);
} catch (IllegalArgumentException e) {
throw new UserException(ExitCodes.DATA_ERROR, "String value must contain only ASCII");
} catch (final IllegalArgumentException e) {
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
}
keystore.save(env.configFile(), new char[0]);
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.settings;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import org.elasticsearch.cli.Command;
@ -30,6 +31,7 @@ import org.elasticsearch.cli.UserException;
import org.elasticsearch.env.Environment;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasToString;
public class AddStringKeyStoreCommandTests extends KeyStoreCommandTestCase {
InputStream input;
@ -139,6 +141,16 @@ public class AddStringKeyStoreCommandTests extends KeyStoreCommandTestCase {
assertThat(e.getMessage(), containsString("The setting name can not be null"));
}
public void testUpperCaseInName() throws Exception {
createKeystore("");
terminal.addSecretInput("value");
final String key = randomAlphaOfLength(4) + randomAlphaOfLength(1).toUpperCase(Locale.ROOT) + randomAlphaOfLength(4);
final UserException e = expectThrows(UserException.class, () -> execute(key));
assertThat(
e,
hasToString(containsString("Setting name [" + key + "] does not match the allowed setting name pattern [[a-z0-9_\\-.]+]")));
}
void setInput(String inputStr) {
input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
}