Skip to content

Commit 654c37c

Browse files
authored
[Entitlements] Consider only system modules in the boot layer (#117017)
1 parent 6e50380 commit 654c37c

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
import org.elasticsearch.logging.LogManager;
1414
import org.elasticsearch.logging.Logger;
1515

16+
import java.lang.module.ModuleFinder;
17+
import java.lang.module.ModuleReference;
1618
import java.util.Optional;
19+
import java.util.Set;
20+
import java.util.stream.Collectors;
1721

1822
/**
1923
* Implementation of the {@link EntitlementChecker} interface, providing additional
@@ -23,20 +27,43 @@
2327
public class ElasticsearchEntitlementChecker implements EntitlementChecker {
2428
private static final Logger logger = LogManager.getLogger(ElasticsearchEntitlementChecker.class);
2529

30+
private static final Set<Module> systemModules = findSystemModules();
31+
32+
private static Set<Module> findSystemModules() {
33+
var systemModulesDescriptors = ModuleFinder.ofSystem()
34+
.findAll()
35+
.stream()
36+
.map(ModuleReference::descriptor)
37+
.collect(Collectors.toUnmodifiableSet());
38+
39+
return ModuleLayer.boot()
40+
.modules()
41+
.stream()
42+
.filter(m -> systemModulesDescriptors.contains(m.getDescriptor()))
43+
.collect(Collectors.toUnmodifiableSet());
44+
}
45+
2646
@Override
2747
public void checkSystemExit(Class<?> callerClass, int status) {
2848
var requestingModule = requestingModule(callerClass);
2949
if (isTriviallyAllowed(requestingModule)) {
3050
return;
3151
}
52+
53+
// TODO: this will be checked using policies
54+
if (requestingModule.isNamed() && requestingModule.getName().equals("org.elasticsearch.server")) {
55+
logger.debug("Allowed: caller in {} is entitled to exit the JVM", requestingModule.getName());
56+
return;
57+
}
58+
3259
// Hard-forbidden until we develop the permission granting scheme
3360
throw new NotEntitledException("Missing entitlement for " + requestingModule);
3461
}
3562

3663
private static Module requestingModule(Class<?> callerClass) {
3764
if (callerClass != null) {
3865
Module callerModule = callerClass.getModule();
39-
if (callerModule.getLayer() != ModuleLayer.boot()) {
66+
if (systemModules.contains(callerModule) == false) {
4067
// fast path
4168
return callerModule;
4269
}
@@ -50,19 +77,15 @@ private static Module requestingModule(Class<?> callerClass) {
5077
.walk(
5178
s -> s.skip(framesToSkip)
5279
.map(f -> f.getDeclaringClass().getModule())
53-
.filter(m -> m.getLayer() != ModuleLayer.boot())
80+
.filter(m -> systemModules.contains(m) == false)
5481
.findFirst()
5582
);
5683
return module.orElse(null);
5784
}
5885

5986
private static boolean isTriviallyAllowed(Module requestingModule) {
6087
if (requestingModule == null) {
61-
logger.debug("Trivially allowed: Entire call stack is in the boot module layer");
62-
return true;
63-
}
64-
if (requestingModule == System.class.getModule()) {
65-
logger.debug("Trivially allowed: Caller is in {}", System.class.getModule().getName());
88+
logger.debug("Trivially allowed: entire call stack is in composed of classes in system modules");
6689
return true;
6790
}
6891
logger.trace("Not trivially allowed");

0 commit comments

Comments
 (0)