nuked LicenseSpec; merge to ESLicense

Original commit: elastic/x-pack-elasticsearch@f7dc1b7c9c
This commit is contained in:
Areek Zillur 2014-10-27 12:49:18 -04:00
parent 93607c8403
commit d1afd77bde
8 changed files with 90 additions and 190 deletions

View File

@ -257,7 +257,7 @@ public class ESLicense implements Comparable<ESLicense> {
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<ESLicense> {
.maxNodes(license.maxNodes())
.expiryDate(license.expiryDate())
.issuer(license.issuer())
.signature(license.signature());
.signature(signature);
}
public ESLicense verifyAndBuild() {

View File

@ -68,9 +68,9 @@ public class ESLicenseSigner {
}
public ImmutableSet<ESLicense> sign(Set<LicenseSpec> licenseSpecs) throws IOException {
public ImmutableSet<ESLicense> sign(Set<ESLicense> licenseSpecs) throws IOException {
final ImmutableSet.Builder<ESLicense> 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();
}

View File

@ -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<String, Object> 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<LicenseSpec> fromSource(String content) throws IOException, ParseException {
return fromSource(content.getBytes(LicensesCharset.UTF_8));
}
public static Set<LicenseSpec> fromSource(byte[] bytes) throws IOException, ParseException {
return fromXContents(XContentFactory.xContent(bytes).createParser(bytes));
}
private static Set<LicenseSpec> fromXContents(XContentParser parser) throws IOException, ParseException {
Set<LicenseSpec> licenseSpecs = new HashSet<>();
final Map<String, Object> licenseSpecMap = parser.mapAndClose();
@SuppressWarnings("unchecked")
final List<Map<String, Object>> licenseSpecDefinitions = (ArrayList<Map<String, Object>>)licenseSpecMap.get("licenses");
for (Map<String, Object> licenseSpecDef : licenseSpecDefinitions) {
final LicenseSpec licenseSpec = fromXContent(licenseSpecDef);
licenseSpecs.add(licenseSpec);
}
return licenseSpecs;
}
}

View File

@ -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<String, Object> 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<ESLicense> fromSource(String content) throws IOException, ParseException {
return fromSource(content.getBytes(LicensesCharset.UTF_8));
}
public static Set<ESLicense> fromSource(byte[] bytes) throws IOException, ParseException {
return fromXContents(XContentFactory.xContent(bytes).createParser(bytes));
}
private static Set<ESLicense> fromXContents(XContentParser parser) throws IOException, ParseException {
Set<ESLicense> licenseSpecs = new HashSet<>();
final Map<String, Object> licenseSpecMap = parser.mapAndClose();
@SuppressWarnings("unchecked")
final List<Map<String, Object>> licenseSpecDefinitions = (ArrayList<Map<String, Object>>)licenseSpecMap.get("licenses");
for (Map<String, Object> licenseSpecDef : licenseSpecDefinitions) {
final ESLicense licenseSpec = fromXContent(licenseSpecDef);
licenseSpecs.add(licenseSpec);
}
return licenseSpecs;
}
}

View File

@ -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<LicenseSpec> licenseSpecs;
private final Set<ESLicense> licenseSpecs;
private final String publicKeyFilePath;
private final String privateKeyFilePath;
Options(Set<LicenseSpec> licenseSpecs, String publicKeyFilePath, String privateKeyFilePath) {
Options(Set<ESLicense> 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<LicenseSpec> licenseSpecs = new HashSet<>();
Set<ESLicense> 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!");
}

View File

@ -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 {

View File

@ -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());

View File

@ -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)));