Skip to content

Commit 1d3c269

Browse files
Lazy compute and cache grantsAll per privilege (elastic#136684) (elastic#136696)
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.
1 parent b61e840 commit 1d3c269

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package org.elasticsearch.xpack.core.security.authz.privilege;
88

99
import org.apache.lucene.util.automaton.Automaton;
10+
import org.apache.lucene.util.automaton.Operations;
11+
import org.elasticsearch.common.util.CachedSupplier;
1012
import org.elasticsearch.common.util.Maps;
1113
import org.elasticsearch.xpack.core.security.support.Automatons;
1214

@@ -18,6 +20,7 @@
1820
import java.util.SortedMap;
1921
import java.util.TreeMap;
2022
import java.util.function.Predicate;
23+
import java.util.function.Supplier;
2124

2225
import static org.elasticsearch.xpack.core.security.support.Automatons.patterns;
2326

@@ -29,6 +32,7 @@ public class Privilege {
2932
protected final Set<String> name;
3033
protected final Automaton automaton;
3134
protected final Predicate<String> predicate;
35+
protected final Supplier<Boolean> grantsAll;
3236

3337
public Privilege(String name, String... patterns) {
3438
this(Collections.singleton(name), patterns);
@@ -42,6 +46,7 @@ public Privilege(Set<String> name, Automaton automaton) {
4246
this.name = name;
4347
this.automaton = automaton;
4448
this.predicate = Automatons.predicate(automaton);
49+
this.grantsAll = CachedSupplier.wrap(() -> Operations.isTotal(automaton));
4550
}
4651

4752
public Set<String> name() {
@@ -80,6 +85,13 @@ public Automaton getAutomaton() {
8085
return automaton;
8186
}
8287

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

0 commit comments

Comments
 (0)