Skip to content

Commit d58db78

Browse files
committed
add basic cluster string validation
1 parent c184382 commit d58db78

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/IdentifierBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.cluster.metadata.MetadataCreateIndexService;
1414
import org.elasticsearch.common.Strings;
1515
import org.elasticsearch.indices.InvalidIndexNameException;
16+
import org.elasticsearch.transport.RemoteClusterService;
1617
import org.elasticsearch.xpack.esql.core.util.Holder;
1718
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IdentifierContext;
1819
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IndexStringContext;
@@ -82,12 +83,20 @@ public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) {
8283
if (clusterString == null) {
8384
hasSeenStar.set(indexPattern.contains(WILDCARD) || hasSeenStar.get());
8485
validateIndexPattern(indexPattern, c, hasSeenStar.get());
86+
} else {
87+
validateClusterString(clusterString, c);
8588
}
8689
patterns.add(clusterString != null ? clusterString + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern);
8790
});
8891
return Strings.collectionToDelimitedString(patterns, ",");
8992
}
9093

94+
protected static void validateClusterString(String clusterString, EsqlBaseParser.IndexPatternContext ctx) {
95+
if (clusterString.indexOf(RemoteClusterService.REMOTE_CLUSTER_INDEX_SEPARATOR) != -1) {
96+
throw new ParsingException(source(ctx), "cluster string [{}] must not contain ':'", clusterString);
97+
}
98+
}
99+
91100
private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx, boolean hasSeenStar) {
92101
// multiple index names can be in the same double quote, e.g. indexPattern = "idx1, *, -idx2"
93102
String[] indices = indexPattern.split(",");

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ private void clustersAndIndices(String command, String indexString1, String inde
615615
);
616616
}
617617

618-
public void testValidQuotingFromIndexPattern() {
618+
public void testValidFromIndexPattern() {
619619
var patterns = randomList(1, 5, () -> {
620620
String pattern = randomIndexIdentifier();// index or alias
621621
if (randomBoolean()) {// pattern
@@ -664,6 +664,11 @@ private static char randomCharacterFrom(String str) {
664664
return str.charAt(randomInt(str.length() - 1));
665665
}
666666

667+
public void testInvalidFromIndexPattern() {
668+
expectError("FROM \"remote:\":index", "line 1:6: cluster string [remote:] must not contain ':'");
669+
expectError("FROM \"remote:invalid\":index", "line 1:6: cluster string [remote:invalid] must not contain ':'");
670+
}
671+
667672
public void testInvalidQuotingAsFromIndexPattern() {
668673
expectError("FROM \"foo", ": token recognition error at: '\"foo'");
669674
expectError("FROM \"foo | LIMIT 1", ": token recognition error at: '\"foo | LIMIT 1'");

0 commit comments

Comments
 (0)