From 8eb97c5509a16b0a24e29d48a6a6a651a871f192 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 26 Jan 2016 20:18:22 -0500 Subject: [PATCH] Script settings This commit is the x-plugins side of the refactoring of script settings. Relates elastic/elasticsearchelastic/elasticsearch#16197 Original commit: elastic/x-pack-elasticsearch@4c429933b95a53943e006a2ebc44b17c93217cd5 --- .../messy/tests/MessyTestUtils.java | 17 +++++++++++++---- .../messy/tests/ScriptConditionTests.java | 2 +- .../smoke-test-watcher-with-groovy/build.gradle | 2 +- .../java/org/elasticsearch/marvel/MarvelF.java | 2 +- .../java/org/elasticsearch/shield/ShieldF.java | 2 +- .../elasticsearch/watcher/WatcherPlugin.java | 3 ++- .../elasticsearch/watcher/support/Script.java | 3 ++- .../xmustache/XMustacheScriptEngineService.java | 13 ++++++++----- .../elasticsearch/script/SleepScriptEngine.java | 16 ++++++++++------ .../org/elasticsearch/watcher/WatcherF.java | 2 +- .../condition/script/ScriptConditionTests.java | 2 +- .../watcher/test/WatcherTestUtils.java | 13 +++++++++---- 12 files changed, 50 insertions(+), 27 deletions(-) diff --git a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java index 448f9a9074a..f1763f188c9 100644 --- a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java +++ b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/MessyTestUtils.java @@ -10,8 +10,10 @@ import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.script.ScriptContextRegistry; +import org.elasticsearch.script.ScriptEngineRegistry; import org.elasticsearch.script.ScriptEngineService; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.ScriptSettings; import org.elasticsearch.script.groovy.GroovyScriptEngineService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; @@ -28,8 +30,8 @@ import java.util.Set; public final class MessyTestUtils { public static ScriptServiceProxy getScriptServiceProxy(ThreadPool tp) throws Exception { Settings settings = Settings.settingsBuilder() - .put("script.inline", "on") - .put("script.indexed", "on") + .put("script.inline", "true") + .put("script.indexed", "true") .put("path.home", LuceneTestCase.createTempDir()) .build(); XMustacheScriptEngineService mustacheScriptEngineService = new XMustacheScriptEngineService(settings); @@ -37,8 +39,15 @@ public final class MessyTestUtils { Set engineServiceSet = new HashSet<>(); engineServiceSet.add(mustacheScriptEngineService); engineServiceSet.add(groovyScriptEngineService); - ScriptContextRegistry registry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); + ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry( + Arrays.asList( + new ScriptEngineRegistry.ScriptEngineRegistration(GroovyScriptEngineService.class, GroovyScriptEngineService.TYPES), + new ScriptEngineRegistry.ScriptEngineRegistration(XMustacheScriptEngineService.class, XMustacheScriptEngineService.TYPES) + ) + ); + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); - return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, new ResourceWatcherService(settings, tp), registry)); + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); + return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings)); } } diff --git a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java index dff371393ea..b9c6c0c1992 100644 --- a/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java +++ b/elasticsearch/qa/messy-test-watcher-with-groovy/src/test/java/org/elasticsearch/messy/tests/ScriptConditionTests.java @@ -68,7 +68,7 @@ public class ScriptConditionTests extends ESTestCase { public void testExecuteMergedParams() throws Exception { ScriptServiceProxy scriptService = getScriptServiceProxy(tp); - Script script = Script.inline("ctx.payload.hits.total > threshold").lang(ScriptService.DEFAULT_LANG).params(singletonMap("threshold", 1)).build(); + Script script = Script.inline("ctx.payload.hits.total > threshold").lang(Script.DEFAULT_LANG).params(singletonMap("threshold", 1)).build(); ExecutableScriptCondition executable = new ExecutableScriptCondition(new ScriptCondition(script), logger, scriptService); SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response)); diff --git a/elasticsearch/qa/smoke-test-watcher-with-groovy/build.gradle b/elasticsearch/qa/smoke-test-watcher-with-groovy/build.gradle index a2f70806254..24676900959 100644 --- a/elasticsearch/qa/smoke-test-watcher-with-groovy/build.gradle +++ b/elasticsearch/qa/smoke-test-watcher-with-groovy/build.gradle @@ -8,7 +8,7 @@ dependencies { integTest { cluster { plugin 'x-pack', project(':x-plugins:elasticsearch:x-pack') - systemProperty 'es.script.inline', 'on' + systemProperty 'es.script.inline', 'true' systemProperty 'es.shield.enabled', 'false' systemProperty 'es.marvel.enabled', 'false' } diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MarvelF.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MarvelF.java index 81950304ff4..f905d0fe2d6 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MarvelF.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MarvelF.java @@ -27,7 +27,7 @@ public class MarvelF { public static void main(String[] args) throws Throwable { Settings.Builder settings = Settings.builder(); - settings.put("script.inline", "on"); + settings.put("script.inline", "true"); settings.put("security.manager.enabled", "false"); settings.put("plugins.load_classpath_plugins", "false"); settings.put("cluster.name", MarvelF.class.getSimpleName()); diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/ShieldF.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/ShieldF.java index 506a8b71018..d33162a458d 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/ShieldF.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/ShieldF.java @@ -36,7 +36,7 @@ public class ShieldF { Settings.Builder settings = Settings.builder(); settings.put("http.cors.enabled", "true"); settings.put("http.cors.allow-origin", "*"); - settings.put("script.inline", "on"); + settings.put("script.inline", "true"); settings.put("shield.enabled", "true"); settings.put("security.manager.enabled", "false"); // Disable Marvel to prevent cluster activity diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java index 5f59cd923db..fc5f329a375 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java @@ -20,6 +20,7 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.script.ScriptEngineRegistry; import org.elasticsearch.script.ScriptModule; import org.elasticsearch.shield.authz.AuthorizationModule; import org.elasticsearch.watcher.actions.WatcherActionModule; @@ -185,7 +186,7 @@ public class WatcherPlugin extends Plugin { public void onModule(ScriptModule module) { module.registerScriptContext(ScriptServiceProxy.INSTANCE); if (enabled && !transportClient) { - module.addScriptEngine(XMustacheScriptEngineService.class); + module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(XMustacheScriptEngineService.class, XMustacheScriptEngineService.TYPES)); } } diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java index c102414f10c..dd220069200 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/Script.java @@ -14,6 +14,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService.ScriptType; +import org.elasticsearch.script.ScriptSettings; import java.io.IOException; import java.util.Collections; @@ -25,7 +26,7 @@ import java.util.Map; public class Script implements ToXContent { public static final ScriptType DEFAULT_TYPE = ScriptType.INLINE; - public static final String DEFAULT_LANG = ScriptService.DEFAULT_LANG; + public static final String DEFAULT_LANG = ScriptSettings.DEFAULT_LANG; private final String script; private final @Nullable ScriptType type; diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheScriptEngineService.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheScriptEngineService.java index 6875864f944..d47523a5cea 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheScriptEngineService.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheScriptEngineService.java @@ -23,6 +23,7 @@ import org.elasticsearch.search.lookup.SearchLookup; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.Collections; +import java.util.List; import java.util.Locale; import java.util.Map; @@ -33,6 +34,8 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S public static final String NAME = "xmustache"; + public static final List TYPES = Collections.singletonList(NAME); + /** * @param settings automatically wired by Guice. * */ @@ -58,17 +61,17 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S } @Override - public String[] types() { - return new String[] { NAME }; + public List getTypes() { + return TYPES; } @Override - public String[] extensions() { - return new String[] { NAME }; + public List getExtensions() { + return TYPES; } @Override - public boolean sandboxed() { + public boolean isSandboxed() { return true; } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/script/SleepScriptEngine.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/script/SleepScriptEngine.java index a55ee8f3354..131748a3dd7 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/script/SleepScriptEngine.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/script/SleepScriptEngine.java @@ -11,14 +11,18 @@ import org.elasticsearch.search.lookup.SearchLookup; import java.io.IOException; import java.util.Collections; +import java.util.List; import java.util.Map; /** * A dummy script engine used for testing. Scripts must be a number. Running the script */ public class SleepScriptEngine implements ScriptEngineService { + public static final String NAME = "sleep"; + public static final List TYPES = Collections.singletonList(NAME); + public static class TestPlugin extends Plugin { public TestPlugin() { @@ -35,23 +39,23 @@ public class SleepScriptEngine implements ScriptEngineService { } public void onModule(ScriptModule module) { - module.addScriptEngine(SleepScriptEngine.class); + module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(SleepScriptEngine.class, SleepScriptEngine.TYPES)); } } @Override - public String[] types() { - return new String[]{ NAME }; + public List getTypes() { + return TYPES; } @Override - public String[] extensions() { - return types(); + public List getExtensions() { + return TYPES; } @Override - public boolean sandboxed() { + public boolean isSandboxed() { return true; } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/WatcherF.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/WatcherF.java index 6daa8f7e417..67980215353 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/WatcherF.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/WatcherF.java @@ -29,7 +29,7 @@ public class WatcherF { Settings.Builder settings = Settings.builder(); settings.put("http.cors.enabled", "true"); settings.put("http.cors.allow-origin", "*"); - settings.put("script.inline", "on"); + settings.put("script.inline", "true"); settings.put("shield.enabled", "false"); settings.put("security.manager.enabled", "false"); settings.put("cluster.name", WatcherF.class.getSimpleName()); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java index f66a3de6ce2..6e3bd04adec 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/condition/script/ScriptConditionTests.java @@ -67,7 +67,7 @@ public class ScriptConditionTests extends ESTestCase { public void testExecuteMergedParams() throws Exception { ScriptServiceProxy scriptService = getScriptServiceProxy(tp); - Script script = Script.inline("ctx.payload.hits.total > threshold").lang(ScriptService.DEFAULT_LANG).params(singletonMap("threshold", 1)).build(); + Script script = Script.inline("ctx.payload.hits.total > threshold").lang(Script.DEFAULT_LANG).params(singletonMap("threshold", 1)).build(); ExecutableScriptCondition executable = new ExecutableScriptCondition(new ScriptCondition(script), logger, scriptService); SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 500l, new ShardSearchFailure[0]); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response)); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java index 9e571b96b65..619ecfed0f0 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/watcher/test/WatcherTestUtils.java @@ -21,8 +21,10 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.env.Environment; import org.elasticsearch.script.ScriptContextRegistry; +import org.elasticsearch.script.ScriptEngineRegistry; import org.elasticsearch.script.ScriptEngineService; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.ScriptSettings; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.threadpool.ThreadPool; @@ -240,16 +242,19 @@ public final class WatcherTestUtils { public static ScriptServiceProxy getScriptServiceProxy(ThreadPool tp) throws Exception { Settings settings = Settings.settingsBuilder() - .put("script.inline", "on") - .put("script.indexed", "on") + .put("script.inline", "true") + .put("script.indexed", "true") .put("path.home", createTempDir()) .build(); XMustacheScriptEngineService mustacheScriptEngineService = new XMustacheScriptEngineService(settings); Set engineServiceSet = new HashSet<>(); engineServiceSet.add(mustacheScriptEngineService); - ScriptContextRegistry registry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE)); - return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, new ResourceWatcherService(settings, tp), registry)); + ScriptEngineRegistry scriptEngineRegistry = + new ScriptEngineRegistry(Collections.singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(XMustacheScriptEngineService.class, XMustacheScriptEngineService.TYPES))); + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); + return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings), engineServiceSet, new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings)); } public static SearchType getRandomSupportedSearchType() {