From 0a1e09c64403880cd40cb026544faf83f5f33510 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Tue, 20 Mar 2018 12:19:07 -0400 Subject: [PATCH] Remove date from rest resp for non-exp licenses (elastic/x-pack-elasticsearch#4149) This is related to elastic/x-pack-elasticsearch#3877. This commit removes the expiration from the json rest response for licenses that do not expire. Original commit: elastic/x-pack-elasticsearch@f767e9d7567495038ff1ad70ae1de61f7d5a3e61 --- .../org/elasticsearch/license/License.java | 15 +++++++-- .../license/LicenseSerializationTests.java | 31 +++++++++++++++++++ .../license/StartBasicLicenseTests.java | 23 +++++++++----- .../monitoring/integration/MonitoringIT.java | 6 ++-- .../upgrades/BasicLicenseUpgradeIT.java | 2 +- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/plugin/core/src/main/java/org/elasticsearch/license/License.java b/plugin/core/src/main/java/org/elasticsearch/license/License.java index 5001503cd86..f1658e8942a 100644 --- a/plugin/core/src/main/java/org/elasticsearch/license/License.java +++ b/plugin/core/src/main/java/org/elasticsearch/license/License.java @@ -137,7 +137,13 @@ public class License implements ToXContentObject { this.subscriptionType = subscriptionType; this.feature = feature; this.signature = signature; - this.expiryDate = expiryDate; + // We will validate that only a basic license can have the BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS + // in the validate() method. + if (expiryDate == -1) { + this.expiryDate = LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS; + } else { + this.expiryDate = expiryDate; + } this.maxNodes = maxNodes; this.startDate = startDate; if (version == VERSION_START) { @@ -289,6 +295,8 @@ public class License implements ToXContentObject { throw new IllegalStateException("maxNodes has to be set"); } else if (expiryDate == -1) { throw new IllegalStateException("expiryDate has to be set"); + } else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && "basic".equals(type) == false) { + throw new IllegalStateException("only basic licenses are allowed to have no expiration"); } } @@ -389,7 +397,10 @@ public class License implements ToXContentObject { if (version == VERSION_START) { builder.field(Fields.FEATURE, feature); } - builder.dateField(Fields.EXPIRY_DATE_IN_MILLIS, Fields.EXPIRY_DATE, expiryDate); + + if (expiryDate != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) { + builder.dateField(Fields.EXPIRY_DATE_IN_MILLIS, Fields.EXPIRY_DATE, expiryDate); + } builder.field(Fields.MAX_NODES, maxNodes); builder.field(Fields.ISSUED_TO, issuedTo); builder.field(Fields.ISSUER, issuer); diff --git a/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java b/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java index 87277817232..d7cf5ab50fb 100644 --- a/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java +++ b/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java @@ -17,6 +17,7 @@ import org.elasticsearch.test.ESTestCase; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Map; +import java.util.UUID; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsNull.notNullValue; @@ -99,4 +100,34 @@ public class LicenseSerializationTests extends ESTestCase { map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); assertThat(map.get("status"), nullValue()); } + + public void testLicenseRestViewNonExpiringBasic() throws Exception { + long now = System.currentTimeMillis(); + + License.Builder specBuilder = License.builder() + .uid(UUID.randomUUID().toString()) + .issuedTo("test") + .maxNodes(1000) + .issueDate(now) + .type("basic") + .expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS); + License license = SelfGeneratedLicense.create(specBuilder); + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); + license.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(License.REST_VIEW_MODE, "true"))); + builder.flush(); + Map map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); + + // should have an extra status field, human readable issue_data and no expiry_date + assertThat(map.get("status"), notNullValue()); + assertThat(map.get("type"), equalTo("basic")); + assertThat(map.get("issue_date"), notNullValue()); + assertThat(map.get("expiry_date"), nullValue()); + assertThat(map.get("expiry_date_in_millis"), nullValue()); + assertThat(map.get("status"), equalTo("active")); + builder = XContentFactory.contentBuilder(XContentType.JSON); + license.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.flush(); + map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); + assertThat(map.get("status"), nullValue()); + } } diff --git a/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java b/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java index 1f0fa178a6c..55b14a4d792 100644 --- a/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java +++ b/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java @@ -83,19 +83,26 @@ public class StartBasicLicenseTests extends AbstractLicensesIntegrationTestCase long expirationMillis = licensingClient.prepareGetLicense().get().license().expiryDate(); assertEquals(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS, expirationMillis); - Response response3 = restClient.performRequest("GET", "/_xpack/license/basic_status"); + Response response3 = restClient.performRequest("GET", "/_xpack/license"); String body3 = Streams.copyToString(new InputStreamReader(response3.getEntity().getContent(), StandardCharsets.UTF_8)); + assertTrue(body3.contains("\"type\" : \"basic\"")); + assertFalse(body3.contains("expiry_date")); + assertFalse(body3.contains("expiry_date_in_millis")); + + + Response response4 = restClient.performRequest("GET", "/_xpack/license/basic_status"); + String body4 = Streams.copyToString(new InputStreamReader(response4.getEntity().getContent(), StandardCharsets.UTF_8)); assertEquals(200, response3.getStatusLine().getStatusCode()); - assertEquals("{\"eligible_to_start_basic\":false}", body3); + assertEquals("{\"eligible_to_start_basic\":false}", body4); ResponseException ex = expectThrows(ResponseException.class, () -> restClient.performRequest("POST", "/_xpack/license/start_basic")); - Response response4 = ex.getResponse(); - String body4 = Streams.copyToString(new InputStreamReader(response4.getEntity().getContent(), StandardCharsets.UTF_8)); - assertEquals(403, response4.getStatusLine().getStatusCode()); - assertTrue(body4.contains("\"basic_was_started\":false")); - assertTrue(body4.contains("\"acknowledged\":true")); - assertTrue(body4.contains("\"error_message\":\"Operation failed: Current license is basic.\"")); + Response response5 = ex.getResponse(); + String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8)); + assertEquals(403, response5.getStatusLine().getStatusCode()); + assertTrue(body5.contains("\"basic_was_started\":false")); + assertTrue(body5.contains("\"acknowledged\":true")); + assertTrue(body5.contains("\"error_message\":\"Operation failed: Current license is basic.\"")); } public void testUnacknowledgedStartBasicLicense() throws Exception { diff --git a/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java b/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java index b0324b03ab9..9bb95fd7246 100644 --- a/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java +++ b/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java @@ -330,8 +330,10 @@ public class MonitoringIT extends ESSingleNodeTestCase { String status = (String) license.get(License.Fields.STATUS); assertThat(status, not(isEmptyOrNullString())); - Long expiryDate = (Long) license.get(License.Fields.EXPIRY_DATE_IN_MILLIS); - assertThat(expiryDate, greaterThan(0L)); + if ("basic".equals(license.get("type")) == false) { + Long expiryDate = (Long) license.get(License.Fields.EXPIRY_DATE_IN_MILLIS); + assertThat(expiryDate, greaterThan(0L)); + } Boolean clusterNeedsTLS = (Boolean) license.get("cluster_needs_tls"); assertThat(clusterNeedsTLS, isOneOf(true, null)); diff --git a/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/BasicLicenseUpgradeIT.java b/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/BasicLicenseUpgradeIT.java index d1ba0d6e95d..9ed7f561ba3 100644 --- a/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/BasicLicenseUpgradeIT.java +++ b/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/BasicLicenseUpgradeIT.java @@ -36,6 +36,6 @@ public class BasicLicenseUpgradeIT extends AbstractUpgradeTestCase { Map licenseMap = (Map) licenseResponseMap.get("license"); assertEquals("basic", licenseMap.get("type")); assertEquals("active", licenseMap.get("status")); - assertEquals(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS, licenseMap.get("expiry_date_in_millis")); + assertNull(licenseMap.get("expiry_date_in_millis")); } }