Remove LicensingCharset & reformat code

Original commit: elastic/x-pack-elasticsearch@be5dc1c817
This commit is contained in:
Areek Zillur 2014-11-05 10:53:33 -05:00
parent 1eec394f8f
commit 9b30af3958
31 changed files with 106 additions and 125 deletions

View File

@ -29,7 +29,7 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
private final int maxNodes; private final int maxNodes;
private ESLicense(String uid, String issuer, String issuedTo, long issueDate, String type, private ESLicense(String uid, String issuer, String issuedTo, long issueDate, String type,
String subscriptionType, String feature, String signature, long expiryDate, int maxNodes) { String subscriptionType, String feature, String signature, long expiryDate, int maxNodes) {
this.uid = uid; this.uid = uid;
this.issuer = issuer; this.issuer = issuer;
this.issuedTo = issuedTo; this.issuedTo = issuedTo;
@ -379,25 +379,25 @@ public class ESLicense implements Comparable<ESLicense>, ToXContent {
public Builder verify() { public Builder verify() {
if (issuer == null) { if (issuer == null) {
throw new IllegalStateException("issuer can not be null"); throw new IllegalStateException("issuer can not be null");
} else if (issuedTo == null) { } else if (issuedTo == null) {
throw new IllegalStateException("issuedTo can not be null"); throw new IllegalStateException("issuedTo can not be null");
} else if (issueDate == -1) { } else if (issueDate == -1) {
throw new IllegalStateException("issueDate has to be set"); throw new IllegalStateException("issueDate has to be set");
} else if (type == null) { } else if (type == null) {
throw new IllegalStateException("type can not be null"); throw new IllegalStateException("type can not be null");
} else if (subscriptionType == null) { } else if (subscriptionType == null) {
throw new IllegalStateException("subscriptionType can not be null"); throw new IllegalStateException("subscriptionType can not be null");
} else if (uid == null) { } else if (uid == null) {
throw new IllegalStateException("uid can not be null"); throw new IllegalStateException("uid can not be null");
} else if (feature == null) { } else if (feature == null) {
throw new IllegalStateException("at least one feature has to be enabled"); throw new IllegalStateException("at least one feature has to be enabled");
} else if (signature == null) { } else if (signature == null) {
throw new IllegalStateException("signature can not be null"); throw new IllegalStateException("signature can not be null");
} else if (maxNodes == -1) { } else if (maxNodes == -1) {
throw new IllegalStateException("maxNodes has to be set"); throw new IllegalStateException("maxNodes has to be set");
} else if (expiryDate == -1) { } else if (expiryDate == -1) {
throw new IllegalStateException("expiryDate has to be set"); throw new IllegalStateException("expiryDate has to be set");
} }
return this; return this;
} }

View File

@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.*; import org.elasticsearch.common.xcontent.*;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
public class ESLicenses { public class ESLicenses {
@ -37,7 +38,7 @@ public class ESLicenses {
} }
public static List<ESLicense> fromSource(String content) throws IOException { public static List<ESLicense> fromSource(String content) throws IOException {
return fromSource(content.getBytes(LicensesCharset.UTF_8), true); return fromSource(content.getBytes(StandardCharsets.UTF_8), true);
} }
public static List<ESLicense> fromSource(byte[] bytes) throws IOException { public static List<ESLicense> fromSource(byte[] bytes) throws IOException {

View File

@ -1,17 +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.core;
import java.nio.charset.Charset;
public class LicensesCharset {
/**
* All operations in should use the universal UTF-8 character set.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
}

View File

@ -24,7 +24,7 @@ public class ResourcePublicKeyDataProvider implements PublicKeyDataProvider {
@Override @Override
public byte[] getEncryptedPublicKeyData() throws KeyNotFoundException { public byte[] getEncryptedPublicKeyData() throws KeyNotFoundException {
try(InputStream inputStream = this.getClass().getResourceAsStream(resource)) { try (InputStream inputStream = this.getClass().getResourceAsStream(resource)) {
return Streams.copyToByteArray(inputStream); return Streams.copyToByteArray(inputStream);
} catch (IOException ex) { } catch (IOException ex) {
throw new KeyNotFoundException(ex); throw new KeyNotFoundException(ex);

View File

@ -18,10 +18,10 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.LicensesCharset;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -110,7 +110,7 @@ public class ESLicenseSigner {
final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license); final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license);
final byte[] hash = Hasher.hash(Base64.encodeBase64String( final byte[] hash = Hasher.hash(Base64.encodeBase64String(
Files.readAllBytes(publicKeyPath)) Files.readAllBytes(publicKeyPath))
).getBytes(LicensesCharset.UTF_8); ).getBytes(StandardCharsets.UTF_8);
int headerLength = MAGIC_LENGTH + hash.length + 4 + 4; int headerLength = MAGIC_LENGTH + hash.length + 4 + 4;
byte[] bytes = new byte[headerLength + licenseSignature.length]; byte[] bytes = new byte[headerLength + licenseSignature.length];

View File

@ -12,12 +12,12 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicensesCharset;
import org.elasticsearch.license.licensor.ESLicenseSigner; import org.elasticsearch.license.licensor.ESLicenseSigner;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.ParseException; import java.text.ParseException;
@ -48,7 +48,7 @@ public class LicenseGeneratorTool {
switch (command) { switch (command) {
case "--license": case "--license":
String licenseInput = args[++i]; String licenseInput = args[++i];
licenseSpecs.addAll(ESLicenses.fromSource(licenseInput.getBytes(LicensesCharset.UTF_8), false)); licenseSpecs.addAll(ESLicenses.fromSource(licenseInput.getBytes(StandardCharsets.UTF_8), false));
break; break;
case "--licenseFile": case "--licenseFile":
File licenseFile = new File(args[++i]); File licenseFile = new File(args[++i]);

View File

@ -5,7 +5,6 @@
*/ */
package org.elasticsearch.license.plugin.action.get; package org.elasticsearch.license.plugin.action.get;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest; import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;

View File

@ -28,7 +28,7 @@ public class TransportGetLicenseAction extends TransportMasterNodeReadOperationA
@Inject @Inject
public TransportGetLicenseAction(Settings settings, TransportService transportService, ClusterService clusterService, LicensesManagerService licensesManagerService, public TransportGetLicenseAction(Settings settings, TransportService transportService, ClusterService clusterService, LicensesManagerService licensesManagerService,
ThreadPool threadPool, ActionFilters actionFilters) { ThreadPool threadPool, ActionFilters actionFilters) {
super(settings, GetLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters); super(settings, GetLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters);
this.licensesManagerService = licensesManagerService; this.licensesManagerService = licensesManagerService;
} }

View File

@ -31,6 +31,7 @@ public class PutLicenseRequest extends AcknowledgedRequest<PutLicenseRequest> {
/** /**
* Parses licenses from json format to an instance of {@link org.elasticsearch.license.core.ESLicenses} * Parses licenses from json format to an instance of {@link org.elasticsearch.license.core.ESLicenses}
*
* @param licenseDefinition licenses definition * @param licenseDefinition licenses definition
*/ */
public PutLicenseRequest licenses(String licenseDefinition) { public PutLicenseRequest licenses(String licenseDefinition) {

View File

@ -11,15 +11,11 @@ import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.ElasticsearchLicenseException;
import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;

View File

@ -27,10 +27,11 @@ public interface LicensesClientService {
/** /**
* Registers a feature for licensing * Registers a feature for licensing
* @param feature - name of the feature to register (must be in sync with license Generator feature name) *
* @param feature - name of the feature to register (must be in sync with license Generator feature name)
* @param trialLicenseOptions - Trial license specification used to generate a one-time trial license for the feature; * @param trialLicenseOptions - Trial license specification used to generate a one-time trial license for the feature;
* use <code>null</code> if no trial license should be generated for the feature * use <code>null</code> if no trial license should be generated for the feature
* @param listener - used to notify on feature enable/disable and specify trial license specification * @param listener - used to notify on feature enable/disable and specify trial license specification
*/ */
void register(String feature, TrialLicenseOptions trialLicenseOptions, Listener listener); void register(String feature, TrialLicenseOptions trialLicenseOptions, Listener listener);
} }

View File

@ -13,9 +13,7 @@ import org.elasticsearch.license.core.ESLicense;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.license.plugin.core.LicensesService.DeleteLicenseRequestHolder; import static org.elasticsearch.license.plugin.core.LicensesService.*;
import static org.elasticsearch.license.plugin.core.LicensesService.LicensesUpdateResponse;
import static org.elasticsearch.license.plugin.core.LicensesService.PutLicenseRequestHolder;
@ImplementedBy(LicensesService.class) @ImplementedBy(LicensesService.class)
public interface LicensesManagerService { public interface LicensesManagerService {

View File

@ -20,7 +20,6 @@ import java.util.Set;
/** /**
* Contains metadata about registered licenses * Contains metadata about registered licenses
*
*/ */
public class LicensesMetaData implements MetaData.Custom { public class LicensesMetaData implements MetaData.Custom {
@ -39,7 +38,7 @@ public class LicensesMetaData implements MetaData.Custom {
/** /**
* Constructs new licenses metadata * Constructs new licenses metadata
* *
* @param signatures set of esLicense signatures * @param signatures set of esLicense signatures
* @param encodedTrialLicenses set of encoded trial licenses * @param encodedTrialLicenses set of encoded trial licenses
*/ */
public LicensesMetaData(Set<String> signatures, Set<String> encodedTrialLicenses) { public LicensesMetaData(Set<String> signatures, Set<String> encodedTrialLicenses) {
@ -177,7 +176,7 @@ public class LicensesMetaData implements MetaData.Custom {
@Override @Override
public void toXContent(LicensesMetaData licensesMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException { public void toXContent(LicensesMetaData licensesMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.array(Fields.LICENSES, licensesMetaData.signatures.toArray(new String[licensesMetaData.signatures.size()])); builder.array(Fields.LICENSES, licensesMetaData.signatures.toArray(new String[licensesMetaData.signatures.size()]));
builder.array(Fields.TRIAL_LICENSES, licensesMetaData.encodedTrialLicenses.toArray(new String [licensesMetaData.encodedTrialLicenses.size()])); builder.array(Fields.TRIAL_LICENSES, licensesMetaData.encodedTrialLicenses.toArray(new String[licensesMetaData.encodedTrialLicenses.size()]));
} }
@Override @Override

View File

@ -50,7 +50,7 @@ import static org.elasticsearch.license.core.ESLicenses.reduceAndMap;
* Interfaces through which this is exposed are: * Interfaces through which this is exposed are:
* - LicensesManagerService - responsible for managing signed and one-time-trial licenses * - LicensesManagerService - responsible for managing signed and one-time-trial licenses
* - LicensesClientService - responsible for feature registration and notification to consumer plugin(s) * - LicensesClientService - responsible for feature registration and notification to consumer plugin(s)
* * <p/>
* <p/> * <p/>
* Registration Scheme: * Registration Scheme:
* <p/> * <p/>
@ -59,7 +59,7 @@ import static org.elasticsearch.license.core.ESLicenses.reduceAndMap;
* If the feature can not be registered immediately, it is queued up and registered on the first clusterChanged event with * If the feature can not be registered immediately, it is queued up and registered on the first clusterChanged event with
* no {@link GatewayService#STATE_NOT_RECOVERED_BLOCK} block * no {@link GatewayService#STATE_NOT_RECOVERED_BLOCK} block
* Upon successful registration, the feature(s) are notified appropriately using the notification scheme * Upon successful registration, the feature(s) are notified appropriately using the notification scheme
* * <p/>
* <p/> * <p/>
* Notification Scheme: * Notification Scheme:
* <p/> * <p/>
@ -431,11 +431,10 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
/** /**
* Calls {@link #notifyFeaturesAndScheduleNotification(LicensesMetaData)} with <code>currentLicensesMetaData</code> * Calls {@link #notifyFeaturesAndScheduleNotification(LicensesMetaData)} with <code>currentLicensesMetaData</code>
* if it was not already notified on. * if it was not already notified on.
* * <p/>
* Upon completion sets <code>currentLicensesMetaData</code> to {@link #lastObservedLicensesState} * Upon completion sets <code>currentLicensesMetaData</code> to {@link #lastObservedLicensesState}
* to ensure the same license(s) are not notified on from * to ensure the same license(s) are not notified on from
* {@link #clusterChanged(ClusterChangedEvent)} * {@link #clusterChanged(ClusterChangedEvent)}
*
*/ */
private void notifyFeaturesAndScheduleNotificationIfNeeded(final LicensesMetaData currentLicensesMetaData) { private void notifyFeaturesAndScheduleNotificationIfNeeded(final LicensesMetaData currentLicensesMetaData) {
final LicensesMetaData lastNotifiedLicensesMetaData = lastObservedLicensesState.get(); final LicensesMetaData lastNotifiedLicensesMetaData = lastObservedLicensesState.get();
@ -461,6 +460,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
/** /**
* Checks license expiry for all the registered feature(s) * Checks license expiry for all the registered feature(s)
*
* @return -1 if there are no expiring license(s) for any registered feature(s), else * @return -1 if there are no expiring license(s) for any registered feature(s), else
* returns the minimum of the expiry times of all the registered feature(s) to * returns the minimum of the expiry times of all the registered feature(s) to
* schedule an expiry notification * schedule an expiry notification

View File

@ -13,6 +13,7 @@ public enum LicensesStatus {
EXPIRED((byte) 2); EXPIRED((byte) 2);
private byte id; private byte id;
LicensesStatus(byte id) { LicensesStatus(byte id) {
this.id = id; this.id = id;
} }

View File

@ -9,9 +9,9 @@ import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.LicensesCharset;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -81,16 +81,16 @@ public class TrialLicenseUtils {
uid = UUID.randomUUID().toString(); uid = UUID.randomUUID().toString();
} }
return ESLicense.builder() return ESLicense.builder()
.type(DEFAULT_TYPE) .type(DEFAULT_TYPE)
.subscriptionType(DEFAULT_SUBSCRIPTION_TYPE) .subscriptionType(DEFAULT_SUBSCRIPTION_TYPE)
.issuer(DEFAULT_ISSUER) .issuer(DEFAULT_ISSUER)
.uid(uid) .uid(uid)
.issuedTo(issuedTo) .issuedTo(issuedTo)
.issueDate(issueDate) .issueDate(issueDate)
.feature(feature) .feature(feature)
.maxNodes(maxNodes) .maxNodes(maxNodes)
.expiryDate(expiryDate) .expiryDate(expiryDate)
.build(); .build();
} }
} }
@ -110,17 +110,17 @@ public class TrialLicenseUtils {
int uidLen = byteBuffer.getInt(); int uidLen = byteBuffer.getInt();
byte[] uidBytes = new byte[uidLen]; byte[] uidBytes = new byte[uidLen];
byteBuffer.get(uidBytes); byteBuffer.get(uidBytes);
String uid = new String(uidBytes, LicensesCharset.UTF_8); String uid = new String(uidBytes, StandardCharsets.UTF_8);
int issuedToLen = byteBuffer.getInt(); int issuedToLen = byteBuffer.getInt();
byte[] issuedToBytes = new byte[issuedToLen]; byte[] issuedToBytes = new byte[issuedToLen];
byteBuffer.get(issuedToBytes); byteBuffer.get(issuedToBytes);
String issuedTo = new String(issuedToBytes, LicensesCharset.UTF_8); String issuedTo = new String(issuedToBytes, StandardCharsets.UTF_8);
int featureLen = byteBuffer.getInt(); int featureLen = byteBuffer.getInt();
byte[] featureBytes = new byte[featureLen]; byte[] featureBytes = new byte[featureLen];
byteBuffer.get(featureBytes); byteBuffer.get(featureBytes);
String feature = new String(featureBytes, LicensesCharset.UTF_8); String feature = new String(featureBytes, StandardCharsets.UTF_8);
int maxNodes = byteBuffer.getInt(); int maxNodes = byteBuffer.getInt();
long issueDate = byteBuffer.getLong(); long issueDate = byteBuffer.getLong();
@ -137,9 +137,9 @@ public class TrialLicenseUtils {
} }
public static String toEncodedTrialLicense(ESLicense trialLicense) { public static String toEncodedTrialLicense(ESLicense trialLicense) {
byte[] uidBytes = trialLicense.uid().getBytes(LicensesCharset.UTF_8); byte[] uidBytes = trialLicense.uid().getBytes(StandardCharsets.UTF_8);
byte[] featureBytes = trialLicense.feature().getBytes(LicensesCharset.UTF_8); byte[] featureBytes = trialLicense.feature().getBytes(StandardCharsets.UTF_8);
byte[] issuedToBytes = trialLicense.issuedTo().getBytes(LicensesCharset.UTF_8); byte[] issuedToBytes = trialLicense.issuedTo().getBytes(StandardCharsets.UTF_8);
// uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate // uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate
int len = 4 + uidBytes.length + 4 + issuedToBytes.length + 4 + featureBytes.length + 4 + 8 + 8; int len = 4 + uidBytes.length + 4 + issuedToBytes.length + 4 + featureBytes.length + 4 + 8 + 8;

View File

@ -33,25 +33,25 @@ public class RestGetLicenseAction extends BaseRestHandler {
/** /**
* Output Format: * Output Format:
* { * {
*   "licenses" : [ *   "licenses" : [
*     { *     {
*       "uid" : ..., *       "uid" : ...,
*       "type" : ..., *       "type" : ...,
*       "subscription_type" :..., *       "subscription_type" :...,
*       "issued_to" : ... (cluster name if one-time trial license, else value from signed license), *       "issued_to" : ... (cluster name if one-time trial license, else value from signed license),
*       "issue_date" : YY-MM-DD (date string in UTC), *       "issue_date" : YY-MM-DD (date string in UTC),
*       "expiry_date" : YY-MM-DD (date string in UTC), *       "expiry_date" : YY-MM-DD (date string in UTC),
*       "feature" : ..., *       "feature" : ...,
*       "max_nodes" : ... *       "max_nodes" : ...
*     }, *     },
* {...} * {...}
*   ] *   ]
* }p * }p
* * <p/>
* There will be only one license displayed per feature, the selected license will have the latest expiry_date * There will be only one license displayed per feature, the selected license will have the latest expiry_date
* out of all other licenses for the feature. * out of all other licenses for the feature.
* * <p/>
* The licenses are sorted by latest issue_date * The licenses are sorted by latest issue_date
*/ */

View File

@ -5,9 +5,6 @@
*/ */
package org.elasticsearch.license.plugin.rest; package org.elasticsearch.license.plugin.rest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;

View File

@ -14,20 +14,22 @@ import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.licensor.ESLicenseSigner; import org.elasticsearch.license.licensor.ESLicenseSigner;
import org.elasticsearch.license.manager.ESLicenseManager; import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.text.ParseException; import java.util.HashSet;
import java.util.*; import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomRealisticUnicodeOfCodepointLengthBetween; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomRealisticUnicodeOfCodepointLengthBetween;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.ElasticsearchTestCase.randomFrom; import static org.elasticsearch.test.ElasticsearchTestCase.randomFrom;
@RunWith(value = com.carrotsearch.randomizedtesting.RandomizedRunner.class) @RunWith(value = com.carrotsearch.randomizedtesting.RandomizedRunner.class)
public class AbstractLicensingTestBase { public class AbstractLicensingTestBase {

View File

@ -7,7 +7,11 @@ package org.elasticsearch.license;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;

View File

@ -15,18 +15,19 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.ParseException; import java.util.Arrays;
import java.util.*; import java.util.List;
import java.util.Set;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class LicenseGenerationToolTests extends AbstractLicensingTestBase { public class LicenseGenerationToolTests extends AbstractLicensingTestBase {

View File

@ -6,24 +6,18 @@
package org.elasticsearch.license.licensor; package org.elasticsearch.license.licensor;
import org.elasticsearch.license.AbstractLicensingTestBase; import org.elasticsearch.license.AbstractLicensingTestBase;
import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicensesCharset;
import org.hamcrest.core.IsNull;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.*; import java.util.*;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue; import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class LicenseSerializationTests extends AbstractLicensingTestBase { public class LicenseSerializationTests extends AbstractLicensingTestBase {
@ -33,7 +27,7 @@ public class LicenseSerializationTests extends AbstractLicensingTestBase {
String issueDate = dateMathString("now", now); String issueDate = dateMathString("now", now);
String expiryDate = dateMathString("now+10d/d", now); String expiryDate = dateMathString("now+10d/d", now);
String licenseSpecs = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("shield", issueDate, expiryDate))); String licenseSpecs = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("shield", issueDate, expiryDate)));
Set<ESLicense> esLicensesOutput = new HashSet<>(ESLicenses.fromSource(licenseSpecs.getBytes(LicensesCharset.UTF_8), false)); Set<ESLicense> esLicensesOutput = new HashSet<>(ESLicenses.fromSource(licenseSpecs.getBytes(StandardCharsets.UTF_8), false));
ESLicense generatedLicense = esLicensesOutput.iterator().next(); ESLicense generatedLicense = esLicensesOutput.iterator().next();
assertThat(esLicensesOutput.size(), equalTo(1)); assertThat(esLicensesOutput.size(), equalTo(1));
@ -51,8 +45,8 @@ public class LicenseSerializationTests extends AbstractLicensingTestBase {
String licenseSpecs = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("shield", shieldIssueDate, shieldExpiryDate))); String licenseSpecs = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("shield", shieldIssueDate, shieldExpiryDate)));
String licenseSpecs1 = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("marvel", marvelIssueDate, marvelExpiryDate))); String licenseSpecs1 = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("marvel", marvelIssueDate, marvelExpiryDate)));
Set<ESLicense> esLicensesOutput = new HashSet<>(); Set<ESLicense> esLicensesOutput = new HashSet<>();
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs.getBytes(LicensesCharset.UTF_8), false)); esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs.getBytes(StandardCharsets.UTF_8), false));
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs1.getBytes(LicensesCharset.UTF_8), false)); esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs1.getBytes(StandardCharsets.UTF_8), false));
assertThat(esLicensesOutput.size(), equalTo(2)); assertThat(esLicensesOutput.size(), equalTo(2));
for (ESLicense esLicense : esLicensesOutput) { for (ESLicense esLicense : esLicensesOutput) {
assertThat(esLicense.issueDate(), equalTo(DateUtils.beginningOfTheDay((esLicense.feature().equals("shield")) ? shieldIssueDate : marvelIssueDate))); assertThat(esLicense.issueDate(), equalTo(DateUtils.beginningOfTheDay((esLicense.feature().equals("shield")) ? shieldIssueDate : marvelIssueDate)));
@ -70,7 +64,7 @@ public class LicenseSerializationTests extends AbstractLicensingTestBase {
ArrayList<LicenseSpec> specs = new ArrayList<>(licenseSpecs.values()); ArrayList<LicenseSpec> specs = new ArrayList<>(licenseSpecs.values());
String licenseSpecsSource = generateESLicenseSpecString(specs); String licenseSpecsSource = generateESLicenseSpecString(specs);
Set<ESLicense> esLicensesOutput = new HashSet<>(ESLicenses.fromSource(licenseSpecsSource.getBytes(LicensesCharset.UTF_8), false)); Set<ESLicense> esLicensesOutput = new HashSet<>(ESLicenses.fromSource(licenseSpecsSource.getBytes(StandardCharsets.UTF_8), false));
assertThat(esLicensesOutput.size(), equalTo(licenseSpecs.size())); assertThat(esLicensesOutput.size(), equalTo(licenseSpecs.size()));
for (ESLicense license : esLicensesOutput) { for (ESLicense license : esLicensesOutput) {

View File

@ -104,8 +104,8 @@ public class LicenseVerificationToolTests extends AbstractLicensingTestBase {
for (ESLicense license : output) { for (ESLicense license : output) {
licensesOutput.add( licensesOutput.add(
ESLicense.builder() ESLicense.builder()
.fromLicenseSpec(license, expectedLicenses.get(license.feature()).signature()) .fromLicenseSpec(license, expectedLicenses.get(license.feature()).signature())
.build() .build()
); );
} }

View File

@ -11,11 +11,14 @@ import org.elasticsearch.license.core.ESLicense;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.*; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
public class LicenseSignatureTest extends AbstractLicensingTestBase { public class LicenseSignatureTest extends AbstractLicensingTestBase {

View File

@ -12,10 +12,12 @@ import org.elasticsearch.license.core.ESLicense;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.*; import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class LicenseVerificationTests extends AbstractLicensingTestBase { public class LicenseVerificationTests extends AbstractLicensingTestBase {

View File

@ -28,7 +28,9 @@ import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.InternalTestCluster;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;

View File

@ -51,7 +51,7 @@ public class LicensesPluginIntegrationTests extends AbstractLicensesIntegrationT
@Test @Test
public void testTrialLicenseAndSignedLicenseNotification() throws Exception { public void testTrialLicenseAndSignedLicenseNotification() throws Exception {
logger.info("using "+ ((useEagerLicenseRegistrationPlugin) ? "eager" : "lazy") + " consumer plugin"); logger.info("using " + ((useEagerLicenseRegistrationPlugin) ? "eager" : "lazy") + " consumer plugin");
logger.info(" --> trial license generated"); logger.info(" --> trial license generated");
// managerService should report feature to be enabled on all data nodes // managerService should report feature to be enabled on all data nodes
assertLicenseManagerEnabledFeatureFor(getCurrentFeatureName()); assertLicenseManagerEnabledFeatureFor(getCurrentFeatureName());

View File

@ -10,10 +10,8 @@ import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.base.Predicate;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.gateway.GatewayService;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.manager.ESLicenseManager; import org.elasticsearch.license.manager.ESLicenseManager;
@ -27,9 +25,7 @@ import java.util.*;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomFrom;
import static org.elasticsearch.license.plugin.core.LicensesService.LicensesUpdateResponse; import static org.elasticsearch.license.plugin.core.LicensesService.LicensesUpdateResponse;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.TEST; import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.TEST;
@ -332,7 +328,8 @@ public class LicensesServiceTests extends AbstractLicensesIntegrationTests {
private Action assertExpiryAction(String feature, String licenseType, TimeValue expiryDuration) { private Action assertExpiryAction(String feature, String licenseType, TimeValue expiryDuration) {
return new Action(new Runnable() { return new Action(new Runnable() {
@Override @Override
public void run() {} public void run() {
}
}, 1, 0, TimeValue.timeValueMillis(expiryDuration.getMillis() * 2), }, 1, 0, TimeValue.timeValueMillis(expiryDuration.getMillis() * 2),
"should trigger onDisable for " + feature + " once [" + licenseType + " license expiry]"); "should trigger onDisable for " + feature + " once [" + licenseType + " license expiry]");
} }

View File

@ -12,7 +12,7 @@ import org.elasticsearch.common.settings.Settings;
/** /**
* Registers licenses upon the start of the service lifecycle * Registers licenses upon the start of the service lifecycle
* see {@link org.elasticsearch.license.plugin.consumer.EagerLicenseRegistrationPluginService} * see {@link org.elasticsearch.license.plugin.consumer.EagerLicenseRegistrationPluginService}
* * <p/>
* License registration might happen before clusterService start() * License registration might happen before clusterService start()
*/ */
public class EagerLicenseRegistrationConsumerPlugin extends TestConsumerPluginBase { public class EagerLicenseRegistrationConsumerPlugin extends TestConsumerPluginBase {

View File

@ -12,7 +12,7 @@ import org.elasticsearch.common.settings.Settings;
/** /**
* Registers licenses only after cluster has recovered * Registers licenses only after cluster has recovered
* see {@link org.elasticsearch.license.plugin.consumer.LazyLicenseRegistrationPluginService} * see {@link org.elasticsearch.license.plugin.consumer.LazyLicenseRegistrationPluginService}
* * <p/>
* License registration happens after clusterservice start() * License registration happens after clusterservice start()
*/ */
public class LazyLicenseRegistrationConsumerPlugin extends TestConsumerPluginBase { public class LazyLicenseRegistrationConsumerPlugin extends TestConsumerPluginBase {