From 6e9da227a273144c0c829dd8be99a8348d26b618 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Mon, 4 May 2020 15:05:46 -0600 Subject: [PATCH 1/3] Validate V2 templates more strictly This commit changes the validation for V2 index and component templates to re-use the same validation that V1 templates used. This includes things like invalid template names, index patterns, etc. This also adds validation that template names do not contain `*`. Supercedes #53970 Relates to #53101 --- .../TransportSimulateIndexTemplateAction.java | 3 +- .../metadata/MetadataCreateIndexService.java | 2 +- .../MetadataIndexTemplateService.java | 118 +++++++++++++----- .../MetadataIndexTemplateServiceTests.java | 16 +-- .../cluster/shards/ClusterShardLimitIT.java | 2 +- 5 files changed, 100 insertions(+), 41 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java index 0bc91c9c0276a..746da562dee42 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/template/post/TransportSimulateIndexTemplateAction.java @@ -57,6 +57,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.function.Function; @@ -106,7 +107,7 @@ protected void masterOperation(Task task, SimulateIndexTemplateRequest request, ClusterState simulateOnClusterState = state; if (request.getIndexTemplateRequest() != null) { // we'll "locally" add the template defined by the user in the cluster state (as if it existed in the system) - String simulateTemplateToAdd = "simulate_new_template_" + UUIDs.randomBase64UUID(); + String simulateTemplateToAdd = "simulate_new_template_" + UUIDs.randomBase64UUID().toLowerCase(Locale.ROOT); simulateOnClusterState = indexTemplateService.addIndexTemplateV2(state, request.getIndexTemplateRequest().create(), simulateTemplateToAdd, request.getIndexTemplateRequest().indexTemplate()); } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java index c2c9b9b8640f2..0f734a5aee9cc 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -981,7 +981,7 @@ private static List validatePrivateSettingsNotExplicitlySet(Settings set for (final String key : settings.keySet()) { final Setting setting = indexScopedSettings.get(key); if (setting == null) { - assert indexScopedSettings.isPrivateSetting(key); + assert indexScopedSettings.isPrivateSetting(key) : "expected [" + key + "] to be private but it was not"; } else if (setting.isPrivateIndex()) { validationErrors.add("private index setting [" + key + "] can not be set explicitly"); } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java index 280e546f983a7..846a9205680d7 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -233,6 +233,7 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean final Template finalTemplate = new Template(finalSettings, stringMappings == null ? null : new CompressedXContent(stringMappings), template.template().aliases()); final ComponentTemplate finalComponentTemplate = new ComponentTemplate(finalTemplate, template.version(), template.metadata()); + validate(name, finalComponentTemplate); logger.info("adding component template [{}]", name); return ClusterState.builder(currentState) .metadata(Metadata.builder(currentState.metadata()).put(name, finalComponentTemplate)) @@ -402,6 +403,7 @@ public ClusterState addIndexTemplateV2(final ClusterState currentState, final bo template.priority(), template.version(), template.metadata()); } + validate(name, finalIndexTemplate); logger.info("adding index template [{}]", name); return ClusterState.builder(currentState) .metadata(Metadata.builder(currentState.metadata()).put(name, finalIndexTemplate)) @@ -922,70 +924,126 @@ private static void validateTemplate(Settings validateSettings, String mappings, } } - private void validate(PutRequest request) { + private void validate(String name, ComponentTemplate template) { + validate(name, + template.template().settings(), + Collections.emptyList(), + Optional.ofNullable(template.template().aliases()) + .map(aliases -> aliases.values().stream() + .map(aliasMeta -> { + Alias a = new Alias(aliasMeta.alias()); + if (aliasMeta.filter() != null) { + a.filter(aliasMeta.filter().string()); + } + a.searchRouting(aliasMeta.searchRouting()); + a.indexRouting(aliasMeta.indexRouting()); + a.isHidden(aliasMeta.isHidden()); + a.writeIndex(aliasMeta.writeIndex()); + return a; + }) + .collect(Collectors.toList())) + .orElse(Collections.emptyList())); + } + + private void validate(String name, IndexTemplateV2 template) { + Optional