From 7ed49325f4f36dea4f6f0a78a818fe78dad7b100 Mon Sep 17 00:00:00 2001 From: Lorenzo Dematte Date: Wed, 29 Jan 2025 09:13:11 +0100 Subject: [PATCH 1/2] Fix EntitlementInitialization to work across multiple versions --- .../EntitlementInitialization.java | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java index 4bc7c54e4cfda..1e3177f133321 100644 --- a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java +++ b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java @@ -32,6 +32,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -48,6 +50,7 @@ public class EntitlementInitialization { private static final String AGENTS_PACKAGE_NAME = "co.elastic.apm.agent"; private static final Module ENTITLEMENTS_MODULE = PolicyManager.class.getModule(); + private static final int MINIMUM_SUPPORTED_JAVA_VERSION = 21; private static ElasticsearchEntitlementChecker manager; @@ -60,11 +63,21 @@ public static EntitlementChecker checker() { public static void initialize(Instrumentation inst) throws Exception { manager = initChecker(); - Map checkMethods = INSTRUMENTER_FACTORY.lookupMethods(EntitlementChecker.class); + Map checkMethods = new HashMap<>(); + int javaVersion = Runtime.version().feature(); + Set> interfaces = new HashSet<>(); + for (int i = MINIMUM_SUPPORTED_JAVA_VERSION; 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()); - Instrumenter instrumenter = INSTRUMENTER_FACTORY.newInstrumenter(EntitlementChecker.class, checkMethods); + Instrumenter instrumenter = INSTRUMENTER_FACTORY.newInstrumenter(latestCheckerInterface, checkMethods); inst.addTransformer(new Transformer(instrumenter, classesToTransform), true); inst.retransformClasses(findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform)); } @@ -114,20 +127,11 @@ private static PolicyManager createPolicyManager() { private static ElasticsearchEntitlementChecker initChecker() { 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; try { constructor = clazz.getConstructor(PolicyManager.class); @@ -141,6 +145,24 @@ private static ElasticsearchEntitlementChecker initChecker() { } } + private static Class getVersionSpecificCheckerClass(int javaVersion, String packageName, String baseClassName) { + final String classNamePrefix; + 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<>( "entitlement", InstrumentationService.class, From 7670d06078d31d1097640fb8c462975734cc3783 Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Wed, 29 Jan 2025 08:25:32 +0000 Subject: [PATCH 2/2] [CI] Auto commit changes from spotless --- .../initialization/EntitlementInitialization.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java index 1e3177f133321..d3a2fb392a0e4 100644 --- a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java +++ b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java @@ -66,15 +66,18 @@ public static void initialize(Instrumentation inst) throws Exception { Map checkMethods = new HashMap<>(); int javaVersion = Runtime.version().feature(); Set> interfaces = new HashSet<>(); - for (int i = MINIMUM_SUPPORTED_JAVA_VERSION; i <= javaVersion; ++ i) { + for (int i = MINIMUM_SUPPORTED_JAVA_VERSION; i <= javaVersion; ++i) { interfaces.add(getVersionSpecificCheckerClass(i, "org.elasticsearch.entitlement.bridge", "EntitlementChecker")); } - for (var checkerInterface: interfaces) { + for (var checkerInterface : interfaces) { checkMethods.putAll(INSTRUMENTER_FACTORY.lookupMethods(checkerInterface)); } var latestCheckerInterface = getVersionSpecificCheckerClass( - javaVersion, "org.elasticsearch.entitlement.bridge", "EntitlementChecker"); + javaVersion, + "org.elasticsearch.entitlement.bridge", + "EntitlementChecker" + ); var classesToTransform = checkMethods.keySet().stream().map(MethodKey::className).collect(Collectors.toSet()); Instrumenter instrumenter = INSTRUMENTER_FACTORY.newInstrumenter(latestCheckerInterface, checkMethods); @@ -149,8 +152,7 @@ private static Class getVersionSpecificCheckerClass(int javaVersion, String p final String classNamePrefix; if (javaVersion >= 23) { classNamePrefix = "Java23"; - } - else { + } else { classNamePrefix = ""; } final String className = packageName + "." + classNamePrefix + baseClassName;