diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java index be7c8536aeb..1b9f45a284f 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/action/GetJobsAction.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.prelert.action; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.Action; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequestValidationException; @@ -50,6 +51,7 @@ import org.elasticsearch.xpack.prelert.utils.ExceptionsHelper; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -62,12 +64,16 @@ public class GetJobsAction extends Action METRIC_WHITELIST = Arrays.asList(ALL, CONFIG, DATA_COUNTS, + MODEL_SIZE_STATS, SCHEDULER_STATE, STATUS); + private GetJobsAction() { super(NAME); } @@ -87,6 +93,8 @@ public class GetJobsAction extends Action PARSER = new ObjectParser<>(NAME, Request::new); public static final ParseField METRIC = new ParseField("metric"); + + static { PARSER.declareString(Request::setJobId, Job.ID); PARSER.declareObject(Request::setPageParams, PageParams.PARSER, PageParams.PAGE); @@ -171,7 +179,13 @@ public class GetJobsAction extends Action stats) { - if (stats.contains("_all")) { + for (String s : stats) { + if (!METRIC_WHITELIST.contains(s)) { + throw new ElasticsearchStatusException("Metric [" + s + "] is not a valid metric. " + + "Accepted metrics are: [" + METRIC_WHITELIST + "]", RestStatus.BAD_REQUEST); + } + } + if (stats.contains(ALL)) { all(); } else { config(stats.contains(CONFIG)); diff --git a/elasticsearch/src/test/resources/rest-api-spec/test/jobs_get_stats.yaml b/elasticsearch/src/test/resources/rest-api-spec/test/jobs_get_stats.yaml index fc2289974ef..c62a1eb68b4 100644 --- a/elasticsearch/src/test/resources/rest-api-spec/test/jobs_get_stats.yaml +++ b/elasticsearch/src/test/resources/rest-api-spec/test/jobs_get_stats.yaml @@ -129,3 +129,11 @@ setup: - is_false: jobs.0.model_size_stats - match: { jobs.0.scheduler_state.status: STOPPED } +--- +"Test bad metric": + - do: + catch: request + xpack.prelert.get_jobs: + job_id: "job-stats-test" + metric: "foobar" +