From d1afd77bdee87f510215e53746c7b1b5553ad576 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Mon, 27 Oct 2014 12:49:18 -0400 Subject: [PATCH] nuked LicenseSpec; merge to ESLicense Original commit: elastic/x-pack-elasticsearch@f7dc1b7c9c98a9f80945992dbc9c1dbd111f4308 --- .../elasticsearch/license/core/ESLicense.java | 4 +- .../license/licensor/ESLicenseSigner.java | 35 ++-- .../license/licensor/LicenseSpec.java | 151 ------------------ .../license/licensor/LicenseSpecs.java | 66 ++++++++ .../licensor/tools/LicenseGeneratorTool.java | 12 +- .../manager/LicenseVerificationTests.java | 4 +- .../license/plugin/LicenseTransportTests.java | 4 +- .../license/plugin/LicensesServiceTests.java | 4 +- 8 files changed, 90 insertions(+), 190 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java create mode 100644 src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java diff --git a/src/main/java/org/elasticsearch/license/core/ESLicense.java b/src/main/java/org/elasticsearch/license/core/ESLicense.java index 8f1929121f1..e6520967986 100644 --- a/src/main/java/org/elasticsearch/license/core/ESLicense.java +++ b/src/main/java/org/elasticsearch/license/core/ESLicense.java @@ -257,7 +257,7 @@ public class ESLicense implements Comparable { return this; } - public Builder fromLicense(ESLicense license) { + public Builder fromLicenseSpec(ESLicense license, String signature) { return uid(license.uid()) .issuedTo(license.issuedTo()) .issueDate(license.issueDate()) @@ -267,7 +267,7 @@ public class ESLicense implements Comparable { .maxNodes(license.maxNodes()) .expiryDate(license.expiryDate()) .issuer(license.issuer()) - .signature(license.signature()); + .signature(signature); } public ESLicense verifyAndBuild() { diff --git a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java index bffd59b88e5..9c83fe31ee9 100644 --- a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java +++ b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java @@ -68,9 +68,9 @@ public class ESLicenseSigner { } - public ImmutableSet sign(Set licenseSpecs) throws IOException { + public ImmutableSet sign(Set licenseSpecs) throws IOException { final ImmutableSet.Builder builder = ImmutableSet.builder(); - for (LicenseSpec licenseSpec : licenseSpecs) { + for (ESLicense licenseSpec : licenseSpecs) { builder.add(sign(licenseSpec)); } return builder.build(); @@ -84,17 +84,17 @@ public class ESLicenseSigner { * @return a signed ESLicense (with signature) * @throws IOException */ - public ESLicense sign(LicenseSpec licenseSpec) throws IOException { + public ESLicense sign(ESLicense licenseSpec) throws IOException { License.Builder licenseBuilder = new License.Builder() - .withGoodBeforeDate(licenseSpec.expiryDate) - .withIssueDate(licenseSpec.issueDate) - .withProductKey(licenseSpec.uid) - .withHolder(licenseSpec.issuedTo) - .withIssuer(licenseSpec.issuer) - .addFeature("feature:" + licenseSpec.feature, licenseSpec.expiryDate) - .addFeature("maxNodes:" + String.valueOf(licenseSpec.maxNodes)) - .addFeature("type:" + licenseSpec.type.string()) - .addFeature("subscription_type:" + licenseSpec.subscriptionType.string()); + .withGoodBeforeDate(licenseSpec.expiryDate()) + .withIssueDate(licenseSpec.issueDate()) + .withProductKey(licenseSpec.uid()) + .withHolder(licenseSpec.issuedTo()) + .withIssuer(licenseSpec.issuer()) + .addFeature("feature:" + licenseSpec.feature(), licenseSpec.expiryDate()) + .addFeature("maxNodes:" + String.valueOf(licenseSpec.maxNodes())) + .addFeature("type:" + licenseSpec.type().string()) + .addFeature("subscription_type:" + licenseSpec.subscriptionType().string()); final License license = licenseBuilder.build(); @@ -117,16 +117,7 @@ public class ESLicenseSigner { String signature = Base64.encodeBase64String(bytes); return ESLicense.builder() - .uid(licenseSpec.uid) - .issuedTo(licenseSpec.issuedTo) - .issueDate(licenseSpec.issueDate) - .type(licenseSpec.type) - .subscriptionType(licenseSpec.subscriptionType) - .feature(licenseSpec.feature) - .maxNodes(licenseSpec.maxNodes) - .expiryDate(licenseSpec.expiryDate) - .issuer(licenseSpec.issuer) - .signature(signature) + .fromLicenseSpec(licenseSpec, signature) .verifyAndBuild(); } diff --git a/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java b/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java deleted file mode 100644 index 74e2c0f815e..00000000000 --- a/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java +++ /dev/null @@ -1,151 +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.LicensesCharset; - -import java.io.IOException; -import java.text.ParseException; -import java.util.*; - -import static org.elasticsearch.license.core.ESLicense.SubscriptionType; -import static org.elasticsearch.license.core.ESLicense.Type; - -public class LicenseSpec { - final String uid; - final String issuer; - final String issuedTo; - final long issueDate; - final Type type; - final SubscriptionType subscriptionType; - final String feature; - final long expiryDate; - final int maxNodes; - - private LicenseSpec(String uid, String issuer, String issuedTo, long issueDate, Type type, - SubscriptionType subscriptionType, String feature, long expiryDate, - int maxNodes) { - this.uid = uid; - this.issuer = issuer; - this.issuedTo = issuedTo; - this.issueDate = issueDate; - this.type = type; - this.subscriptionType = subscriptionType; - this.feature = feature; - this.expiryDate = expiryDate; - this.maxNodes = maxNodes; - } - - - private static class Builder { - private String uid; - private String issuer; - private String issuedTo; - private long issueDate = -1; - private Type type; - private SubscriptionType subscriptionType = SubscriptionType.DEFAULT; - private String feature; - private long expiryDate = -1; - private int maxNodes; - - - public Builder uid(String uid) { - this.uid = uid; - return this; - } - - public Builder issuer(String issuer) { - this.issuer = issuer; - return this; - } - - public Builder issuedTo(String issuedTo) { - this.issuedTo = issuedTo; - return this; - } - - public Builder issueDate(long issueDate) { - this.issueDate = issueDate; - return this; - } - - public Builder type(Type type) { - this.type = type; - return this; - } - - public Builder subscriptionType(SubscriptionType subscriptionType) { - this.subscriptionType = subscriptionType; - return this; - } - - public Builder feature(String feature) { - this.feature = feature; - return this; - } - - public Builder expiryDate(long expiryDate) { - this.expiryDate = expiryDate; - return this; - } - - public Builder maxNodes(int maxNodes) { - this.maxNodes = maxNodes; - return this; - } - - public LicenseSpec build() { - if (uid == null) { - uid = UUID.randomUUID().toString(); - } - //TODO: verify params - return new LicenseSpec(uid, issuer, issuedTo, issueDate, type, subscriptionType, - feature ,expiryDate, maxNodes); - } - } - - - private static LicenseSpec fromXContent(Map map) throws IOException, ParseException { - Builder builder = new Builder() - .uid((String) map.get("uid")) - .type(Type.fromString((String) map.get("type"))) - .subscriptionType(SubscriptionType.fromString((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 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(String content) throws IOException, ParseException { - return fromSource(content.getBytes(LicensesCharset.UTF_8)); - } - - 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 LicenseSpec licenseSpec = fromXContent(licenseSpecDef); - licenseSpecs.add(licenseSpec); - } - return licenseSpecs; - } -} - diff --git a/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java b/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java new file mode 100644 index 00000000000..5ec2cb845f0 --- /dev/null +++ b/src/main/java/org/elasticsearch/license/licensor/LicenseSpecs.java @@ -0,0 +1,66 @@ +/* + * 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 org.elasticsearch.license.core.LicensesCharset; + +import java.io.IOException; +import java.text.ParseException; +import java.util.*; + +import static org.elasticsearch.license.core.ESLicense.SubscriptionType; +import static org.elasticsearch.license.core.ESLicense.Type; + +public class LicenseSpecs { + + private static ESLicense fromXContent(Map map) throws IOException, ParseException { + ESLicense.Builder builder = new ESLicense.Builder() + .type(Type.fromString((String) map.get("type"))) + .subscriptionType(SubscriptionType.fromString((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(String content) throws IOException, ParseException { + return fromSource(content.getBytes(LicensesCharset.UTF_8)); + } + + 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 686c4c7b36b..9ec3694d394 100644 --- a/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -12,7 +12,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.licensor.ESLicenseSigner; -import org.elasticsearch.license.licensor.LicenseSpec; +import org.elasticsearch.license.licensor.LicenseSpecs; import java.io.File; import java.io.IOException; @@ -26,11 +26,11 @@ import java.util.Set; public class LicenseGeneratorTool { static class Options { - private final Set licenseSpecs; + private final Set licenseSpecs; private final String publicKeyFilePath; private final String privateKeyFilePath; - Options(Set licenseSpecs, String publicKeyFilePath, String privateKeyFilePath) { + Options(Set licenseSpecs, String publicKeyFilePath, String privateKeyFilePath) { this.licenseSpecs = licenseSpecs; this.publicKeyFilePath = publicKeyFilePath; this.privateKeyFilePath = privateKeyFilePath; @@ -38,7 +38,7 @@ public class LicenseGeneratorTool { } private static Options parse(String[] args) throws IOException, ParseException { - Set licenseSpecs = new HashSet<>(); + Set licenseSpecs = new HashSet<>(); String privateKeyPath = null; String publicKeyPath = null; @@ -47,13 +47,13 @@ public class LicenseGeneratorTool { switch (command) { case "--license": String licenseInput = args[++i]; - licenseSpecs.addAll(LicenseSpec.fromSource(licenseInput)); + licenseSpecs.addAll(LicenseSpecs.fromSource(licenseInput)); break; case "--licenseFile": File licenseFile = new File(args[++i]); if (licenseFile.exists()) { final byte[] bytes = Files.readAllBytes(Paths.get(licenseFile.getAbsolutePath())); - licenseSpecs.addAll(LicenseSpec.fromSource(bytes)); + licenseSpecs.addAll(LicenseSpecs.fromSource(bytes)); } else { throw new IllegalArgumentException(licenseFile.getAbsolutePath() + " does not exist!"); } diff --git a/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java b/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java index 4f09dd6da19..6028e6832ce 100644 --- a/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java +++ b/src/test/java/org/elasticsearch/license/manager/LicenseVerificationTests.java @@ -139,10 +139,8 @@ public class LicenseVerificationTests extends AbstractLicensingTestBase { ESLicense esLicense = ESLicenses.reduceAndMap(esLicensesOutput).get(TestUtils.SHIELD); final ESLicense tamperedLicense = ESLicense.builder() - .fromLicense(esLicense) + .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .feature(TestUtils.SHIELD) - .issuer("elasticsqearch") .verifyAndBuild(); try { diff --git a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java index 165a06191f7..295633b029e 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java @@ -138,10 +138,8 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest { ESLicense esLicense = ESLicenses.reduceAndMap(esLicenses).get(TestUtils.SHIELD); final ESLicense tamperedLicense = ESLicense.builder() - .fromLicense(esLicense) + .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .feature(TestUtils.SHIELD) - .issuer("elasticsqearch") .verifyAndBuild(); PutLicenseRequestBuilder builder = new PutLicenseRequestBuilder(client().admin().cluster()); diff --git a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java index 07265cd4c96..c81ed572d58 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java @@ -128,10 +128,8 @@ public class LicensesServiceTests extends ElasticsearchIntegrationTest { ESLicense esLicense = ESLicenses.reduceAndMap(licenses).get(TestUtils.SHIELD); final ESLicense tamperedLicense = ESLicense.builder() - .fromLicense(esLicense) + .fromLicenseSpec(esLicense, esLicense.signature()) .expiryDate(esLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000l) - .feature(TestUtils.SHIELD) - .issuer("elasticsqearch") .verifyAndBuild(); assertTrue(LicensesStatus.INVALID == licensesManagerService.checkLicenses(Collections.singleton(tamperedLicense)));