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.
This commit is contained in:
parent
b789ce737b
commit
296c239611
|
@ -34,6 +34,7 @@ import org.elasticsearch.common.settings.Settings;
|
|||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.index.IndexNotFoundException;
|
||||
import org.elasticsearch.indices.IndexClosedException;
|
||||
import org.elasticsearch.indices.InvalidIndexNameException;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
@ -601,6 +602,7 @@ public class IndexNameExpressionResolver extends AbstractComponent {
|
|||
if (Strings.isEmpty(expression)) {
|
||||
throw indexNotFoundException(expression);
|
||||
}
|
||||
validateAliasOrIndex(expression);
|
||||
if (aliasOrIndexExists(options, metaData, expression)) {
|
||||
if (result != null) {
|
||||
result.add(expression);
|
||||
|
@ -654,6 +656,16 @@ public class IndexNameExpressionResolver extends AbstractComponent {
|
|||
return result;
|
||||
}
|
||||
|
||||
private static void validateAliasOrIndex(String expression) {
|
||||
// Expressions can not start with an underscore. This is reserved for APIs. If the check gets here, the API
|
||||
// does not exist and the path is interpreted as an expression. If the expression begins with an underscore,
|
||||
// throw a specific error that is different from the [[IndexNotFoundException]], which is typically thrown
|
||||
// if the expression can't be found.
|
||||
if (expression.charAt(0) == '_') {
|
||||
throw new InvalidIndexNameException(expression, "must not start with '_'.");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean aliasOrIndexExists(IndicesOptions options, MetaData metaData, String expression) {
|
||||
AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
|
||||
//treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.elasticsearch.common.Strings;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.IndexNotFoundException;
|
||||
import org.elasticsearch.indices.IndexClosedException;
|
||||
import org.elasticsearch.indices.InvalidIndexNameException;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -641,7 +642,7 @@ public class IndexNameExpressionResolverTests extends ESTestCase {
|
|||
// when ignoreAliases option is set, concreteIndexNames resolves the provided expressions
|
||||
// only against the defined indices
|
||||
IndicesOptions ignoreAliasesOptions = IndicesOptions.fromOptions(false, false, true, false, true, false, true);
|
||||
|
||||
|
||||
String[] indexNamesIndexWildcard = indexNameExpressionResolver.concreteIndexNames(state, ignoreAliasesOptions, "foo*");
|
||||
|
||||
assertEquals(1, indexNamesIndexWildcard.length);
|
||||
|
@ -1126,4 +1127,14 @@ public class IndexNameExpressionResolverTests extends ESTestCase {
|
|||
assertEquals("test-index", indices[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidIndex() {
|
||||
MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("test"));
|
||||
ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
|
||||
IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen());
|
||||
|
||||
InvalidIndexNameException iine = expectThrows(InvalidIndexNameException.class,
|
||||
() -> indexNameExpressionResolver.concreteIndexNames(context, "_foo"));
|
||||
assertEquals("Invalid index name [_foo], must not start with '_'.", iine.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,3 +160,11 @@ setup:
|
|||
- is_true: test_index_2.settings
|
||||
- is_true: test_index_3.settings
|
||||
|
||||
---
|
||||
"Should return an exception when querying invalid indices":
|
||||
|
||||
- do:
|
||||
catch: bad_request
|
||||
indices.get:
|
||||
index: _foo
|
||||
|
||||
|
|
Loading…
Reference in New Issue