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@f8c744d2a2
This commit is contained in:
parent
69b3ffa40a
commit
a27dc257c9
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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' }
|
||||
|
|
Loading…
Reference in New Issue