From b9944aa63763f12bf7ef45f805d1c43f98b7ec0d Mon Sep 17 00:00:00 2001 From: Aditya Dhulipala Date: Tue, 12 Jun 2018 00:53:36 -0700 Subject: [PATCH] Validate xContentType in PutWatchRequest. (#31088) Trying to post a new watch without any body currently results in a NullPointerException. This change fixes that by validating that Post and Put requests always have a body. Closes #30057 --- .../transport/actions/put/PutWatchRequest.java | 3 +++ .../rest-api-spec/api/xpack.watcher.put_watch.json | 2 +- .../test/watcher/put_watch/10_basic.yml | 13 +++++++++++++ .../action/WatchRequestValidationTests.java | 6 ++++++ 4 files changed, 23 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 21b3fc39bba10..a29b3b0a86b2e 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 @@ -126,6 +126,9 @@ public ActionRequestValidationException validate() { if (source == null) { validationException = ValidateActions.addValidationError("watch source is missing", validationException); } + if (xContentType == null) { + validationException = ValidateActions.addValidationError("request body is missing", validationException); + } return validationException; } 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 d41f8ea221eac..7e29aeaaf43f7 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 @@ -29,7 +29,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/10_basic.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml index 50bb7e60ff41c..74d78820560a6 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 @@ -37,3 +37,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/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 05505398ff5c9..e2e13b3022b9b 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()));