Skip to content

Commit fffcf93

Browse files
committed
Watcher: Fix chain input toXcontent serialization (#31721)
The xcontent parameters were not passed to the xcontent serialization of the chain input for each chain. This could lead to wrongly stored watches, which did not contain passwords but only their redacted counterparts, when an input inside of a chain input contained a password.
1 parent 821a533 commit fffcf93

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
"Test get watch api with chained input and basic auth":
3+
- do:
4+
cluster.health:
5+
wait_for_status: yellow
6+
7+
- do:
8+
xpack.watcher.put_watch:
9+
id: "my_watch"
10+
body: >
11+
{
12+
"trigger": {
13+
"schedule": {
14+
"cron": "0 0 0 1 * ? 2099"
15+
}
16+
},
17+
"input": {
18+
"chain": {
19+
"inputs": [
20+
{
21+
"http": {
22+
"http": {
23+
"request": {
24+
"url" : "http://localhost/",
25+
"auth": {
26+
"basic": {
27+
"username": "Username123",
28+
"password": "Password123"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
]
36+
}
37+
},
38+
"actions": {
39+
"logging": {
40+
"logging": {
41+
"text": "logging statement here"
42+
}
43+
}
44+
}
45+
}
46+
47+
- do:
48+
xpack.watcher.get_watch:
49+
id: "my_watch"
50+
- match: { found : true}
51+
- match: { _id: "my_watch" }

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
4141
builder.startArray(INPUTS.getPreferredName());
4242
for (Tuple<String, Input> tuple : inputs) {
4343
builder.startObject().startObject(tuple.v1());
44-
builder.field(tuple.v2().type(), tuple.v2());
44+
builder.field(tuple.v2().type(), tuple.v2(), params);
4545
builder.endObject().endObject();
4646
}
4747
builder.endArray();

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.ElasticsearchParseException;
1010
import org.elasticsearch.common.Strings;
1111
import org.elasticsearch.common.bytes.BytesReference;
12+
import org.elasticsearch.common.collect.Tuple;
1213
import org.elasticsearch.common.settings.Settings;
1314
import org.elasticsearch.common.xcontent.ToXContent;
1415
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -18,6 +19,7 @@
1819
import org.elasticsearch.test.ESTestCase;
1920
import org.elasticsearch.test.SecuritySettingsSourceField;
2021
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
22+
import org.elasticsearch.xpack.core.watcher.input.Input;
2123
import org.elasticsearch.xpack.core.watcher.watch.Payload;
2224
import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate;
2325
import org.elasticsearch.xpack.watcher.common.http.auth.basic.BasicAuth;
@@ -29,6 +31,7 @@
2931
import org.elasticsearch.xpack.watcher.input.simple.SimpleInputFactory;
3032
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;
3133

34+
import java.util.Collections;
3235
import java.util.HashMap;
3336
import java.util.Map;
3437

@@ -46,6 +49,7 @@
4649
import static org.hamcrest.Matchers.hasSize;
4750
import static org.hamcrest.Matchers.instanceOf;
4851
import static org.hamcrest.Matchers.is;
52+
import static org.hamcrest.Matchers.sameInstance;
4953

5054
public class ChainInputTests extends ESTestCase {
5155

@@ -220,4 +224,24 @@ public void testParsingShouldBeStrictWhenStartingInputs() throws Exception {
220224
expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser));
221225
assertThat(e.getMessage(), containsString("Expected starting JSON object after [first] in watch [test]"));
222226
}
227+
228+
public void testThatXContentParametersArePassedToInputs() throws Exception {
229+
ToXContent.Params randomParams = new ToXContent.MapParams(Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)));
230+
ChainInput chainInput = new ChainInput(Collections.singletonList(Tuple.tuple("whatever", new Input() {
231+
@Override
232+
public String type() {
233+
return "test";
234+
}
235+
236+
@Override
237+
public XContentBuilder toXContent(XContentBuilder builder, Params params) {
238+
assertThat(params, sameInstance(randomParams));
239+
return builder;
240+
}
241+
})));
242+
243+
try (XContentBuilder builder = jsonBuilder()) {
244+
chainInput.toXContent(builder, randomParams);
245+
}
246+
}
223247
}

0 commit comments

Comments
 (0)