From a27dc257c9599a7803d968db8fc386ae379a0801 Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Mon, 14 Aug 2017 20:39:39 +1000 Subject: [PATCH] Gracefully handle no content(-type) in Put License (elastic/x-pack-elasticsearch#2258) PUT /_xpack/license with no content or content-type should fail with an appropriate error message rather than throwing NPE. Original commit: elastic/x-pack-elasticsearch@f8c744d2a281e8255c811259c89ee105d99c81f9 --- .../org/elasticsearch/license/License.java | 23 +++++++++++-------- .../license/RestPutLicenseAction.java | 8 +++++-- .../test/license/20_put_license.yml | 9 ++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/license/License.java b/plugin/src/main/java/org/elasticsearch/license/License.java index b51bf060e6a..05d24ff8774 100644 --- a/plugin/src/main/java/org/elasticsearch/license/License.java +++ b/plugin/src/main/java/org/elasticsearch/license/License.java @@ -5,6 +5,14 @@ */ package org.elasticsearch.license; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Base64; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; + import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; @@ -19,15 +27,6 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Base64; -import java.util.Comparator; -import java.util.List; -import java.util.Locale; - /** * Data structure for license. Use {@link Builder} to build a license. * Provides serialization/deserialization & validation methods for license object @@ -490,6 +489,12 @@ public class License implements ToXContentObject { } public static License fromSource(BytesReference bytes, XContentType xContentType) throws IOException { + if (bytes == null || bytes.length() == 0) { + throw new ElasticsearchParseException("failed to parse license - no content provided"); + } + if (xContentType == null) { + throw new ElasticsearchParseException("failed to parse license - no content-type provided"); + } // EMPTY is safe here because we don't call namedObject final XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes); License license = null; diff --git a/plugin/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java b/plugin/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java index 96b53486a1f..8a92e108eb2 100644 --- a/plugin/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java +++ b/plugin/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java @@ -5,6 +5,9 @@ */ package org.elasticsearch.license; +import java.io.IOException; + +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -17,8 +20,6 @@ import org.elasticsearch.rest.action.RestBuilderListener; import org.elasticsearch.xpack.XPackClient; import org.elasticsearch.xpack.rest.XPackRestHandler; -import java.io.IOException; - import static org.elasticsearch.rest.RestRequest.Method.POST; import static org.elasticsearch.rest.RestRequest.Method.PUT; @@ -37,6 +38,9 @@ public class RestPutLicenseAction extends XPackRestHandler { @Override public RestChannelConsumer doPrepareRequest(final RestRequest request, final XPackClient client) throws IOException { + if (request.hasContent() == false) { + throw new IllegalArgumentException("The license must be provided in the request body"); + } PutLicenseRequest putLicenseRequest = new PutLicenseRequest(); putLicenseRequest.license(request.content(), request.getXContentType()); putLicenseRequest.acknowledge(request.paramAsBoolean("acknowledge", false)); diff --git a/plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yml b/plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yml index 8f59ca5a572..46e6501ccac 100644 --- a/plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yml +++ b/plugin/src/test/resources/rest-api-spec/test/license/20_put_license.yml @@ -93,3 +93,12 @@ teardown: xpack.license.get: {} - length: { license: 11 } +--- +"Should fail gracefully when body content is not provided": + + - do: + catch: request + xpack.license.post: + acknowledge: true + + - match: { error.root_cause.0.reason: 'The license must be provided in the request body' }