Skip to content

Commit 1724fab

Browse files
authored
[7.17] Register mustache size limit setting (#119291) (#119294)
* Register mustache size limit setting (#119291) In #114002 the maximum size of a mustache script output was made configurable. However, the setting was not registered. This commit registers the setting so that it can be set in elasticsearch.yml. * fix compile * fix compile * fix test compile * fix license header
1 parent fa2883d commit 1724fab

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docs/changelog/119291.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 119291
2+
summary: Register mustache size limit setting
3+
area: Infra/Scripting
4+
type: bug
5+
issues: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
package org.elasticsearch.script.mustache;
9+
10+
import org.elasticsearch.ElasticsearchParseException;
11+
import org.elasticsearch.action.search.SearchRequest;
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.plugins.Plugin;
14+
import org.elasticsearch.script.ScriptType;
15+
import org.elasticsearch.test.ESSingleNodeTestCase;
16+
17+
import java.util.Arrays;
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
21+
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
22+
import static org.hamcrest.Matchers.equalTo;
23+
24+
public class MustacheSettingsIT extends ESSingleNodeTestCase {
25+
@Override
26+
protected Collection<Class<? extends Plugin>> getPlugins() {
27+
return Arrays.asList(MustachePlugin.class);
28+
}
29+
30+
@Override
31+
protected Settings nodeSettings() {
32+
return Settings.builder().put(MustacheScriptEngine.MUSTACHE_RESULT_SIZE_LIMIT.getKey(), "10b").build();
33+
}
34+
35+
public void testResultSizeLimit() throws Exception {
36+
createIndex("test");
37+
client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "value1").endObject()).get();
38+
client().admin().indices().prepareRefresh().get();
39+
40+
String query = "{ \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\" }";
41+
SearchRequest searchRequest = new SearchRequest();
42+
searchRequest.indices("test");
43+
ElasticsearchParseException e = expectThrows(
44+
ElasticsearchParseException.class,
45+
() -> new SearchTemplateRequestBuilder(client()).setRequest(searchRequest)
46+
.setScript(query)
47+
.setScriptType(ScriptType.INLINE)
48+
.setScriptParams(Collections.singletonMap("my_size", 1))
49+
.get()
50+
);
51+
assertThat(e.getMessage(), equalTo("Mustache script result size limit exceeded"));
52+
}
53+
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustachePlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.node.DiscoveryNodes;
1515
import org.elasticsearch.common.settings.ClusterSettings;
1616
import org.elasticsearch.common.settings.IndexScopedSettings;
17+
import org.elasticsearch.common.settings.Setting;
1718
import org.elasticsearch.common.settings.Settings;
1819
import org.elasticsearch.common.settings.SettingsFilter;
1920
import org.elasticsearch.plugins.ActionPlugin;
@@ -61,4 +62,9 @@ public List<RestHandler> getRestHandlers(
6162
new RestRenderSearchTemplateAction()
6263
);
6364
}
65+
66+
@Override
67+
public List<Setting<?>> getSettings() {
68+
return Arrays.asList(MustacheScriptEngine.MUSTACHE_RESULT_SIZE_LIMIT);
69+
}
6470
}

0 commit comments

Comments
 (0)