Skip to content

Commit 5ddf638

Browse files
ES-12969 Prohibit _reindex, _delete_by_query and _update_by_query from issuing cross-project calls
1 parent fc80e5e commit 5ddf638

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ protected RestChannelConsumer doPrepareRequest(RestRequest request, NodeClient c
4444
// Build the internal request
4545
Request internal = setCommonOptions(request, buildRequest(request));
4646

47+
// Only requests supporting remote indices can have IndicesOptions allowing cross-project index expressions
48+
assert internal.supportsRemoteIndicesSearch()
49+
|| internal.getSearchRequest().indicesOptions().resolveCrossProjectIndexExpression() == false;
50+
4751
// Executes the request and waits for completion
4852
if (request.paramAsBoolean("wait_for_completion", true)) {
4953
Map<String, String> params = new HashMap<>();

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,18 +479,26 @@ private static void ensureRemoteExpressionRequireIgnoreUnavailable(IndicesOption
479479
return;
480480
}
481481
if (RemoteClusterAware.isRemoteIndexName(current)) {
482-
List<String> crossClusterIndices = new ArrayList<>();
483-
for (int i = 0; i < expressions.length; i++) {
484-
if (RemoteClusterAware.isRemoteIndexName(expressions[i])) {
485-
crossClusterIndices.add(expressions[i]);
486-
}
487-
}
482+
List<String> crossClusterIndices = getRemoteIndexExpressions(expressions);
488483
throw new IllegalArgumentException(
489484
"Cross-cluster calls are not supported in this context but remote indices were requested: " + crossClusterIndices
490485
);
491486
}
492487
}
493488

489+
/**
490+
* Extracts the list of remote index expressions from the given array of index expressions
491+
*/
492+
public static List<String> getRemoteIndexExpressions(String... expressions) {
493+
List<String> crossClusterIndices = new ArrayList<>();
494+
for (int i = 0; i < expressions.length; i++) {
495+
if (RemoteClusterAware.isRemoteIndexName(expressions[i])) {
496+
crossClusterIndices.add(expressions[i]);
497+
}
498+
}
499+
return crossClusterIndices;
500+
}
501+
494502
/**
495503
* Translates the provided index expression into actual concrete indices, properly deduplicated.
496504
*

server/src/main/java/org/elasticsearch/index/reindex/AbstractBulkByScrollRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ public AbstractBulkByScrollRequest(SearchRequest searchRequest, boolean setDefau
137137
*/
138138
protected abstract Self self();
139139

140+
/**
141+
* Whether the request supports remote indices in the search request.
142+
*/
143+
public boolean supportsRemoteIndicesSearch() {
144+
return false;
145+
}
146+
140147
@Override
141148
public ActionRequestValidationException validate() {
142149
ActionRequestValidationException e = searchRequest.validate();

server/src/main/java/org/elasticsearch/index/reindex/ReindexRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ protected ReindexRequest self() {
8383
return this;
8484
}
8585

86+
@Override
87+
public boolean supportsRemoteIndicesSearch() {
88+
return true;
89+
}
90+
8691
@Override
8792
public ActionRequestValidationException validate() {
8893
ActionRequestValidationException e = super.validate();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import static org.hamcrest.Matchers.emptyArray;
7979
import static org.hamcrest.Matchers.endsWith;
8080
import static org.hamcrest.Matchers.equalTo;
81+
import static org.hamcrest.Matchers.hasLength;
82+
import static org.hamcrest.Matchers.hasSize;
8183
import static org.hamcrest.Matchers.is;
8284
import static org.hamcrest.Matchers.notNullValue;
8385
import static org.hamcrest.Matchers.nullValue;
@@ -3486,6 +3488,18 @@ public void testResolveWriteIndexAbstractionMultipleMatches() {
34863488
);
34873489
}
34883490

3491+
public void testGetRemoteIndexExpressions() {
3492+
{
3493+
List<String> remoteIndexExpressions = IndexNameExpressionResolver.getRemoteIndexExpressions("index-1");
3494+
assertThat(remoteIndexExpressions, empty());
3495+
}
3496+
{
3497+
List<String> remoteIndexExpressions = IndexNameExpressionResolver.getRemoteIndexExpressions("index-1", "remote:index-1", "idx-*", "remote-2:idx-5");
3498+
assertThat(remoteIndexExpressions, hasSize(2));
3499+
assertThat(remoteIndexExpressions, contains("remote:index-1", "remote-2:idx-5"));
3500+
}
3501+
}
3502+
34893503
public static IndexMetadata.Builder indexBuilder(String index) {
34903504
return indexBuilder(index, Settings.EMPTY);
34913505
}

0 commit comments

Comments
 (0)