|
9 | 9 |
|
10 | 10 | package org.elasticsearch.cluster.metadata; |
11 | 11 |
|
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | +import org.elasticsearch.CrossProjectResolvableRequest; |
12 | 15 | import org.elasticsearch.action.support.IndexComponentSelector; |
13 | 16 | import org.elasticsearch.action.support.IndicesOptions; |
14 | 17 | import org.elasticsearch.action.support.UnsupportedSelectorException; |
|
18 | 21 | import org.elasticsearch.index.Index; |
19 | 22 | import org.elasticsearch.index.IndexNotFoundException; |
20 | 23 | import org.elasticsearch.indices.SystemIndices.SystemIndexAccessLevel; |
| 24 | +import org.elasticsearch.transport.RemoteClusterAware; |
21 | 25 |
|
22 | 26 | import java.util.ArrayList; |
23 | 27 | import java.util.HashSet; |
|
28 | 32 |
|
29 | 33 | public class IndexAbstractionResolver { |
30 | 34 |
|
| 35 | + private static final Logger logger = LogManager.getLogger(IndexAbstractionResolver.class); |
| 36 | + |
31 | 37 | private final IndexNameExpressionResolver indexNameExpressionResolver; |
32 | 38 |
|
33 | 39 | public IndexAbstractionResolver(IndexNameExpressionResolver indexNameExpressionResolver) { |
34 | 40 | this.indexNameExpressionResolver = indexNameExpressionResolver; |
35 | 41 | } |
36 | 42 |
|
| 43 | + public List<CrossProjectResolvableRequest.RewrittenExpression> resolveIndexAbstractionsForOrigin( |
| 44 | + Iterable<CrossProjectResolvableRequest.RewrittenExpression> rewrittenExpressions, |
| 45 | + IndicesOptions indicesOptions, |
| 46 | + ProjectMetadata projectMetadata, |
| 47 | + Function<IndexComponentSelector, Set<String>> allAuthorizedAndAvailableBySelector, |
| 48 | + BiPredicate<String, IndexComponentSelector> isAuthorized, |
| 49 | + boolean includeDataStreams |
| 50 | + ) { |
| 51 | + // TODO handle exclusions and consolidate with `resolveIndexAbstractions` somehow, maybe? |
| 52 | + List<CrossProjectResolvableRequest.RewrittenExpression> finalRewrittenExpressions = new ArrayList<>(); |
| 53 | + for (CrossProjectResolvableRequest.RewrittenExpression rewrittenExpression : rewrittenExpressions) { |
| 54 | + // qualified original expression, nothing to rewrite (TODO handle origin) |
| 55 | + if (RemoteClusterAware.isRemoteIndexName(rewrittenExpression.original())) { |
| 56 | + logger.info("Skipping rewriting of qualified expression [{}] for origin", rewrittenExpression.original()); |
| 57 | + finalRewrittenExpressions.add(rewrittenExpression); |
| 58 | + continue; |
| 59 | + } |
| 60 | + // no expressions targeting origin, also nothing to rewrite |
| 61 | + if (rewrittenExpression.canonicalExpressions() |
| 62 | + .stream() |
| 63 | + .noneMatch(CrossProjectResolvableRequest.CanonicalExpression::isUnqualified)) { |
| 64 | + logger.info( |
| 65 | + "Skipping rewriting of qualified expression [{}] because there are no origin expressions", |
| 66 | + rewrittenExpression.original() |
| 67 | + ); |
| 68 | + finalRewrittenExpressions.add(rewrittenExpression); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + String indexAbstraction = rewrittenExpression.original(); |
| 73 | + |
| 74 | + // Always check to see if there's a selector on the index expression |
| 75 | + Tuple<String, String> expressionAndSelector = IndexNameExpressionResolver.splitSelectorExpression(indexAbstraction); |
| 76 | + String selectorString = expressionAndSelector.v2(); |
| 77 | + if (indicesOptions.allowSelectors() == false && selectorString != null) { |
| 78 | + throw new UnsupportedSelectorException(indexAbstraction); |
| 79 | + } |
| 80 | + indexAbstraction = expressionAndSelector.v1(); |
| 81 | + IndexComponentSelector selector = IndexComponentSelector.getByKeyOrThrow(selectorString); |
| 82 | + |
| 83 | + // we always need to check for date math expressions |
| 84 | + indexAbstraction = IndexNameExpressionResolver.resolveDateMathExpression(indexAbstraction); |
| 85 | + |
| 86 | + if (indicesOptions.expandWildcardExpressions() && Regex.isSimpleMatchPattern(indexAbstraction)) { |
| 87 | + Set<String> resolvedIndices = new HashSet<>(); |
| 88 | + for (String authorizedIndex : allAuthorizedAndAvailableBySelector.apply(selector)) { |
| 89 | + if (Regex.simpleMatch(indexAbstraction, authorizedIndex) |
| 90 | + && isIndexVisible( |
| 91 | + indexAbstraction, |
| 92 | + selectorString, |
| 93 | + authorizedIndex, |
| 94 | + indicesOptions, |
| 95 | + projectMetadata, |
| 96 | + indexNameExpressionResolver, |
| 97 | + includeDataStreams |
| 98 | + )) { |
| 99 | + resolveSelectorsAndCollect(authorizedIndex, selectorString, indicesOptions, resolvedIndices, projectMetadata); |
| 100 | + } |
| 101 | + } |
| 102 | + if (resolvedIndices.isEmpty()) { |
| 103 | + // es core honours allow_no_indices for each wildcard expression, we do the same here by throwing index not found. |
| 104 | + if (indicesOptions.allowNoIndices() == false) { |
| 105 | + // TODO gotta do something here |
| 106 | + } |
| 107 | + finalRewrittenExpressions.add(replaceOriginIndices(rewrittenExpression, Set.of())); |
| 108 | + } else { |
| 109 | + finalRewrittenExpressions.add(replaceOriginIndices(rewrittenExpression, resolvedIndices)); |
| 110 | + } |
| 111 | + } else { |
| 112 | + Set<String> resolvedIndices = new HashSet<>(); |
| 113 | + resolveSelectorsAndCollect(indexAbstraction, selectorString, indicesOptions, resolvedIndices, projectMetadata); |
| 114 | + |
| 115 | + // TODO this is probably not right? |
| 116 | + if (indicesOptions.ignoreUnavailable() == false) { |
| 117 | + finalRewrittenExpressions.add(replaceOriginIndices(rewrittenExpression, resolvedIndices)); |
| 118 | + } else if (isAuthorized.test(indexAbstraction, selector)) { |
| 119 | + finalRewrittenExpressions.add(replaceOriginIndices(rewrittenExpression, resolvedIndices)); |
| 120 | + } else { |
| 121 | + finalRewrittenExpressions.add(replaceOriginIndices(rewrittenExpression, Set.of())); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + // TODO assert size is the same |
| 126 | + return finalRewrittenExpressions; |
| 127 | + } |
| 128 | + |
| 129 | + private static CrossProjectResolvableRequest.RewrittenExpression replaceOriginIndices( |
| 130 | + CrossProjectResolvableRequest.RewrittenExpression rewrittenExpression, |
| 131 | + Set<String> resolvedIndices |
| 132 | + ) { |
| 133 | + List<CrossProjectResolvableRequest.CanonicalExpression> resolvedOriginalExpressions = resolvedIndices.stream() |
| 134 | + .map(CrossProjectResolvableRequest.CanonicalExpression::new) |
| 135 | + .toList(); |
| 136 | + List<CrossProjectResolvableRequest.CanonicalExpression> qualifiedExpressions = rewrittenExpression.canonicalExpressions() |
| 137 | + .stream() |
| 138 | + .filter(CrossProjectResolvableRequest.CanonicalExpression::isQualified) |
| 139 | + .toList(); |
| 140 | + List<CrossProjectResolvableRequest.CanonicalExpression> combined = new ArrayList<>(resolvedOriginalExpressions); |
| 141 | + combined.addAll(qualifiedExpressions); |
| 142 | + var e = new CrossProjectResolvableRequest.RewrittenExpression(rewrittenExpression.original(), combined); |
| 143 | + logger.info("Replaced origin indices for expression [{}] with [{}]", rewrittenExpression, e); |
| 144 | + return e; |
| 145 | + } |
| 146 | + |
37 | 147 | public List<String> resolveIndexAbstractions( |
38 | 148 | Iterable<String> indices, |
39 | 149 | IndicesOptions indicesOptions, |
@@ -105,6 +215,7 @@ && isIndexVisible( |
105 | 215 | // discarded from the `finalIndices` list. Other "ways of unavailable" must be handled by the action |
106 | 216 | // handler, see: https://github.com/elastic/elasticsearch/issues/90215 |
107 | 217 | finalIndices.addAll(resolvedIndices); |
| 218 | + |
108 | 219 | } |
109 | 220 | } |
110 | 221 | } |
|
0 commit comments