-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[Entitlements] Add test entitlement bootstrap and initialization classes #127814
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
24199aa
0289a2a
e166e3d
36479fc
53336dd
c955eed
24d9709
fc40a30
714fbda
9b1e178
e0eee89
e4221b0
2eb56fb
d47a5ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.bootstrap; | ||
|
||
import com.sun.tools.attach.AgentInitializationException; | ||
import com.sun.tools.attach.AgentLoadException; | ||
import com.sun.tools.attach.AttachNotSupportedException; | ||
import com.sun.tools.attach.VirtualMachine; | ||
|
||
import org.elasticsearch.core.SuppressForbidden; | ||
import org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap; | ||
import org.elasticsearch.logging.LogManager; | ||
import org.elasticsearch.logging.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
class TestEntitlementBootstrap { | ||
|
||
private static final Logger logger = LogManager.getLogger(TestEntitlementBootstrap.class); | ||
|
||
/** | ||
* Activates entitlement checking in tests. | ||
*/ | ||
public static void bootstrap() { | ||
logger.debug("Loading entitlement agent"); | ||
loadAgent(EntitlementBootstrap.findAgentJar()); | ||
} | ||
|
||
@SuppressForbidden(reason = "The VirtualMachine API is the only way to attach a java agent dynamically") | ||
private static void loadAgent(String agentPath) { | ||
try { | ||
VirtualMachine vm = VirtualMachine.attach(Long.toString(ProcessHandle.current().pid())); | ||
try { | ||
vm.loadAgent(agentPath, TestEntitlementInitialization.class.getName()); | ||
} finally { | ||
vm.detach(); | ||
} | ||
} catch (AttachNotSupportedException | IOException | AgentLoadException | AgentInitializationException e) { | ||
throw new IllegalStateException("Unable to attach entitlement agent", e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.bootstrap; | ||
|
||
import org.elasticsearch.entitlement.bridge.EntitlementChecker; | ||
import org.elasticsearch.entitlement.initialization.DynamicInstrumentation; | ||
import org.elasticsearch.entitlement.initialization.EntitlementCheckerUtils; | ||
import org.elasticsearch.entitlement.initialization.FilesEntitlementsValidation; | ||
import org.elasticsearch.entitlement.initialization.HardcodedEntitlements; | ||
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.PolicyManager; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Test-only version of {@code EntitlementInitialization} | ||
*/ | ||
public class TestEntitlementInitialization { | ||
|
||
private static final Module ENTITLEMENTS_MODULE = PolicyManager.class.getModule(); | ||
|
||
private static ElasticsearchEntitlementChecker manager; | ||
|
||
// Note: referenced by bridge reflectively | ||
public static EntitlementChecker checker() { | ||
return manager; | ||
} | ||
|
||
public static void initialize(Instrumentation inst) throws Exception { | ||
manager = initChecker(); | ||
DynamicInstrumentation.initialize( | ||
inst, | ||
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()), | ||
false | ||
); | ||
} | ||
|
||
private static PolicyManager createPolicyManager() { | ||
|
||
// TODO: parse policies. Locate them using help from TestBuildInfo | ||
Map<String, Policy> pluginPolicies = Map.of(); | ||
|
||
// TODO: create here the test pathLookup | ||
PathLookup pathLookup = null; | ||
|
||
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup); | ||
|
||
return new PolicyManager( | ||
HardcodedEntitlements.serverPolicy(null, null), | ||
HardcodedEntitlements.agentEntitlements(), | ||
pluginPolicies, | ||
null, // TestScopeResolver.createScopeResolver | ||
Map.of(), // TODO: a map that always return nulls? Replace with functor | ||
ENTITLEMENTS_MODULE, // TODO: this will need to change -- encapsulate it when we extract isTriviallyAllowed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh interesting. What do you have in mind here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you go with Ryan's suggestion to replace the methods altogether, this will be part of the prod code -- the test code could just test for package name like junit or gradle. I'll wait and see how that evolves :) |
||
pathLookup, | ||
Set.of() | ||
); | ||
} | ||
|
||
private static ElasticsearchEntitlementChecker initChecker() { | ||
final PolicyManager policyManager = createPolicyManager(); | ||
|
||
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass( | ||
ElasticsearchEntitlementChecker.class, | ||
Runtime.version().feature() | ||
); | ||
|
||
Constructor<?> constructor; | ||
try { | ||
constructor = clazz.getConstructor(PolicyManager.class); | ||
} catch (NoSuchMethodException e) { | ||
throw new AssertionError("entitlement impl is missing no arg constructor", e); | ||
} | ||
try { | ||
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager); | ||
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.