15
15
import org .elasticsearch .entitlement .runtime .api .ElasticsearchEntitlementChecker ;
16
16
import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
17
17
import org .elasticsearch .entitlement .runtime .policy .Policy ;
18
+ import org .elasticsearch .entitlement .runtime .policy .PolicyChecker ;
19
+ import org .elasticsearch .entitlement .runtime .policy .PolicyCheckerImpl ;
18
20
import org .elasticsearch .entitlement .runtime .policy .PolicyManager ;
19
21
20
22
import java .lang .instrument .Instrumentation ;
@@ -70,30 +72,11 @@ public static void initialize(Instrumentation inst) throws Exception {
70
72
71
73
DynamicInstrumentation .initialize (
72
74
inst ,
73
- EntitlementCheckerUtils . getVersionSpecificCheckerClass (EntitlementChecker .class , Runtime .version ().feature ()),
75
+ getVersionSpecificCheckerClass (EntitlementChecker .class , Runtime .version ().feature ()),
74
76
verifyBytecode
75
77
);
76
78
}
77
79
78
- private static PolicyManager createPolicyManager () {
79
- EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
80
- Map <String , Policy > pluginPolicies = bootstrapArgs .pluginPolicies ();
81
- PathLookup pathLookup = bootstrapArgs .pathLookup ();
82
-
83
- FilesEntitlementsValidation .validate (pluginPolicies , pathLookup );
84
-
85
- return new PolicyManager (
86
- HardcodedEntitlements .serverPolicy (pathLookup .pidFile (), bootstrapArgs .serverPolicyPatch ()),
87
- HardcodedEntitlements .agentEntitlements (),
88
- pluginPolicies ,
89
- EntitlementBootstrap .bootstrapArgs ().scopeResolver (),
90
- EntitlementBootstrap .bootstrapArgs ().sourcePaths (),
91
- ENTITLEMENTS_MODULE ,
92
- pathLookup ,
93
- bootstrapArgs .suppressFailureLogClasses ()
94
- );
95
- }
96
-
97
80
/**
98
81
* If bytecode verification is enabled, ensure these classes get loaded before transforming/retransforming them.
99
82
* For these classes, the order in which we transform and verify them matters. Verification during class transformation is at least an
@@ -113,23 +96,71 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
113
96
}
114
97
115
98
private static ElasticsearchEntitlementChecker initChecker () {
116
- final PolicyManager policyManager = createPolicyManager ();
99
+ final PolicyChecker policyChecker = createPolicyChecker ();
117
100
118
- final Class <?> clazz = EntitlementCheckerUtils .getVersionSpecificCheckerClass (
119
- ElasticsearchEntitlementChecker .class ,
120
- Runtime .version ().feature ()
121
- );
101
+ final Class <?> clazz = getVersionSpecificCheckerClass (ElasticsearchEntitlementChecker .class , Runtime .version ().feature ());
122
102
123
103
Constructor <?> constructor ;
124
104
try {
125
- constructor = clazz .getConstructor (PolicyManager .class );
105
+ constructor = clazz .getConstructor (PolicyChecker .class );
126
106
} catch (NoSuchMethodException e ) {
127
- throw new AssertionError ("entitlement impl is missing no arg constructor" , e );
107
+ throw new AssertionError ("entitlement impl is missing required constructor: [" + clazz . getName () + "] " , e );
128
108
}
129
109
try {
130
- return (ElasticsearchEntitlementChecker ) constructor .newInstance (policyManager );
110
+ return (ElasticsearchEntitlementChecker ) constructor .newInstance (policyChecker );
131
111
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e ) {
132
112
throw new AssertionError (e );
133
113
}
134
114
}
115
+
116
+ private static PolicyCheckerImpl createPolicyChecker () {
117
+ EntitlementBootstrap .BootstrapArgs bootstrapArgs = EntitlementBootstrap .bootstrapArgs ();
118
+ Map <String , Policy > pluginPolicies = bootstrapArgs .pluginPolicies ();
119
+ PathLookup pathLookup = bootstrapArgs .pathLookup ();
120
+
121
+ FilesEntitlementsValidation .validate (pluginPolicies , pathLookup );
122
+
123
+ PolicyManager policyManager = new PolicyManager (
124
+ HardcodedEntitlements .serverPolicy (pathLookup .pidFile (), bootstrapArgs .serverPolicyPatch ()),
125
+ HardcodedEntitlements .agentEntitlements (),
126
+ pluginPolicies ,
127
+ EntitlementBootstrap .bootstrapArgs ().scopeResolver (),
128
+ EntitlementBootstrap .bootstrapArgs ().sourcePaths (),
129
+ pathLookup
130
+ );
131
+ return new PolicyCheckerImpl (
132
+ bootstrapArgs .suppressFailureLogClasses (),
133
+ ENTITLEMENTS_MODULE ,
134
+ policyManager ,
135
+ bootstrapArgs .pathLookup ()
136
+ );
137
+ }
138
+
139
+ /**
140
+ * Returns the "most recent" checker class compatible with the provided runtime Java version.
141
+ * For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
142
+ * The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
143
+ * the various mainXX source sets).
144
+ */
145
+ static Class <?> getVersionSpecificCheckerClass (Class <?> baseClass , int javaVersion ) {
146
+ String packageName = baseClass .getPackageName ();
147
+ String baseClassName = baseClass .getSimpleName ();
148
+
149
+ final String classNamePrefix ;
150
+ if (javaVersion >= 23 ) {
151
+ // All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
152
+ classNamePrefix = "Java23" ;
153
+ } else {
154
+ // For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks
155
+ classNamePrefix = "" ;
156
+ }
157
+ final String className = packageName + "." + classNamePrefix + baseClassName ;
158
+ Class <?> clazz ;
159
+ try {
160
+ clazz = Class .forName (className );
161
+ } catch (ClassNotFoundException e ) {
162
+ throw new AssertionError ("entitlement lib cannot find entitlement class " + className , e );
163
+ }
164
+ return clazz ;
165
+ }
135
166
}
0 commit comments