diff --git a/sample/license_spec.json b/sample/license_spec.json index a4aef47283f..b2c60e97613 100644 --- a/sample/license_spec.json +++ b/sample/license_spec.json @@ -1 +1 @@ -{"licenses":[{"type":"internal","subscription_type":"none","issued_to":"issuedTo","issuer":"issuer","issue_date":"2014-09-29","expiry_date":"2015-08-29","feature":"shield","max_nodes":1}]} +{"licenses":[{"uid": "893361dc-9749-4997-93cb-802e3d7fa4a8", "type":"internal","subscription_type":"none","issued_to":"issuedTo","issuer":"issuer","issue_date":"2014-09-29","expiry_date":"2015-08-29","feature":"shield","max_nodes":1}]} diff --git a/src/main/java/org/elasticsearch/license/core/DateUtils.java b/src/main/java/org/elasticsearch/license/core/DateUtils.java index fc9e2bbc3dc..111f8031fd7 100644 --- a/src/main/java/org/elasticsearch/license/core/DateUtils.java +++ b/src/main/java/org/elasticsearch/license/core/DateUtils.java @@ -5,57 +5,24 @@ */ package org.elasticsearch.license.core; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; +import org.elasticsearch.common.joda.FormatDateTimeFormatter; +import org.elasticsearch.common.joda.Joda; +import org.elasticsearch.common.joda.time.MutableDateTime; +import org.elasticsearch.common.joda.time.format.DateTimeFormatter; public class DateUtils { - public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); - private static DateFormat getDateFormat() { - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TIME_ZONE); - dateFormat.setLenient(false); - return dateFormat; + private final static FormatDateTimeFormatter formatDateTimeFormatter = Joda.forPattern("yyyy-MM-dd"); + + private final static DateTimeFormatter dateTimeFormatter = formatDateTimeFormatter.parser(); + + public static long endOfTheDay(String date) { + MutableDateTime dateTime = dateTimeFormatter.parseMutableDateTime(date); + dateTime.dayOfMonth().roundCeiling(); + return dateTime.getMillis(); } - public static long longExpiryDateFromDate(long date) { - Date dateObj = new Date(date); - - Calendar calendar = Calendar.getInstance(); - calendar.clear(); - calendar.setTimeZone(TIME_ZONE); - calendar.setTimeInMillis(dateObj.getTime()); - - calendar.set(Calendar.HOUR, 23); - calendar.set(Calendar.MINUTE, 59); - calendar.set(Calendar.SECOND, 59); - - return calendar.getTimeInMillis(); - } - - public static long longFromDateString(String dateStr) throws ParseException { - Date dateObj = getDateFormat().parse(dateStr); - Calendar calendar = Calendar.getInstance(); - calendar.clear(); - calendar.setTimeZone(TIME_ZONE); - calendar.setTimeInMillis(dateObj.getTime()); - return calendar.getTimeInMillis(); - } - - public static long longExpiryDateFromString(String dateStr) throws ParseException { - return longExpiryDateFromDate(longFromDateString(dateStr)); - } - - public static String dateStringFromLongDate(long date) { - Date dateObj = new Date(date); - Calendar calendar = Calendar.getInstance(); - calendar.clear(); - calendar.setTimeZone(TIME_ZONE); - calendar.setTimeInMillis(dateObj.getTime()); - return getDateFormat().format(calendar.getTime()); + public static long beginningOfTheDay(String date) { + return dateTimeFormatter.parseDateTime(date).getMillis(); } } diff --git a/src/main/java/org/elasticsearch/license/core/ESLicense.java b/src/main/java/org/elasticsearch/license/core/ESLicense.java index fd57b038558..06c9fef0b2b 100644 --- a/src/main/java/org/elasticsearch/license/core/ESLicense.java +++ b/src/main/java/org/elasticsearch/license/core/ESLicense.java @@ -119,6 +119,31 @@ public class ESLicense implements Comparable, ToXContent { return Long.compare(expiryDate, o.expiryDate); } + public void verify() { + if (issuer == null) { + throw new IllegalStateException("issuer can not be null"); + } else if (issuedTo == null) { + throw new IllegalStateException("issuedTo can not be null"); + } else if (issueDate == -1) { + throw new IllegalStateException("issueDate has to be set"); + } else if (type == null) { + throw new IllegalStateException("type can not be null"); + } else if (subscriptionType == null) { + throw new IllegalStateException("subscriptionType can not be null"); + } else if (uid == null) { + throw new IllegalStateException("uid can not be null"); + } else if (feature == null) { + throw new IllegalStateException("at least one feature has to be enabled"); + } else if (signature == null) { + throw new IllegalStateException("signature can not be null"); + } else if (maxNodes == -1) { + throw new IllegalStateException("maxNodes has to be set"); + } else if (expiryDate == -1) { + throw new IllegalStateException("expiryDate has to be set"); + } + } + + static ESLicense readESLicense(StreamInput in) throws IOException { in.readVInt(); // Version for future extensibility Builder builder = builder(); @@ -132,7 +157,8 @@ public class ESLicense implements Comparable, ToXContent { builder.issuedTo(in.readString()); builder.issuer(in.readString()); builder.signature(in.readOptionalString()); - return builder.verifyAndBuild(); + builder.verify(); + return builder.build(); } public void writeTo(StreamOutput out) throws IOException { @@ -161,7 +187,7 @@ public class ESLicense implements Comparable, ToXContent { builder.field(XFields.MAX_NODES, maxNodes); builder.field(XFields.ISSUED_TO, issuedTo); builder.field(XFields.ISSUER, issuer); - if (signature != null) { + if (signature != null && !params.paramAsBoolean(ESLicenses.OMIT_SIGNATURE, false)) { builder.field(XFields.SIGNATURE, signature); } builder.endObject(); @@ -196,50 +222,16 @@ public class ESLicense implements Comparable, ToXContent { static final XContentBuilderString SIGNATURE = new XContentBuilderString(Fields.SIGNATURE); } - public static ESLicense fromXContent(XContentParser parser) throws IOException { - Builder builder = new Builder(); - XContentParser.Token token = parser.currentToken(); - if (token == XContentParser.Token.START_OBJECT) { - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - String currentFieldName = parser.currentName(); - token = parser.nextToken(); - if (token.isValue()) { - if (Fields.UID.equals(currentFieldName)) { - builder.uid(parser.text()); - } else if (Fields.TYPE.equals(currentFieldName)) { - builder.type(parser.text()); - } else if (Fields.SUBSCRIPTION_TYPE.equals(currentFieldName)) { - builder.subscriptionType(parser.text()); - } else if (Fields.ISSUE_DATE.equals(currentFieldName)) { - builder.issueDate(parser.longValue()); - } else if (Fields.FEATURE.equals(currentFieldName)) { - builder.feature(parser.text()); - } else if (Fields.EXPIRY_DATE.equals(currentFieldName)) { - builder.expiryDate(parser.longValue()); - } else if (Fields.MAX_NODES.equals(currentFieldName)) { - builder.maxNodes(parser.intValue()); - } else if (Fields.ISSUED_TO.equals(currentFieldName)) { - builder.issuedTo(parser.text()); - } else if (Fields.ISSUER.equals(currentFieldName)) { - builder.issuer(parser.text()); - } else if (Fields.SIGNATURE.equals(currentFieldName)) { - builder.signature(parser.text()); - } - // Ignore unknown elements - might be new version of license - } else if (token == XContentParser.Token.START_ARRAY) { - // It was probably created by newer version - ignoring - parser.skipChildren(); - } else if (token == XContentParser.Token.START_OBJECT) { - // It was probably created by newer version - ignoring - parser.skipChildren(); - } - } - } + private static long parseDate(XContentParser parser, String description) throws IOException { + if (parser.currentToken() == XContentParser.Token.VALUE_NUMBER) { + return parser.longValue(); } else { - throw new ElasticsearchParseException("failed to parse licenses expected a license object"); + try { + return DateUtils.endOfTheDay((parser.text())); + } catch (IllegalArgumentException ex) { + throw new ElasticsearchParseException("invalid " + description + " date format " + parser.text()); + } } - return builder.verifyAndBuild(); } public static Builder builder() { @@ -324,10 +316,49 @@ public class ESLicense implements Comparable, ToXContent { .signature(signature); } - public ESLicense verifyAndBuild() { - verify(); - return new ESLicense(uid, issuer, issuedTo, issueDate, type, - subscriptionType, feature, signature, expiryDate, maxNodes); + public Builder fromXContent(XContentParser parser) throws IOException { + XContentParser.Token token = parser.currentToken(); + if (token == XContentParser.Token.START_OBJECT) { + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + String currentFieldName = parser.currentName(); + token = parser.nextToken(); + if (token.isValue()) { + if (Fields.UID.equals(currentFieldName)) { + uid(parser.text()); + } else if (Fields.TYPE.equals(currentFieldName)) { + type(parser.text()); + } else if (Fields.SUBSCRIPTION_TYPE.equals(currentFieldName)) { + subscriptionType(parser.text()); + } else if (Fields.ISSUE_DATE.equals(currentFieldName)) { + issueDate(parseDate(parser, "issue")); + } else if (Fields.FEATURE.equals(currentFieldName)) { + feature(parser.text()); + } else if (Fields.EXPIRY_DATE.equals(currentFieldName)) { + expiryDate(parseDate(parser, "expiration")); + } else if (Fields.MAX_NODES.equals(currentFieldName)) { + maxNodes(parser.intValue()); + } else if (Fields.ISSUED_TO.equals(currentFieldName)) { + issuedTo(parser.text()); + } else if (Fields.ISSUER.equals(currentFieldName)) { + issuer(parser.text()); + } else if (Fields.SIGNATURE.equals(currentFieldName)) { + signature(parser.text()); + } + // Ignore unknown elements - might be new version of license + } else if (token == XContentParser.Token.START_ARRAY) { + // It was probably created by newer version - ignoring + parser.skipChildren(); + } else if (token == XContentParser.Token.START_OBJECT) { + // It was probably created by newer version - ignoring + parser.skipChildren(); + } + } + } + } else { + throw new ElasticsearchParseException("failed to parse licenses expected a license object"); + } + return this; } public ESLicense build() { @@ -335,7 +366,7 @@ public class ESLicense implements Comparable, ToXContent { subscriptionType, feature, signature, expiryDate, maxNodes); } - private void verify() { + public Builder verify() { if (issuer == null) { throw new IllegalStateException("issuer can not be null"); } else if (issuedTo == null) { @@ -357,6 +388,7 @@ public class ESLicense implements Comparable, ToXContent { } else if (expiryDate == -1) { throw new IllegalStateException("expiryDate has to be set"); } + return this; } } diff --git a/src/main/java/org/elasticsearch/license/core/ESLicenses.java b/src/main/java/org/elasticsearch/license/core/ESLicenses.java index 81d07396462..070e81bba42 100644 --- a/src/main/java/org/elasticsearch/license/core/ESLicenses.java +++ b/src/main/java/org/elasticsearch/license/core/ESLicenses.java @@ -16,7 +16,9 @@ import java.util.*; public class ESLicenses { - final static class Fields { + public static final String OMIT_SIGNATURE = "rest_api"; + + private final static class Fields { static final String LICENSES = "licenses"; } @@ -35,14 +37,18 @@ public class ESLicenses { } public static List fromSource(String content) throws IOException { - return fromSource(content.getBytes(LicensesCharset.UTF_8)); + return fromSource(content.getBytes(LicensesCharset.UTF_8), true); } public static List fromSource(byte[] bytes) throws IOException { - return fromXContent(XContentFactory.xContent(bytes).createParser(bytes)); + return fromXContent(XContentFactory.xContent(bytes).createParser(bytes), true); } - public static List fromXContent(XContentParser parser) throws IOException { + public static List fromSource(byte[] bytes, boolean verify) throws IOException { + return fromXContent(XContentFactory.xContent(bytes).createParser(bytes), verify); + } + + public static List fromXContent(XContentParser parser, boolean verify) throws IOException { List esLicenses = new ArrayList<>(); if (parser.nextToken() == XContentParser.Token.START_OBJECT) { if (parser.nextToken() == XContentParser.Token.FIELD_NAME) { @@ -50,7 +56,11 @@ public class ESLicenses { if (Fields.LICENSES.equals(currentFieldName)) { if (parser.nextToken() == XContentParser.Token.START_ARRAY) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { - esLicenses.add(ESLicense.fromXContent(parser)); + ESLicense.Builder builder = ESLicense.builder().fromXContent(parser); + if (verify) { + builder.verify(); + } + esLicenses.add(builder.build()); } } else { throw new ElasticsearchParseException("failed to parse licenses expected an array of licenses"); diff --git a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java index af1fb2e785a..48ee552d796 100644 --- a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java +++ b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java @@ -121,7 +121,8 @@ public class ESLicenseSigner { return ESLicense.builder() .fromLicenseSpec(licenseSpec, signature) - .verifyAndBuild(); + .verify() + .build(); } private void featureToXContent(ESLicense license, XContentBuilder builder) throws IOException { diff --git a/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java b/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java deleted file mode 100644 index 72bb32d3fdd..00000000000 --- a/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.license.licensor; - -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.license.core.DateUtils; -import org.elasticsearch.license.core.ESLicense; - -import java.io.IOException; -import java.text.ParseException; -import java.util.*; - -public class LicenseSpecs { - - private static ESLicense fromXContent(Map map) throws IOException, ParseException { - ESLicense.Builder builder = new ESLicense.Builder() - .type((String) map.get("type")) - .subscriptionType((String) map.get("subscription_type")) - .feature((String) map.get("feature")) - .maxNodes((int) map.get("max_nodes")) - .issuedTo((String) map.get("issued_to")) - .issuer((String) map.get("issuer")); - - String uid = (String) map.get("uid"); - if (uid == null) { - builder.uid(UUID.randomUUID().toString()); - } - - String issueDate = (String) map.get("issue_date"); - builder.issueDate(DateUtils.longFromDateString(issueDate)); - - String expiryDate = (String) map.get("expiry_date"); - builder.expiryDate(DateUtils.longExpiryDateFromString(expiryDate)); - - return builder.build(); - } - - public static Set fromSource(byte[] bytes) throws IOException, ParseException { - return fromXContents(XContentFactory.xContent(bytes).createParser(bytes)); - } - - private static Set fromXContents(XContentParser parser) throws IOException, ParseException { - Set licenseSpecs = new HashSet<>(); - final Map licenseSpecMap = parser.mapAndClose(); - @SuppressWarnings("unchecked") - final List> licenseSpecDefinitions = (ArrayList>)licenseSpecMap.get("licenses"); - for (Map licenseSpecDef : licenseSpecDefinitions) { - final ESLicense licenseSpec = fromXContent(licenseSpecDef); - licenseSpecs.add(licenseSpec); - } - return licenseSpecs; - } -} - diff --git a/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java b/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java index e6b66bf19a4..00bc7e9314c 100644 --- a/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -14,7 +14,6 @@ import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.LicensesCharset; import org.elasticsearch.license.licensor.ESLicenseSigner; -import org.elasticsearch.license.licensor.LicenseSpecs; import java.io.File; import java.io.IOException; @@ -49,13 +48,13 @@ public class LicenseGeneratorTool { switch (command) { case "--license": String licenseInput = args[++i]; - licenseSpecs.addAll(LicenseSpecs.fromSource(licenseInput.getBytes(LicensesCharset.UTF_8))); + licenseSpecs.addAll(ESLicenses.fromSource(licenseInput.getBytes(LicensesCharset.UTF_8), false)); break; case "--licenseFile": File licenseFile = new File(args[++i]); if (licenseFile.exists()) { final byte[] bytes = Files.readAllBytes(Paths.get(licenseFile.getAbsolutePath())); - licenseSpecs.addAll(LicenseSpecs.fromSource(bytes)); + licenseSpecs.addAll(ESLicenses.fromSource(bytes, false)); } else { throw new IllegalArgumentException(licenseFile.getAbsolutePath() + " does not exist!"); } diff --git a/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java b/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java index 93a35f0bd2f..2327ebb0b4b 100644 --- a/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java +++ b/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java @@ -6,18 +6,19 @@ package org.elasticsearch.license.plugin.rest; import org.elasticsearch.client.Client; +import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.license.core.DateUtils; -import org.elasticsearch.license.core.ESLicense; +import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.plugin.action.get.GetLicenseAction; import org.elasticsearch.license.plugin.action.get.GetLicenseRequest; import org.elasticsearch.license.plugin.action.get.GetLicenseResponse; import org.elasticsearch.rest.*; import org.elasticsearch.rest.action.support.RestBuilderListener; -import java.io.IOException; +import java.util.Map; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestStatus.OK; @@ -30,17 +31,6 @@ public class RestGetLicenseAction extends BaseRestHandler { controller.registerHandler(GET, "/_licenses", this); } - @Override - public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { - client.admin().cluster().execute(GetLicenseAction.INSTANCE, new GetLicenseRequest(), new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(GetLicenseResponse response, XContentBuilder builder) throws Exception { - toXContent(response, builder); - return new BytesRestResponse(OK, builder); - } - }); - } - /** * Output Format: * { @@ -57,31 +47,25 @@ public class RestGetLicenseAction extends BaseRestHandler { *     }, * {...} *   ] - * } + * }p * * There will be only one license displayed per feature, the selected license will have the latest expiry_date * out of all other licenses for the feature. * * The licenses are sorted by latest issue_date */ - private static void toXContent(GetLicenseResponse response, XContentBuilder builder) throws IOException { - builder.startObject(); - builder.startArray("licenses"); - for (ESLicense license : response.licenses()) { - builder.startObject(); - builder.field("uid", license.uid()); - builder.field("type", license.type()); - builder.field("subscription_type", license.subscriptionType()); - builder.field("issued_to", license.issuedTo()); - builder.field("issue_date", DateUtils.dateStringFromLongDate(license.issueDate())); - builder.field("expiry_date", DateUtils.dateStringFromLongDate(license.expiryDate())); - builder.field("feature", license.feature()); - builder.field("max_nodes", license.maxNodes()); - - builder.endObject(); - } - builder.endArray(); - builder.endObject(); + @Override + public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { + final Map overrideParams = ImmutableMap.of(ESLicenses.OMIT_SIGNATURE, "true"); + final ToXContent.Params params = new ToXContent.DelegatingMapParams(overrideParams, request); + client.admin().cluster().execute(GetLicenseAction.INSTANCE, new GetLicenseRequest(), new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(GetLicenseResponse response, XContentBuilder builder) throws Exception { + ESLicenses.toXContent(response.licenses(), builder, params); + return new BytesRestResponse(OK, builder); + } + }); } + } diff --git a/src/test/java/org/elasticsearch/license/TestUtils.java b/src/test/java/org/elasticsearch/license/TestUtils.java index 837955c7820..313759227a9 100644 --- a/src/test/java/org/elasticsearch/license/TestUtils.java +++ b/src/test/java/org/elasticsearch/license/TestUtils.java @@ -6,6 +6,7 @@ package org.elasticsearch.license; import org.apache.commons.io.FileUtils; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicenses; @@ -15,11 +16,9 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.ParseException; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.junit.Assert.assertTrue; public class TestUtils { @@ -27,30 +26,26 @@ public class TestUtils { public final static String SHIELD = "shield"; public final static String MARVEL = "marvel"; - public static String generateESLicenses(Map featureAttributes) { - StringBuilder licenseBuilder = new StringBuilder(); - int size = featureAttributes.values().size(); - int i = 0; + public static String generateESLicenses(Map featureAttributes) throws IOException { + XContentBuilder licenses = jsonBuilder(); + licenses.startObject(); + licenses.startArray("licenses"); for (FeatureAttributes attributes : featureAttributes.values()) { - licenseBuilder.append("{\n" + - " \"type\" : \"" + attributes.type + "\",\n" + - " \"subscription_type\" : \"" + attributes.subscriptionType + "\",\n" + - " \"issued_to\" : \"" + attributes.issuedTo + "\",\n" + - " \"issuer\" : \"" + attributes.issuer + "\",\n" + - " \"issue_date\" : \"" + attributes.issueDate + "\",\n" + - " \"expiry_date\" : \"" + attributes.expiryDate + "\",\n" + - " \"feature\" : \"" + attributes.featureType + "\",\n" + - " \"max_nodes\" : " + attributes.maxNodes + - "}"); - if (++i < size) { - licenseBuilder.append(",\n"); - } + licenses.startObject() + .field("uid", attributes.uid) + .field("type", attributes.type) + .field("subscription_type", attributes.subscriptionType) + .field("issued_to", attributes.issuedTo) + .field("issuer", attributes.issuer) + .field("issue_date", attributes.issueDate) + .field("expiry_date", attributes.expiryDate) + .field("feature", attributes.featureType) + .field("max_nodes", attributes.maxNodes) + .endObject(); } - return "{\n" + - " \"licenses\" : [" + - licenseBuilder.toString() + - "]\n" + - "}"; + licenses.endArray(); + licenses.endObject(); + return licenses.string(); } @@ -91,8 +86,8 @@ public class TestUtils { assertTrue("expected value for type was: " + attributes.type + " but got: " + esLicense.type(), esLicense.type().equals(attributes.type)); assertTrue("expected value for subscriptionType was: " + attributes.subscriptionType + " but got: " + esLicense.subscriptionType(), esLicense.subscriptionType().equals(attributes.subscriptionType)); assertTrue("expected value for feature was: " + attributes.featureType + " but got: " + esLicense.feature(), esLicense.feature().equals(attributes.featureType)); - assertTrue("expected value for issueDate was: " + DateUtils.longFromDateString(attributes.issueDate) + " but got: " + esLicense.issueDate(), esLicense.issueDate() == DateUtils.longFromDateString(attributes.issueDate)); - assertTrue("expected value for expiryDate: " + DateUtils.longExpiryDateFromString(attributes.expiryDate) + " but got: " + esLicense.expiryDate(), esLicense.expiryDate() == DateUtils.longExpiryDateFromString(attributes.expiryDate)); + assertTrue("expected value for issueDate was: " + DateUtils.beginningOfTheDay(attributes.issueDate) + " but got: " + esLicense.issueDate(), esLicense.issueDate() == DateUtils.beginningOfTheDay(attributes.issueDate)); + assertTrue("expected value for expiryDate: " + DateUtils.endOfTheDay(attributes.expiryDate) + " but got: " + esLicense.expiryDate(), esLicense.expiryDate() == DateUtils.endOfTheDay(attributes.expiryDate)); assertTrue("expected value for maxNodes: " + attributes.maxNodes + " but got: " + esLicense.maxNodes(), esLicense.maxNodes() == attributes.maxNodes); assertTrue("generated licenses should have non-null uid field", esLicense.uid() != null); @@ -139,6 +134,7 @@ public class TestUtils { public static class FeatureAttributes { + public final String uid; public final String featureType; public final String type; public final String subscriptionType; @@ -149,6 +145,11 @@ public class TestUtils { public final String issuer; public FeatureAttributes(String featureType, String type, String subscriptionType, String issuedTo, String issuer, int maxNodes, String issueDateStr, String expiryDateStr) throws ParseException { + this(UUID.randomUUID().toString(), featureType, type, subscriptionType, issuedTo, issuer, maxNodes, issueDateStr, expiryDateStr); + } + + public FeatureAttributes(String uid, String featureType, String type, String subscriptionType, String issuedTo, String issuer, int maxNodes, String issueDateStr, String expiryDateStr) throws ParseException { + this.uid = uid; this.featureType = featureType; this.type = type; this.subscriptionType = subscriptionType; diff --git a/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java b/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java index e42ab95ac3d..65e0d764a01 100644 --- a/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java +++ b/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java @@ -141,7 +141,8 @@ public class LicenseVerificationTests extends AbstractLicensingTestBase { final ESLicense tamperedLicense = ESLicense.builder() .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .verifyAndBuild(); + .verify() + .build(); try { esLicenseProvider.setLicenses(Collections.singleton(tamperedLicense)); @@ -161,8 +162,8 @@ public class LicenseVerificationTests extends AbstractLicensingTestBase { ESLicense license = licenseProvider.getESLicense(featureType); assertTrue("License should have issuedTo of " + featureAttributes.issuedTo, license.issuedTo().equals(featureAttributes.issuedTo)); assertTrue("License should have issuer of " + featureAttributes.issuer, license.issuer().equals(featureAttributes.issuer)); - assertTrue("License should have issue date of " + DateUtils.longFromDateString(featureAttributes.issueDate), license.issueDate() == DateUtils.longFromDateString(featureAttributes.issueDate)); - assertTrue("License should have expiry date of " + DateUtils.longExpiryDateFromString(featureAttributes.expiryDate) + " got: " + license.expiryDate(), license.expiryDate() == DateUtils.longExpiryDateFromString(featureAttributes.expiryDate)); + assertTrue("License should have issue date of " + DateUtils.beginningOfTheDay(featureAttributes.issueDate), license.issueDate() == DateUtils.beginningOfTheDay(featureAttributes.issueDate)); + assertTrue("License should have expiry date of " + DateUtils.endOfTheDay(featureAttributes.expiryDate) + " got: " + license.expiryDate(), license.expiryDate() == DateUtils.endOfTheDay(featureAttributes.expiryDate)); assertTrue("License should have type of " + featureAttributes.type + " got: " + license.type(), license.type().equals(featureAttributes.type)); assertTrue("License should have subscription type of " + featureAttributes.subscriptionType, license.subscriptionType().equals(featureAttributes.subscriptionType)); diff --git a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java index 295633b029e..93be4f9e856 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java @@ -140,7 +140,8 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest { final ESLicense tamperedLicense = ESLicense.builder() .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .verifyAndBuild(); + .verify() + .build(); PutLicenseRequestBuilder builder = new PutLicenseRequestBuilder(client().admin().cluster()); builder.setLicense(Collections.singletonList(tamperedLicense)); diff --git a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java index 442a4639f22..7c44968428e 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java @@ -130,7 +130,8 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { final ESLicense tamperedLicense = ESLicense.builder() .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .verifyAndBuild(); + .verify() + .build(); assertTrue(LicensesStatus.INVALID == licensesManagerService.checkLicenses(Collections.singleton(tamperedLicense))); }