Skip to content
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
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down