Skip to content

Commit de09a89

Browse files
Prohibit _reindex, _delete_by_query and _update_by_query from issuing cross-project calls (#137203)
This PR introduces an assertion to make sure that delete-by-query/update-by-query can't have indices options enabling flat-world, and also extracts the method to find cross-project/cross-cluster index expressions from a list of index expressions that is used in ActionFilters to disable cross-project requests in Serverless. This change doesn't prohibit cross-cluster update-by-query/delete-by-query requests even though these requests were never supposed to work in CCS, since doing so could lead to a breaking change: elastic/dev#3350 ES-12969
1 parent d84f91a commit de09a89

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,7 @@ 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 = RemoteClusterAware.getRemoteIndexExpressions(expressions);
488483
throw new IllegalArgumentException(
489484
"Cross-cluster calls are not supported in this context but remote indices were requested: " + crossClusterIndices
490485
);

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/main/java/org/elasticsearch/transport/RemoteClusterAware.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public static boolean isRemoteIndexName(String indexExpression) {
6969
return idx > 0 && isSelector == false;
7070
}
7171

72+
/**
73+
* Extracts the list of remote index expressions from the given array of index expressions
74+
*/
75+
public static List<String> getRemoteIndexExpressions(String... expressions) {
76+
List<String> crossClusterIndices = new ArrayList<>();
77+
for (int i = 0; i < expressions.length; i++) {
78+
if (isRemoteIndexName(expressions[i])) {
79+
crossClusterIndices.add(expressions[i]);
80+
}
81+
}
82+
return crossClusterIndices;
83+
}
84+
7285
/**
7386
* @param indexExpression expects a single index expression at a time (not a csv list of expression)
7487
* @return cluster alias in the index expression. If none is present, returns RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY

server/src/test/java/org/elasticsearch/transport/RemoteClusterAwareTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
import java.util.Map;
1717
import java.util.Set;
1818

19+
import static org.hamcrest.Matchers.contains;
1920
import static org.hamcrest.Matchers.containsInAnyOrder;
2021
import static org.hamcrest.Matchers.containsString;
22+
import static org.hamcrest.Matchers.empty;
2123
import static org.hamcrest.Matchers.equalTo;
2224
import static org.hamcrest.Matchers.hasKey;
25+
import static org.hamcrest.Matchers.hasSize;
2326
import static org.hamcrest.Matchers.not;
2427

2528
public class RemoteClusterAwareTests extends ESTestCase {
@@ -158,6 +161,23 @@ public void testGroupClusterIndicesFail() {
158161

159162
}
160163

164+
public void testGetRemoteIndexExpressions() {
165+
{
166+
List<String> remoteIndexExpressions = RemoteClusterAware.getRemoteIndexExpressions("index-1");
167+
assertThat(remoteIndexExpressions, empty());
168+
}
169+
{
170+
List<String> remoteIndexExpressions = RemoteClusterAware.getRemoteIndexExpressions(
171+
"index-1",
172+
"remote:index-1",
173+
"idx-*",
174+
"remote-2:idx-5"
175+
);
176+
assertThat(remoteIndexExpressions, hasSize(2));
177+
assertThat(remoteIndexExpressions, contains("remote:index-1", "remote-2:idx-5"));
178+
}
179+
}
180+
161181
private static class RemoteClusterAwareTest extends RemoteClusterAware {
162182
RemoteClusterAwareTest() {
163183
super(Settings.EMPTY);

0 commit comments

Comments
 (0)