Skip to content

Commit ef9eaea

Browse files
committed
[Rollup] Metric config parser must use builder so validation runs (#31159)
The parser for the Metric config was directly instantiating the config object, rather than using the builder. That means it was bypassing the validation logic built into the builder, and would allow users to create invalid metric configs (like using unsupported metrics). The job would later blow up and abort due to bad configs, but this isn't immediately obvious to the user since the PutJob API succeeded.
1 parent 9258ea0 commit ef9eaea

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.io.stream.Writeable;
15-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
15+
import org.elasticsearch.common.xcontent.ObjectParser;
1616
import org.elasticsearch.common.xcontent.ToXContentFragment;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
1818
import org.elasticsearch.index.mapper.NumberFieldMapper;
@@ -75,12 +75,11 @@ public class MetricConfig implements Writeable, ToXContentFragment {
7575
MAPPER_TYPES = types;
7676
}
7777

78-
public static final ConstructingObjectParser<MetricConfig, Void> PARSER = new ConstructingObjectParser<>(
79-
NAME, a -> new MetricConfig((String)a[0], (List<String>) a[1]));
78+
public static final ObjectParser<MetricConfig.Builder, Void> PARSER = new ObjectParser<>(NAME, MetricConfig.Builder::new);
8079

8180
static {
82-
PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD);
83-
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), METRICS);
81+
PARSER.declareString(MetricConfig.Builder::setField, FIELD);
82+
PARSER.declareStringArray(MetricConfig.Builder::setMetrics, METRICS);
8483
}
8584

8685
MetricConfig(String name, List<String> metrics) {
@@ -257,4 +256,4 @@ public MetricConfig build() {
257256
return new MetricConfig(field, metrics);
258257
}
259258
}
260-
}
259+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class RollupJobConfig implements NamedWriteable, ToXContentObject {
6363
static {
6464
PARSER.declareString(RollupJobConfig.Builder::setId, RollupField.ID);
6565
PARSER.declareObject(RollupJobConfig.Builder::setGroupConfig, (p, c) -> GroupConfig.PARSER.apply(p,c).build(), GROUPS);
66-
PARSER.declareObjectArray(RollupJobConfig.Builder::setMetricsConfig, MetricConfig.PARSER, METRICS);
66+
PARSER.declareObjectArray(RollupJobConfig.Builder::setMetricsConfig, (p, c) -> MetricConfig.PARSER.apply(p, c).build(), METRICS);
6767
PARSER.declareString((params, val) ->
6868
params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT);
6969
PARSER.declareString(RollupJobConfig.Builder::setIndexPattern, INDEX_PATTERN);

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/MetricsConfigSerializingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class MetricsConfigSerializingTests extends AbstractSerializingTestCase<MetricConfig> {
2525
@Override
2626
protected MetricConfig doParseInstance(XContentParser parser) throws IOException {
27-
return MetricConfig.PARSER.apply(parser, null);
27+
return MetricConfig.PARSER.apply(parser, null).build();
2828
}
2929

3030
@Override
@@ -36,7 +36,7 @@ protected Writeable.Reader<MetricConfig> instanceReader() {
3636
protected MetricConfig createTestInstance() {
3737
return ConfigTestHelpers.getMetricConfig().build();
3838
}
39-
39+
4040
public void testValidateNoMapping() throws IOException {
4141
ActionRequestValidationException e = new ActionRequestValidationException();
4242
Map<String, Map<String, FieldCapabilities>> responseMap = new HashMap<>();

x-pack/plugin/src/test/resources/rest-api-spec/test/rollup/put_job.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,33 @@ setup:
188188
]
189189
}
190190
191+
---
192+
"Unknown Metric":
193+
194+
- do:
195+
catch: /Unsupported metric \[does_not_exist\]/
196+
headers:
197+
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
198+
xpack.rollup.put_job:
199+
id: foo
200+
body: >
201+
{
202+
"index_pattern": "foo",
203+
"rollup_index": "foo_rollup",
204+
"cron": "*/30 * * * * ?",
205+
"page_size" :10,
206+
"groups" : {
207+
"date_histogram": {
208+
"field": "the_field",
209+
"interval": "1h"
210+
}
211+
},
212+
"metrics": [
213+
{
214+
"field": "value_field",
215+
"metrics": ["min", "max", "sum", "does_not_exist"]
216+
}
217+
]
218+
}
219+
220+

0 commit comments

Comments
 (0)