Skip to content

Commit 8affbc5

Browse files
A few small speedup in authz hot path for fieldcaps (elastic#119558) (elastic#119591)
A few obvious things I noticed looking at profiling (right now security is literally 10%+ of large fieldcaps call). * streams are needlessly expensive * using an unnecessary atomic has unpredictable overhead
1 parent 8bf218b commit 8affbc5

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Objects;
4040
import java.util.Set;
4141
import java.util.concurrent.ConcurrentHashMap;
42-
import java.util.concurrent.atomic.AtomicInteger;
4342
import java.util.function.BiPredicate;
4443
import java.util.function.Predicate;
4544
import java.util.function.Supplier;
@@ -443,25 +442,27 @@ public IndicesAccessControl authorize(
443442
FieldPermissionsCache fieldPermissionsCache
444443
) {
445444
// Short circuit if the indicesPermission allows all access to every index
446-
if (Arrays.stream(groups).anyMatch(Group::isTotal)) {
447-
return IndicesAccessControl.allowAll();
445+
for (Group group : groups) {
446+
if (group.isTotal()) {
447+
return IndicesAccessControl.allowAll();
448+
}
448449
}
449450

450451
final Map<String, IndexResource> resources = Maps.newMapWithExpectedSize(requestedIndicesOrAliases.size());
451-
final AtomicInteger totalResourceCountHolder = new AtomicInteger(0);
452+
int totalResourceCount = 0;
452453

453454
for (String indexOrAlias : requestedIndicesOrAliases) {
454455
final IndexResource resource = new IndexResource(indexOrAlias, lookup.get(indexOrAlias));
455456
resources.put(resource.name, resource);
456-
totalResourceCountHolder.getAndAdd(resource.size());
457+
totalResourceCount += resource.size();
457458
}
458459

459460
final boolean overallGranted = isActionGranted(action, resources);
460-
461+
final int finalTotalResourceCount = totalResourceCount;
461462
final Supplier<Map<String, IndicesAccessControl.IndexAccessControl>> indexPermissions = () -> buildIndicesAccessControl(
462463
action,
463464
resources,
464-
totalResourceCountHolder.get(),
465+
finalTotalResourceCount,
465466
fieldPermissionsCache
466467
);
467468

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,12 @@ private static boolean isChildActionAuthorizedByParentOnLocalNode(RequestInfo re
527527
+ Arrays.stream(indices).filter(Regex::isSimpleMatchPattern).toList();
528528

529529
// Check if the parent context has already successfully authorized access to the child's indices
530-
return Arrays.stream(indices).allMatch(indicesAccessControl::hasIndexPermissions);
530+
for (String index : indices) {
531+
if (indicesAccessControl.hasIndexPermissions(index) == false) {
532+
return false;
533+
}
534+
}
535+
return true;
531536
}
532537

533538
@Override

0 commit comments

Comments
 (0)