Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] Validate index name in parser #112081

Merged
merged 22 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
240a4d4
validate index name in parser
fang-xing-esql Aug 22, 2024
f4e2fac
validate index string in parser
fang-xing-esql Aug 22, 2024
d122c23
Merge branch 'main' into validate-index-name
fang-xing-esql Aug 22, 2024
9845f28
validate index name in parser
fang-xing-esql Aug 23, 2024
316f204
fix bug
fang-xing-esql Aug 23, 2024
f675063
Merge branch 'main' into validate-index-name
fang-xing-esql Aug 23, 2024
cb96e18
Merge branch 'main' into validate-index-name
fang-xing-esql Aug 23, 2024
909edbf
add more tests
fang-xing-esql Aug 24, 2024
0d4716f
Update docs/changelog/112081.yaml
fang-xing-esql Aug 24, 2024
9f0ec40
Merge branch 'main' into validate-index-name
fang-xing-esql Sep 9, 2024
8cfd07d
update according to review comments
fang-xing-esql Sep 9, 2024
71fce7a
more tests for index names with date math
fang-xing-esql Sep 9, 2024
0dfee87
Merge branch 'main' into validate-index-name
fang-xing-esql Sep 23, 2024
1717c7e
Merge branch 'main' into validate-index-name
fang-xing-esql Sep 25, 2024
afb62e3
modify tests according to review comments
fang-xing-esql Sep 27, 2024
578ee34
Merge branch 'main' into validate-index-name
fang-xing-esql Sep 28, 2024
31577ef
Merge branch 'main' into validate-index-name
fang-xing-esql Oct 1, 2024
9465693
allow invalid index names if there is index name with wildcard and ex…
fang-xing-esql Oct 1, 2024
9ecf17c
Merge branch 'main' into validate-index-name
fang-xing-esql Oct 1, 2024
a868ab8
Merge branch 'main' into validate-index-name
fang-xing-esql Oct 1, 2024
cc0acdf
update according to review comments
fang-xing-esql Oct 2, 2024
7dc33fa
Merge branch 'main' into validate-index-name
fang-xing-esql Oct 2, 2024
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/112081.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 112081
summary: "[ES|QL] Validate index name in parser"
area: ES|QL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
package org.elasticsearch.xpack.esql.parser;

import org.antlr.v4.runtime.tree.TerminalNode;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetadataCreateIndexService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IdentifierContext;
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IndexStringContext;

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

import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR;
import static org.elasticsearch.xpack.esql.parser.ParserUtils.source;

abstract class IdentifierBuilder extends AbstractBuilder {

Expand Down Expand Up @@ -48,10 +53,32 @@ public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) {
List<String> patterns = new ArrayList<>(ctx.size());
ctx.forEach(c -> {
String indexPattern = visitIndexString(c.indexString());
validateIndexPattern(indexPattern, c);
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
patterns.add(
c.clusterString() != null ? c.clusterString().getText() + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern
);
});
return Strings.collectionToDelimitedString(patterns, ",");
}

private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx) {
String[] indices = indexPattern.replace("*", "").split(",");
try {
for (String index : indices) {
if (index.isBlank()) {
continue;
}
index = removeExclusion(index.strip());
String temp = IndexNameExpressionResolver.resolveDateMathExpression(index);
index = temp.equals(index) ? index : removeExclusion(temp);
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
MetadataCreateIndexService.validateIndexOrAliasName(index, InvalidIndexNameException::new);
}
} catch (InvalidIndexNameException | ElasticsearchParseException e) {
throw new ParsingException(e, source(ctx), e.getMessage());
}
}

private static String removeExclusion(String indexPattern) {
return indexPattern.charAt(0) == '-' ? indexPattern.substring(1) : indexPattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.esql.parser;

import org.elasticsearch.common.logging.LoggerMessageFormat;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.esql.VerificationException;
import org.elasticsearch.xpack.esql.core.expression.Literal;
Expand Down Expand Up @@ -124,4 +125,8 @@ void expectError(String query, List<QueryParam> params, String errorMessage) {
);
assertThat(e.getMessage(), containsString(errorMessage));
}

void expectError(String query, String arg, String errorMessage) {
expectError(LoggerMessageFormat.format(null, query, arg), errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,24 +367,16 @@ public void testStringAsIndexPattern() {
"<logstash-{now/M{yyyy.MM}}>,<logstash-{now/d{yyyy.MM.dd|+12:00}}>",
command + " <logstash-{now/M{yyyy.MM}}>, \"<logstash-{now/d{yyyy.MM.dd|+12:00}}>\""
);

assertStringAsIndexPattern("foo,test,xyz", command + " \"\"\"foo\"\"\", test,\"xyz\"");

assertStringAsIndexPattern("`backtick`,``multiple`back``ticks```", command + " `backtick`, ``multiple`back``ticks```");

assertStringAsIndexPattern("test,metadata,metaata,.metadata", command + " test,\"metadata\", metaata, .metadata");

assertStringAsIndexPattern(".dot", command + " .dot");

assertStringAsIndexPattern("cluster:index", command + " cluster:index");
assertStringAsIndexPattern("cluster:index|pattern", command + " cluster:\"index|pattern\"");
assertStringAsIndexPattern("cluster:.index", command + " cluster:.index");
assertStringAsIndexPattern("cluster*:index*", command + " cluster*:index*");
assertStringAsIndexPattern("cluster*:*", command + " cluster*:*");
assertStringAsIndexPattern("*:index*", command + " *:index*");
assertStringAsIndexPattern("*:index|pattern", command + " *:\"index|pattern\"");
assertStringAsIndexPattern("*:*", command + " *:*");
assertStringAsIndexPattern("*:*,cluster*:index|pattern,i|p", command + " *:*, cluster*:\"index|pattern\", \"i|p\"");
}
}

