Skip to content

Commit 601d8f7

Browse files
introduce and handle more specific selector validation exceptions
1 parent 0428284 commit 601d8f7

File tree

7 files changed

+61
-18
lines changed

7 files changed

+61
-18
lines changed

server/src/main/java/org/elasticsearch/action/support/IndexComponentSelector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static IndexComponentSelector getByKeyOrThrow(@Nullable String key) {
8383
}
8484
IndexComponentSelector selector = getByKey(key);
8585
if (selector == null) {
86-
throw new IllegalArgumentException(
86+
throw new InvalidSelectorException(
8787
"Unknown key of index component selector [" + key + "], available options are: " + KEY_REGISTRY.keySet()
8888
);
8989
}
@@ -107,7 +107,7 @@ public static IndexComponentSelector read(StreamInput in) throws IOException {
107107
static IndexComponentSelector getById(byte id) {
108108
IndexComponentSelector indexComponentSelector = ID_REGISTRY.get(id);
109109
if (indexComponentSelector == null) {
110-
throw new IllegalArgumentException(
110+
throw new InvalidSelectorException(
111111
"Unknown id of index component selector [" + id + "], available options are: " + ID_REGISTRY
112112
);
113113
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.support;
11+
12+
/**
13+
* Exception thrown when a :: selector is invalid.
14+
*/
15+
public class InvalidSelectorException extends IllegalArgumentException {
16+
17+
public InvalidSelectorException(String message) {
18+
super(message);
19+
}
20+
21+
public InvalidSelectorException(String message, Throwable cause) {
22+
super(message, cause);
23+
}
24+
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.support;
11+
12+
/**
13+
* Exception thrown when a :: selector is not supported.
14+
*/
15+
public class UnsupportedSelectorException extends IllegalArgumentException {
16+
17+
public UnsupportedSelectorException(String expression) {
18+
super("Index component selectors are not supported in this context but found selector in expression [" + expression + "]");
19+
}
20+
21+
}

server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.action.support.IndexComponentSelector;
1313
import org.elasticsearch.action.support.IndicesOptions;
14+
import org.elasticsearch.action.support.UnsupportedSelectorException;
1415
import org.elasticsearch.common.regex.Regex;
1516
import org.elasticsearch.core.Nullable;
1617
import org.elasticsearch.core.Tuple;
@@ -57,11 +58,7 @@ public List<String> resolveIndexAbstractions(
5758
Tuple<String, String> expressionAndSelector = IndexNameExpressionResolver.splitSelectorExpression(indexAbstraction);
5859
String selectorString = expressionAndSelector.v2();
5960
if (indicesOptions.allowSelectors() == false && selectorString != null) {
60-
throw new IllegalArgumentException(
61-
"Index component selectors are not supported in this context but found selector in expression ["
62-
+ indexAbstraction
63-
+ "]"
64-
);
61+
throw new UnsupportedSelectorException(indexAbstraction);
6562
}
6663
indexAbstraction = expressionAndSelector.v1();
6764
IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(selectorString);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.IndicesRequest;
1616
import org.elasticsearch.action.support.IndexComponentSelector;
1717
import org.elasticsearch.action.support.IndicesOptions;
18+
import org.elasticsearch.action.support.UnsupportedSelectorException;
1819
import org.elasticsearch.cluster.ClusterState;
1920
import org.elasticsearch.cluster.metadata.IndexAbstraction.Type;
2021
import org.elasticsearch.cluster.project.ProjectResolver;
@@ -2394,13 +2395,11 @@ private static IndexComponentSelector resolveAndValidateSelectorString(Supplier<
23942395
* Checks the selectors that have been returned from splitting an expression and throws an exception if any were present.
23952396
* @param expression Original expression
23962397
* @param selector Selectors to validate
2397-
* @throws IllegalArgumentException if selectors are present
2398+
* @throws UnsupportedSelectorException if selectors are present
23982399
*/
23992400
private static void ensureNoSelectorsProvided(String expression, IndexComponentSelector selector) {
24002401
if (selector != null) {
2401-
throw new IllegalArgumentException(
2402-
"Index component selectors are not supported in this context but found selector in expression [" + expression + "]"
2403-
);
2402+
throw new UnsupportedSelectorException(expression);
24042403
}
24052404
}
24062405

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.elasticsearch.action.index.TransportIndexAction;
2929
import org.elasticsearch.action.search.SearchRequest;
3030
import org.elasticsearch.action.support.GroupedActionListener;
31+
import org.elasticsearch.action.support.InvalidSelectorException;
3132
import org.elasticsearch.action.support.SubscribableListener;
33+
import org.elasticsearch.action.support.UnsupportedSelectorException;
3234
import org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest;
3335
import org.elasticsearch.action.update.TransportUpdateAction;
3436
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
@@ -503,10 +505,12 @@ private void authorizeAction(
503505
indicesAndAliasesResolver.resolve(action, request, projectMetadata, authorizedIndices)
504506
),
505507
e -> {
506-
if (e instanceof InvalidIndexNameException) {
508+
if (e instanceof InvalidIndexNameException
509+
|| e instanceof InvalidSelectorException
510+
|| e instanceof UnsupportedSelectorException) {
507511
logger.debug(
508512
() -> Strings.format(
509-
"failed [%s] action authorization for [%s] due [%s] exception",
513+
"failed [%s] action authorization for [%s] due to [%s] exception",
510514
action,
511515
authentication,
512516
e.getClass().getSimpleName()

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolver.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.search.SearchRequest;
1616
import org.elasticsearch.action.support.IndexComponentSelector;
1717
import org.elasticsearch.action.support.IndicesOptions;
18+
import org.elasticsearch.action.support.UnsupportedSelectorException;
1819
import org.elasticsearch.cluster.metadata.AliasMetadata;
1920
import org.elasticsearch.cluster.metadata.IndexAbstraction;
2021
import org.elasticsearch.cluster.metadata.IndexAbstractionResolver;
@@ -315,11 +316,7 @@ ResolvedIndices resolveIndicesAndAliases(
315316
// First, if a selector is present, check to make sure that selectors are even allowed here
316317
if (indicesOptions.allowSelectors() == false && allIndicesPatternSelector != null) {
317318
String originalIndexExpression = indicesRequest.indices()[0];
318-
throw new IllegalArgumentException(
319-
"Index component selectors are not supported in this context but found selector in expression ["
320-
+ originalIndexExpression
321-
+ "]"
322-
);
319+
throw new UnsupportedSelectorException(originalIndexExpression);
323320
}
324321
if (indicesOptions.expandWildcardExpressions()) {
325322
IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(allIndicesPatternSelector);

0 commit comments

Comments
 (0)