Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8f324ae
Check if index patterns conform to valid format before validation
pawankartik-elastic Feb 13, 2025
1ddbce4
Update docs/changelog/122497.yaml
pawankartik-elastic Feb 13, 2025
46758cb
Let `validateClusterString()` look for `REMOTE_CLUSTER_INDEX_SEPARATOR`
pawankartik-elastic Feb 13, 2025
2f2ee85
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Feb 13, 2025
7ca6d70
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Apr 7, 2025
9dd96df
Mute 3 cases till further clarification and fix error string message
pawankartik-elastic Apr 7, 2025
0686c8f
Fix tests
pawankartik-elastic Apr 7, 2025
7c55d6c
Fix bug in breaking down indices
pawankartik-elastic Apr 8, 2025
2653377
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Apr 10, 2025
5832ff0
Tiny refactoring around how wildcard is processed and added tests
pawankartik-elastic Apr 10, 2025
6cdbf5a
[CI] Auto commit changes from spotless
Apr 10, 2025
c2160bb
Drop duplicated test cases and fix flaky-ness caused by quoting
pawankartik-elastic Apr 10, 2025
544171b
Set cluster string to `null` when it cannot be associated with an index
pawankartik-elastic Apr 11, 2025
133a7c7
Generate correct invalid patterns
pawankartik-elastic Apr 11, 2025
7da29d8
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Apr 11, 2025
5c70d51
Address review comments and don't break indices into its constituents
pawankartik-elastic Apr 16, 2025
7ad6ac3
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Jun 18, 2025
3058610
Adhere to the new grammar
pawankartik-elastic Jun 19, 2025
376126e
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Jun 19, 2025
1a43029
Update docs/changelog/122497.yaml
pawankartik-elastic Jun 19, 2025
8363b70
Update x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/…
pawankartik-elastic Jun 20, 2025
9c16afb
Apply suggestions from review
pawankartik-elastic Jun 23, 2025
8f9f519
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Jun 23, 2025
b6339e7
Apply suggestions from code review
pawankartik-elastic Jun 23, 2025
0cc5580
Apply suggestions from review
pawankartik-elastic Jun 23, 2025
816fcc1
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Jun 23, 2025
0346762
Do not mention asterisk as an invalid char
pawankartik-elastic Jun 25, 2025
9d275a5
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/122497.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 122497
summary: Check if index patterns conform to valid format before validation
area: CCS
type: bug
issues: []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently permits white spaces between separators and index name, like FROM "remote : idx" and FROM idx :: failures; it fails with less helpful error messages later down the line.

I think this is related to #129768, but not exactly the same. To avoid scope creep, we can tackle this in a follow-up or make it part of the other issue. I added a comment #129768 (comment) so we don't forget.

Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
import org.elasticsearch.core.Tuple;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.transport.RemoteClusterService;
import org.elasticsearch.xpack.esql.core.util.Holder;
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IdentifierContext;
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IndexStringContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.SelectorResolver.SELECTOR_SEPARATOR;
import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR;
import static org.elasticsearch.transport.RemoteClusterAware.isRemoteIndexName;
import static org.elasticsearch.transport.RemoteClusterAware.splitIndexName;
import static org.elasticsearch.xpack.esql.core.util.StringUtils.EXCLUSION;
import static org.elasticsearch.xpack.esql.core.util.StringUtils.WILDCARD;
import static org.elasticsearch.xpack.esql.parser.ParserUtils.source;
Expand Down Expand Up @@ -87,33 +88,12 @@ public String visitSelectorString(EsqlBaseParser.SelectorStringContext ctx) {

public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) {
List<String> patterns = new ArrayList<>(ctx.size());
Holder<Boolean> hasSeenStar = new Holder<>(false);
ctx.forEach(c -> {
String indexPattern = visitIndexString(c.indexString());
String clusterString = visitClusterString(c.clusterString());
String selectorString = visitSelectorString(c.selectorString());
// skip validating index on remote cluster, because the behavior of remote cluster is not consistent with local cluster
// For example, invalid#index is an invalid index name, however FROM *:invalid#index does not return an error
if (clusterString == null) {
hasSeenStar.set(indexPattern.contains(WILDCARD) || hasSeenStar.get());
validateIndexPattern(indexPattern, c, hasSeenStar.get());
// Other instances of Elasticsearch may have differing selectors so only validate selector string if remote cluster
// string is unset
if (selectorString != null) {
try {
// Ensures that the selector provided is one of the valid kinds
IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexPattern, selectorString);
} catch (InvalidIndexNameException e) {
throw new ParsingException(e, source(c), e.getMessage());
}
}
} else {
validateClusterString(clusterString, c);
// Do not allow selectors on remote cluster expressions until they are supported
if (selectorString != null) {
throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, indexPattern, selectorString), c);
}
}

validateClusterAndIndexPatterns(indexPattern, c, clusterString, selectorString);
patterns.add(reassembleIndexName(clusterString, indexPattern, selectorString));
});
return Strings.collectionToDelimitedString(patterns, ",");
Expand Down Expand Up @@ -148,35 +128,109 @@ protected static void validateClusterString(String clusterString, EsqlBaseParser
}
}

