From d0fbd9d1c4977b785c060cb5326d444f3d62c94e Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 2 Dec 2015 15:31:10 +0100 Subject: [PATCH] index template: Disallow index template pattern to be the same as an alias name This can cause index creation to fail down the line. --- .../MetaDataIndexTemplateService.java | 3 +++ .../MetaDataIndexTemplateServiceTests.java | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java index 3d7d19b27b9..790cb99c64b 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java @@ -217,6 +217,9 @@ public class MetaDataIndexTemplateService extends AbstractComponent { for (Alias alias : request.aliases) { //we validate the alias only partially, as we don't know yet to which index it'll get applied to aliasValidator.validateAliasStandalone(alias); + if (request.template.equals(alias.name())) { + throw new IllegalArgumentException("Alias [" + alias.name() + "] cannot be the same as the template pattern [" + request.template + "]"); + } } } diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java index f983d885124..c642bdb1e79 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java @@ -20,6 +20,8 @@ package org.elasticsearch.action.admin.indices.template.put; import org.elasticsearch.Version; +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.cluster.metadata.AliasValidator; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService; import org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService; @@ -28,13 +30,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.indices.InvalidIndexTemplateException; import org.elasticsearch.test.ESTestCase; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; public class MetaDataIndexTemplateServiceTests extends ESTestCase { @@ -68,6 +67,17 @@ public class MetaDataIndexTemplateServiceTests extends ESTestCase { assertThat(throwables.get(0).getMessage(), containsString("index must have 1 or more primary shards")); } + public void testIndexTemplateWithAliasNameEqualToTemplatePattern() { + PutRequest request = new PutRequest("api", "foobar_template"); + request.template("foobar"); + request.aliases(Collections.singleton(new Alias("foobar"))); + + List errors = putTemplate(request); + assertThat(errors.size(), equalTo(1)); + assertThat(errors.get(0), instanceOf(IllegalArgumentException.class)); + assertThat(errors.get(0).getMessage(), equalTo("Alias [foobar] cannot be the same as the template pattern [foobar]")); + } + private static List putTemplate(PutRequest request) { MetaDataCreateIndexService createIndexService = new MetaDataCreateIndexService( Settings.EMPTY, @@ -79,7 +89,7 @@ public class MetaDataIndexTemplateServiceTests extends ESTestCase { new HashSet<>(), null, null); - MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, null); + MetaDataIndexTemplateService service = new MetaDataIndexTemplateService(Settings.EMPTY, null, createIndexService, new AliasValidator(Settings.EMPTY)); final List throwables = new ArrayList<>(); service.putTemplate(request, new MetaDataIndexTemplateService.PutListener() {