@@ -308,13 +308,36 @@ public synchronized EventProcessor build(final PluginContext pluginContext) {
308
308
}
309
309
310
310
private static ScriptService initScriptService (final Settings settings , final ThreadPool threadPool ) {
311
+ final List <Whitelist > painlessBaseWhitelist = getPainlessBaseWhiteList ();
311
312
final Map <ScriptContext <?>, List <Whitelist >> scriptContexts = Map .of (
312
- IngestScript .CONTEXT , PainlessPlugin . BASE_WHITELISTS ,
313
- IngestConditionalScript .CONTEXT , PainlessPlugin . BASE_WHITELISTS );
313
+ IngestScript .CONTEXT , painlessBaseWhitelist ,
314
+ IngestConditionalScript .CONTEXT , painlessBaseWhitelist );
314
315
315
316
Map <String , ScriptEngine > engines = new HashMap <>();
316
317
engines .put (PainlessScriptEngine .NAME , new PainlessScriptEngine (settings , scriptContexts ));
317
318
engines .put (MustacheScriptEngine .NAME , new MustacheScriptEngine ());
318
319
return new ScriptService (settings , engines , ScriptModule .CORE_CONTEXTS , threadPool ::absoluteTimeInMillis );
319
320
}
321
+
322
+ /**
323
+ * @implNote handles breaking changes introduced in Elasticsearch 8.14 series; once 8.14 is
324
+ * released and all builds of this plugin depend on Elasticsearch 8.14+, this can
325
+ * be simplified to call {@code PainlessPlugin.baseWhiteList()} directly.
326
+ * @return the PainlessPlugin's default base whitelists
327
+ */
328
+ @ SuppressWarnings ({"JavaReflectionMemberAccess" , "unchecked" })
329
+ static List <Whitelist > getPainlessBaseWhiteList () {
330
+ Class <PainlessPlugin > cls = PainlessPlugin .class ;
331
+ try {
332
+ try {
333
+ // In 8.14+: PainlessPlugin.baseWhiteList()
334
+ return (List <Whitelist >) cls .getMethod ("baseWhiteList" ).invoke (null );
335
+ } catch (NoSuchMethodException e ) {
336
+ // in 8.x->8.13.x: PainlessPlugin.BASE_WHITELISTS
337
+ return (List <Whitelist >) cls .getField ("BASE_WHITELISTS" ).get (null );
338
+ }
339
+ } catch (java .lang .reflect .InvocationTargetException | IllegalAccessException | NoSuchFieldException e ) {
340
+ throw new RuntimeException ("Unsupported PainlessPlugin does not provide access to its base whitelists" , e );
341
+ }
342
+ }
320
343
}
0 commit comments