From 67ccbe79ff0df1399be5c28775237dd3f7c99c6d Mon Sep 17 00:00:00 2001 From: Aditya Dhulipala Date: Mon, 4 Jun 2018 21:02:04 -0700 Subject: [PATCH 1/4] Validate xContentType in PutWatchRequest. This closes #30057. Trying to post a new watch by executing `POST _xpack/watcher/watch/my_watch` without any body will result in a NullPointerException. This change fixes that by validating that POST & PUT requests always have a non-null body. --- .../core/watcher/transport/actions/put/PutWatchRequest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java index 1985602d4df30..d04e3e7ea7d92 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java @@ -135,6 +135,9 @@ public ActionRequestValidationException validate() { if (source == null) { validationException = ValidateActions.addValidationError("watch source is missing", validationException); } + if (xContentType == null) { + validationException = ValidateActions.addValidationError("Content-Type is missing", validationException); + } return validationException; } From 013252dcb0b43dc8ce38106138b974714c2f0446 Mon Sep 17 00:00:00 2001 From: Aditya Dhulipala Date: Tue, 5 Jun 2018 20:48:54 -0700 Subject: [PATCH 2/4] Unit tests for xContentType validation in PutWatchRequest. --- .../core/watcher/transport/actions/put/PutWatchRequest.java | 2 +- .../transport/action/WatchRequestValidationTests.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java index d04e3e7ea7d92..47bc800cec72b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java @@ -136,7 +136,7 @@ public ActionRequestValidationException validate() { validationException = ValidateActions.addValidationError("watch source is missing", validationException); } if (xContentType == null) { - validationException = ValidateActions.addValidationError("Content-Type is missing", validationException); + validationException = ValidateActions.addValidationError("request body is missing", validationException); } return validationException; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java index 893e493bd1589..b1fa736292cc4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java @@ -86,6 +86,12 @@ public void testPutWatchSourceNull() { assertThat(e.validationErrors(), hasItem("watch source is missing")); } + public void testPutWatchContentNull() { + ActionRequestValidationException e = new PutWatchRequest("foo", BytesArray.EMPTY, null).validate(); + assertThat(e, is(notNullValue())); + assertThat(e.validationErrors(), hasItem("request body is missing")); + } + public void testGetWatchInvalidWatchId() { ActionRequestValidationException e = new GetWatchRequest("id with whitespaces").validate(); assertThat(e, is(notNullValue())); From 610c0d6fe9706affce3ea7aa8b75db4942d5b5c5 Mon Sep 17 00:00:00 2001 From: Aditya Dhulipala Date: Thu, 7 Jun 2018 22:38:24 -0700 Subject: [PATCH 3/4] Integration tests for xContentType validation in PutWatchRequest. --- .../rest-api-spec/api/xpack.watcher.put_watch.json | 2 +- .../put_watch/90_put_watch_content_validation.yml | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/xpack.watcher.put_watch.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/xpack.watcher.put_watch.json index 27007bbfe5741..3eda8b764dbad 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/xpack.watcher.put_watch.json +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/xpack.watcher.put_watch.json @@ -25,7 +25,7 @@ }, "body": { "description" : "The watch", - "required" : true + "required" : false } } } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml new file mode 100644 index 0000000000000..7f2149f5bd675 --- /dev/null +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml @@ -0,0 +1,12 @@ +--- +"Test empty body is rejected by put watch": + - do: + cluster.health: + wait_for_status: yellow + + - do: + catch: bad_request + xpack.watcher.put_watch: + id: "my_watch" + - match: { error.root_cause.0.type: "action_request_validation_exception" } + - match: { error.root_cause.0.reason: "Validation Failed: 1: request body is missing;" } From e4d2613b0d9ced50cb3527ea86df6b7d7bda8bed Mon Sep 17 00:00:00 2001 From: Aditya Dhulipala Date: Fri, 8 Jun 2018 07:36:54 -0700 Subject: [PATCH 4/4] Moved validation integration test into 10_basic suite. --- .../test/watcher/put_watch/10_basic.yml | 13 +++++++++++++ .../put_watch/90_put_watch_content_validation.yml | 12 ------------ 2 files changed, 13 insertions(+), 12 deletions(-) delete mode 100644 x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml index ed72f32981d34..b2ea5b8042f69 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml @@ -36,3 +36,16 @@ } } - match: { _id: "my_watch" } + +--- +"Test empty body is rejected by put watch": + - do: + cluster.health: + wait_for_status: yellow + + - do: + catch: bad_request + xpack.watcher.put_watch: + id: "my_watch" + - match: { error.root_cause.0.type: "action_request_validation_exception" } + - match: { error.root_cause.0.reason: "Validation Failed: 1: request body is missing;" } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml deleted file mode 100644 index 7f2149f5bd675..0000000000000 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/90_put_watch_content_validation.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -"Test empty body is rejected by put watch": - - do: - cluster.health: - wait_for_status: yellow - - - do: - catch: bad_request - xpack.watcher.put_watch: - id: "my_watch" - - match: { error.root_cause.0.type: "action_request_validation_exception" } - - match: { error.root_cause.0.reason: "Validation Failed: 1: request body is missing;" }