Skip to content

Commit ba3ee98

Browse files
authored
Security: improve exact index matching performance (#36017)
This commit improves the efficiency of exact index name matching by separating exact matches from those that include wildcards or regular expressions. Internally, exact matching is done using a HashSet instead of adding the exact matches to the automata. For the wildcard and regular expression matches, the underlying implementation has not changed.
1 parent 61c2db5 commit ba3ee98

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ public IndicesPermission(Group... groups) {
6464
}
6565

6666
static Predicate<String> indexMatcher(List<String> indices) {
67+
Set<String> exactMatch = new HashSet<>();
68+
List<String> nonExactMatch = new ArrayList<>();
69+
for (String indexPattern : indices) {
70+
if (indexPattern.startsWith("/") || indexPattern.contains("*") || indexPattern.contains("?")) {
71+
nonExactMatch.add(indexPattern);
72+
} else {
73+
exactMatch.add(indexPattern);
74+
}
75+
}
76+
77+
if (exactMatch.isEmpty() && nonExactMatch.isEmpty()) {
78+
return s -> false;
79+
} else if (exactMatch.isEmpty()) {
80+
return buildAutomataPredicate(nonExactMatch);
81+
} else if (nonExactMatch.isEmpty()) {
82+
return buildExactMatchPredicate(exactMatch);
83+
} else {
84+
return buildExactMatchPredicate(exactMatch).or(buildAutomataPredicate(nonExactMatch));
85+
}
86+
}
87+
88+
private static Predicate<String> buildExactMatchPredicate(Set<String> indices) {
89+
if (indices.size() == 1) {
90+
final String singleValue = indices.iterator().next();
91+
return singleValue::equals;
92+
}
93+
return indices::contains;
94+
}
95+
96+
private static Predicate<String> buildAutomataPredicate(List<String> indices) {
6797
try {
6898
return Automatons.predicate(indices);
6999
} catch (TooComplexToDeterminizeException e) {

0 commit comments

Comments
 (0)