Handle spaces in `action.auto_create_index` gracefully (#21790)

Today if a comma-separated list is passed to action.auto_create_index
leading and trailing whitespaces are not trimmed but since the values are
index expressions whitespaces should be removed for convenience.

Closes #21449
This commit is contained in:
Simon Willnauer 2016-11-24 21:43:58 +01:00 committed by GitHub
parent 953928b2c5
commit 72ef6fa0d7
2 changed files with 25 additions and 1 deletions

View File

@ -111,9 +111,10 @@ public final class AutoCreateIndex {
try {
String[] patterns = Strings.commaDelimitedListToStringArray(value);
for (String pattern : patterns) {
if (pattern == null || pattern.length() == 0) {
if (pattern == null || pattern.trim().length() == 0) {
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must be either [true, false, or a comma separated list of index patterns]");
}
pattern = pattern.trim();
Tuple<String, Boolean> expression;
if (pattern.startsWith("-")) {
if (pattern.length() == 1) {

View File

@ -25,11 +25,16 @@ import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.ESTestCase;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.equalTo;
public class AutoCreateIndexTests extends ESTestCase {
@ -57,6 +62,24 @@ public class AutoCreateIndexTests extends ESTestCase {
}
}
public void testHandleSpaces() { // see #21449
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(),
randomFrom(".marvel-, .security, .watches, .triggered_watches, .watcher-history-",
".marvel-,.security,.watches,.triggered_watches,.watcher-history-")).build();
AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
List<Tuple<String, Boolean>> expressions = autoCreateIndex.getAutoCreate().getExpressions();
Map<String, Boolean> map = new HashMap<>();
for (Tuple<String, Boolean> t : expressions) {
map.put(t.v1(), t.v2());
}
assertTrue(map.get(".marvel-"));
assertTrue(map.get(".security"));
assertTrue(map.get(".watches"));
assertTrue(map.get(".triggered_watches"));
assertTrue(map.get(".watcher-history-"));
assertEquals(5, map.size());
}
public void testAutoCreationDisabled() {
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);