Skip to content

Commit b3c3f9d

Browse files
Handle the indices pattern ["*", "-*"] when grouping indices by cluster name
1 parent 83a13b9 commit b3c3f9d

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,22 @@ public Map<String, OriginalIndices> groupIndices(
201201
boolean returnLocalAll
202202
) {
203203
final Map<String, OriginalIndices> originalIndicesMap = new HashMap<>();
204-
final Map<String, List<String>> groupedIndices = groupClusterIndices(remoteClusterNames, indices);
204+
final Map<String, List<String>> groupedIndices;
205+
/*
206+
* We could use IndicesAndAliasesResolverField.NO_INDICES_OR_ALIASES_ARRAY but that'd require adding dependency on its
207+
* module and doing so results in a circular dependency warnings.
208+
*/
209+
if (indices.length == 2 && indices[0].equals("*") && indices[1].equals("-*")) {
210+
groupedIndices = Map.of();
211+
/*
212+
* We set returnLocalAll to false because this semantic ["*", "-*"] specifically means that it's alright to return
213+
* an empty response and in this context we do not want to fallback to the local cluster.
214+
*/
215+
returnLocalAll = false;
216+
} else {
217+
groupedIndices = groupClusterIndices(remoteClusterNames, indices);
218+
}
219+
205220
if (groupedIndices.isEmpty()) {
206221
if (returnLocalAll) {
207222
// search on _all in the local cluster if neither local indices nor remote indices were specified

x-pack/plugin/security/qa/multi-cluster/src/javaRestTest/java/org/elasticsearch/xpack/remotecluster/RemoteClusterSecurityRCS1ResolveClusterIT.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,15 @@ public void testResolveClusterUnderRCS1() throws Exception {
281281
);
282282
assertThat(exc.getMessage(), containsString(indexOptionTuple.v1()));
283283
}
284-
// TODO: The security pathways are not using the new
285-
// RemoteClusterService.groupIndices(IndicesOptions indicesOptions, String[] indices, boolean returnLocalAll) method
286-
// so this use case still behaves badly - fix in follow on PR
287-
// {
288-
// // TEST CASE 13: Resolution against wildcarded remote cluster expression that matches no remotes
289-
// final Request remoteOnly1 = new Request("GET", "_resolve/cluster/no_such_remote*:*");
290-
// Response response = performRequestWithRemoteSearchUser(remoteOnly1);
291-
// assertOK(response);
292-
// Map<String, Object> responseMap = responseAsMap(response);
293-
// assertThat(responseMap.get(LOCAL_CLUSTER_NAME_REPRESENTATION), nullValue());
294-
// Map<String, Object> remoteMap = (Map<String, Object>) responseMap.get("my_remote_cluster");
295-
// assertThat((Boolean) remoteMap.get("connected"), equalTo(true));
296-
// assertThat((Boolean) remoteMap.get("matching_indices"), equalTo(false));
297-
// assertThat(remoteMap.get("version"), notNullValue());
298-
// }
284+
{
285+
// TEST CASE 13: Resolution against wildcarded remote cluster expression that matches no remotes should result in an
286+
// empty response and not fall back to the local cluster.
287+
final Request remoteOnly1 = new Request("GET", "_resolve/cluster/no_such_remote*:*");
288+
Response response = performRequestWithRemoteSearchUser(remoteOnly1);
289+
assertOK(response);
290+
Map<String, Object> responseMap = responseAsMap(response);
291+
assertThat(responseMap.isEmpty(), is(true));
292+
}
299293
}
300294

301295
private Response performRequestWithRemoteSearchUser(final Request request) throws IOException {

x-pack/plugin/security/qa/multi-cluster/src/javaRestTest/java/org/elasticsearch/xpack/remotecluster/RemoteClusterSecurityRCS2ResolveClusterIT.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,15 @@ public void testResolveCluster() throws Exception {
376376
);
377377
assertThat(exc.getMessage(), containsString(indexOptionTuple.v1()));
378378
}
379-
// TODO: fix this in a follow-on PR
380-
// {
381-
// // TEST CASE 13: Resolution against wildcarded remote cluster expression that matches no remotes
382-
// final Request remoteOnly1 = new Request("GET", "_resolve/cluster/no_such_remote*:*");
383-
// Response response = performRequestWithRemoteSearchUser(remoteOnly1);
384-
// assertOK(response);
385-
// Map<String, Object> responseMap = responseAsMap(response);
386-
// assertThat(responseMap.get(LOCAL_CLUSTER_NAME_REPRESENTATION), nullValue());
387-
// Map<String, Object> remoteMap = (Map<String, Object>) responseMap.get("my_remote_cluster");
388-
// assertThat((Boolean) remoteMap.get("connected"), equalTo(true));
389-
// assertThat((Boolean) remoteMap.get("matching_indices"), equalTo(false));
390-
// assertThat(remoteMap.get("version"), notNullValue());
391-
// }
379+
{
380+
// TEST CASE 13: Resolution against wildcarded remote cluster expression that matches no remotes should result in an
381+
// empty response and not fall back to the local cluster.
382+
final Request remoteOnly1 = new Request("GET", "_resolve/cluster/no_such_remote*:*");
383+
Response response = performRequestWithRemoteSearchUser(remoteOnly1);
384+
assertOK(response);
385+
Map<String, Object> responseMap = responseAsMap(response);
386+
assertThat(responseMap.isEmpty(), is(true));
387+
}
392388
}
393389

394390
private Response performRequestWithRemoteSearchUser(final Request request) throws IOException {

0 commit comments

Comments
 (0)