private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx, boolean hasSeenStar) {
private static void validateClusterAndIndexPatterns(
String indexPattern,
EsqlBaseParser.IndexPatternContext ctx,
String clusterString,
String selectorString
) {
// multiple index names can be in the same double quote, e.g. indexPattern = "idx1, *, -idx2"
String[] indices = indexPattern.split(",");
boolean hasExclusion = false;
String[] patterns = indexPattern.split(",");
boolean isFirstPattern = true;

for (String pattern : patterns) {
pattern = pattern.strip();
String[] indices = new String[] { pattern };

/*
* Just because there was no clusterString before this index pattern does not mean that the indices
* are local indices. Patterns can be clubbed with remote names within quotes such as:
* "remote_one:remote_index,local_index". In this case, clusterString will be null.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the nice comment!

*/
if (isRemoteIndexName(pattern)) {
/*
* Handle scenarios like remote_one:"index1,remote_two:index2". The clusterString here is
* remote_one and is associated with index1 and not index2.
*/
if (clusterString != null && isFirstPattern) {
throw new ParsingException(
source(ctx),
"Index pattern [{}] contains a cluster alias despite specifying one [{}]",
pattern,
clusterString
);
}

// {cluster_alias, indexName}
String[] clusterAliasAndIndex = splitIndexName(pattern);
clusterString = clusterAliasAndIndex[0];
indices[0] = clusterAliasAndIndex[1];

/*
* What if the pattern is index1|index2? We cannot split at the pipe char blindly as that'd mess with
* logstash-like examples. We only split this way if an index is associated with a remote cluster.
*/
indices = Arrays.stream(indices)
.map(IdentifierBuilder::breakPatternIntoIndices)
.flatMap(Arrays::stream)
.toArray(String[]::new);
} else if (clusterString != null) {
// This is not a remote index pattern and the cluster string preceding this quoted pattern
// cannot be associated with it.
if (isFirstPattern == false) {
clusterString = null;
}

// Cluster alias was prefixed to the pattern and did not occur within the pattern.
indices = Arrays.stream(indices)
.map(IdentifierBuilder::breakPatternIntoIndices)
.flatMap(Arrays::stream)
.toArray(String[]::new);
}

if (clusterString != null) {
if (selectorString != null) {
throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, indexPattern, selectorString), ctx);
}
validateClusterString(clusterString, ctx);
}

validateIndicesForCluster(clusterString, indices, ctx);
if (selectorString != null) {
try {
// Ensures that the selector provided is one of the valid kinds
IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexPattern, selectorString);
} catch (InvalidIndexNameException e) {
throw new ParsingException(e, source(ctx), e.getMessage());
}
}

isFirstPattern = false;
}
}

private static void validateIndicesForCluster(String clusterString, String[] indices, EsqlBaseParser.IndexPatternContext ctx) {
for (String index : indices) {
// Strip spaces off first because validation checks are not written to handle them
index = index.strip();
if (isRemoteIndexName(index)) { // skip the validation if there is remote cluster
// Ensure that there are no selectors as they are not yet supported
if (index.contains(SELECTOR_SEPARATOR)) {
throwOnMixingSelectorWithCluster(index, ctx);
}
continue;
}

try {
Tuple<String, String> splitPattern = IndexNameExpressionResolver.splitSelectorExpression(index);
if (splitPattern.v2() != null) {
index = splitPattern.v1();
if (splitPattern.v2() != null && clusterString != null) {
throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, splitPattern.v1(), splitPattern.v2()), ctx);
}

index = splitPattern.v1();
} catch (InvalidIndexNameException e) {
// throws exception if the selector expression is invalid. Selector resolution does not complain about exclusions
throw new ParsingException(e, source(ctx), e.getMessage());
}
hasSeenStar = index.contains(WILDCARD) || hasSeenStar;
var hasSeenStar = index.contains(WILDCARD);
index = index.replace(WILDCARD, "").strip();
if (index.isBlank()) {
continue;
}
hasExclusion = index.startsWith(EXCLUSION);
var hasExclusion = index.startsWith(EXCLUSION);
index = removeExclusion(index);
String tempName;
try {
Expand All @@ -198,9 +252,44 @@ private static void validateIndexPattern(String indexPattern, EsqlBaseParser.Ind
}
throw new ParsingException(e, source(ctx), e.getMessage());
}

}
}

private static String[] breakPatternIntoIndices(String pattern) {
if (pattern.codePoints().anyMatch(ch -> ch == ',')) {
throw new IllegalArgumentException("Found grouped index patterns, expecting a single pattern");
}

// Fast path: if there's no pipe char, no point in attempting to break down the string.
if (pattern.contains("|") == false) {
return new String[] { pattern };
}

var indices = new ArrayList<String>();
var sb = new StringBuilder();
var inDateMathExpr = false;
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
sb.append(c);
if (c == '<') {
inDateMathExpr = true;
} else if (c == '>') {
inDateMathExpr = false;
} else if (c == '|' && inDateMathExpr == false) {
sb.deleteCharAt(sb.length() - 1);
indices.add(sb.toString());
sb.setLength(0);
}
}

if (sb.isEmpty() == false) {
indices.add(sb.toString());
}

return indices.toArray(new String[0]);
}

private static String removeExclusion(String indexPattern) {
return indexPattern.charAt(0) == EXCLUSION.charAt(0) ? indexPattern.substring(1) : indexPattern;
}
Expand Down
Loading
Loading