From 8d2e10a272fa8495620e5a53e8bc1aa15550cbfe Mon Sep 17 00:00:00 2001 From: Lorenzo Dematte Date: Tue, 6 May 2025 18:07:37 +0200 Subject: [PATCH] Uniform main and backport code --- .../EntitlementCheckerUtils.java | 41 +++++++++++++++++++ .../EntitlementInitialization.java | 40 ++++-------------- 2 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementCheckerUtils.java diff --git a/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementCheckerUtils.java b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementCheckerUtils.java new file mode 100644 index 0000000000000..684f20ae4b0bc --- /dev/null +++ b/libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementCheckerUtils.java @@ -0,0 +1,41 @@ +/* + * 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.entitlement.initialization; + +class EntitlementCheckerUtils { + + /** + * Returns the "most recent" checker class compatible with the provided runtime Java version. + * For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23). + * The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see + * the various mainXX source sets). + */ + static Class getVersionSpecificCheckerClass(Class baseClass, int javaVersion) { + String packageName = baseClass.getPackageName(); + String baseClassName = baseClass.getSimpleName(); + + final String classNamePrefix; + if (javaVersion >= 23) { + // All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation + classNamePrefix = "Java23"; + } else { + // For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks + 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; + } +} 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 53bfb5c57b23c..bd7c946fc1640 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 @@ -68,7 +68,11 @@ public static void initialize(Instrumentation inst) throws Exception { ensureClassesSensitiveToVerificationAreInitialized(); } - DynamicInstrumentation.initialize(inst, getVersionSpecificCheckerClass(EntitlementChecker.class), verifyBytecode); + DynamicInstrumentation.initialize( + inst, + EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()), + verifyBytecode + ); } private static PolicyManager createPolicyManager() { @@ -108,39 +112,13 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() { } } - /** - * Returns the "most recent" checker class compatible with the current runtime Java version. - * For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23). - * The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see - * the various mainXX source sets). - */ - private static Class getVersionSpecificCheckerClass(Class baseClass) { - String packageName = baseClass.getPackageName(); - String baseClassName = baseClass.getSimpleName(); - int javaVersion = Runtime.version().feature(); - - final String classNamePrefix; - if (javaVersion >= 23) { - // All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation - classNamePrefix = "Java23"; - } else { - // For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks - 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 ElasticsearchEntitlementChecker initChecker() { final PolicyManager policyManager = createPolicyManager(); - final Class clazz = getVersionSpecificCheckerClass(ElasticsearchEntitlementChecker.class); + final Class clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass( + ElasticsearchEntitlementChecker.class, + Runtime.version().feature() + ); Constructor constructor; try {