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@f767e9d756
This commit is contained in:
Tim Brooks 2018-03-20 12:19:07 -04:00 committed by GitHub
parent c16e5f1f92
commit 0a1e09c644
5 changed files with 64 additions and 13 deletions

View File

@ -137,7 +137,13 @@ public class License implements ToXContentObject {
this.subscriptionType = subscriptionType; this.subscriptionType = subscriptionType;
this.feature = feature; this.feature = feature;
this.signature = signature; 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.maxNodes = maxNodes;
this.startDate = startDate; this.startDate = startDate;
if (version == VERSION_START) { if (version == VERSION_START) {
@ -289,6 +295,8 @@ public class License implements ToXContentObject {
throw new IllegalStateException("maxNodes has to be set"); throw new IllegalStateException("maxNodes has to be set");
} else if (expiryDate == -1) { } else if (expiryDate == -1) {
throw new IllegalStateException("expiryDate has to be set"); 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) { if (version == VERSION_START) {
builder.field(Fields.FEATURE, feature); 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.MAX_NODES, maxNodes);
builder.field(Fields.ISSUED_TO, issuedTo); builder.field(Fields.ISSUED_TO, issuedTo);
builder.field(Fields.ISSUER, issuer); builder.field(Fields.ISSUER, issuer);

View File

@ -17,6 +17,7 @@ import org.elasticsearch.test.ESTestCase;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue; 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(); map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
assertThat(map.get("status"), nullValue()); 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<String, Object> 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());
}
} }

View File

@ -83,19 +83,26 @@ public class StartBasicLicenseTests extends AbstractLicensesIntegrationTestCase
long expirationMillis = licensingClient.prepareGetLicense().get().license().expiryDate(); long expirationMillis = licensingClient.prepareGetLicense().get().license().expiryDate();
assertEquals(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS, expirationMillis); 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)); 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(200, response3.getStatusLine().getStatusCode());
assertEquals("{\"eligible_to_start_basic\":false}", body3); assertEquals("{\"eligible_to_start_basic\":false}", body4);
ResponseException ex = expectThrows(ResponseException.class, ResponseException ex = expectThrows(ResponseException.class,
() -> restClient.performRequest("POST", "/_xpack/license/start_basic")); () -> restClient.performRequest("POST", "/_xpack/license/start_basic"));
Response response4 = ex.getResponse(); Response response5 = ex.getResponse();
String body4 = Streams.copyToString(new InputStreamReader(response4.getEntity().getContent(), StandardCharsets.UTF_8)); String body5 = Streams.copyToString(new InputStreamReader(response5.getEntity().getContent(), StandardCharsets.UTF_8));
assertEquals(403, response4.getStatusLine().getStatusCode()); assertEquals(403, response5.getStatusLine().getStatusCode());
assertTrue(body4.contains("\"basic_was_started\":false")); assertTrue(body5.contains("\"basic_was_started\":false"));
assertTrue(body4.contains("\"acknowledged\":true")); assertTrue(body5.contains("\"acknowledged\":true"));
assertTrue(body4.contains("\"error_message\":\"Operation failed: Current license is basic.\"")); assertTrue(body5.contains("\"error_message\":\"Operation failed: Current license is basic.\""));
} }
public void testUnacknowledgedStartBasicLicense() throws Exception { public void testUnacknowledgedStartBasicLicense() throws Exception {

View File

@ -330,8 +330,10 @@ public class MonitoringIT extends ESSingleNodeTestCase {
String status = (String) license.get(License.Fields.STATUS); String status = (String) license.get(License.Fields.STATUS);
assertThat(status, not(isEmptyOrNullString())); assertThat(status, not(isEmptyOrNullString()));
Long expiryDate = (Long) license.get(License.Fields.EXPIRY_DATE_IN_MILLIS); if ("basic".equals(license.get("type")) == false) {
assertThat(expiryDate, greaterThan(0L)); Long expiryDate = (Long) license.get(License.Fields.EXPIRY_DATE_IN_MILLIS);
assertThat(expiryDate, greaterThan(0L));
}
Boolean clusterNeedsTLS = (Boolean) license.get("cluster_needs_tls"); Boolean clusterNeedsTLS = (Boolean) license.get("cluster_needs_tls");
assertThat(clusterNeedsTLS, isOneOf(true, null)); assertThat(clusterNeedsTLS, isOneOf(true, null));

View File

@ -36,6 +36,6 @@ public class BasicLicenseUpgradeIT extends AbstractUpgradeTestCase {
Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license"); Map<String, Object> licenseMap = (Map<String, Object>) licenseResponseMap.get("license");
assertEquals("basic", licenseMap.get("type")); assertEquals("basic", licenseMap.get("type"));
assertEquals("active", licenseMap.get("status")); 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"));
} }
} }