From 105b689ec040f6cc186220060e9d7099773926d2 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 24 Apr 2017 15:45:26 -0700 Subject: [PATCH] Templates: Convert template uses to ScriptService.compileTemplate (elastic/x-pack-elasticsearch#1170) This is the xpack side of elastic/elasticsearch#24280 Original commit: elastic/x-pack-elasticsearch@90b7b2c6b7f17c97568f789e1612f31257183874 --- .../xpack/common/text/TextTemplateEngine.java | 10 ++----- .../SecurityIndexSearcherWrapper.java | 6 ++-- .../search/WatcherSearchTemplateService.java | 5 ++-- .../xpack/common/text/TextTemplateTests.java | 28 ++++++++----------- .../script/ScriptTransformTests.java | 4 ++- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/text/TextTemplateEngine.java b/plugin/src/main/java/org/elasticsearch/xpack/common/text/TextTemplateEngine.java index feefd5e3d44..fcfb5d94dc9 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/text/TextTemplateEngine.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/text/TextTemplateEngine.java @@ -14,6 +14,7 @@ import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.xpack.watcher.Watcher; import java.util.Collections; @@ -55,13 +56,8 @@ public class TextTemplateEngine extends AbstractComponent { options.put(Script.CONTENT_TYPE_OPTION, mediaType); } Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel); - CompiledScript compiledScript = service.compile(script, Watcher.SCRIPT_CONTEXT); - ExecutableScript executable = service.executable(compiledScript, model); - Object result = executable.run(); - if (result instanceof BytesReference) { - return ((BytesReference) result).utf8ToString(); - } - return result.toString(); + CompiledTemplate compiledTemplate = service.compileTemplate(script, Watcher.SCRIPT_CONTEXT); + return compiledTemplate.run(model).utf8ToString(); } private String trimContentType(TextTemplate textTemplate) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java index a4c3dc3f26d..bd07263ae8f 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java @@ -63,6 +63,7 @@ import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.xpack.security.authc.Authentication; import org.elasticsearch.xpack.security.authz.AuthorizationService; import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader; @@ -277,9 +278,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper { params.put("_user", userModel); // Always enforce mustache script lang: script = new Script(script.getType(), "mustache", script.getIdOrCode(), script.getOptions(), params); - CompiledScript compiledScript = scriptService.compile(script, ScriptContext.Standard.SEARCH); - ExecutableScript executable = scriptService.executable(compiledScript, script.getParams()); - return (BytesReference) executable.run(); + CompiledTemplate compiledTemplate = scriptService.compileTemplate(script, ScriptContext.Standard.SEARCH); + return compiledTemplate.run(script.getParams()); } else { return querySource; } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java index c40f0d1f16d..cfe8b1f56e1 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java @@ -17,6 +17,7 @@ import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.xpack.watcher.Watcher; import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.watcher.support.Variables; @@ -53,8 +54,8 @@ public class WatcherSearchTemplateService extends AbstractComponent { } // Templates are always of lang mustache: Script template = new Script(source.getType(), "mustache", source.getIdOrCode(), source.getOptions(), watcherContextParams); - CompiledScript compiledScript = scriptService.compile(template, Watcher.SCRIPT_CONTEXT); - return (BytesReference) scriptService.executable(compiledScript, template.getParams()).run(); + CompiledTemplate compiledTemplate = scriptService.compileTemplate(template, Watcher.SCRIPT_CONTEXT); + return compiledTemplate.run(template.getParams()); } public SearchRequest toSearchRequest(WatcherSearchTemplateRequest request) throws IOException { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java b/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java index d8b6b2c2e6e..ad73c4150b0 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/common/text/TextTemplateTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.common.text; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -15,6 +16,7 @@ import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.Watcher; import org.junit.Before; @@ -38,13 +40,11 @@ public class TextTemplateTests extends ESTestCase { private ScriptService service; private TextTemplateEngine engine; - private ExecutableScript script; private final String lang = "mustache"; @Before public void init() throws Exception { service = mock(ScriptService.class); - script = mock(ExecutableScript.class); engine = new TextTemplateEngine(Settings.EMPTY, service); } @@ -57,12 +57,10 @@ public class TextTemplateTests extends ESTestCase { merged = unmodifiableMap(merged); ScriptType type = randomFrom(ScriptType.values()); - CompiledScript compiledScript = mock(CompiledScript.class); - when(service.compile(new Script(type, lang, templateText, + CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text"); + when(service.compileTemplate(new Script(type, lang, templateText, type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, - merged), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledScript); - when(service.executable(compiledScript, model)).thenReturn(script); - when(script.run()).thenReturn("rendered_text"); + merged), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledTemplate); TextTemplate template = templateBuilder(type, templateText, params); assertThat(engine.render(template, model), is("rendered_text")); @@ -74,12 +72,10 @@ public class TextTemplateTests extends ESTestCase { Map model = singletonMap("key", "model_val"); ScriptType type = randomFrom(ScriptType.values()); - CompiledScript compiledScript = mock(CompiledScript.class); - when(service.compile(new Script(type, lang, templateText, + CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text"); + when(service.compileTemplate(new Script(type, lang, templateText, type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, - model), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledScript); - when(service.executable(compiledScript, model)).thenReturn(script); - when(script.run()).thenReturn("rendered_text"); + model), Watcher.SCRIPT_CONTEXT)).thenReturn(compiledTemplate); TextTemplate template = templateBuilder(type, templateText, params); assertThat(engine.render(template, model), is("rendered_text")); @@ -89,12 +85,10 @@ public class TextTemplateTests extends ESTestCase { String templateText = "_template"; Map model = singletonMap("key", "model_val"); - CompiledScript compiledScript = mock(CompiledScript.class); - when(service.compile(new Script(ScriptType.INLINE, lang, templateText, + CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text"); + when(service.compileTemplate(new Script(ScriptType.INLINE, lang, templateText, Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_CONTEXT)) - .thenReturn(compiledScript); - when(service.executable(compiledScript, model)).thenReturn(script); - when(script.run()).thenReturn("rendered_text"); + .thenReturn(compiledTemplate); TextTemplate template = new TextTemplate(templateText); assertThat(engine.render(template, model), is("rendered_text")); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index 932c6339fac..e759c163b23 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.watcher.transform.script; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -14,6 +15,7 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.template.CompiledTemplate; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; @@ -169,7 +171,7 @@ public class ScriptTransformTests extends ESTestCase { String errorMessage = "expected error message"; ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"), Collections.emptyList(), "whatever", "whatever"); - when(scriptService.compile(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException); + when(scriptService.compileTemplate(anyObject(), eq(Watcher.SCRIPT_CONTEXT))).thenThrow(scriptException); ScriptTransformFactory transformFactory = new ScriptTransformFactory(Settings.builder().build(), scriptService);