|
18 | 18 | */
|
19 | 19 | package org.elasticsearch.ingest.common;
|
20 | 20 |
|
| 21 | +import org.elasticsearch.ElasticsearchException; |
| 22 | +import org.elasticsearch.ExceptionsHelper; |
21 | 23 | import org.elasticsearch.action.support.WriteRequest;
|
| 24 | +import org.elasticsearch.common.Strings; |
22 | 25 | import org.elasticsearch.common.bytes.BytesArray;
|
23 | 26 | import org.elasticsearch.common.bytes.BytesReference;
|
24 | 27 | import org.elasticsearch.common.settings.Settings;
|
| 28 | +import org.elasticsearch.common.util.set.Sets; |
25 | 29 | import org.elasticsearch.common.xcontent.XContentType;
|
26 | 30 | import org.elasticsearch.plugins.Plugin;
|
27 | 31 | import org.elasticsearch.script.MockScriptEngine;
|
|
33 | 37 | import java.util.Collection;
|
34 | 38 | import java.util.Collections;
|
35 | 39 | import java.util.Map;
|
| 40 | +import java.util.function.Consumer; |
36 | 41 | import java.util.function.Function;
|
37 | 42 |
|
38 | 43 | import static org.hamcrest.Matchers.equalTo;
|
@@ -64,6 +69,66 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
64 | 69 | }
|
65 | 70 | }
|
66 | 71 |
|
| 72 | + public void testScriptDisabled() throws Exception { |
| 73 | + String pipelineIdWithoutScript = randomAlphaOfLengthBetween(5, 10); |
| 74 | + String pipelineIdWithScript = pipelineIdWithoutScript + "_script"; |
| 75 | + internalCluster().startNode(); |
| 76 | + |
| 77 | + BytesReference pipelineWithScript = new BytesArray("{\n" + |
| 78 | + " \"processors\" : [\n" + |
| 79 | + " {\"script\" : {\"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"my_script\"}}\n" + |
| 80 | + " ]\n" + |
| 81 | + "}"); |
| 82 | + BytesReference pipelineWithoutScript = new BytesArray("{\n" + |
| 83 | + " \"processors\" : [\n" + |
| 84 | + " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + |
| 85 | + " ]\n" + |
| 86 | + "}"); |
| 87 | + |
| 88 | + Consumer<String> checkPipelineExists = (id) -> assertThat(client().admin().cluster().prepareGetPipeline(id) |
| 89 | + .get().pipelines().get(0).getId(), equalTo(id)); |
| 90 | + |
| 91 | + client().admin().cluster().preparePutPipeline(pipelineIdWithScript, pipelineWithScript, XContentType.JSON).get(); |
| 92 | + client().admin().cluster().preparePutPipeline(pipelineIdWithoutScript, pipelineWithoutScript, XContentType.JSON).get(); |
| 93 | + |
| 94 | + checkPipelineExists.accept(pipelineIdWithScript); |
| 95 | + checkPipelineExists.accept(pipelineIdWithoutScript); |
| 96 | + |
| 97 | + |
| 98 | + internalCluster().stopCurrentMasterNode(); |
| 99 | + internalCluster().startNode(Settings.builder().put("script.allowed_types", "none")); |
| 100 | + |
| 101 | + checkPipelineExists.accept(pipelineIdWithoutScript); |
| 102 | + checkPipelineExists.accept(pipelineIdWithScript); |
| 103 | + |
| 104 | + client().prepareIndex("index", "doc", "1") |
| 105 | + .setSource("x", 0) |
| 106 | + .setPipeline(pipelineIdWithoutScript) |
| 107 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 108 | + .get(); |
| 109 | + |
| 110 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, |
| 111 | + () -> client().prepareIndex("index", "doc", "2") |
| 112 | + .setSource("x", 0) |
| 113 | + .setPipeline(pipelineIdWithScript) |
| 114 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 115 | + .get()); |
| 116 | + assertThat(exception.getHeaderKeys(), equalTo(Sets.newHashSet("processor_type"))); |
| 117 | + assertThat(exception.getHeader("processor_type"), equalTo(Arrays.asList("unknown"))); |
| 118 | + assertThat(exception.getRootCause().getMessage(), |
| 119 | + equalTo("pipeline with id [" + pipelineIdWithScript + "] could not be loaded, caused by " + |
| 120 | + "[ElasticsearchParseException[Error updating pipeline with id [" + pipelineIdWithScript + "]]; " + |
| 121 | + "nested: ElasticsearchException[java.lang.IllegalArgumentException: cannot execute [inline] scripts]; " + |
| 122 | + "nested: IllegalArgumentException[cannot execute [inline] scripts];; " + |
| 123 | + "ElasticsearchException[java.lang.IllegalArgumentException: cannot execute [inline] scripts]; " + |
| 124 | + "nested: IllegalArgumentException[cannot execute [inline] scripts];; java.lang.IllegalArgumentException: " + |
| 125 | + "cannot execute [inline] scripts]")); |
| 126 | + |
| 127 | + Map<String, Object> source = client().prepareGet("index", "doc", "1").get().getSource(); |
| 128 | + assertThat(source.get("x"), equalTo(0)); |
| 129 | + assertThat(source.get("y"), equalTo(0)); |
| 130 | + } |
| 131 | + |
67 | 132 | public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exception {
|
68 | 133 | internalCluster().startNode();
|
69 | 134 |
|
|
0 commit comments