|
@@ -32,6 +32,8 @@ import java.lang.instrument.Instrumentation;
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
@@ -60,11 +62,24 @@ public class EntitlementInitialization {
|
|
public static void initialize(Instrumentation inst) throws Exception {
|
|
public static void initialize(Instrumentation inst) throws Exception {
|
|
manager = initChecker();
|
|
manager = initChecker();
|
|
|
|
|
|
- Map<MethodKey, CheckMethod> checkMethods = INSTRUMENTER_FACTORY.lookupMethods(EntitlementChecker.class);
|
|
|
|
|
|
+ Map<MethodKey, CheckMethod> checkMethods = new HashMap<>();
|
|
|
|
+ int javaVersion = Runtime.version().feature();
|
|
|
|
+ Set<Class<?>> interfaces = new HashSet<>();
|
|
|
|
+ for (int i = 17; i <= javaVersion; ++i) {
|
|
|
|
+ interfaces.add(getVersionSpecificCheckerClass(i, "org.elasticsearch.entitlement.bridge", "EntitlementChecker"));
|
|
|
|
+ }
|
|
|
|
+ for (var checkerInterface : interfaces) {
|
|
|
|
+ checkMethods.putAll(INSTRUMENTER_FACTORY.lookupMethods(checkerInterface));
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ var latestCheckerInterface = getVersionSpecificCheckerClass(
|
|
|
|
+ javaVersion,
|
|
|
|
+ "org.elasticsearch.entitlement.bridge",
|
|
|
|
+ "EntitlementChecker"
|
|
|
|
+ );
|
|
var classesToTransform = checkMethods.keySet().stream().map(MethodKey::className).collect(Collectors.toSet());
|
|
var classesToTransform = checkMethods.keySet().stream().map(MethodKey::className).collect(Collectors.toSet());
|
|
|
|
|
|
- Instrumenter instrumenter = INSTRUMENTER_FACTORY.newInstrumenter(EntitlementChecker.class, checkMethods);
|
|
|
|
|
|
+ Instrumenter instrumenter = INSTRUMENTER_FACTORY.newInstrumenter(latestCheckerInterface, checkMethods);
|
|
inst.addTransformer(new Transformer(instrumenter, classesToTransform), true);
|
|
inst.addTransformer(new Transformer(instrumenter, classesToTransform), true);
|
|
inst.retransformClasses(findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform));
|
|
inst.retransformClasses(findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform));
|
|
}
|
|
}
|
|
@@ -99,7 +114,9 @@ public class EntitlementInitialization {
|
|
)
|
|
)
|
|
),
|
|
),
|
|
new Scope("org.apache.httpcomponents.httpclient", List.of(new OutboundNetworkEntitlement())),
|
|
new Scope("org.apache.httpcomponents.httpclient", List.of(new OutboundNetworkEntitlement())),
|
|
- new Scope("io.netty.transport", List.of(new InboundNetworkEntitlement(), new OutboundNetworkEntitlement()))
|
|
|
|
|
|
+ new Scope("io.netty.transport", List.of(new InboundNetworkEntitlement(), new OutboundNetworkEntitlement())),
|
|
|
|
+ new Scope("org.apache.lucene.core", List.of(new LoadNativeLibrariesEntitlement())),
|
|
|
|
+ new Scope("org.elasticsearch.nativeaccess", List.of(new LoadNativeLibrariesEntitlement()))
|
|
)
|
|
)
|
|
);
|
|
);
|
|
// agents run without a module, so this is a special hack for the apm agent
|
|
// agents run without a module, so this is a special hack for the apm agent
|
|
@@ -112,20 +129,11 @@ public class EntitlementInitialization {
|
|
private static ElasticsearchEntitlementChecker initChecker() {
|
|
private static ElasticsearchEntitlementChecker initChecker() {
|
|
final PolicyManager policyManager = createPolicyManager();
|
|
final PolicyManager policyManager = createPolicyManager();
|
|
|
|
|
|
- int javaVersion = Runtime.version().feature();
|
|
|
|
- final String classNamePrefix;
|
|
|
|
- if (javaVersion >= 23) {
|
|
|
|
- classNamePrefix = "Java23";
|
|
|
|
- } else {
|
|
|
|
- classNamePrefix = "";
|
|
|
|
- }
|
|
|
|
- final String className = "org.elasticsearch.entitlement.runtime.api." + classNamePrefix + "ElasticsearchEntitlementChecker";
|
|
|
|
- Class<?> clazz;
|
|
|
|
- try {
|
|
|
|
- clazz = Class.forName(className);
|
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
|
- throw new AssertionError("entitlement lib cannot find entitlement impl", e);
|
|
|
|
- }
|
|
|
|
|
|
+ Class<?> clazz = getVersionSpecificCheckerClass(
|
|
|
|
+ Runtime.version().feature(),
|
|
|
|
+ "org.elasticsearch.entitlement.runtime.api",
|
|
|
|
+ "ElasticsearchEntitlementChecker"
|
|
|
|
+ );
|
|
Constructor<?> constructor;
|
|
Constructor<?> constructor;
|
|
try {
|
|
try {
|
|
constructor = clazz.getConstructor(PolicyManager.class);
|
|
constructor = clazz.getConstructor(PolicyManager.class);
|
|
@@ -139,6 +147,27 @@ public class EntitlementInitialization {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static Class<?> getVersionSpecificCheckerClass(int javaVersion, String packageName, String baseClassName) {
|
|
|
|
+ final String classNamePrefix;
|
|
|
|
+ if (javaVersion == 21) {
|
|
|
|
+ classNamePrefix = "Java21";
|
|
|
|
+ } else if (javaVersion == 22) {
|
|
|
|
+ classNamePrefix = "Java22";
|
|
|
|
+ } else if (javaVersion >= 23) {
|
|
|
|
+ classNamePrefix = "Java23";
|
|
|
|
+ } else {
|
|
|
|
+ classNamePrefix = "";
|
|
|
|
+ }
|
|
|
|
+ final String className = packageName + "." + classNamePrefix + baseClassName;
|
|
|
|
+ Class<?> clazz;
|
|
|
|
+ try {
|
|
|
|
+ clazz = Class.forName(className);
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
+ throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
|
|
|
|
+ }
|
|
|
|
+ return clazz;
|
|
|
|
+ }
|
|
|
|
+
|
|
private static final InstrumentationService INSTRUMENTER_FACTORY = new ProviderLocator<>(
|
|
private static final InstrumentationService INSTRUMENTER_FACTORY = new ProviderLocator<>(
|
|
"entitlement",
|
|
"entitlement",
|
|
InstrumentationService.class,
|
|
InstrumentationService.class,
|