Skip to content

Split PolicyChecker from PolicyManager #128004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.elasticsearch.entitlement.qa.test;

import org.elasticsearch.entitlement.qa.entitled.EntitledActions;
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;

import java.io.IOException;
import java.nio.file.FileSystems;
Expand All @@ -19,6 +19,7 @@
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.util.Arrays;
import java.util.Objects;

import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
Expand All @@ -37,7 +38,8 @@ static void checkToRealPathForInvalidTarget() throws IOException {
try {
EntitledActions.pathToRealPath(invalidLink); // throws NoSuchFileException when checking entitlements due to invalid target
} catch (NoSuchFileException e) {
assert Arrays.stream(e.getStackTrace()).anyMatch(t -> t.getClassName().equals(PolicyManager.class.getName()))
assert Arrays.stream(e.getStackTrace())
.anyMatch(t -> Objects.equals(t.getModuleName(), PolicyChecker.class.getModule().getName()))
: "Expected NoSuchFileException to be thrown by entitlements check";
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
import org.elasticsearch.entitlement.runtime.policy.Policy;
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
import org.elasticsearch.entitlement.runtime.policy.PolicyCheckerImpl;
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;

import java.lang.instrument.Instrumentation;
Expand Down Expand Up @@ -75,25 +77,6 @@ public static void initialize(Instrumentation inst) throws Exception {
);
}

private static PolicyManager createPolicyManager() {
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
PathLookup pathLookup = bootstrapArgs.pathLookup();

FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);

return new PolicyManager(
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
HardcodedEntitlements.agentEntitlements(),
pluginPolicies,
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
ENTITLEMENTS_MODULE,
pathLookup,
bootstrapArgs.suppressFailureLogPackages()
);
}

/**
* If bytecode verification is enabled, ensure these classes get loaded before transforming/retransforming them.
* For these classes, the order in which we transform and verify them matters. Verification during class transformation is at least an
Expand All @@ -113,7 +96,7 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
}

private static ElasticsearchEntitlementChecker initChecker() {
final PolicyManager policyManager = createPolicyManager();
final PolicyChecker policyChecker = createPolicyChecker();

final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
ElasticsearchEntitlementChecker.class,
Expand All @@ -122,14 +105,38 @@ private static ElasticsearchEntitlementChecker initChecker() {

Constructor<?> constructor;
try {
constructor = clazz.getConstructor(PolicyManager.class);
constructor = clazz.getConstructor(PolicyChecker.class);
} catch (NoSuchMethodException e) {
throw new AssertionError("entitlement impl is missing no arg constructor", e);
throw new AssertionError("entitlement impl is missing required constructor: [" + clazz.getName() + "]", e);
}
try {
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyChecker);
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new AssertionError(e);
}
}

private static PolicyCheckerImpl createPolicyChecker() {
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
PathLookup pathLookup = bootstrapArgs.pathLookup();

FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);

PolicyManager policyManager = new PolicyManager(
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
HardcodedEntitlements.agentEntitlements(),
pluginPolicies,
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
pathLookup
);
return new PolicyCheckerImpl(
bootstrapArgs.suppressFailureLogPackages(),
ENTITLEMENTS_MODULE,
policyManager,
bootstrapArgs.pathLookup()
);
}

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