Skip to content

Commit ccb9f27

Browse files
Split PolicyChecker from PolicyManager (elastic#128004)
* Split PolicyChecker from PolicyManager * Restore EntitlementCheckerUtils * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent be8e427 commit ccb9f27

File tree

11 files changed

+1263
-1140
lines changed

11 files changed

+1263
-1140
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
import javax.net.ssl.SSLContext;
8989
import javax.net.ssl.SSLSocketFactory;
9090

91+
/**
92+
* Contains one "check" method for each distinct JDK method we want to instrument.
93+
*/
9194
@SuppressWarnings("unused") // Called from instrumentation code inserted by the Entitlements agent
9295
public interface EntitlementChecker {
9396

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/PathActions.java

Lines changed: 4 additions & 2 deletions
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/EntitlementInitialization.java

Lines changed: 30 additions & 23 deletions
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;
@@ -75,25 +77,6 @@ public static void initialize(Instrumentation inst) throws Exception {
7577
DynamicInstrumentation.initialize(inst, latestCheckerInterface, verifyBytecode);
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.suppressFailureLogPackages()
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
@@ -117,7 +100,7 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
117100
}
118101

119102
private static ElasticsearchEntitlementChecker initChecker() {
120-
final PolicyManager policyManager = createPolicyManager();
103+
final PolicyChecker policyChecker = createPolicyChecker();
121104

122105
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
123106
ElasticsearchEntitlementChecker.class,
@@ -126,14 +109,38 @@ private static ElasticsearchEntitlementChecker initChecker() {
126109

127110
Constructor<?> constructor;
128111
try {
129-
constructor = clazz.getConstructor(PolicyManager.class);
112+
constructor = clazz.getConstructor(PolicyChecker.class);
130113
} catch (NoSuchMethodException e) {
131-
throw new AssertionError("entitlement impl is missing no arg constructor", e);
114+
throw new AssertionError("entitlement impl is missing required constructor: [" + clazz.getName() + "]", e);
132115
}
133116
try {
134-
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
117+
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyChecker);
135118
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
136119
throw new AssertionError(e);
137120
}
138121
}
122+
123+
private static PolicyCheckerImpl createPolicyChecker() {
124+
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
125+
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
126+
PathLookup pathLookup = bootstrapArgs.pathLookup();
127+
128+
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
129+
130+
PolicyManager policyManager = new PolicyManager(
131+
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
132+
HardcodedEntitlements.agentEntitlements(),
133+
pluginPolicies,
134+
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
135+
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
136+
pathLookup
137+
);
138+
return new PolicyCheckerImpl(
139+
bootstrapArgs.suppressFailureLogPackages(),
140+
ENTITLEMENTS_MODULE,
141+
policyManager,
142+
bootstrapArgs.pathLookup()
143+
);
144+
}
145+
139146
}

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

Lines changed: 2 additions & 2 deletions
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)