Skip to content

Commit 60de9dc

Browse files
committed
Split PolicyChecker from PolicyManager
1 parent ca921a0 commit 60de9dc

File tree

11 files changed

+1113
-1042
lines changed

11 files changed

+1113
-1042
lines changed

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
import javax.net.ssl.SSLContext;
9797
import javax.net.ssl.SSLSocketFactory;
9898

99+
/**
100+
* Contains one "check" method for each distinct JDK method we want to instrument.
101+
*/
99102
@SuppressWarnings("unused") // Called from instrumentation code inserted by the Entitlements agent
100103
public interface EntitlementChecker {
101104

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementCheckerUtils.java

-41
This file was deleted.

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

+31-8
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static void initialize(Instrumentation inst) throws Exception {
7070

7171
DynamicInstrumentation.initialize(
7272
inst,
73-
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
73+
getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
7474
verifyBytecode
7575
);
7676
}
@@ -88,9 +88,7 @@ private static PolicyManager createPolicyManager() {
8888
pluginPolicies,
8989
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
9090
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
91-
ENTITLEMENTS_MODULE,
92-
pathLookup,
93-
bootstrapArgs.suppressFailureLogClasses()
91+
pathLookup
9492
);
9593
}
9694

@@ -115,10 +113,7 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
115113
private static ElasticsearchEntitlementChecker initChecker() {
116114
final PolicyManager policyManager = createPolicyManager();
117115

118-
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
119-
ElasticsearchEntitlementChecker.class,
120-
Runtime.version().feature()
121-
);
116+
final Class<?> clazz = getVersionSpecificCheckerClass(ElasticsearchEntitlementChecker.class, Runtime.version().feature());
122117

123118
Constructor<?> constructor;
124119
try {
@@ -132,4 +127,32 @@ private static ElasticsearchEntitlementChecker initChecker() {
132127
throw new AssertionError(e);
133128
}
134129
}
130+
131+
/**
132+
* Returns the "most recent" checker class compatible with the provided runtime Java version.
133+
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
134+
* The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
135+
* the various mainXX source sets).
136+
*/
137+
static Class<?> getVersionSpecificCheckerClass(Class<?> baseClass, int javaVersion) {
138+
String packageName = baseClass.getPackageName();
139+
String baseClassName = baseClass.getSimpleName();
140+
141+
final String classNamePrefix;
142+
if (javaVersion >= 23) {
143+
// All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
144+
classNamePrefix = "Java23";
145+
} else {
146+
// For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks
147+
classNamePrefix = "";
148+
}
149+
final String className = packageName + "." + classNamePrefix + baseClassName;
150+
Class<?> clazz;
151+
try {
152+
clazz = Class.forName(className);
153+
} catch (ClassNotFoundException e) {
154+
throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
155+
}
156+
return clazz;
157+
}
135158
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/package-info.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@
192192
* implementation (normally on {@link org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker}, unless it is a
193193
* version-specific method) calls the appropriate methods on {@link org.elasticsearch.entitlement.runtime.policy.PolicyManager},
194194
* forwarding the caller class and a specific set of arguments. These methods all start with check, roughly matching an entitlement type
195-
* (e.g. {@link org.elasticsearch.entitlement.runtime.policy.PolicyManager#checkInboundNetworkAccess},
196-
* {@link org.elasticsearch.entitlement.runtime.policy.PolicyManager#checkFileRead}).
195+
* (e.g. {@link org.elasticsearch.entitlement.runtime.policy.PolicyChecker#checkInboundNetworkAccess},
196+
* {@link org.elasticsearch.entitlement.runtime.policy.PolicyChecker#checkFileRead}).
197197
* </p>
198198
* <p>
199199
* Most of the entitlements are "flag" entitlements: when present, it grants the caller the right to perform an action (or a set of

0 commit comments

Comments
 (0)