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 fcfb5d94dc9..a4bd7c21731 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 @@ -57,7 +57,7 @@ public class TextTemplateEngine extends AbstractComponent { } Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel); CompiledTemplate compiledTemplate = service.compileTemplate(script, Watcher.SCRIPT_CONTEXT); - return compiledTemplate.run(model).utf8ToString(); + return compiledTemplate.run(model); } 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 4559b7b3e90..35ca66874d3 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 @@ -134,9 +134,9 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper { BooleanQuery.Builder filter = new BooleanQuery.Builder(); for (BytesReference bytesReference : permissions.getQueries()) { QueryShardContext queryShardContext = queryShardContextProvider.apply(shardId); - bytesReference = evaluateTemplate(bytesReference); - try (XContentParser parser = XContentFactory.xContent(bytesReference) - .createParser(queryShardContext.getXContentRegistry(), bytesReference)) { + String templateResult = evaluateTemplate(bytesReference.utf8ToString()); + try (XContentParser parser = XContentFactory.xContent(templateResult) + .createParser(queryShardContext.getXContentRegistry(), templateResult)) { QueryBuilder queryBuilder = queryShardContext.newParseContext(parser).parseInnerQueryBuilder(); verifyRoleQuery(queryBuilder); failIfQueryUsesClient(scriptService, queryBuilder, queryShardContext); @@ -244,7 +244,7 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper { } } - BytesReference evaluateTemplate(BytesReference querySource) throws IOException { + String evaluateTemplate(String querySource) throws IOException { // EMPTY is safe here because we never use namedObject try (XContentParser parser = XContentFactory.xContent(querySource).createParser(NamedXContentRegistry.EMPTY, querySource)) { XContentParser.Token token = parser.nextToken(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java index a5c4f57aaa0..14c7104f9cd 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java @@ -9,6 +9,7 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -53,9 +54,9 @@ public class ExecutableSearchInput extends ExecutableInput new BytesArray("rendered_text"); + CompiledTemplate compiledTemplate = templateParams -> "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(compiledTemplate); @@ -72,7 +72,7 @@ public class TextTemplateTests extends ESTestCase { Map model = singletonMap("key", "model_val"); ScriptType type = randomFrom(ScriptType.values()); - CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text"); + CompiledTemplate compiledTemplate = templateParams -> "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(compiledTemplate); @@ -85,7 +85,7 @@ public class TextTemplateTests extends ESTestCase { String templateText = "_template"; Map model = singletonMap("key", "model_val"); - CompiledTemplate compiledTemplate = templateParams -> new BytesArray("rendered_text"); + CompiledTemplate compiledTemplate = templateParams -> "rendered_text"; when(service.compileTemplate(new Script(ScriptType.INLINE, lang, templateText, Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_CONTEXT)) .thenReturn(compiledTemplate); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java index feb25434b54..d2d11dbb5be 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperUnitTests.java @@ -465,7 +465,7 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase { Script script = new Script(ScriptType.INLINE, "mustache", query, Collections.singletonMap("custom", "value")); builder = jsonBuilder().startObject().field("template"); script.toXContent(builder, ToXContent.EMPTY_PARAMS); - BytesReference querySource = builder.endObject().bytes(); + String querySource = builder.endObject().string(); securityIndexSearcherWrapper.evaluateTemplate(querySource); ArgumentCaptor