From 2dd82675fb003bbceb2c8531245716e3ed26ac8f Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 21 Aug 2012 21:49:16 +0200 Subject: [PATCH] when using wildcard notation delete all indices might kick in even though its disabled --- .../delete/TransportDeleteIndexAction.java | 24 ++++++++++++++++--- .../cluster/metadata/MetaData.java | 11 ++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java b/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java index b45f78eb819..979d120a07d 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java @@ -33,6 +33,7 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.percolator.PercolatorService; import org.elasticsearch.threadpool.ThreadPool; @@ -84,10 +85,27 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi @Override protected void doExecute(DeleteIndexRequest request, ActionListener listener) { - if (disableDeleteAllIndices && (request.indices() == null || request.indices().length == 0 || (request.indices().length == 1 && request.indices()[0].equals("_all")))) { - throw new ElasticSearchIllegalArgumentException("deleting all indices is disabled"); + ClusterState state = clusterService.state(); + String[] indicesOrAliases = request.indices(); + request.indices(state.metaData().concreteIndices(request.indices())); + if (disableDeleteAllIndices) { + // simple check on the original indices with "all" default parameter + if (indicesOrAliases == null || indicesOrAliases.length == 0 || (indicesOrAliases.length == 1 && indicesOrAliases[0].equals("_all"))) { + throw new ElasticSearchIllegalArgumentException("deleting all indices is disabled"); + } + // if we end up matching on all indices, check, if its a wildcard parameter, or a "-something" structure + if (request.indices().length == state.metaData().concreteAllIndices().length && indicesOrAliases.length > 0) { + boolean hasRegex = false; + for (String indexOrAlias : indicesOrAliases) { + if (Regex.isSimpleMatchPattern(indexOrAlias)) { + hasRegex = true; + } + } + if (indicesOrAliases.length > 0 && (hasRegex || indicesOrAliases[0].charAt(0) == '-')) { + throw new ElasticSearchIllegalArgumentException("deleting all indices is disabled"); + } + } } - request.indices(clusterService.state().metaData().concreteIndices(request.indices())); super.doExecute(request, listener); } diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index ff63af0ffb7..b10541194c6 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -135,6 +135,7 @@ public class MetaData implements Iterable { private final transient int totalNumberOfShards; private final String[] allIndices; + private final ImmutableSet allIndicesSet; private final String[] allOpenIndices; private final ImmutableMap> aliases; @@ -166,6 +167,7 @@ public class MetaData implements Iterable { allIndicesLst.add(indexMetaData.index()); } allIndices = allIndicesLst.toArray(new String[allIndicesLst.size()]); + allIndicesSet = ImmutableSet.copyOf(allIndices); List allOpenIndices = Lists.newArrayList(); for (IndexMetaData indexMetaData : indices.values()) { @@ -294,6 +296,10 @@ public class MetaData implements Iterable { return allIndices; } + public ImmutableSet concreteAllIndicesAsSet() { + return allIndicesSet; + } + public String[] getConcreteAllIndices() { return concreteAllIndices(); } @@ -508,7 +514,7 @@ public class MetaData implements Iterable { if (aliasesOrIndices == null || aliasesOrIndices.length == 0) { return allOnlyOpen ? concreteAllOpenIndices() : concreteAllIndices(); } - aliasesOrIndices = convertFromWildcards(aliasesOrIndices, allOnlyOpen, false); + aliasesOrIndices = convertFromWildcards(aliasesOrIndices, allOnlyOpen, ignoreMissing); // optimize for single element index (common case) if (aliasesOrIndices.length == 1) { String aliasOrIndex = aliasesOrIndices[0]; @@ -602,6 +608,9 @@ public class MetaData implements Iterable { aliasOrIndex = aliasOrIndex.substring(1); } if (!Regex.isSimpleMatchPattern(aliasOrIndex)) { + if (!ignoreMissing && !aliasAndIndexToIndexMap.containsKey(aliasOrIndex)) { + throw new IndexMissingException(new Index(aliasOrIndex)); + } if (result != null) { if (add) { result.add(aliasOrIndex);