Skip to content

Commit cce99ce

Browse files
richard-dennehyelasticsearchmachinen1v0lg
authored andcommitted
record index resolution for _all expressions (elastic#135425)
* record index resolution for _all expressions * [CI] Auto commit changes from spotless * cleanup * fix compile * include remote expressions in _all index resolution * address review comments * clean up tests * [CI] Auto commit changes from spotless * address review comments * [CI] Auto commit changes from spotless * update remote indices list * use order-insensitive assertion --------- Co-authored-by: elasticsearchmachine <[email protected]> Co-authored-by: Nikolaj Volgushev <[email protected]>
1 parent a751cdb commit cce99ce

File tree

2 files changed

+226
-91
lines changed

2 files changed

+226
-91
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.logging.log4j.Logger;
1111
import org.elasticsearch.action.AliasesRequest;
1212
import org.elasticsearch.action.IndicesRequest;
13+
import org.elasticsearch.action.ResolvedIndexExpression;
1314
import org.elasticsearch.action.ResolvedIndexExpressions;
1415
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
1516
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.core.Tuple;
3334
import org.elasticsearch.index.Index;
3435
import org.elasticsearch.index.IndexNotFoundException;
36+
import org.elasticsearch.search.crossproject.CrossProjectIndexExpressionsRewriter;
3537
import org.elasticsearch.search.crossproject.CrossProjectModeDecider;
3638
import org.elasticsearch.search.crossproject.TargetProjects;
3739
import org.elasticsearch.transport.LinkedProjectConfig;
@@ -347,14 +349,16 @@ ResolvedIndices resolveIndicesAndAliases(
347349
} else {
348350
isAllIndices = IndexNameExpressionResolver.isAllIndices(indicesList(indicesRequest.indices()));
349351
}
352+
350353
if (isAllIndices) {
351354
// First, if a selector is present, check to make sure that selectors are even allowed here
352355
if (indicesOptions.allowSelectors() == false && allIndicesPatternSelector != null) {
353356
String originalIndexExpression = indicesRequest.indices()[0];
354357
throw new UnsupportedSelectorException(originalIndexExpression);
355358
}
356359
if (indicesOptions.expandWildcardExpressions()) {
357-
// TODO implement CPS index rewriting for all-indices requests
360+
var localExpressions = new HashSet<String>();
361+
358362
IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(allIndicesPatternSelector);
359363
for (String authorizedIndex : authorizedIndices.all(selector)) {
360364
if (IndexAbstractionResolver.isIndexVisible(
@@ -366,12 +370,43 @@ ResolvedIndices resolveIndicesAndAliases(
366370
nameExpressionResolver,
367371
indicesRequest.includeDataStreams()
368372
)) {
369-
resolvedIndicesBuilder.addLocal(
373+
localExpressions.add(
370374
IndexNameExpressionResolver.combineSelectorExpression(authorizedIndex, allIndicesPatternSelector)
371375
);
372376
}
373377
}
378+
379+
var resolvedExpressionsBuilder = ResolvedIndexExpressions.builder();
380+
final var indexExpression = indicesRequest.indices() != null && indicesRequest.indices().length > 0
381+
? indicesRequest.indices()[0]
382+
: Metadata.ALL;
383+
384+
Set<String> remoteIndices = Collections.emptySet();
385+
if (crossProjectModeDecider.resolvesCrossProject(replaceable)) {
386+
remoteIndices = CrossProjectIndexExpressionsRewriter.rewriteIndexExpression(
387+
indexExpression,
388+
authorizedProjects.originProjectAlias(),
389+
authorizedProjects.allProjectAliases()
390+
).remoteExpressions();
391+
}
392+
393+
resolvedExpressionsBuilder.addExpressions(
394+
indexExpression,
395+
localExpressions,
396+
ResolvedIndexExpression.LocalIndexResolutionResult.SUCCESS,
397+
remoteIndices
398+
);
399+
var resolved = resolvedExpressionsBuilder.build();
400+
401+
if (crossProjectModeDecider.crossProjectEnabled()) {
402+
setResolvedIndexExpressionsIfUnset(replaceable, resolved);
403+
}
404+
resolvedIndicesBuilder.addLocal(resolved.getLocalIndicesList());
405+
resolvedIndicesBuilder.addRemote(resolved.getRemoteIndicesList());
406+
} else if (crossProjectModeDecider.crossProjectEnabled()) {
407+
setResolvedIndexExpressionsIfUnset(replaceable, ResolvedIndexExpressions.builder().build());
374408
}
409+
375410
// if we cannot replace wildcards the indices list stays empty. Same if there are no authorized indices.
376411
// we honour allow_no_indices like es core does.
377412
} else {

0 commit comments

Comments
 (0)