Major refactoring of LicenseManager; Initial integration with LicenseService; still a lot of TODOs

Original commit: elastic/x-pack-elasticsearch@880984062a
This commit is contained in:
Areek Zillur 2014-10-08 23:19:06 -04:00
parent 0bcdb016be
commit d7ec84afd7
25 changed files with 623 additions and 399 deletions

View File

@ -49,6 +49,16 @@ public class LicenseBuilders {
} }
} }
public static ESLicenses removeFeatures(ESLicenses licenses, Set<FeatureType> featureTypesToDelete) {
final LicensesBuilder licensesBuilder = licensesBuilder();
for (ESLicense license : licenses) {
if (!featureTypesToDelete.contains(license.feature())) {
licensesBuilder.license(license);
}
}
return licensesBuilder.build();
}
public static class LicensesBuilder { public static class LicensesBuilder {
private Map<FeatureType, ESLicense> licenseMap = new HashMap<>(); private Map<FeatureType, ESLicense> licenseMap = new HashMap<>();

View File

@ -95,7 +95,7 @@ public class LicenseVerificationTool {
Options options = parse(args); Options options = parse(args);
// verify licenses // verify licenses
ESLicenseManager licenseManager = new ESLicenseManager(options.licenses, options.publicKeyFilePath); ESLicenseManager licenseManager = ESLicenseManager.createLocalBasedInstance(options.licenses, options.publicKeyFilePath);
licenseManager.verifyLicenses(); licenseManager.verifyLicenses();
// dump effective licences // dump effective licences

View File

@ -11,51 +11,111 @@ import net.nicholaswilliams.java.licensing.encryption.Hasher;
import net.nicholaswilliams.java.licensing.encryption.PasswordProvider; import net.nicholaswilliams.java.licensing.encryption.PasswordProvider;
import net.nicholaswilliams.java.licensing.exception.ExpiredLicenseException; import net.nicholaswilliams.java.licensing.exception.ExpiredLicenseException;
import net.nicholaswilliams.java.licensing.exception.InvalidLicenseException; import net.nicholaswilliams.java.licensing.exception.InvalidLicenseException;
import org.apache.commons.codec.binary.Base64; import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders; import org.elasticsearch.license.core.LicenseBuilders;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.license.core.ESLicenses.*; import static org.elasticsearch.license.core.ESLicenses.*;
import static org.elasticsearch.license.manager.ESLicenseProvider.ClusterStateLicenseProvider;
import static org.elasticsearch.license.manager.ESLicenseProvider.FileBasedESLicenseProvider;
import static org.elasticsearch.license.manager.ESLicenseProvider.extractSignedLicence;
/** /**
* Class responsible for reading signed licenses, maintaining an effective esLicenses instance, verification of licenses * Class responsible for reading signed licenses, maintaining an effective esLicenses instance, verification of licenses
* and querying against licenses on a feature basis * and querying against licenses on a feature basis
* * <p/>
* TODO: * TODO:
* - integration with cluster state * - integration with cluster state
* - use ESLicenseProvider to query license from cluster state * - use ESLicenseProvider to query license from cluster state
*/ */
public class ESLicenseManager { public class ESLicenseManager {
private final LicenseManager licenseManager; private static ESLicenseManager instance = null;
private final ESLicenses esLicenses; private static FilePublicKeyDataProvider publicKeyDataProvider;
private final FilePublicKeyDataProvider publicKeyDataProvider; private final ESLicenseProvider licenseProvider;
public ESLicenseManager(Set<ESLicenses> esLicensesSet, String publicKeyFile) throws IOException { private final LicenseManager licenseManager;
this.publicKeyDataProvider = new FilePublicKeyDataProvider(publicKeyFile);
this.esLicenses = merge(esLicensesSet); public static ESLicenseManager getInstance() {
LicenseManagerProperties.setLicenseProvider(new ESLicenseProvider()); if (ESLicenseManager.instance == null) {
throw new IllegalStateException("License manager has not been created!");
}
return ESLicenseManager.instance;
}
/**
* Creates a LicenseManager instance where the Licenses are queried from the cluster state
*
* @param clusterService used to query for appropriate license(s) for validation
* @param publicKeyPath used to decrypt the licenses
* @return {@link org.elasticsearch.license.manager.ESLicenseManager} instance backed by licenses residing
* in the cluster state
*/
public static ESLicenseManager createClusterStateBasedInstance(ClusterService clusterService, String publicKeyPath) {
if (ESLicenseManager.instance == null) {
ESLicenseManager.publicKeyDataProvider = new FilePublicKeyDataProvider(publicKeyPath);
return new ESLicenseManager(ESLicenseProvider.createClusterBasedLicenseProvider(clusterService, publicKeyPath));
} else if (ESLicenseManager.instance.licenseProvider instanceof ClusterStateLicenseProvider) {
return ESLicenseManager.instance;
} else {
throw new IllegalStateException("Manager already initiated with File based license provider");
}
}
public static ESLicenseManager createClusterStateBasedInstance(ClusterService clusterService) {
return createClusterStateBasedInstance(clusterService, getPublicKeyPath());
}
public static ESLicenseManager createLocalBasedInstance(ESLicenses esLicenses, String publicKeyPath) {
return createLocalBasedInstance(Collections.singleton(esLicenses), publicKeyPath);
}
/**
* Creates a LicenseManager instance where the Licenses are queried from a set of pre-generated licenses
* @param esLicensesSet a set of pre-generated licenses stored in the license manager
* @param publicKeyPath used to decrypt the licenses
* @return {@link org.elasticsearch.license.manager.ESLicenseManager} instance backed by pre-generated licenses
*/
public static ESLicenseManager createLocalBasedInstance(Set<ESLicenses> esLicensesSet, String publicKeyPath) {
if (ESLicenseManager.instance == null) {
ESLicenseManager.publicKeyDataProvider = new FilePublicKeyDataProvider(publicKeyPath);
return new ESLicenseManager(ESLicenseProvider.createFileBasedLicenseProvider(merge(esLicensesSet), publicKeyPath));
} else if (ESLicenseManager.instance.licenseProvider instanceof FileBasedESLicenseProvider) {
return ESLicenseManager.instance;
} else {
throw new IllegalStateException("Manager already initiated with Cluster state based license provider");
}
}
private static String getPublicKeyPath() {
URL resource = ESLicenseManager.class.getResource("public.key");
if (resource == null) {
//test REMOVE NOCOMMIT!!!!
resource = ESLicenseManager.class.getResource("/org.elasticsearch.license.plugin/test_pub.key");
}
try {
return Paths.get(resource.toURI()).toFile().getAbsolutePath();
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}
private ESLicenseManager(ESLicenseProvider licenseProvider) {
LicenseManagerProperties.setLicenseProvider(licenseProvider);
LicenseManagerProperties.setPublicKeyDataProvider(publicKeyDataProvider); LicenseManagerProperties.setPublicKeyDataProvider(publicKeyDataProvider);
LicenseManagerProperties.setLicenseValidator(new DefaultLicenseValidator()); LicenseManagerProperties.setLicenseValidator(new DefaultLicenseValidator());
LicenseManagerProperties.setPublicKeyPasswordProvider(new ESPublicKeyPasswordProvider()); LicenseManagerProperties.setPublicKeyPasswordProvider(new ESPublicKeyPasswordProvider());
this.licenseProvider = licenseProvider;
this.licenseManager = LicenseManager.getInstance(); this.licenseManager = LicenseManager.getInstance();
} }
public ESLicenseManager(ESLicenses esLicenses, String publicKeyFile) throws IOException {
this(Collections.singleton(esLicenses), publicKeyFile);
}
private static ESLicenses merge(Set<ESLicenses> esLicensesSet) { private static ESLicenses merge(Set<ESLicenses> esLicensesSet) {
ESLicenses mergedLicenses = null; ESLicenses mergedLicenses = null;
for (ESLicenses licenses : esLicensesSet) { for (ESLicenses licenses : esLicensesSet) {
@ -65,65 +125,38 @@ public class ESLicenseManager {
} }
public ESLicenses getEffectiveLicenses() { public ESLicenses getEffectiveLicenses() {
return esLicenses; return licenseProvider.getEffectiveLicenses();
} }
private License getLicense(FeatureType featureType) { public void verifyLicenses(ESLicenses esLicenses) {
ESLicense esLicense = esLicenses.get(featureType); try {
if (esLicense != null) { for (FeatureType featureType : esLicenses.features()) {
String signature = esLicense.signature(); ESLicense esLicense = esLicenses.get(featureType);
try { // verify signature
License license = this.licenseManager.decryptAndVerifyLicense(extractSignedLicence(signature)); final License license = this.licenseManager.decryptAndVerifyLicense(
extractSignedLicence(
esLicense.signature(),
publicKeyDataProvider.getPublicKeyFile().getAbsolutePath()));
// validate license
this.licenseManager.validateLicense(license); this.licenseManager.validateLicense(license);
return license;
} catch (IOException e) { // verify all readable license fields
throw new IllegalStateException("bogus"); verifyLicenseFields(license, esLicense);
} }
} } catch (ExpiredLicenseException e) {
return null; throw new InvalidLicenseException("Expired License");
} } catch (InvalidLicenseException e) {
/**
* Extract a signedLicense (SIGNED_LICENSE_CONTENT) from the signature.
* Validates the public key used to decrypt the license by comparing their hashes
* <p/>
* Signature structure:
* | MAGIC | HEADER_LENGTH | VERSION | PUB_KEY_DIGEST | SIGNED_LICENSE_CONTENT |
*
* @param signature of a single license
* @return signed license content for the license
* @throws IOException
*/
private SignedLicense extractSignedLicence(String signature) throws IOException {
byte[] signatureBytes = Base64.decodeBase64(signature);
ByteBuffer byteBuffer = ByteBuffer.wrap(signatureBytes);
byteBuffer = (ByteBuffer) byteBuffer.position(13);
int start = byteBuffer.getInt();
int version = byteBuffer.getInt();
byte[] hash = new byte[start - 13 - 4 - 4];
byteBuffer.get(hash);
final byte[] computedHash = Hasher.hash(Base64.encodeBase64String(
Files.readAllBytes(Paths.get(publicKeyDataProvider.getPublicKeyFile().getAbsolutePath())))
).getBytes(Charset.forName("UTF-8"));
if (!Arrays.equals(hash, computedHash)) {
throw new InvalidLicenseException("Invalid License"); throw new InvalidLicenseException("Invalid License");
} catch (IOException e) {
// bogus
throw new IllegalStateException(e);
} }
return new ObjectSerializer().readObject(SignedLicense.class, Arrays.copyOfRange(signatureBytes, start, signatureBytes.length));
} }
public void verifyLicenses() { public void verifyLicenses() {
for (FeatureType featureType : esLicenses.features()) { verifyLicenses(getEffectiveLicenses());
final License license = getLicense(featureType);
assert license != null : "license should not be null for feature: " + featureType.string();
verifyLicenseFields(license, esLicenses.get(featureType));
}
} }
private static void verifyLicenseFields(License license, ESLicense eslicense) { private static void verifyLicenseFields(License license, ESLicense eslicense) {
boolean licenseValid = license.getProductKey().equals(eslicense.uid()) boolean licenseValid = license.getProductKey().equals(eslicense.uid())
&& license.getHolder().equals(eslicense.issuedTo()) && license.getHolder().equals(eslicense.issuedTo())
@ -163,13 +196,15 @@ public class ESLicenseManager {
} }
//TODO wrap License validation methods so a plugin does not have to provide featureType param
public boolean hasLicenseForFeature(FeatureType featureType) { public boolean hasLicenseForFeature(FeatureType featureType) {
try { try {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
if (license == null) { if (license != null) {
return false; return license.hasLicenseForFeature(featureType.string());
} }
return license.hasLicenseForFeature(featureType.string()); return false;
} catch (ExpiredLicenseException e) { } catch (ExpiredLicenseException e) {
return false; return false;
} catch (InvalidLicenseException e) { } catch (InvalidLicenseException e) {
@ -178,42 +213,42 @@ public class ESLicenseManager {
} }
public boolean hasLicenseForNodes(FeatureType featureType, int nodes) { public boolean hasLicenseForNodes(FeatureType featureType, int nodes) {
ESLicense esLicense = generateESLicense(featureType); ESLicense esLicense = getESLicense(featureType);
return esLicense.maxNodes() >= nodes; return esLicense.maxNodes() >= nodes;
} }
public String getIssuerForLicense(FeatureType featureType) { public String getIssuerForLicense(FeatureType featureType) {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
return license.getIssuer(); return license.getIssuer();
} }
public long getIssueDateForLicense(FeatureType featureType) { public long getIssueDateForLicense(FeatureType featureType) {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
return license.getIssueDate(); return license.getIssueDate();
} }
public long getExpiryDateForLicense(FeatureType featureType) { public long getExpiryDateForLicense(FeatureType featureType) {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
return license.getGoodBeforeDate(); return license.getGoodBeforeDate();
} }
public String getIssuedToForLicense(FeatureType featureType) { public String getIssuedToForLicense(FeatureType featureType) {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
return license.getHolder(); return license.getHolder();
} }
public Type getTypeForLicense(FeatureType featureType) { public Type getTypeForLicense(FeatureType featureType) {
ESLicense esLicense = generateESLicense(featureType); ESLicense esLicense = getESLicense(featureType);
return esLicense.type(); return esLicense.type();
} }
public SubscriptionType getSubscriptionTypeForLicense(FeatureType featureType) { public SubscriptionType getSubscriptionTypeForLicense(FeatureType featureType) {
ESLicense esLicense = generateESLicense(featureType); ESLicense esLicense = getESLicense(featureType);
return esLicense.subscriptionType(); return esLicense.subscriptionType();
} }
private ESLicense generateESLicense(FeatureType featureType) { ESLicense getESLicense(FeatureType featureType) {
final License license = getLicense(featureType); final License license = licenseManager.getLicense(featureType);
return convertToESLicense(license); return convertToESLicense(license);
} }
@ -245,18 +280,15 @@ public class ESLicenseManager {
return licenseBuilder.build(); return licenseBuilder.build();
} }
/**
* Used by the underlying license manager (make sure it is never called for now) // only for testing
* This should be retrieving licenses from the custom metadata in the cluster state public void clearAndAddLicenses(ESLicenses licenses) {
*/ this.licenseManager.clearLicenseCache();
public class ESLicenseProvider implements LicenseProvider { assert this.licenseProvider instanceof FileBasedESLicenseProvider;
@Override this.licenseProvider.addLicenses(licenses);
public SignedLicense getLicense(Object context) {
throw new NotImplementedException();
}
} }
private class ESPublicKeyPasswordProvider implements PasswordProvider { private static class ESPublicKeyPasswordProvider implements PasswordProvider {
private final String DEFAULT_PASS_PHRASE = "elasticsearch-license"; private final String DEFAULT_PASS_PHRASE = "elasticsearch-license";
@Override @Override

View File

@ -0,0 +1,183 @@
/*
* 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.manager;
import net.nicholaswilliams.java.licensing.LicenseProvider;
import net.nicholaswilliams.java.licensing.ObjectSerializer;
import net.nicholaswilliams.java.licensing.SignedLicense;
import net.nicholaswilliams.java.licensing.encryption.Hasher;
import net.nicholaswilliams.java.licensing.exception.InvalidLicenseException;
import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.plugin.core.LicensesMetaData;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import static org.elasticsearch.license.core.ESLicenses.ESLicense;
import static org.elasticsearch.license.core.ESLicenses.FeatureType;
public abstract class ESLicenseProvider implements LicenseProvider {
/**
* Factory for {@link org.elasticsearch.license.manager.ESLicenseProvider.ClusterStateLicenseProvider}
*/
public static ESLicenseProvider createClusterBasedLicenseProvider(ClusterService clusterService, String publicKeyPath) {
return new ClusterStateLicenseProvider(clusterService, publicKeyPath);
}
/**
* Factory for {@link org.elasticsearch.license.manager.ESLicenseProvider.FileBasedESLicenseProvider}
*/
public static ESLicenseProvider createFileBasedLicenseProvider(ESLicenses esLicenses, String publicKeyPath) {
return new FileBasedESLicenseProvider(esLicenses, publicKeyPath);
}
public abstract ESLicense getESLicense(FeatureType featureType);
public abstract ESLicenses getEffectiveLicenses();
// only for testing
public abstract void addLicenses(ESLicenses esLicenses);
/**
* LicenseProvider backed by the current cluster state
* Uses the clusterService to query for the latest licenses
* for {@link org.elasticsearch.license.manager.ESLicenseManager}
* consumption
*/
public static class ClusterStateLicenseProvider extends ESLicenseProvider {
private final ClusterService clusterService;
private final String publicKeyPath;
private ClusterStateLicenseProvider(ClusterService clusterService, String publicKeyPath) {
this.clusterService = clusterService;
this.publicKeyPath = publicKeyPath;
}
private ESLicenses getLicensesFromClusterState() {
final ClusterState state = clusterService.state();
return (ESLicenses) state.metaData().custom(LicensesMetaData.TYPE);
}
public ESLicenses getESLicenses() {
return getLicensesFromClusterState();
}
@Override
public SignedLicense getLicense(Object context) {
assert context instanceof FeatureType;
FeatureType featureType = (FeatureType) context;
final ESLicenses licenses = getLicensesFromClusterState();
final ESLicense esLicense = licenses.get(featureType);
if (esLicense == null) {
throw new InvalidLicenseException("Invalid License");
}
try {
return extractSignedLicence(esLicense.signature(), publicKeyPath);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override
public ESLicense getESLicense(FeatureType featureType) {
return getLicensesFromClusterState().get(featureType);
}
@Override
public ESLicenses getEffectiveLicenses() {
return getLicensesFromClusterState();
}
@Override
public void addLicenses(ESLicenses esLicenses) {
throw new UnsupportedOperationException("Licenses can only be added by updating the cluster state");
}
}
/**
* Extract a signedLicense (SIGNED_LICENSE_CONTENT) from the signature.
* Validates the public key used to decrypt the license by comparing their hashes
* <p/>
* Signature structure:
* | MAGIC | HEADER_LENGTH | VERSION | PUB_KEY_DIGEST | SIGNED_LICENSE_CONTENT |
*
* @param signature of a single license
* @param publicKeyPath location of the public key used to decode the signature
* @return signed license content for the license
* @throws IOException
*/
static SignedLicense extractSignedLicence(String signature, String publicKeyPath) throws IOException {
byte[] signatureBytes = Base64.decodeBase64(signature);
ByteBuffer byteBuffer = ByteBuffer.wrap(signatureBytes);
byteBuffer = (ByteBuffer) byteBuffer.position(13);
int start = byteBuffer.getInt();
int version = byteBuffer.getInt();
byte[] hash = new byte[start - 13 - 4 - 4];
byteBuffer.get(hash);
final byte[] computedHash = Hasher.hash(Base64.encodeBase64String(
Files.readAllBytes(Paths.get(publicKeyPath))
)).getBytes(Charset.forName("UTF-8"));
if (!Arrays.equals(hash, computedHash)) {
throw new InvalidLicenseException("Invalid License");
}
return new ObjectSerializer().readObject(SignedLicense.class, Arrays.copyOfRange(signatureBytes, start, signatureBytes.length));
}
/**
* Not thread-safe (allows setting licenses); used only for testing and in command-line tools
*/
public static class FileBasedESLicenseProvider extends ESLicenseProvider {
private ESLicenses esLicenses;
private final String publicKeyPath;
private FileBasedESLicenseProvider(ESLicenses esLicenses, String publicKeyPath) {
this.esLicenses = esLicenses;
this.publicKeyPath = publicKeyPath;
}
@Override
public SignedLicense getLicense(Object context) {
assert context instanceof FeatureType;
FeatureType featureType = (FeatureType) context;
ESLicense esLicense = esLicenses.get(featureType);
if (esLicense == null) {
throw new InvalidLicenseException("Invalid License");
}
try {
return extractSignedLicence(esLicense.signature(), publicKeyPath);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override
public ESLicense getESLicense(FeatureType featureType) {
return esLicenses.get(featureType);
}
@Override
public ESLicenses getEffectiveLicenses() {
return esLicenses;
}
@Override
public void addLicenses(ESLicenses esLicenses) {
this.esLicenses = esLicenses;
}
}
}

View File

@ -20,9 +20,6 @@ import static org.elasticsearch.license.core.ESLicenses.ESLicense;
public class Utils { public class Utils {
private Utils() {
}
public static ESLicenses getESLicensesFromSignatures(final LicenseManager licenseManager, Set<String> signatures) { public static ESLicenses getESLicensesFromSignatures(final LicenseManager licenseManager, Set<String> signatures) {
final LicenseBuilders.LicensesBuilder licensesBuilder = LicenseBuilders.licensesBuilder(); final LicenseBuilders.LicensesBuilder licensesBuilder = LicenseBuilders.licensesBuilder();
for (String signature : signatures) { for (String signature : signatures) {
@ -41,5 +38,34 @@ public class Utils {
return ESLicenseManager.convertToESLicense(licenseManager.decryptAndVerifyLicense(signedLicense)); return ESLicenseManager.convertToESLicense(licenseManager.decryptAndVerifyLicense(signedLicense));
} }
public static boolean isSame(ESLicenses firstLicenses, ESLicenses secondLicenses) {
// we do the build to make sure we weed out any expired licenses
final ESLicenses licenses1 = LicenseBuilders.licensesBuilder().licenses(firstLicenses).build();
final ESLicenses licenses2 = LicenseBuilders.licensesBuilder().licenses(secondLicenses).build();
// check if the effective licenses have the same feature set
if (!licenses1.features().equals(licenses2.features())) {
return false;
}
// for every feature license, check if all the attributes are the same
for (ESLicenses.FeatureType featureType : licenses1.features()) {
ESLicense license1 = licenses1.get(featureType);
ESLicense license2 = licenses2.get(featureType);
if (!license1.uid().equals(license2.uid())
|| license1.feature() != license2.feature()
|| license1.subscriptionType() != license2.subscriptionType()
|| license1.type() != license2.type()
|| license1.expiryDate() != license2.expiryDate()
|| license1.issueDate() != license2.issueDate()
|| !license1.issuedTo().equals(license2.issuedTo())
|| license1.maxNodes() != license2.maxNodes()
|| license1.signature().equals(license2.signature())) {
return false;
}
}
return true;
}
} }

View File

@ -11,7 +11,7 @@ import org.elasticsearch.license.plugin.core.LicensesService;
public class LicenseModule extends AbstractModule { public class LicenseModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
//requestInjection(LicensesService.class); //TODO: bind LicensesManagementService and LicensesValidationService to LicensesServices instead
bind(LicensesService.class).asEagerSingleton(); bind(LicensesService.class).asEagerSingleton();
} }
} }

View File

@ -11,11 +11,14 @@ import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.delete.TransportDeleteLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseAction; import org.elasticsearch.license.plugin.action.get.GetLicenseAction;
import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction; import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesMetaData; import org.elasticsearch.license.plugin.core.LicensesMetaData;
import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction;
import org.elasticsearch.license.plugin.rest.RestGetLicenseAction; import org.elasticsearch.license.plugin.rest.RestGetLicenseAction;
import org.elasticsearch.license.plugin.rest.RestPutLicenseAction; import org.elasticsearch.license.plugin.rest.RestPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.core.LicensesService;
@ -24,7 +27,6 @@ import org.elasticsearch.rest.RestModule;
import java.util.Collection; import java.util.Collection;
//TODO: plugin hooks
public class LicensePlugin extends AbstractPlugin { public class LicensePlugin extends AbstractPlugin {
static { static {
@ -45,11 +47,13 @@ public class LicensePlugin extends AbstractPlugin {
// Register REST endpoint // Register REST endpoint
module.addRestAction(RestPutLicenseAction.class); module.addRestAction(RestPutLicenseAction.class);
module.addRestAction(RestGetLicenseAction.class); module.addRestAction(RestGetLicenseAction.class);
module.addRestAction(RestDeleteLicenseAction.class);
} }
public void onModule(ActionModule module) { public void onModule(ActionModule module) {
module.registerAction(PutLicenseAction.INSTANCE, TransportPutLicenseAction.class); module.registerAction(PutLicenseAction.INSTANCE, TransportPutLicenseAction.class);
module.registerAction(GetLicenseAction.INSTANCE, TransportGetLicenseAction.class); module.registerAction(GetLicenseAction.INSTANCE, TransportGetLicenseAction.class);
module.registerAction(DeleteLicenseAction.INSTANCE, TransportDeleteLicenseAction.class);
} }
@Override @Override
@ -62,5 +66,4 @@ public class LicensePlugin extends AbstractPlugin {
public Collection<Class<? extends Module>> modules() { public Collection<Class<? extends Module>> modules() {
return ImmutableSet.<Class<? extends Module>>of(LicenseModule.class); return ImmutableSet.<Class<? extends Module>>of(LicenseModule.class);
} }
//TODO: module binding? (LicenseModule)
} }

View File

@ -1,9 +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.plugin;
public class Utils {
}

View File

@ -27,8 +27,9 @@ public class Utils {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
licensesBuilder.licenseAsIs(licenseFromMap(in.readMap())); licensesBuilder.licenseAsIs(licenseFromMap(in.readMap()));
} }
return licensesBuilder.build();
} }
return licensesBuilder.build(); return null;
} }
public static void writeLicensesTo(ESLicenses esLicenses, StreamOutput out) throws IOException { public static void writeLicensesTo(ESLicenses esLicenses, StreamOutput out) throws IOException {

View File

@ -61,7 +61,7 @@ public class TransportDeleteLicenseAction extends TransportMasterNodeOperationAc
//listener.onResponse(new DeleteLicenseResponse(licenses)); //listener.onResponse(new DeleteLicenseResponse(licenses));
//TODO:: add features of the license to be deleted //TODO:: add features of the license to be deleted
licensesService.unregisteredLicenses(clusterService, "delete_licenses []", request, new ActionListener<ClusterStateUpdateResponse>() { licensesService.unregisteredLicenses("delete_licenses []", request, new ActionListener<ClusterStateUpdateResponse>() {
@Override @Override
public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) { public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
listener.onResponse(new DeleteLicenseResponse(clusterStateUpdateResponse.isAcknowledged())); listener.onResponse(new DeleteLicenseResponse(clusterStateUpdateResponse.isAcknowledged()));

View File

@ -9,6 +9,7 @@ import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import java.io.IOException; import java.io.IOException;
@ -27,10 +28,9 @@ public class GetLicenseResponse extends ActionResponse {
} }
public ESLicenses licenses() { public ESLicenses licenses() {
return licenses; return (licenses != null) ? licenses : LicenseBuilders.licensesBuilder().build();
} }
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);

View File

@ -55,8 +55,9 @@ public class TransportPutLicenseAction extends TransportMasterNodeOperationActio
@Override @Override
protected void masterOperation(final PutLicenseRequest request, ClusterState state, final ActionListener<PutLicenseResponse> listener) throws ElasticsearchException { protected void masterOperation(final PutLicenseRequest request, ClusterState state, final ActionListener<PutLicenseResponse> listener) throws ElasticsearchException {
//TODO //TODO license validation
licensesService.registerLicenses(clusterService, "put_licenses []",request, new ActionListener<ClusterStateUpdateResponse>() {
licensesService.registerLicenses("put_licenses []",request, new ActionListener<ClusterStateUpdateResponse>() {
@Override @Override
public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) { public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
listener.onResponse(new PutLicenseResponse(clusterStateUpdateResponse.isAcknowledged())); listener.onResponse(new PutLicenseResponse(clusterStateUpdateResponse.isAcknowledged()));

View File

@ -46,8 +46,10 @@ public class LicensesMetaData implements MetaData.Custom, ESLicenses {
private static ImmutableMap<FeatureType, ESLicense> map(Iterable<ESLicense> esLicenses) { private static ImmutableMap<FeatureType, ESLicense> map(Iterable<ESLicense> esLicenses) {
final ImmutableMap.Builder<FeatureType, ESLicense> builder = ImmutableMap.builder(); final ImmutableMap.Builder<FeatureType, ESLicense> builder = ImmutableMap.builder();
for (ESLicense esLicense : esLicenses) { if (esLicenses != null) {
builder.put(esLicense.feature(), esLicense); for (ESLicense esLicense : esLicenses) {
builder.put(esLicense.feature(), esLicense);
}
} }
return builder.build(); return builder.build();
} }

View File

@ -8,19 +8,22 @@ package org.elasticsearch.license.plugin.core;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.*; import org.elasticsearch.cluster.*;
import org.elasticsearch.cluster.ack.ClusterStateUpdateRequest;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse; import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders; import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest; import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalNode;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.HashSet;
import java.util.Set;
/** /**
* Service responsible for maintaining and providing access to licenses on nodes. * Service responsible for maintaining and providing access to licenses on nodes.
@ -31,11 +34,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class LicensesService extends AbstractLifecycleComponent<LicensesService> implements ClusterStateListener { public class LicensesService extends AbstractLifecycleComponent<LicensesService> implements ClusterStateListener {
private AtomicBoolean registerClusterStateListener = new AtomicBoolean(false); private ESLicenseManager esLicenseManager;
private InternalNode node;
private ClusterService clusterService;
@Inject @Inject
public LicensesService(Settings settings) { public LicensesService(Settings settings, Node node) {
super(settings); super(settings);
this.node = (InternalNode) node;
} }
/** /**
@ -44,14 +52,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
* This method can be only called on the master node. It tries to create a new licenses on the master * This method can be only called on the master node. It tries to create a new licenses on the master
* and if it was successful it adds the license to cluster metadata. * and if it was successful it adds the license to cluster metadata.
*/ */
public void registerLicenses(ClusterService clusterService, String source, final PutLicenseRequest request, final ActionListener<ClusterStateUpdateResponse> listener) { public void registerLicenses(String source, final PutLicenseRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
if (registerClusterStateListener.compareAndSet(false, true)) {
if (DiscoveryNode.dataNode(settings) || DiscoveryNode.masterNode(settings)) {
clusterService.add(this);
}
}
final LicensesMetaData newLicenseMetaData = new LicensesMetaData(request.license()); final LicensesMetaData newLicenseMetaData = new LicensesMetaData(request.license());
//TODO: add a source field to request
clusterService.submitStateUpdateTask(source, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, listener) { clusterService.submitStateUpdateTask(source, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, listener) {
@Override @Override
protected ClusterStateUpdateResponse newResponse(boolean acknowledged) { protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
@ -60,11 +62,12 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override @Override
public ClusterState execute(ClusterState currentState) throws Exception { public ClusterState execute(ClusterState currentState) throws Exception {
// TODO check if newLicenseMetaData actually needs a cluster update
MetaData metaData = currentState.metaData(); MetaData metaData = currentState.metaData();
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE);
esLicenseManager.verifyLicenses(newLicenseMetaData);
if (currentLicenses == null) { if (currentLicenses == null) {
// no licenses were registered // no licenses were registered
currentLicenses = newLicenseMetaData; currentLicenses = newLicenseMetaData;
@ -72,6 +75,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
// merge previous license with new one // merge previous license with new one
currentLicenses = new LicensesMetaData(LicenseBuilders.merge(currentLicenses, newLicenseMetaData)); currentLicenses = new LicensesMetaData(LicenseBuilders.merge(currentLicenses, newLicenseMetaData));
} }
mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses); mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses);
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
} }
@ -79,8 +83,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
} }
//TODO public void unregisteredLicenses(String source, final DeleteLicenseRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
public void unregisteredLicenses(ClusterService clusterService, String source, final DeleteLicenseRequest request, final ActionListener<ClusterStateUpdateResponse> listener) { final Set<ESLicenses.FeatureType> featuresToDelete = asFeatureTypes(request.features());
clusterService.submitStateUpdateTask(source, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, listener) { clusterService.submitStateUpdateTask(source, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, listener) {
@Override @Override
protected ClusterStateUpdateResponse newResponse(boolean acknowledged) { protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
@ -89,18 +93,12 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override @Override
public ClusterState execute(ClusterState currentState) throws Exception { public ClusterState execute(ClusterState currentState) throws Exception {
// TODO check if newLicenseMetaData actually needs a cluster update
MetaData metaData = currentState.metaData(); MetaData metaData = currentState.metaData();
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData()); MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE); LicensesMetaData currentLicenses = metaData.custom(LicensesMetaData.TYPE);
//TODO: implement deletion if (currentLicenses != null) {
if (currentLicenses == null) { currentLicenses = new LicensesMetaData(LicenseBuilders.removeFeatures(currentLicenses, featuresToDelete));
// no licenses were registered
//currentLicenses = newLicenseMetaData;
} else {
// merge previous license with new one
//currentLicenses = new LicensesMetaData(LicenseBuilders.merge(currentLicenses, newLicenseMetaData));
} }
mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses); mdBuilder.putCustom(LicensesMetaData.TYPE, currentLicenses);
return ClusterState.builder(currentState).metaData(mdBuilder).build(); return ClusterState.builder(currentState).metaData(mdBuilder).build();
@ -110,7 +108,12 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override @Override
protected void doStart() throws ElasticsearchException { protected void doStart() throws ElasticsearchException {
//TODO clusterService = node.injector().getInstance(ClusterService.class);
esLicenseManager = ESLicenseManager.createClusterStateBasedInstance(clusterService);
if (DiscoveryNode.dataNode(settings) || DiscoveryNode.masterNode(settings)) {
clusterService.add(this);
}
} }
@Override @Override
@ -127,4 +130,12 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
public void clusterChanged(ClusterChangedEvent event) { public void clusterChanged(ClusterChangedEvent event) {
//TODO //TODO
} }
private static Set<ESLicenses.FeatureType> asFeatureTypes(Set<String> featureTypeStrings) {
Set<ESLicenses.FeatureType> featureTypes = new HashSet<>(featureTypeStrings.size());
for (String featureString : featureTypeStrings) {
featureTypes.add(ESLicenses.FeatureType.fromString(featureString));
}
return featureTypes;
}
} }

View File

@ -5,6 +5,7 @@
*/ */
package org.elasticsearch.license.plugin.rest; package org.elasticsearch.license.plugin.rest;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
@ -38,7 +39,6 @@ public class RestDeleteLicenseAction extends BaseRestHandler {
@Inject @Inject
public RestDeleteLicenseAction(Settings settings, RestController controller, Client client, TransportDeleteLicenseAction transportDeleteLicenseAction) { public RestDeleteLicenseAction(Settings settings, RestController controller, Client client, TransportDeleteLicenseAction transportDeleteLicenseAction) {
super(settings, controller, client); super(settings, controller, client);
controller.registerHandler(DELETE, "/_cluster/license/", this);
controller.registerHandler(DELETE, "/_cluster/license/{features}", this); controller.registerHandler(DELETE, "/_cluster/license/{features}", this);
this.transportDeleteLicenseAction = transportDeleteLicenseAction; this.transportDeleteLicenseAction = transportDeleteLicenseAction;
} }
@ -47,10 +47,11 @@ public class RestDeleteLicenseAction extends BaseRestHandler {
@Override @Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
final String[] features = Strings.splitStringByCommaToArray(request.param("features")); final String[] features = Strings.splitStringByCommaToArray(request.param("features"));
if (features.length == 0) {
throw new ElasticsearchIllegalArgumentException("no features specified for license deletion");
}
DeleteLicenseRequest deleteLicenseRequest = new DeleteLicenseRequest(getFeaturesToDelete(features)); DeleteLicenseRequest deleteLicenseRequest = new DeleteLicenseRequest(getFeaturesToDelete(features));
deleteLicenseRequest.listenerThreaded(false); deleteLicenseRequest.listenerThreaded(false);
//deleteLicenseRequest.license(request.content().toUtf8());
transportDeleteLicenseAction.execute(deleteLicenseRequest, new AcknowledgedRestListener<DeleteLicenseResponse>(channel)); transportDeleteLicenseAction.execute(deleteLicenseRequest, new AcknowledgedRestListener<DeleteLicenseResponse>(channel));
} }
@ -64,7 +65,6 @@ public class RestDeleteLicenseAction extends BaseRestHandler {
break; break;
} else { } else {
result.add(FeatureType.fromString(feature).string()); result.add(FeatureType.fromString(feature).string());
} }
} }
return result.toArray(new String[result.size()]); return result.toArray(new String[result.size()]);

View File

@ -52,6 +52,5 @@ public class RestGetLicenseAction extends BaseRestHandler {
return new BytesRestResponse(OK, builder); return new BytesRestResponse(OK, builder);
} }
}); });
//client.admin().cluster().execute(GetLicenseAction.INSTANCE, getLicenseRequest, )
} }
} }

View File

@ -40,6 +40,5 @@ public class RestPutLicenseAction extends BaseRestHandler {
putLicenseRequest.listenerThreaded(false); putLicenseRequest.listenerThreaded(false);
putLicenseRequest.license(request.content().toUtf8()); putLicenseRequest.license(request.content().toUtf8());
transportPutLicensesAction.execute(putLicenseRequest, new AcknowledgedRestListener<PutLicenseResponse>(channel)); transportPutLicensesAction.execute(putLicenseRequest, new AcknowledgedRestListener<PutLicenseResponse>(channel));
// client.admin().cluster().execute(PutLicenseAction.INSTANCE, putLicenseRequest, );
} }
} }

View File

@ -0,0 +1,45 @@
/*
* 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;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool;
import org.junit.BeforeClass;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class AbstractLicensingTestBase {
protected static String pubKeyPath = null;
protected static String priKeyPath = null;
@BeforeClass
public static void setup() throws IOException {
// Generate temp KeyPair spec
File privateKeyFile = File.createTempFile("privateKey", ".key");
File publicKeyFile = File.createTempFile("publicKey", ".key");
AbstractLicensingTestBase.pubKeyPath = publicKeyFile.getAbsolutePath();
AbstractLicensingTestBase.priKeyPath = privateKeyFile.getAbsolutePath();
assert privateKeyFile.delete();
assert publicKeyFile.delete();
// Generate keyPair
String[] args = new String[4];
args[0] = "--publicKeyPath";
args[1] = AbstractLicensingTestBase.pubKeyPath;
args[2] = "--privateKeyPath";
args[3] = AbstractLicensingTestBase.priKeyPath;
KeyPairGeneratorTool.main(args);
}
public String generateSignedLicenses(Map<ESLicenses.FeatureType, TestUtils.FeatureAttributes> map) throws IOException {
String licenseString = TestUtils.generateESLicenses(map);
return TestUtils.runLicenseGenerationTool(licenseString, pubKeyPath, priKeyPath);
}
}

View File

@ -48,6 +48,18 @@ public class TestUtils {
} }
public static String runLicenseGenerationTool(String licenseInput, String pubKeyPath, String priKeyPath) throws IOException {
String args[] = new String[6];
args[0] = "--license";
args[1] = licenseInput;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
return runLicenseGenerationTool(args);
}
public static String runLicenseGenerationTool(String[] args) throws IOException { public static String runLicenseGenerationTool(String[] args) throws IOException {
File temp = File.createTempFile("temp", ".out"); File temp = File.createTempFile("temp", ".out");
temp.deleteOnExit(); temp.deleteOnExit();

View File

@ -5,14 +5,12 @@
*/ */
package org.elasticsearch.license.licensor; package org.elasticsearch.license.licensor;
import org.elasticsearch.license.AbstractLicensingTestBase;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseUtils; import org.elasticsearch.license.core.LicenseUtils;
import org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
@ -22,30 +20,7 @@ import static org.elasticsearch.license.core.ESLicenses.FeatureType;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class LicenseGenerationTests { public class LicenseGenerationTests extends AbstractLicensingTestBase {
private static String pubKeyPath = null;
private static String priKeyPath = null;
@BeforeClass
public static void setup() throws IOException {
// Generate temp KeyPair spec
File privateKeyFile = File.createTempFile("privateKey", ".key");
File publicKeyFile = File.createTempFile("publicKey", ".key");
LicenseGenerationTests.pubKeyPath = publicKeyFile.getAbsolutePath();
LicenseGenerationTests.priKeyPath = privateKeyFile.getAbsolutePath();
assert privateKeyFile.delete();
assert publicKeyFile.delete();
// Generate keyPair
String[] args = new String[4];
args[0] = "--publicKeyPath";
args[1] = LicenseGenerationTests.pubKeyPath;
args[2] = "--privateKeyPath";
args[3] = LicenseGenerationTests.priKeyPath;
KeyPairGeneratorTool.main(args);
}
@Test @Test
public void testSimpleLicenseGeneration() throws ParseException, IOException { public void testSimpleLicenseGeneration() throws ParseException, IOException {
@ -54,17 +29,7 @@ public class LicenseGenerationTests {
new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, "2014-12-13", "2015-12-13"); new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, "2014-12-13", "2015-12-13");
map.put(FeatureType.SHIELD, featureAttributes); map.put(FeatureType.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map); String licenseOutput = generateSignedLicenses(map);
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
ESLicenses esLicensesOutput = LicenseUtils.readLicensesFromString(licenseOutput); ESLicenses esLicensesOutput = LicenseUtils.readLicensesFromString(licenseOutput);
@ -82,16 +47,7 @@ public class LicenseGenerationTests {
map.put(FeatureType.SHIELD, shildFeatureAttributes); map.put(FeatureType.SHIELD, shildFeatureAttributes);
map.put(FeatureType.MARVEL, marvelFeatureAttributes); map.put(FeatureType.MARVEL, marvelFeatureAttributes);
String licenseString = TestUtils.generateESLicenses(map); String licenseOutput = generateSignedLicenses(map);
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
ESLicenses esLicensesOutput = LicenseUtils.readLicensesFromString(licenseOutput); ESLicenses esLicensesOutput = LicenseUtils.readLicensesFromString(licenseOutput);

View File

@ -6,13 +6,11 @@
package org.elasticsearch.license.licensor; package org.elasticsearch.license.licensor;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.elasticsearch.license.AbstractLicensingTestBase;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseUtils; import org.elasticsearch.license.core.LicenseUtils;
import org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool;
import org.elasticsearch.license.licensor.tools.LicenseVerificationTool; import org.elasticsearch.license.licensor.tools.LicenseVerificationTool;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
@ -21,30 +19,7 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class LicenseVerificationToolTests { public class LicenseVerificationToolTests extends AbstractLicensingTestBase {
private static String pubKeyPath = null;
private static String priKeyPath = null;
@BeforeClass
public static void setup() throws IOException {
// Generate temp KeyPair spec
File privateKeyFile = File.createTempFile("privateKey", ".key");
File publicKeyFile = File.createTempFile("publicKey", ".key");
LicenseVerificationToolTests.pubKeyPath = publicKeyFile.getAbsolutePath();
LicenseVerificationToolTests.priKeyPath = privateKeyFile.getAbsolutePath();
assert privateKeyFile.delete();
assert publicKeyFile.delete();
// Generate keyPair
String[] args = new String[4];
args[0] = "--publicKeyPath";
args[1] = LicenseVerificationToolTests.pubKeyPath;
args[2] = "--privateKeyPath";
args[3] = LicenseVerificationToolTests.priKeyPath;
KeyPairGeneratorTool.main(args);
}
@Test @Test
public void testEffectiveLicenseGeneration() throws Exception { public void testEffectiveLicenseGeneration() throws Exception {
@ -63,13 +38,7 @@ public class LicenseVerificationToolTests {
signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map)); signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map));
String secondLicenseFile = getAsFilePath(signedLicense); String secondLicenseFile = getAsFilePath(signedLicense);
String[] args = new String[4]; String effectiveLicenseStr = runLicenseVerificationTool(new String[]{firstLicenseFile, secondLicenseFile});
args[0] = "--licensesFiles";
args[1] = firstLicenseFile + ":" + secondLicenseFile;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
String effectiveLicenseStr = runLicenseVerificationTool(args);
ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr); ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr);
map.put(ESLicenses.FeatureType.SHIELD, featureWithLongerExpiryDate); map.put(ESLicenses.FeatureType.SHIELD, featureWithLongerExpiryDate);
@ -95,13 +64,7 @@ public class LicenseVerificationToolTests {
signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map)); signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map));
String secondLicenseFile = getAsFilePath(signedLicense); String secondLicenseFile = getAsFilePath(signedLicense);
String[] args = new String[4]; String effectiveLicenseStr = runLicenseVerificationTool(new String[]{firstLicenseFile, secondLicenseFile});
args[0] = "--licensesFiles";
args[1] = firstLicenseFile + ":" + secondLicenseFile;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
String effectiveLicenseStr = runLicenseVerificationTool(args);
ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr); ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr);
// verify that the effective license contains both feature licenses // verify that the effective license contains both feature licenses
@ -134,13 +97,7 @@ public class LicenseVerificationToolTests {
signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map)); signedLicense = runLicenseGenerationTool(TestUtils.generateESLicenses(map));
String secondLicenseFile = getAsFilePath(signedLicense); String secondLicenseFile = getAsFilePath(signedLicense);
String[] args = new String[4]; String effectiveLicenseStr = runLicenseVerificationTool(new String[]{firstLicenseFile, secondLicenseFile});
args[0] = "--licensesFiles";
args[1] = firstLicenseFile + ":" + secondLicenseFile;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
String effectiveLicenseStr = runLicenseVerificationTool(args);
ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr); ESLicenses effectiveLicense = LicenseUtils.readLicensesFromString(effectiveLicenseStr);
map.put(ESLicenses.FeatureType.SHIELD, shieldFeatureWithLongerExpiryDate); map.put(ESLicenses.FeatureType.SHIELD, shieldFeatureWithLongerExpiryDate);
@ -150,7 +107,19 @@ public class LicenseVerificationToolTests {
TestUtils.verifyESLicenses(effectiveLicense, map); TestUtils.verifyESLicenses(effectiveLicense, map);
} }
public static String runLicenseVerificationTool(String[] args) throws IOException { public static String runLicenseVerificationTool(String[] licenseFiles) throws IOException {
StringBuilder licenseFilePathString = new StringBuilder();
for (int i = 0; i < licenseFiles.length; i++) {
licenseFilePathString.append(licenseFiles[i]);
if (i != licenseFiles.length - 1) {
licenseFilePathString.append(":");
}
}
String[] args = new String[4];
args[0] = "--licensesFiles";
args[1] = licenseFilePathString.toString();
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
File temp = File.createTempFile("temp", ".out"); File temp = File.createTempFile("temp", ".out");
temp.deleteOnExit(); temp.deleteOnExit();
try (FileOutputStream outputStream = new FileOutputStream(temp)) { try (FileOutputStream outputStream = new FileOutputStream(temp)) {
@ -159,16 +128,8 @@ public class LicenseVerificationToolTests {
return FileUtils.readFileToString(temp); return FileUtils.readFileToString(temp);
} }
public static String runLicenseGenerationTool(String licenseInput) throws IOException { public String runLicenseGenerationTool(String licenseInput) throws IOException {
String args[] = new String[6]; return TestUtils.runLicenseGenerationTool(licenseInput, pubKeyPath, priKeyPath);
args[0] = "--license";
args[1] = licenseInput;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
return TestUtils.runLicenseGenerationTool(args);
} }
private static String getAsFilePath(String content) throws IOException { private static String getAsFilePath(String content) throws IOException {

View File

@ -6,52 +6,39 @@
package org.elasticsearch.license.manager; package org.elasticsearch.license.manager;
import net.nicholaswilliams.java.licensing.exception.InvalidLicenseException; import net.nicholaswilliams.java.licensing.exception.InvalidLicenseException;
import org.elasticsearch.license.AbstractLicensingTestBase;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders; import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool; import org.junit.After;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collections; import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.license.core.ESLicenses.FeatureType; import static org.elasticsearch.license.core.ESLicenses.FeatureType;
import static org.elasticsearch.license.core.LicenseUtils.readLicensesFromString; import static org.elasticsearch.license.core.LicenseUtils.readLicensesFromString;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class LicenseVerificationTests { public class LicenseVerificationTests extends AbstractLicensingTestBase {
private static String pubKeyPath = null; private static ESLicenseManager esLicenseManager;
private static String priKeyPath = null;
private final static ESLicenses EMPTY_LICENSES = LicenseBuilders.licensesBuilder().build();
@BeforeClass @BeforeClass
public static void setup() throws IOException { public static void setupManager() {
esLicenseManager = ESLicenseManager.createLocalBasedInstance(LicenseBuilders.licensesBuilder().build(), pubKeyPath);
// Generate temp KeyPair spec
File privateKeyFile = File.createTempFile("privateKey", ".key");
File publicKeyFile = File.createTempFile("publicKey", ".key");
LicenseVerificationTests.pubKeyPath = publicKeyFile.getAbsolutePath();
LicenseVerificationTests.priKeyPath = privateKeyFile.getAbsolutePath();
assert privateKeyFile.delete();
assert publicKeyFile.delete();
// Generate keyPair
String[] args = new String[4];
args[0] = "--publicKeyPath";
args[1] = LicenseVerificationTests.pubKeyPath;
args[2] = "--privateKeyPath";
args[3] = LicenseVerificationTests.priKeyPath;
KeyPairGeneratorTool.main(args);
} }
@After
public void clearManager() {
esLicenseManager.clearAndAddLicenses(EMPTY_LICENSES);
}
@Test @Test
public void testGeneratedLicenses() throws Exception { public void testGeneratedLicenses() throws Exception {
Date issueDate = new Date(); Date issueDate = new Date();
@ -62,25 +49,14 @@ public class LicenseVerificationTests {
new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr); new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr);
map.put(FeatureType.SHIELD, featureAttributes); map.put(FeatureType.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map); ESLicenses esLicensesOutput = readLicensesFromString(generateSignedLicenses(map));
String[] args = new String[6]; esLicenseManager.clearAndAddLicenses(esLicensesOutput);
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
ESLicenses esLicensesOutput = readLicensesFromString(licenseOutput);
ESLicenseManager esLicenseManager = new ESLicenseManager(esLicensesOutput, pubKeyPath);
esLicenseManager.verifyLicenses(); esLicenseManager.verifyLicenses();
verifyLicenseManager(esLicenseManager, map); verifyLicenseManager(esLicenseManager, map);
} }
@Test @Test
@ -97,59 +73,66 @@ public class LicenseVerificationTests {
map.put(FeatureType.SHIELD, shildFeatureAttributes); map.put(FeatureType.SHIELD, shildFeatureAttributes);
map.put(FeatureType.MARVEL, marvelFeatureAttributes); map.put(FeatureType.MARVEL, marvelFeatureAttributes);
String licenseString = TestUtils.generateESLicenses(map); ESLicenses esLicensesOutput = readLicensesFromString(generateSignedLicenses(map));
String[] args = new String[6]; esLicenseManager.clearAndAddLicenses(esLicensesOutput);
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args); //printLicense(esLicenseManager.getEffectiveLicenses());
ESLicenses esLicensesOutput = readLicensesFromString(licenseOutput);
ESLicenseManager esLicenseManager = new ESLicenseManager(esLicensesOutput, pubKeyPath);
esLicenseManager.verifyLicenses(); esLicenseManager.verifyLicenses();
verifyLicenseManager(esLicenseManager, map); verifyLicenseManager(esLicenseManager, map);
} }
private static Date getDateBeforeDays(Date originalDate, int days) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTimeZone(DateUtils.TIME_ZONE);
calendar.setTimeInMillis(originalDate.getTime());
int originalDays = calendar.get(Calendar.DAY_OF_YEAR);
calendar.set(Calendar.DAY_OF_YEAR, originalDays - days);
return calendar.getTime();
}
private static Date getDateAfterDays(Date originalDate, int days) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTimeZone(DateUtils.TIME_ZONE);
calendar.setTimeInMillis(originalDate.getTime());
calendar.add(Calendar.DAY_OF_YEAR, days);
return calendar.getTime();
}
@Test @Test
public void testLicenseExpiry() throws Exception { public void testLicenseExpiry() throws Exception {
Date issueDate = new Date(); Date issueDate = getDateBeforeDays(new Date(), 60);
Date expiryDate = getDateAfterDays(new Date(), 30);
Date expiredExpiryDate = getDateBeforeDays(new Date(), 10);
String issueDateStr = DateUtils.dateStringFromLongDate(issueDate.getTime()); String issueDateStr = DateUtils.dateStringFromLongDate(issueDate.getTime());
String expiryDateStr = DateUtils.dateStringFromLongDate(DateUtils.longExpiryDateFromDate(issueDate.getTime() + 24 * 60 * 60l)); String expiryDateStr = DateUtils.dateStringFromLongDate(DateUtils.longExpiryDateFromDate(expiryDate.getTime()));
String expiredExpiryDateStr = DateUtils.dateStringFromLongDate(DateUtils.longExpiryDateFromDate(issueDate.getTime() - 5 * 24 * 60 * 60 * 1000l)); final long longExpiryDateFromDate = DateUtils.longExpiryDateFromDate(expiredExpiryDate.getTime());
assert longExpiryDateFromDate < System.currentTimeMillis();
String expiredExpiryDateStr = DateUtils.dateStringFromLongDate(longExpiryDateFromDate);
Map<FeatureType, TestUtils.FeatureAttributes> map = new HashMap<>(); Map<FeatureType, TestUtils.FeatureAttributes> map = new HashMap<>();
TestUtils.FeatureAttributes shildFeatureAttributes = TestUtils.FeatureAttributes shildFeatureAttributes =
new TestUtils.FeatureAttributes("shield", "trial", "none", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr); new TestUtils.FeatureAttributes("shield", "trial", "none", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr);
TestUtils.FeatureAttributes marvelFeatureAttributes = TestUtils.FeatureAttributes marvelFeatureAttributes =
new TestUtils.FeatureAttributes("marvel", "subscription", "silver", "foo1 bar Inc.", "elasticsearc3h", 10, issueDateStr, expiredExpiryDateStr); new TestUtils.FeatureAttributes("marvel", "internal", "silver", "foo1 bar Inc.", "elasticsearc3h", 10, issueDateStr, expiredExpiryDateStr);
map.put(FeatureType.SHIELD, shildFeatureAttributes); map.put(FeatureType.SHIELD, shildFeatureAttributes);
map.put(FeatureType.MARVEL, marvelFeatureAttributes); map.put(FeatureType.MARVEL, marvelFeatureAttributes);
String licenseString = TestUtils.generateESLicenses(map); ESLicenses esLicensesOutput = readLicensesFromString(generateSignedLicenses(map));
String[] args = new String[6]; esLicenseManager.clearAndAddLicenses(esLicensesOutput);
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
ESLicenses esLicensesOutput = readLicensesFromString(licenseOutput);
ESLicenseManager esLicenseManager = new ESLicenseManager(esLicensesOutput, pubKeyPath);
// All validation for shield license should be normal as expected // All validation for shield license should be normal as expected
verifyLicenseManager(esLicenseManager, Collections.singletonMap(FeatureType.SHIELD, shildFeatureAttributes)); verifyLicenseManager(esLicenseManager, Collections.singletonMap(FeatureType.SHIELD, shildFeatureAttributes));
@ -168,19 +151,7 @@ public class LicenseVerificationTests {
new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr); new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, issueDateStr, expiryDateStr);
map.put(FeatureType.SHIELD, featureAttributes); map.put(FeatureType.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map); ESLicenses esLicensesOutput = readLicensesFromString(generateSignedLicenses(map));
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
ESLicenses esLicensesOutput = readLicensesFromString(licenseOutput);
ESLicenses.ESLicense esLicense = esLicensesOutput.get(FeatureType.SHIELD); ESLicenses.ESLicense esLicense = esLicensesOutput.get(FeatureType.SHIELD);
@ -194,14 +165,13 @@ public class LicenseVerificationTests {
ESLicenses tamperedLicenses = LicenseBuilders.licensesBuilder().license(tamperedLicense).build(); ESLicenses tamperedLicenses = LicenseBuilders.licensesBuilder().license(tamperedLicense).build();
ESLicenseManager esLicenseManager = null;
try { try {
esLicenseManager = new ESLicenseManager(tamperedLicenses, pubKeyPath); esLicenseManager.clearAndAddLicenses(tamperedLicenses);
assertTrue("License manager should always report the original (signed) expiry date", esLicenseManager.getExpiryDateForLicense(FeatureType.SHIELD) == originalExpiryDate); assertTrue("License manager should always report the original (signed) expiry date of: " + originalExpiryDate + " but got: " + esLicenseManager.getExpiryDateForLicense(FeatureType.SHIELD), esLicenseManager.getExpiryDateForLicense(FeatureType.SHIELD) == originalExpiryDate);
esLicenseManager.verifyLicenses(); esLicenseManager.verifyLicenses();
fail(); fail();
} catch (InvalidLicenseException e) { } catch (InvalidLicenseException e) {
assertTrue("Exception should contain 'Invalid License' ", e.getMessage().contains("Invalid License")); assertTrue("Exception should contain 'Invalid License' but got: " + e.getMessage(), e.getMessage().contains("Invalid License"));
} }
} }
@ -213,12 +183,12 @@ public class LicenseVerificationTests {
assertTrue("License should have issuedTo of " + featureAttributes.issuedTo, esLicenseManager.getIssuedToForLicense(featureType).equals(featureAttributes.issuedTo)); assertTrue("License should have issuedTo of " + featureAttributes.issuedTo, esLicenseManager.getIssuedToForLicense(featureType).equals(featureAttributes.issuedTo));
assertTrue("License should have issuer of " + featureAttributes.issuer, esLicenseManager.getIssuerForLicense(featureType).equals(featureAttributes.issuer)); assertTrue("License should have issuer of " + featureAttributes.issuer, esLicenseManager.getIssuerForLicense(featureType).equals(featureAttributes.issuer));
assertTrue("License should have issue date of " + DateUtils.longFromDateString(featureAttributes.issueDate), esLicenseManager.getIssueDateForLicense(featureType) == DateUtils.longFromDateString(featureAttributes.issueDate)); assertTrue("License should have issue date of " + DateUtils.longFromDateString(featureAttributes.issueDate), esLicenseManager.getIssueDateForLicense(featureType) == DateUtils.longFromDateString(featureAttributes.issueDate));
assertTrue("License should have expiry date of " + DateUtils.longExpiryDateFromString(featureAttributes.expiryDate), esLicenseManager.getExpiryDateForLicense(featureType) == DateUtils.longExpiryDateFromString(featureAttributes.expiryDate)); assertTrue("License should have expiry date of " + DateUtils.longExpiryDateFromString(featureAttributes.expiryDate) + " got: " + esLicenseManager.getExpiryDateForLicense(featureType), esLicenseManager.getExpiryDateForLicense(featureType) == DateUtils.longExpiryDateFromString(featureAttributes.expiryDate));
assertTrue("License should have type of " + featureAttributes.featureType, esLicenseManager.getTypeForLicense(featureType) == ESLicenses.Type.fromString(featureAttributes.type)); assertTrue("License should have type of " + featureAttributes.type + " got: " + esLicenseManager.getTypeForLicense(featureType).string(), esLicenseManager.getTypeForLicense(featureType) == ESLicenses.Type.fromString(featureAttributes.type));
assertTrue("License should have subscription type of " + featureAttributes.subscriptionType, esLicenseManager.getSubscriptionTypeForLicense(featureType) == ESLicenses.SubscriptionType.fromString(featureAttributes.subscriptionType)); assertTrue("License should have subscription type of " + featureAttributes.subscriptionType, esLicenseManager.getSubscriptionTypeForLicense(featureType) == ESLicenses.SubscriptionType.fromString(featureAttributes.subscriptionType));
assertTrue("License should be valid for shield", esLicenseManager.hasLicenseForFeature(featureType)); assertTrue("License should be valid for " + featureType.string(), esLicenseManager.hasLicenseForFeature(featureType));
assertTrue("License should be valid for maxNodes = " + (featureAttributes.maxNodes - 1), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes - 1)); assertTrue("License should be valid for maxNodes = " + (featureAttributes.maxNodes - 1), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes - 1));
assertTrue("License should be valid for maxNodes = " + (featureAttributes.maxNodes), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes)); assertTrue("License should be valid for maxNodes = " + (featureAttributes.maxNodes), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes));
assertFalse("License should not be valid for maxNodes = " + (featureAttributes.maxNodes + 1), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes + 1)); assertFalse("License should not be valid for maxNodes = " + (featureAttributes.maxNodes + 1), esLicenseManager.hasLicenseForNodes(featureType, featureAttributes.maxNodes + 1));

View File

@ -6,41 +6,32 @@
package org.elasticsearch.license.plugin; package org.elasticsearch.license.plugin;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionModule;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.core.LicenseUtils; import org.elasticsearch.license.core.LicenseUtils;
import org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseAction; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse;
import org.elasticsearch.license.plugin.action.delete.TransportDeleteLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseRequest; import org.elasticsearch.license.plugin.action.get.GetLicenseRequest;
import org.elasticsearch.license.plugin.action.get.GetLicenseResponse; import org.elasticsearch.license.plugin.action.get.GetLicenseResponse;
import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction; import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest; import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseResponse; import org.elasticsearch.license.plugin.action.put.PutLicenseResponse;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesMetaData;
import org.elasticsearch.license.plugin.rest.RestGetLicenseAction;
import org.elasticsearch.license.plugin.rest.RestPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.rest.RestModule;
import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.InternalTestCluster;
import org.junit.After;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -55,7 +46,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
@ClusterScope(scope = SUITE, numDataNodes = 10) @ClusterScope(scope = SUITE, numDataNodes = 10)
public class LicenseTransportTests extends ElasticsearchIntegrationTest { public class LicenseTransportTests extends ElasticsearchIntegrationTest {
private static String pubKeyPath = null; private static String pubKeyPath = null;
private static String priKeyPath = null; private static String priKeyPath = null;
@ -68,33 +58,29 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
} }
@BeforeClass @BeforeClass
public static void setup() throws IOException { public static void setup() throws IOException, URISyntaxException {
priKeyPath = Paths.get(LicenseTransportTests.class.getResource("/org.elasticsearch.license.plugin/test_pri.key").toURI()).toAbsolutePath().toString();
// Generate temp KeyPair spec pubKeyPath = Paths.get(LicenseTransportTests.class.getResource("/org.elasticsearch.license.plugin/test_pub.key").toURI()).toAbsolutePath().toString();
File privateKeyFile = File.createTempFile("privateKey", ".key");
File publicKeyFile = File.createTempFile("publicKey", ".key");
LicenseTransportTests.pubKeyPath = publicKeyFile.getAbsolutePath();
LicenseTransportTests.priKeyPath = privateKeyFile.getAbsolutePath();
assert privateKeyFile.delete();
assert publicKeyFile.delete();
// Generate keyPair
String[] args = new String[4];
args[0] = "--publicKeyPath";
args[1] = LicenseTransportTests.pubKeyPath;
args[2] = "--privateKeyPath";
args[3] = LicenseTransportTests.priKeyPath;
KeyPairGeneratorTool.main(args);
} }
/*
* TODO:
* - add more delete tests
* - add put invalid licenses tests
* - add multiple licenses of the same feature tests
*/
@Test @Test
public void testEmptyGetLicense() throws Exception { public void testEmptyGetLicense() throws Exception {
final ActionFuture<DeleteLicenseResponse> deleteFuture = licenseDeleteAction().execute(new DeleteLicenseRequest("marvel", "shield"));
final DeleteLicenseResponse deleteLicenseResponse = deleteFuture.get();
assertTrue(deleteLicenseResponse.isAcknowledged());
final ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest()); final ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest());
final GetLicenseResponse getLicenseResponse = getLicenseFuture.get(); final GetLicenseResponse getLicenseResponse = getLicenseFuture.get();
//TODO assertThat("expected 0 licenses; but got: " + getLicenseResponse.licenses(), getLicenseResponse.licenses().licenses().size(), equalTo(0));
//assertThat(getLicenseResponse.licenses(), nullValue());
} }
@Test @Test
@ -104,25 +90,14 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
TestUtils.FeatureAttributes featureAttributes = TestUtils.FeatureAttributes featureAttributes =
new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, "2014-12-13", "2015-12-13"); new TestUtils.FeatureAttributes("shield", "subscription", "platinum", "foo bar Inc.", "elasticsearch", 2, "2014-12-13", "2015-12-13");
map.put(ESLicenses.FeatureType.SHIELD, featureAttributes); map.put(ESLicenses.FeatureType.SHIELD, featureAttributes);
String licenseString = TestUtils.generateESLicenses(map); String licenseString = TestUtils.generateESLicenses(map);
String licenseOutput = TestUtils.runLicenseGenerationTool(licenseString, pubKeyPath, priKeyPath);
String[] args = new String[6];
args[0] = "--license";
args[1] = licenseString;
args[2] = "--publicKeyPath";
args[3] = pubKeyPath;
args[4] = "--privateKeyPath";
args[5] = priKeyPath;
String licenseOutput = TestUtils.runLicenseGenerationTool(args);
PutLicenseRequest putLicenseRequest = new PutLicenseRequest(); PutLicenseRequest putLicenseRequest = new PutLicenseRequest();
//putLicenseRequest.license(licenseString); //putLicenseRequest.license(licenseString);
final ESLicenses putLicenses = LicenseUtils.readLicensesFromString(licenseOutput); final ESLicenses putLicenses = LicenseUtils.readLicensesFromString(licenseOutput);
putLicenseRequest.license(putLicenses); putLicenseRequest.license(putLicenses);
LicenseUtils.printLicense(putLicenses); //LicenseUtils.printLicense(putLicenses);
ensureGreen(); ensureGreen();
final ActionFuture<PutLicenseResponse> putLicenseFuture = licensePutAction().execute(putLicenseRequest); final ActionFuture<PutLicenseResponse> putLicenseFuture = licensePutAction().execute(putLicenseRequest);
@ -131,13 +106,22 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true));
final ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest()); ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest());
final GetLicenseResponse getLicenseResponse = getLicenseFuture.get(); GetLicenseResponse getLicenseResponse = getLicenseFuture.get();
assertThat(getLicenseResponse.licenses(), notNullValue()); assertThat(getLicenseResponse.licenses(), notNullValue());
LicenseUtils.printLicense(getLicenseResponse.licenses()); //LicenseUtils.printLicense(getLicenseResponse.licenses());
assertTrue(isSame(putLicenses, getLicenseResponse.licenses()));
final ActionFuture<DeleteLicenseResponse> deleteFuture = licenseDeleteAction().execute(new DeleteLicenseRequest("marvel", "shield"));
final DeleteLicenseResponse deleteLicenseResponse = deleteFuture.get();
assertTrue(deleteLicenseResponse.isAcknowledged());
getLicenseResponse = licenseGetAction().execute(new GetLicenseRequest()).get();
assertTrue(isSame(getLicenseResponse.licenses(), LicenseBuilders.licensesBuilder().build()));
} }
public TransportGetLicenseAction licenseGetAction() { public TransportGetLicenseAction licenseGetAction() {
@ -149,4 +133,42 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
final InternalTestCluster clients = internalCluster(); final InternalTestCluster clients = internalCluster();
return clients.getInstance(TransportPutLicenseAction.class); return clients.getInstance(TransportPutLicenseAction.class);
} }
public TransportDeleteLicenseAction licenseDeleteAction() {
final InternalTestCluster clients = internalCluster();
return clients.getInstance(TransportDeleteLicenseAction.class);
}
//TODO: convert to asserts
public static boolean isSame(ESLicenses firstLicenses, ESLicenses secondLicenses) {
// we do the build to make sure we weed out any expired licenses
final ESLicenses licenses1 = LicenseBuilders.licensesBuilder().licenses(firstLicenses).build();
final ESLicenses licenses2 = LicenseBuilders.licensesBuilder().licenses(secondLicenses).build();
// check if the effective licenses have the same feature set
if (!licenses1.features().equals(licenses2.features())) {
return false;
}
// for every feature license, check if all the attributes are the same
for (ESLicenses.FeatureType featureType : licenses1.features()) {
ESLicenses.ESLicense license1 = licenses1.get(featureType);
ESLicenses.ESLicense license2 = licenses2.get(featureType);
if (!license1.uid().equals(license2.uid())
|| !license1.feature().string().equals(license2.feature().string())
|| !license1.subscriptionType().string().equals(license2.subscriptionType().string())
|| !license1.type().string().equals(license2.type().string())
|| !license1.issuedTo().equals(license2.issuedTo())
|| !license1.signature().equals(license2.signature())
|| license1.expiryDate() != license2.expiryDate()
|| license1.issueDate() != license2.issueDate()
|| license1.maxNodes() != license2.maxNodes()) {
return false;
}
}
return true;
}
} }