Skip to content

Commit 878750c

Browse files
Lazy compute and cache grantsAll per privilege (elastic#136684) (elastic#136706)
This change avoids calling expensive `Operations.isTotal` every time an application privilege is checked. This is done by caching the result per privilege. It avoids re-building privilege's automaton each time upstream `ApplicationPermission#grants` gets called. (cherry picked from commit 0b6338a) # Conflicts: # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java
1 parent 898da16 commit 878750c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

docs/changelog/136684.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 136684
2+
summary: Lazy compute and cache `grantsAll` per privilege
3+
area: Authorization
4+
type: enhancement
5+
issues: []

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private boolean matchesPrivilege(ApplicationPrivilege other) {
197197
if (this.application.test(other.getApplication()) == false) {
198198
return false;
199199
}
200-
if (Operations.isTotal(privilege.getAutomaton())) {
200+
if (privilege.grantsAll()) {
201201
return true;
202202
}
203203
return Operations.isEmpty(privilege.getAutomaton()) == false

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.apache.lucene.util.automaton.Automaton;
1010
import org.apache.lucene.util.automaton.Operations;
11+
import org.elasticsearch.common.util.CachedSupplier;
1112
import org.elasticsearch.common.util.Maps;
1213
import org.elasticsearch.xpack.core.security.support.Automatons;
1314

@@ -19,6 +20,7 @@
1920
import java.util.SortedMap;
2021
import java.util.TreeMap;
2122
import java.util.function.Predicate;
23+
import java.util.function.Supplier;
2224

2325
import static org.elasticsearch.xpack.core.security.support.Automatons.patterns;
2426

@@ -30,6 +32,7 @@ public class Privilege {
3032
protected final Set<String> name;
3133
protected final Automaton automaton;
3234
protected final Predicate<String> predicate;
35+
protected final Supplier<Boolean> grantsAll;
3336

3437
public Privilege(String name, String... patterns) {
3538
this(Collections.singleton(name), patterns);
@@ -43,6 +46,7 @@ public Privilege(Set<String> name, Automaton automaton) {
4346
this.name = name;
4447
this.automaton = automaton;
4548
this.predicate = Automatons.predicate(automaton);
49+
this.grantsAll = CachedSupplier.wrap(() -> Operations.isTotal(automaton));
4650
}
4751

4852
public Set<String> name() {
@@ -81,6 +85,13 @@ public Automaton getAutomaton() {
8185
return automaton;
8286
}
8387

88+
/**
89+
* Returns true if this privilege grants all names.
90+
*/
91+
public boolean grantsAll() {
92+
return grantsAll.get();
93+
}
94+
8495
/**
8596
* Sorts the map of privileges from least-privilege to most-privilege
8697
*/

0 commit comments

Comments
 (0)