when using wildcard notation delete all indices might kick in even though its disabled

This commit is contained in:
Shay Banon 2012-08-21 21:49:16 +02:00
parent c33ca2921d
commit 2dd82675fb
2 changed files with 31 additions and 4 deletions

View File

@ -33,6 +33,7 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService; import org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.percolator.PercolatorService; import org.elasticsearch.index.percolator.PercolatorService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
@ -84,10 +85,27 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi
@Override @Override
protected void doExecute(DeleteIndexRequest request, ActionListener<DeleteIndexResponse> listener) { protected void doExecute(DeleteIndexRequest request, ActionListener<DeleteIndexResponse> listener) {
if (disableDeleteAllIndices && (request.indices() == null || request.indices().length == 0 || (request.indices().length == 1 && request.indices()[0].equals("_all")))) { ClusterState state = clusterService.state();
throw new ElasticSearchIllegalArgumentException("deleting all indices is disabled"); 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); super.doExecute(request, listener);
} }

View File

@ -135,6 +135,7 @@ public class MetaData implements Iterable<IndexMetaData> {
private final transient int totalNumberOfShards; private final transient int totalNumberOfShards;
private final String[] allIndices; private final String[] allIndices;
private final ImmutableSet<String> allIndicesSet;
private final String[] allOpenIndices; private final String[] allOpenIndices;
private final ImmutableMap<String, ImmutableMap<String, AliasMetaData>> aliases; private final ImmutableMap<String, ImmutableMap<String, AliasMetaData>> aliases;
@ -166,6 +167,7 @@ public class MetaData implements Iterable<IndexMetaData> {
allIndicesLst.add(indexMetaData.index()); allIndicesLst.add(indexMetaData.index());
} }
allIndices = allIndicesLst.toArray(new String[allIndicesLst.size()]); allIndices = allIndicesLst.toArray(new String[allIndicesLst.size()]);
allIndicesSet = ImmutableSet.copyOf(allIndices);
List<String> allOpenIndices = Lists.newArrayList(); List<String> allOpenIndices = Lists.newArrayList();
for (IndexMetaData indexMetaData : indices.values()) { for (IndexMetaData indexMetaData : indices.values()) {
@ -294,6 +296,10 @@ public class MetaData implements Iterable<IndexMetaData> {
return allIndices; return allIndices;
} }
public ImmutableSet<String> concreteAllIndicesAsSet() {
return allIndicesSet;
}
public String[] getConcreteAllIndices() { public String[] getConcreteAllIndices() {
return concreteAllIndices(); return concreteAllIndices();
} }
@ -508,7 +514,7 @@ public class MetaData implements Iterable<IndexMetaData> {
if (aliasesOrIndices == null || aliasesOrIndices.length == 0) { if (aliasesOrIndices == null || aliasesOrIndices.length == 0) {
return allOnlyOpen ? concreteAllOpenIndices() : concreteAllIndices(); return allOnlyOpen ? concreteAllOpenIndices() : concreteAllIndices();
} }
aliasesOrIndices = convertFromWildcards(aliasesOrIndices, allOnlyOpen, false); aliasesOrIndices = convertFromWildcards(aliasesOrIndices, allOnlyOpen, ignoreMissing);
// optimize for single element index (common case) // optimize for single element index (common case)
if (aliasesOrIndices.length == 1) { if (aliasesOrIndices.length == 1) {
String aliasOrIndex = aliasesOrIndices[0]; String aliasOrIndex = aliasesOrIndices[0];
@ -602,6 +608,9 @@ public class MetaData implements Iterable<IndexMetaData> {
aliasOrIndex = aliasOrIndex.substring(1); aliasOrIndex = aliasOrIndex.substring(1);
} }
if (!Regex.isSimpleMatchPattern(aliasOrIndex)) { if (!Regex.isSimpleMatchPattern(aliasOrIndex)) {
if (!ignoreMissing && !aliasAndIndexToIndexMap.containsKey(aliasOrIndex)) {
throw new IndexMissingException(new Index(aliasOrIndex));
}
if (result != null) { if (result != null) {
if (add) { if (add) {
result.add(aliasOrIndex); result.add(aliasOrIndex);