Skip to content

Commit 296c239

Browse files
authored
Add check for invalid index in WildcardExpressionResolver (#26409)
This commit adds validation to the resolving of indexes in the wildcard expression resolver. It no longer throws a 404 Not Found when resolving invalid indices. It throws a 400 instead, as it is an invalid index. This was the behavior of 5.x.
1 parent b789ce7 commit 296c239

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.index.Index;
3535
import org.elasticsearch.index.IndexNotFoundException;
3636
import org.elasticsearch.indices.IndexClosedException;
37+
import org.elasticsearch.indices.InvalidIndexNameException;
3738
import org.joda.time.DateTimeZone;
3839
import org.joda.time.format.DateTimeFormat;
3940
import org.joda.time.format.DateTimeFormatter;
@@ -601,6 +602,7 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
601602
if (Strings.isEmpty(expression)) {
602603
throw indexNotFoundException(expression);
603604
}
605+
validateAliasOrIndex(expression);
604606
if (aliasOrIndexExists(options, metaData, expression)) {
605607
if (result != null) {
606608
result.add(expression);
@@ -654,6 +656,16 @@ private Set<String> innerResolve(Context context, List<String> expressions, Indi
654656
return result;
655657
}
656658

659+
private static void validateAliasOrIndex(String expression) {
660+
// Expressions can not start with an underscore. This is reserved for APIs. If the check gets here, the API
661+
// does not exist and the path is interpreted as an expression. If the expression begins with an underscore,
662+
// throw a specific error that is different from the [[IndexNotFoundException]], which is typically thrown
663+
// if the expression can't be found.
664+
if (expression.charAt(0) == '_') {
665+
throw new InvalidIndexNameException(expression, "must not start with '_'.");
666+
}
667+
}
668+
657669
private static boolean aliasOrIndexExists(IndicesOptions options, MetaData metaData, String expression) {
658670
AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
659671
//treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)

core/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.settings.Settings;
3131
import org.elasticsearch.index.IndexNotFoundException;
3232
import org.elasticsearch.indices.IndexClosedException;
33+
import org.elasticsearch.indices.InvalidIndexNameException;
3334
import org.elasticsearch.test.ESTestCase;
3435

3536
import java.util.Arrays;
@@ -641,7 +642,7 @@ public void testConcreteIndicesWildcardAndAliases() {
641642
// when ignoreAliases option is set, concreteIndexNames resolves the provided expressions
642643
// only against the defined indices
643644
IndicesOptions ignoreAliasesOptions = IndicesOptions.fromOptions(false, false, true, false, true, false, true);
644-
645+
645646
String[] indexNamesIndexWildcard = indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "foo*");
646647

647648
assertEquals(1, indexNamesIndexWildcard.length);
@@ -1126,4 +1127,14 @@ public void testIndicesAliasesRequestIgnoresAliases() {
11261127
assertEquals("test-index", indices[0]);
11271128
}
11281129
}
1130+
1131+
public void testInvalidIndex() {
1132+
MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("test"));
1133+
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
1134+
IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen());
1135+
1136+
InvalidIndexNameException iine = expectThrows(InvalidIndexNameException.class,
1137+
() -> indexNameExpressionResolver.concreteIndexNames(context, "_foo"));
1138+
assertEquals("Invalid index name [_foo], must not start with '_'.", iine.getMessage());
1139+
}
11291140
}

rest-api-spec/src/main/resources/rest-api-spec/test/indices.get/10_basic.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ setup:
160160
- is_true: test_index_2.settings
161161
- is_true: test_index_3.settings
162162

163+
---
164+
"Should return an exception when querying invalid indices":
165+
166+
- do:
167+
catch: bad_request
168+
indices.get:
169+
index: _foo
170+

0 commit comments

Comments
 (0)