Expand All @@ -405,19 +397,45 @@ public void testStringAsLookupIndexPattern() {
);

assertStringAsLookupIndexPattern("foo", "ROW x = 1 | LOOKUP \"\"\"foo\"\"\" ON j");

assertStringAsLookupIndexPattern("`backtick`", "ROW x = 1 | LOOKUP `backtick` ON j");
assertStringAsLookupIndexPattern("``multiple`back``ticks```", "ROW x = 1 | LOOKUP ``multiple`back``ticks``` ON j");

assertStringAsLookupIndexPattern(".dot", "ROW x = 1 | LOOKUP .dot ON j");

assertStringAsLookupIndexPattern("cluster:index", "ROW x = 1 | LOOKUP cluster:index ON j");
assertStringAsLookupIndexPattern("cluster:.index", "ROW x = 1 | LOOKUP cluster:.index ON j");
assertStringAsLookupIndexPattern("cluster*:index*", "ROW x = 1 | LOOKUP cluster*:index* ON j");
assertStringAsLookupIndexPattern("cluster*:*", "ROW x = 1 | LOOKUP cluster*:* ON j");
assertStringAsLookupIndexPattern("*:index*", "ROW x = 1 | LOOKUP *:index* ON j");
assertStringAsLookupIndexPattern("*:*", "ROW x = 1 | LOOKUP *:* ON j");
}

public void testInvalidCharacterInIndexPattern() {
for (String command : List.of("FROM {}", "METRICS {}", "ROW x = 1 | LOOKUP {} ON j")) {
expectError(command, " cluster:\"index|pattern\"", "Invalid index name [index|pattern]");
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
expectError(command, " *:\"index|pattern\"", "Invalid index name [index|pattern]");
expectError(command, " cluster:\"index#pattern\"", "Invalid index name [index#pattern]");
expectError(command, " cluster:index#pattern", "Invalid index name [index#pattern]");
expectError(command, " cluster:\"index pattern\"", "Invalid index name [index pattern]");
expectError(command, " cluster:\"index?pattern\"", "Invalid index name [index?pattern]");
expectError(command, " cluster:index?pattern", "Invalid index name [index?pattern]");
expectError(command, " cluster:\"index>pattern\"", "Invalid index name [index>pattern]");
expectError(command, " cluster:index>pattern", "Invalid index name [index>pattern]");
expectError(command, " cluster:\"index<pattern\"", "Invalid index name [index<pattern]");
expectError(command, " cluster:index<pattern", "Invalid index name [index<pattern]");
expectError(command, " cluster:\"index/pattern\"", "Invalid index name [index/pattern]");
expectError(command, " cluster:index/pattern", "Invalid index name [index/pattern]");
expectError(command, " cluster:\"index\\\\pattern\"", "Invalid index name [index\\pattern]");
expectError(command, " cluster:index\\pattern", "Invalid index name [index\\pattern]");
expectError(command, " cluster:\"..\"", "Invalid index name [..]");
expectError(command, " cluster:..", "Invalid index name [..]");
expectError(command, " cluster:\"_indexpattern\"", "Invalid index name [_indexpattern]");
expectError(command, " cluster:_indexpattern", "Invalid index name [_indexpattern]");
expectError(command, " cluster:\"+indexpattern\"", "Invalid index name [+indexpattern]");
expectError(command, " cluster:+indexpattern", "Invalid index name [+indexpattern]");
expectError(command, " cluster:\"--indexpattern\"", "Invalid index name [-indexpattern]");
expectError(command, " cluster:--indexpattern", "Invalid index name [-indexpattern]");
expectError(command, " cluster:\"<--logstash-{now/M{yyyy.MM}}>\"", "Invalid index name [-logstash-");
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
expectError(command, " --<logstash-{now/M{yyyy.MM}}>", "Invalid index name [-<logstash-{now/M{yyyy.MM}}>]");
}
}

public void testInvalidQuotingAsFromIndexPattern() {
Expand Down Expand Up @@ -1551,7 +1569,7 @@ public void testMetricsIdentifiers() {
Map.entry("metrics foo,test-*", "foo,test-*"),
Map.entry("metrics 123-test@foo_bar+baz1", "123-test@foo_bar+baz1"),
Map.entry("metrics foo, test,xyz", "foo,test,xyz"),
Map.entry("metrics <logstash-{now/M{yyyy.MM}}>>", "<logstash-{now/M{yyyy.MM}}>>")
Map.entry("metrics <logstash-{now/M{yyyy.MM}}>", "<logstash-{now/M{yyyy.MM}}>")
);
for (Map.Entry<String, String> e : patterns.entrySet()) {
assertStatement(e.getKey(), unresolvedRelation(e.getValue()));
Expand Down