Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put License API can return HTTP 500 #79093

Merged
merged 12 commits into from
Oct 21, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.client;

import org.elasticsearch.Build;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.license.DeleteLicenseRequest;
import org.elasticsearch.client.license.GetBasicStatusResponse;
Expand All @@ -23,6 +24,7 @@
import org.elasticsearch.client.license.StartTrialRequest;
import org.elasticsearch.client.license.StartTrialResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.junit.After;
Expand All @@ -40,6 +42,7 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;

public class LicenseIT extends ESRestHighLevelClientTestCase {

Expand Down Expand Up @@ -91,6 +94,40 @@ public void testStartTrial() throws Exception {
}
}

public void testPutInvalidTrialLicense() throws Exception {
assumeTrue("Trial license is only valid when tested against snapshot/test builds",
Build.CURRENT.isSnapshot());

// use a hard-coded trial license for 20 yrs to be able to roll back from another licenses
final String signature = "xx"; // Truncated, so it is expected to fail validation
final String licenseDefinition = Strings.toString(jsonBuilder()
.startObject()
.field("licenses", List.of(
Map.of(
"uid", "96fc37c6-6fc9-43e2-a40d-73143850cd72",
"type", "trial",
// 2018-10-16 07:02:48 UTC
"issue_date_in_millis", "1539673368158",
// 2038-10-11 07:02:48 UTC, 20 yrs later
"expiry_date_in_millis", "2170393368158",
"max_nodes", "5",
"issued_to", "client_rest-high-level_integTestCluster",
"issuer", "elasticsearch",
"start_date_in_millis", "-1",
"signature", signature)))
.endObject());

final PutLicenseRequest request = new PutLicenseRequest();
request.setAcknowledge(true);
request.setLicenseDefinition(licenseDefinition);
ElasticsearchStatusException e = expectThrows(
ElasticsearchStatusException.class,
() -> highLevelClient().license().putLicense(request, RequestOptions.DEFAULT)
);
assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
assertThat(e.getMessage(), stringContainsInOrder("malformed signature for license"));
justincr-elastic marked this conversation as resolved.
Show resolved Hide resolved
}

public static void putTrialLicense() throws IOException {
assumeTrue("Trial license is only valid when tested against snapshot/test builds",
Build.CURRENT.isSnapshot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public static License fromXContent(XContentParser parser) throws IOException {
ByteBuffer byteBuffer = ByteBuffer.wrap(signatureBytes);
version = byteBuffer.getInt();
} catch (Exception e) {
throw new ElasticsearchException("malformed signature for license [" + builder.uid + "]", e);
throw new ElasticsearchParseException("malformed signature for license [" + builder.uid + "]", e);
}
// we take the absolute version, because negative versions
// mean that the license was generated by the cluster (see TrialLicense)
Expand All @@ -651,9 +651,9 @@ public static License fromXContent(XContentParser parser) throws IOException {
version *= -1;
}
if (version == 0) {
throw new ElasticsearchException("malformed signature for license [" + builder.uid + "]");
throw new ElasticsearchParseException("malformed signature for license [" + builder.uid + "]");
} else if (version > VERSION_CURRENT) {
throw new ElasticsearchException("Unknown license version found, please upgrade all nodes to the latest " +
throw new ElasticsearchParseException("Unknown license version found, please upgrade all nodes to the latest " +
"elasticsearch-license plugin");
}
// signature version is the source of truth
Expand Down