Skip to content

Commit 8530cc1

Browse files
committed
Split PolicyChecker from PolicyManager
1 parent d6c8159 commit 8530cc1

File tree

12 files changed

+1304
-1198
lines changed

12 files changed

+1304
-1198
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/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/PathActions.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
package org.elasticsearch.entitlement.qa.test;
1111

1212
import org.elasticsearch.entitlement.qa.entitled.EntitledActions;
13-
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
13+
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1414

1515
import java.io.IOException;
1616
import java.nio.file.FileSystems;
@@ -19,6 +19,7 @@
1919
import java.nio.file.Path;
2020
import java.nio.file.WatchEvent;
2121
import java.util.Arrays;
22+
import java.util.Objects;
2223

2324
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED;
2425
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
@@ -37,7 +38,8 @@ static void checkToRealPathForInvalidTarget() throws IOException {
3738
try {
3839
EntitledActions.pathToRealPath(invalidLink); // throws NoSuchFileException when checking entitlements due to invalid target
3940
} catch (NoSuchFileException e) {
40-
assert Arrays.stream(e.getStackTrace()).anyMatch(t -> t.getClassName().equals(PolicyManager.class.getName()))
41+
assert Arrays.stream(e.getStackTrace())
42+
.anyMatch(t -> Objects.equals(t.getModuleName(), PolicyChecker.class.getModule().getName()))
4143
: "Expected NoSuchFileException to be thrown by entitlements check";
4244
throw e;
4345
}

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

+59-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
1616
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
1717
import org.elasticsearch.entitlement.runtime.policy.Policy;
18+
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
19+
import org.elasticsearch.entitlement.runtime.policy.PolicyCheckerImpl;
1820
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
1921

2022
import java.lang.instrument.Instrumentation;
@@ -70,30 +72,11 @@ public static void initialize(Instrumentation inst) throws Exception {
7072

7173
DynamicInstrumentation.initialize(
7274
inst,
73-
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
75+
getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
7476
verifyBytecode
7577
);
7678
}
7779

78-
private static PolicyManager createPolicyManager() {
79-
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
80-
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
81-
PathLookup pathLookup = bootstrapArgs.pathLookup();
82-
83-
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
84-
85-
return new PolicyManager(
86-
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
87-
HardcodedEntitlements.agentEntitlements(),
88-
pluginPolicies,
89-
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
90-
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
91-
ENTITLEMENTS_MODULE,
92-
pathLookup,
93-
bootstrapArgs.suppressFailureLogClasses()
94-
);
95-
}
96-
9780
/**
9881
* If bytecode verification is enabled, ensure these classes get loaded before transforming/retransforming them.
9982
* For these classes, the order in which we transform and verify them matters. Verification during class transformation is at least an
@@ -113,23 +96,71 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
11396
}
11497

11598
private static ElasticsearchEntitlementChecker initChecker() {
116-
final PolicyManager policyManager = createPolicyManager();
99+
final PolicyChecker policyChecker = createPolicyChecker();
117100

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

123103
Constructor<?> constructor;
124104
try {
125-
constructor = clazz.getConstructor(PolicyManager.class);
105+
constructor = clazz.getConstructor(PolicyChecker.class);
126106
} catch (NoSuchMethodException e) {
127-
throw new AssertionError("entitlement impl is missing no arg constructor", e);
107+
throw new AssertionError("entitlement impl is missing required constructor: [" + clazz.getName() + "]", e);
128108
}
129109
try {
130-
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
110+
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyChecker);
131111
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
132112
throw new AssertionError(e);
133113
}
134114
}
115+
116+
private static PolicyCheckerImpl createPolicyChecker() {
117+
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
118+
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
119+
PathLookup pathLookup = bootstrapArgs.pathLookup();
120+
121+
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
122+
123+
PolicyManager policyManager = new PolicyManager(
124+
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
125+
HardcodedEntitlements.agentEntitlements(),
126+
pluginPolicies,
127+
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
128+
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
129+
pathLookup
130+
);
131+
return new PolicyCheckerImpl(
132+
bootstrapArgs.suppressFailureLogClasses(),
133+
ENTITLEMENTS_MODULE,
134+
policyManager,
135+
bootstrapArgs.pathLookup()
136+
);
137+
}
138+
139+
/**
140+
* Returns the "most recent" checker class compatible with the provided runtime Java version.
141+
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
142+
* The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
143+
* the various mainXX source sets).
144+
*/
145+
static Class<?> getVersionSpecificCheckerClass(Class<?> baseClass, int javaVersion) {
146+
String packageName = baseClass.getPackageName();
147+
String baseClassName = baseClass.getSimpleName();
148+
149+
final String classNamePrefix;
150+
if (javaVersion >= 23) {
151+
// All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
152+
classNamePrefix = "Java23";
153+
} else {
154+
// For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks
155+
classNamePrefix = "";
156+
}
157+
final String className = packageName + "." + classNamePrefix + baseClassName;
158+
Class<?> clazz;
159+
try {
160+
clazz = Class.forName(className);
161+
} catch (ClassNotFoundException e) {
162+
throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
163+
}
164+
return clazz;
165+
}
135166
}

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)