From 55e98f08d89ac30c2d17a143dd8811261ce9be62 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 27 Feb 2019 08:10:02 -0500 Subject: [PATCH] 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. --- .../common/settings/AddStringKeyStoreCommand.java | 4 ++-- .../settings/AddStringKeyStoreCommandTests.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java b/server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java index ee661461801..14785af095d 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java +++ b/server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java @@ -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]); } diff --git a/server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java b/server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java index 07ce84b0b75..45b333ded0a 100644 --- a/server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java +++ b/server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java @@ -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)); }