Skip to content

Commit 8d88b94

Browse files
authored
Scripting: Add optional context parameter to put stored script requests (#25014)
This commit adds an optional `context` url parameter to the put stored script request. When a context is specified, the script is compiled against that context before storing, as a validation the script will work when used in that context.
1 parent 39e59b4 commit 8d88b94

File tree

7 files changed

+37
-11
lines changed

7 files changed

+37
-11
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
3838

3939
private String id;
4040
private String lang;
41+
private String context;
4142
private BytesReference content;
4243
private XContentType xContentType;
4344

4445
public PutStoredScriptRequest() {
4546
super();
4647
}
4748

48-
public PutStoredScriptRequest(String id, String lang, BytesReference content, XContentType xContentType) {
49+
public PutStoredScriptRequest(String id, String lang, String context, BytesReference content, XContentType xContentType) {
4950
super();
5051
this.id = id;
5152
this.lang = lang;
53+
this.context = context;
5254
this.content = content;
5355
this.xContentType = Objects.requireNonNull(xContentType);
5456
}
@@ -94,6 +96,15 @@ public PutStoredScriptRequest lang(String lang) {
9496
return this;
9597
}
9698

99+
public String context() {
100+
return context;
101+
}
102+
103+
public PutStoredScriptRequest context(String context) {
104+
this.context = context;
105+
return this;
106+
}
107+
97108
public BytesReference content() {
98109
return content;
99110
}
@@ -128,6 +139,9 @@ public void readFrom(StreamInput in) throws IOException {
128139
} else {
129140
xContentType = XContentFactory.xContentType(content);
130141
}
142+
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
143+
context = in.readOptionalString();
144+
}
131145
}
132146

133147
@Override
@@ -140,6 +154,9 @@ public void writeTo(StreamOutput out) throws IOException {
140154
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
141155
xContentType.writeTo(out);
142156
}
157+
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
158+
out.writeOptionalString(context);
159+
}
143160
}
144161

145162
@Override

core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public RestPutStoredScriptAction(Settings settings, RestController controller) {
5050
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
5151
String id = request.param("id");
5252
String lang = request.param("lang");
53+
String context = request.param("context");
5354

5455
// In the case where only {lang} is not null, we make it {id} because of
5556
// name ordering issues in the handlers' paths.
@@ -65,7 +66,7 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
6566
"specifying lang [" + lang + "] as part of the url path is deprecated, use request content instead");
6667
}
6768

68-
PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, lang, content, request.getXContentType());
69+
PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, lang, context, content, request.getXContentType());
6970
return channel -> client.admin().cluster().putStoredScript(putRequest, new AcknowledgedRestListener<>(channel));
7071
}
7172
}

core/src/main/java/org/elasticsearch/script/ScriptService.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,12 @@ public void putStoredScript(ClusterService clusterService, PutStoredScriptReques
431431
} else if (isAnyContextEnabled() == false) {
432432
throw new IllegalArgumentException(
433433
"cannot put [" + ScriptType.STORED + "] script, no script contexts are enabled");
434-
} else {
435-
// TODO: executable context here is just a placeholder, replace with optional context name passed into PUT stored script req
436-
Object compiled = scriptEngine.compile(request.id(), source.getCode(), ExecutableScript.CONTEXT, Collections.emptyMap());
437-
438-
if (compiled == null) {
439-
throw new IllegalArgumentException("failed to parse/compile stored script [" + request.id() + "]" +
440-
(source.getCode() == null ? "" : " using code [" + source.getCode() + "]"));
434+
} else if (request.context() != null) {
435+
ScriptContext<?> context = contexts.get(request.context());
436+
if (context == null) {
437+
throw new IllegalArgumentException("Unknown context [" + request.context() + "]");
441438
}
439+
scriptEngine.compile(request.id(), source.getCode(), context, Collections.emptyMap());
442440
}
443441
} catch (ScriptException good) {
444442
throw good;

core/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
public class PutStoredScriptRequestTests extends ESTestCase {
3333

3434
public void testSerialization() throws IOException {
35-
PutStoredScriptRequest storedScriptRequest = new PutStoredScriptRequest("foo", "bar", new BytesArray("{}"), XContentType.JSON);
35+
PutStoredScriptRequest storedScriptRequest =
36+
new PutStoredScriptRequest("foo", "bar", "context", new BytesArray("{}"), XContentType.JSON);
3637

3738
assertEquals(XContentType.JSON, storedScriptRequest.xContentType());
3839
try (BytesStreamOutput output = new BytesStreamOutput()) {
@@ -44,6 +45,7 @@ public void testSerialization() throws IOException {
4445
assertEquals(XContentType.JSON, serialized.xContentType());
4546
assertEquals(storedScriptRequest.lang(), serialized.lang());
4647
assertEquals(storedScriptRequest.id(), serialized.id());
48+
assertEquals(storedScriptRequest.context(), serialized.context());
4749
}
4850
}
4951
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.rest.RestController;
2727
import org.elasticsearch.rest.RestRequest;
2828
import org.elasticsearch.rest.action.AcknowledgedRestListener;
29+
import org.elasticsearch.script.ExecutableScript;
2930
import org.elasticsearch.script.Script;
3031

3132
import java.io.IOException;
@@ -47,7 +48,8 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
4748
String id = request.param("id");
4849
BytesReference content = request.content();
4950

50-
PutStoredScriptRequest put = new PutStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG, content, request.getXContentType());
51+
PutStoredScriptRequest put = new PutStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG, ExecutableScript.CONTEXT.name,
52+
content, request.getXContentType());
5153
return channel -> client.admin().cluster().putStoredScript(put, new AcknowledgedRestListener<>(channel));
5254
}
5355
}

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/16_update2.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
catch: request
3939
put_script:
4040
id: "1"
41+
context: "search"
4142
body: { "script": {"lang": "painless", "code": "_score * foo bar + doc['myParent.weight'].value"} }
4243

4344
- do:
4445
catch: /compile error/
4546
put_script:
4647
id: "1"
48+
context: "search"
4749
body: { "script": {"lang": "painless", "code": "_score * foo bar + doc['myParent.weight'].value"} }

rest-api-spec/src/main/resources/rest-api-spec/api/put_script.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
}
1919
},
2020
"params" : {
21+
"context": {
22+
"type" : "string",
23+
"description" : "Context name to compile script against"
24+
}
2125
}
2226
},
2327
"body": {

0 commit comments

Comments
 (0)