|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.search.crossproject; |
| 11 | + |
| 12 | +import org.elasticsearch.TransportVersion; |
| 13 | +import org.elasticsearch.cluster.metadata.ClusterNameExpressionResolver; |
| 14 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 15 | +import org.elasticsearch.core.Nullable; |
| 16 | +import org.elasticsearch.logging.LogManager; |
| 17 | +import org.elasticsearch.logging.Logger; |
| 18 | +import org.elasticsearch.transport.NoSuchRemoteClusterException; |
| 19 | +import org.elasticsearch.transport.RemoteClusterAware; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility class for rewriting cross-project index expressions. |
| 30 | + * Provides methods that can rewrite qualified and unqualified index expressions to canonical CCS. |
| 31 | + */ |
| 32 | +public class CrossProjectIndexExpressionsRewriter { |
| 33 | + public static TransportVersion NO_MATCHING_PROJECT_EXCEPTION_VERSION = TransportVersion.fromName("no_matching_project_exception"); |
| 34 | + |
| 35 | + private static final Logger logger = LogManager.getLogger(CrossProjectIndexExpressionsRewriter.class); |
| 36 | + private static final String ORIGIN_PROJECT_KEY = "_origin"; |
| 37 | + private static final String WILDCARD = "*"; |
| 38 | + private static final String[] MATCH_ALL = new String[] { WILDCARD }; |
| 39 | + private static final String EXCLUSION = "-"; |
| 40 | + private static final String DATE_MATH = "<"; |
| 41 | + |
| 42 | + /** |
| 43 | + * Rewrites index expressions for cross-project search requests. |
| 44 | + * Handles qualified and unqualified expressions and match-all cases will also hand exclusions in the future. |
| 45 | + * |
| 46 | + * @param originProject the _origin project with its alias |
| 47 | + * @param linkedProjects the list of linked and available projects to consider for a request |
| 48 | + * @param originalIndices the array of index expressions to be rewritten to canonical CCS |
| 49 | + * @return a map from original index expressions to lists of canonical index expressions |
| 50 | + * @throws IllegalArgumentException if exclusions, date math or selectors are present in the index expressions |
| 51 | + * @throws NoMatchingProjectException if a qualified resource cannot be resolved because a project is missing |
| 52 | + */ |
| 53 | + public static Map<String, List<String>> rewriteIndexExpressions( |
| 54 | + ProjectRoutingInfo originProject, |
| 55 | + List<ProjectRoutingInfo> linkedProjects, |
| 56 | + final String[] originalIndices |
| 57 | + ) { |
| 58 | + final String[] indices; |
| 59 | + if (originalIndices == null || originalIndices.length == 0) { // handling of match all cases besides _all and `*` |
| 60 | + indices = MATCH_ALL; |
| 61 | + } else { |
| 62 | + indices = originalIndices; |
| 63 | + } |
| 64 | + assert false == IndexNameExpressionResolver.isNoneExpression(indices) |
| 65 | + : "expression list is *,-* which effectively means a request that requests no indices"; |
| 66 | + assert originProject != null || linkedProjects.isEmpty() == false |
| 67 | + : "either origin project or linked projects must be in project target set"; |
| 68 | + |
| 69 | + Set<String> linkedProjectNames = linkedProjects.stream().map(ProjectRoutingInfo::projectAlias).collect(Collectors.toSet()); |
| 70 | + Map<String, List<String>> canonicalExpressionsMap = new LinkedHashMap<>(indices.length); |
| 71 | + for (String resource : indices) { |
| 72 | + if (canonicalExpressionsMap.containsKey(resource)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + maybeThrowOnUnsupportedResource(resource); |
| 76 | + |
| 77 | + boolean isQualified = RemoteClusterAware.isRemoteIndexName(resource); |
| 78 | + if (isQualified) { |
| 79 | + // handing of qualified expressions |
| 80 | + String[] splitResource = RemoteClusterAware.splitIndexName(resource); |
| 81 | + assert splitResource.length == 2 |
| 82 | + : "Expected two strings (project and indexExpression) for a qualified resource [" |
| 83 | + + resource |
| 84 | + + "], but found [" |
| 85 | + + splitResource.length |
| 86 | + + "]"; |
| 87 | + String projectAlias = splitResource[0]; |
| 88 | + assert projectAlias != null : "Expected a project alias for a qualified resource but was null"; |
| 89 | + String indexExpression = splitResource[1]; |
| 90 | + maybeThrowOnUnsupportedResource(indexExpression); |
| 91 | + |
| 92 | + List<String> canonicalExpressions = rewriteQualified(projectAlias, indexExpression, originProject, linkedProjectNames); |
| 93 | + |
| 94 | + canonicalExpressionsMap.put(resource, canonicalExpressions); |
| 95 | + logger.debug("Rewrote qualified expression [{}] to [{}]", resource, canonicalExpressions); |
| 96 | + } else { |
| 97 | + // un-qualified expression, i.e. flat-world |
| 98 | + List<String> canonicalExpressions = rewriteUnqualified(resource, originProject, linkedProjects); |
| 99 | + canonicalExpressionsMap.put(resource, canonicalExpressions); |
| 100 | + logger.debug("Rewrote unqualified expression [{}] to [{}]", resource, canonicalExpressions); |
| 101 | + } |
| 102 | + } |
| 103 | + return canonicalExpressionsMap; |
| 104 | + } |
| 105 | + |
| 106 | + private static List<String> rewriteUnqualified( |
| 107 | + String indexExpression, |
| 108 | + @Nullable ProjectRoutingInfo origin, |
| 109 | + List<ProjectRoutingInfo> projects |
| 110 | + ) { |
| 111 | + List<String> canonicalExpressions = new ArrayList<>(); |
| 112 | + if (origin != null) { |
| 113 | + canonicalExpressions.add(indexExpression); // adding the original indexExpression for the _origin cluster. |
| 114 | + } |
| 115 | + for (ProjectRoutingInfo targetProject : projects) { |
| 116 | + canonicalExpressions.add(RemoteClusterAware.buildRemoteIndexName(targetProject.projectAlias(), indexExpression)); |
| 117 | + } |
| 118 | + return canonicalExpressions; |
| 119 | + } |
| 120 | + |
| 121 | + private static List<String> rewriteQualified( |
| 122 | + String requestedProjectAlias, |
| 123 | + String indexExpression, |
| 124 | + @Nullable ProjectRoutingInfo originProject, |
| 125 | + Set<String> allProjectAliases |
| 126 | + ) { |
| 127 | + if (originProject != null && ORIGIN_PROJECT_KEY.equals(requestedProjectAlias)) { |
| 128 | + // handling case where we have a qualified expression like: _origin:indexName |
| 129 | + return List.of(indexExpression); |
| 130 | + } |
| 131 | + |
| 132 | + if (originProject == null && ORIGIN_PROJECT_KEY.equals(requestedProjectAlias)) { |
| 133 | + // handling case where we have a qualified expression like: _origin:indexName but no _origin project is set |
| 134 | + throw new NoMatchingProjectException(requestedProjectAlias); |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + if (originProject != null) { |
| 139 | + allProjectAliases.add(originProject.projectAlias()); |
| 140 | + } |
| 141 | + List<String> resourcesMatchingAliases = new ArrayList<>(); |
| 142 | + List<String> allProjectsMatchingAlias = ClusterNameExpressionResolver.resolveClusterNames( |
| 143 | + allProjectAliases, |
| 144 | + requestedProjectAlias |
| 145 | + ); |
| 146 | + |
| 147 | + if (allProjectsMatchingAlias.isEmpty()) { |
| 148 | + throw new NoMatchingProjectException(requestedProjectAlias); |
| 149 | + } |
| 150 | + |
| 151 | + for (String project : allProjectsMatchingAlias) { |
| 152 | + if (originProject != null && project.equals(originProject.projectAlias())) { |
| 153 | + resourcesMatchingAliases.add(indexExpression); |
| 154 | + } else { |
| 155 | + resourcesMatchingAliases.add(RemoteClusterAware.buildRemoteIndexName(project, indexExpression)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return resourcesMatchingAliases; |
| 160 | + } catch (NoSuchRemoteClusterException ex) { |
| 161 | + logger.debug(ex.getMessage(), ex); |
| 162 | + throw new NoMatchingProjectException(requestedProjectAlias); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private static void maybeThrowOnUnsupportedResource(String resource) { |
| 167 | + // TODO To be handled in future PR. |
| 168 | + if (resource.startsWith(EXCLUSION)) { |
| 169 | + throw new IllegalArgumentException("Exclusions are not currently supported but was found in the expression [" + resource + "]"); |
| 170 | + } |
| 171 | + if (resource.startsWith(DATE_MATH)) { |
| 172 | + throw new IllegalArgumentException("Date math are not currently supported but was found in the expression [" + resource + "]"); |
| 173 | + } |
| 174 | + if (IndexNameExpressionResolver.hasSelectorSuffix(resource)) { |
| 175 | + throw new IllegalArgumentException("Selectors are not currently supported but was found in the expression [" + resource + "]"); |
| 176 | + |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments