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@4c429933b9
This commit is contained in:
parent
4586ca2e06
commit
8eb97c5509
|
@ -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<ScriptEngineService> 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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<String> 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<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] extensions() {
|
||||
return new String[] { NAME };
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sandboxed() {
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> 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<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] extensions() {
|
||||
return types();
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sandboxed() {
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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<ScriptEngineService> 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() {
|
||||
|
|
Loading…
Reference in New Issue