diff --git a/core/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java b/core/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java index 5c9152b4751..a9a5afed9f3 100644 --- a/core/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java +++ b/core/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java @@ -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 expression; if (pattern.startsWith("-")) { if (pattern.length() == 1) { diff --git a/core/src/test/java/org/elasticsearch/action/support/AutoCreateIndexTests.java b/core/src/test/java/org/elasticsearch/action/support/AutoCreateIndexTests.java index 8a45ca47535..19342e5e604 100644 --- a/core/src/test/java/org/elasticsearch/action/support/AutoCreateIndexTests.java +++ b/core/src/test/java/org/elasticsearch/action/support/AutoCreateIndexTests.java @@ -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> expressions = autoCreateIndex.getAutoCreate().getExpressions(); + Map map = new HashMap<>(); + for (Tuple 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);