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

@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class ESLicenses {
@ -37,7 +38,7 @@ public class ESLicenses {
}
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 {

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

@ -18,10 +18,10 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.LicensesCharset;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -110,7 +110,7 @@ public class ESLicenseSigner {
final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license);
final byte[] hash = Hasher.hash(Base64.encodeBase64String(
Files.readAllBytes(publicKeyPath))
).getBytes(LicensesCharset.UTF_8);
).getBytes(StandardCharsets.UTF_8);
int headerLength = MAGIC_LENGTH + hash.length + 4 + 4;
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.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicensesCharset;
import org.elasticsearch.license.licensor.ESLicenseSigner;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
@ -48,7 +48,7 @@ public class LicenseGeneratorTool {
switch (command) {
case "--license":
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;
case "--licenseFile":
File licenseFile = new File(args[++i]);

View File

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

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}
*
* @param licenseDefinition licenses definition
*/
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.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject;
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.LicensesService;
import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

View File

@ -27,6 +27,7 @@ public interface LicensesClientService {
/**
* Registers a feature for licensing
*
* @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;
* use <code>null</code> if no trial license should be generated for the feature

View File

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

View File

@ -20,7 +20,6 @@ import java.util.Set;
/**
* Contains metadata about registered licenses
*
*/
public class LicensesMetaData implements MetaData.Custom {

View File

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

View File

@ -13,6 +13,7 @@ public enum LicensesStatus {
EXPIRED((byte) 2);
private byte id;
LicensesStatus(byte 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.unit.TimeValue;
import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.LicensesCharset;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@ -110,17 +110,17 @@ public class TrialLicenseUtils {
int uidLen = byteBuffer.getInt();
byte[] uidBytes = new byte[uidLen];
byteBuffer.get(uidBytes);
String uid = new String(uidBytes, LicensesCharset.UTF_8);
String uid = new String(uidBytes, StandardCharsets.UTF_8);
int issuedToLen = byteBuffer.getInt();
byte[] issuedToBytes = new byte[issuedToLen];
byteBuffer.get(issuedToBytes);
String issuedTo = new String(issuedToBytes, LicensesCharset.UTF_8);
String issuedTo = new String(issuedToBytes, StandardCharsets.UTF_8);
int featureLen = byteBuffer.getInt();
byte[] featureBytes = new byte[featureLen];
byteBuffer.get(featureBytes);
String feature = new String(featureBytes, LicensesCharset.UTF_8);
String feature = new String(featureBytes, StandardCharsets.UTF_8);
int maxNodes = byteBuffer.getInt();
long issueDate = byteBuffer.getLong();
@ -137,9 +137,9 @@ public class TrialLicenseUtils {
}
public static String toEncodedTrialLicense(ESLicense trialLicense) {
byte[] uidBytes = trialLicense.uid().getBytes(LicensesCharset.UTF_8);
byte[] featureBytes = trialLicense.feature().getBytes(LicensesCharset.UTF_8);
byte[] issuedToBytes = trialLicense.issuedTo().getBytes(LicensesCharset.UTF_8);
byte[] uidBytes = trialLicense.uid().getBytes(StandardCharsets.UTF_8);
byte[] featureBytes = trialLicense.feature().getBytes(StandardCharsets.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
int len = 4 + uidBytes.length + 4 + issuedToBytes.length + 4 + featureBytes.length + 4 + 8 + 8;

View File

@ -48,10 +48,10 @@ public class RestGetLicenseAction extends BaseRestHandler {
* {...}
*   ]
* }p
*
* <p/>
* There will be only one license displayed per feature, the selected license will have the latest expiry_date
* out of all other licenses for the feature.
*
* <p/>
* The licenses are sorted by latest issue_date
*/

View File

@ -5,9 +5,6 @@
*/
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.common.inject.Inject;
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.licensor.ESLicenseSigner;
import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomRealisticUnicodeOfCodepointLengthBetween;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.ElasticsearchTestCase.randomFrom;
@RunWith(value = com.carrotsearch.randomizedtesting.RandomizedRunner.class)
public class AbstractLicensingTestBase {

View File

@ -7,7 +7,11 @@ package org.elasticsearch.license;
import org.elasticsearch.license.core.ESLicense;
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.junit.Assert.assertThat;

View File

@ -15,18 +15,19 @@ import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.notNullValue;
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 {

View File

@ -6,24 +6,18 @@
package org.elasticsearch.license.licensor;
import org.elasticsearch.license.AbstractLicensingTestBase;
import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicensesCharset;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import java.io.IOException;
import java.text.ParseException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class LicenseSerializationTests extends AbstractLicensingTestBase {
@ -33,7 +27,7 @@ public class LicenseSerializationTests extends AbstractLicensingTestBase {
String issueDate = dateMathString("now", now);
String expiryDate = dateMathString("now+10d/d", now);
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();
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 licenseSpecs1 = generateESLicenseSpecString(Arrays.asList(new LicenseSpec("marvel", marvelIssueDate, marvelExpiryDate)));
Set<ESLicense> esLicensesOutput = new HashSet<>();
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs.getBytes(LicensesCharset.UTF_8), false));
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs1.getBytes(LicensesCharset.UTF_8), false));
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs.getBytes(StandardCharsets.UTF_8), false));
esLicensesOutput.addAll(ESLicenses.fromSource(licenseSpecs1.getBytes(StandardCharsets.UTF_8), false));
assertThat(esLicensesOutput.size(), equalTo(2));
for (ESLicense esLicense : esLicensesOutput) {
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());
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()));
for (ESLicense license : esLicensesOutput) {

View File

@ -11,11 +11,14 @@ import org.elasticsearch.license.core.ESLicense;
import org.junit.BeforeClass;
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 org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
public class LicenseSignatureTest extends AbstractLicensingTestBase {

View File

@ -12,10 +12,12 @@ import org.elasticsearch.license.core.ESLicense;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
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 {

View File

@ -28,7 +28,9 @@ import org.elasticsearch.license.plugin.core.LicensesStatus;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
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.TimeUnit;

View File

@ -10,10 +10,8 @@ import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.base.Predicate;
import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.gateway.GatewayService;
import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.manager.ESLicenseManager;
@ -27,9 +25,7 @@ import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
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.test.ElasticsearchIntegrationTest.ClusterScope;
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) {
return new Action(new Runnable() {
@Override
public void run() {}
public void run() {
}
}, 1, 0, TimeValue.timeValueMillis(expiryDuration.getMillis() * 2),
"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
* see {@link org.elasticsearch.license.plugin.consumer.EagerLicenseRegistrationPluginService}
*
* <p/>
* License registration might happen before clusterService start()
*/
public class EagerLicenseRegistrationConsumerPlugin extends TestConsumerPluginBase {

View File

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