diff --git a/src/main/java/org/elasticsearch/license/core/ESLicense.java b/src/main/java/org/elasticsearch/license/core/ESLicense.java index 45ad44cee62..a0936ac963f 100644 --- a/src/main/java/org/elasticsearch/license/core/ESLicense.java +++ b/src/main/java/org/elasticsearch/license/core/ESLicense.java @@ -7,10 +7,11 @@ package org.elasticsearch.license.core; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.*; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; -import java.util.*; +import java.util.HashMap; +import java.util.Map; public class ESLicense implements Comparable { diff --git a/src/main/java/org/elasticsearch/license/core/ESLicenses.java b/src/main/java/org/elasticsearch/license/core/ESLicenses.java index f6174721fb6..21825c4d705 100644 --- a/src/main/java/org/elasticsearch/license/core/ESLicenses.java +++ b/src/main/java/org/elasticsearch/license/core/ESLicenses.java @@ -5,17 +5,13 @@ */ package org.elasticsearch.license.core; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.nio.charset.Charset; import java.util.*; public class ESLicenses { @@ -31,7 +27,7 @@ public class ESLicenses { } public static Set fromSource(String content) throws IOException { - return fromSource(content.getBytes(Charset.forName("UTF-8"))); + return fromSource(content.getBytes(LicensesCharset.UTF_8)); } public static Set fromSource(byte[] bytes) throws IOException { @@ -41,6 +37,7 @@ public class ESLicenses { private static Set fromXContent(XContentParser parser) throws IOException { Set esLicenses = new HashSet<>(); final Map licensesMap = parser.mapAndClose(); + @SuppressWarnings("unchecked") final List> licenseMaps = (ArrayList>)licensesMap.get("licenses"); for (Map licenseMap : licenseMaps) { final ESLicense esLicense = ESLicense.fromXContent(licenseMap); diff --git a/src/main/java/org/elasticsearch/license/core/LicensesCharset.java b/src/main/java/org/elasticsearch/license/core/LicensesCharset.java new file mode 100644 index 00000000000..04efbb18a87 --- /dev/null +++ b/src/main/java/org/elasticsearch/license/core/LicensesCharset.java @@ -0,0 +1,17 @@ +/* + * 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"); + +} diff --git a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java index ab2cf52150d..d9f82a8df91 100644 --- a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java +++ b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java @@ -13,13 +13,12 @@ import net.nicholaswilliams.java.licensing.exception.KeyNotFoundException; import net.nicholaswilliams.java.licensing.licensor.LicenseCreator; import net.nicholaswilliams.java.licensing.licensor.LicenseCreatorProperties; import org.apache.commons.codec.binary.Base64; -import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.collect.ImmutableSet; -import org.elasticsearch.license.core.*; +import org.elasticsearch.license.core.ESLicense; +import org.elasticsearch.license.core.LicensesCharset; import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -105,7 +104,7 @@ public class ESLicenseSigner { final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license); final byte[] hash = Hasher.hash(Base64.encodeBase64String( Files.readAllBytes(publicKeyPath)) - ).getBytes(Charset.forName("UTF-8")); + ).getBytes(LicensesCharset.UTF_8); int headerLength = MAGIC_LENGTH + hash.length + 4 + 4; byte[] bytes = new byte[headerLength + licenseSignature.length]; diff --git a/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java b/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java index b2655590859..74e2c0f815e 100644 --- a/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java +++ b/src/main/java/org/elasticsearch/license/licensor/LicenseSpec.java @@ -8,14 +8,14 @@ package org.elasticsearch.license.licensor; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.license.core.DateUtils; +import org.elasticsearch.license.core.LicensesCharset; import java.io.IOException; -import java.nio.charset.Charset; import java.text.ParseException; import java.util.*; -import static org.elasticsearch.license.core.ESLicense.Type; import static org.elasticsearch.license.core.ESLicense.SubscriptionType; +import static org.elasticsearch.license.core.ESLicense.Type; public class LicenseSpec { final String uid; @@ -43,7 +43,7 @@ public class LicenseSpec { } - public static class Builder { + private static class Builder { private String uid; private String issuer; private String issuedTo; @@ -104,6 +104,7 @@ public class LicenseSpec { if (uid == null) { uid = UUID.randomUUID().toString(); } + //TODO: verify params return new LicenseSpec(uid, issuer, issuedTo, issueDate, type, subscriptionType, feature ,expiryDate, maxNodes); } @@ -128,7 +129,7 @@ public class LicenseSpec { } public static Set fromSource(String content) throws IOException, ParseException { - return fromSource(content.getBytes(Charset.forName("UTF-8"))); + return fromSource(content.getBytes(LicensesCharset.UTF_8)); } public static Set fromSource(byte[] bytes) throws IOException, ParseException { @@ -138,6 +139,7 @@ public class LicenseSpec { private static Set fromXContents(XContentParser parser) throws IOException, ParseException { Set licenseSpecs = new HashSet<>(); final Map licenseSpecMap = parser.mapAndClose(); + @SuppressWarnings("unchecked") final List> licenseSpecDefinitions = (ArrayList>)licenseSpecMap.get("licenses"); for (Map licenseSpecDef : licenseSpecDefinitions) { final LicenseSpec licenseSpec = fromXContent(licenseSpecDef); diff --git a/src/main/java/org/elasticsearch/license/plugin/LicenseModule.java b/src/main/java/org/elasticsearch/license/plugin/LicenseModule.java index d0f08b0c525..1ace3805f4f 100644 --- a/src/main/java/org/elasticsearch/license/plugin/LicenseModule.java +++ b/src/main/java/org/elasticsearch/license/plugin/LicenseModule.java @@ -7,8 +7,6 @@ package org.elasticsearch.license.plugin; import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Scopes; -import org.elasticsearch.license.manager.ESLicenseManager; -import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesService; public class LicenseModule extends AbstractModule { diff --git a/src/main/java/org/elasticsearch/license/plugin/LicensePlugin.java b/src/main/java/org/elasticsearch/license/plugin/LicensePlugin.java index 66a177108ba..e562345062e 100644 --- a/src/main/java/org/elasticsearch/license/plugin/LicensePlugin.java +++ b/src/main/java/org/elasticsearch/license/plugin/LicensePlugin.java @@ -6,7 +6,6 @@ package org.elasticsearch.license.plugin; import org.elasticsearch.action.ActionModule; -import org.elasticsearch.action.admin.cluster.ClusterAction; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.collect.ImmutableSet; @@ -22,10 +21,10 @@ import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; import org.elasticsearch.license.plugin.core.LicensesMetaData; +import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction; 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; diff --git a/src/main/java/org/elasticsearch/license/plugin/action/delete/DeleteLicenseRequest.java b/src/main/java/org/elasticsearch/license/plugin/action/delete/DeleteLicenseRequest.java index 8d7c11daf7b..66aeb014370 100644 --- a/src/main/java/org/elasticsearch/license/plugin/action/delete/DeleteLicenseRequest.java +++ b/src/main/java/org/elasticsearch/license/plugin/action/delete/DeleteLicenseRequest.java @@ -5,13 +5,10 @@ */ package org.elasticsearch.license.plugin.action.delete; -import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.license.core.ESLicenses; import java.io.IOException; import java.util.Arrays; diff --git a/src/main/java/org/elasticsearch/license/plugin/action/delete/TransportDeleteLicenseAction.java b/src/main/java/org/elasticsearch/license/plugin/action/delete/TransportDeleteLicenseAction.java index 0af0e1dc300..5c08c9f115d 100644 --- a/src/main/java/org/elasticsearch/license/plugin/action/delete/TransportDeleteLicenseAction.java +++ b/src/main/java/org/elasticsearch/license/plugin/action/delete/TransportDeleteLicenseAction.java @@ -17,9 +17,6 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.plugin.core.LicensesManagerService; -import org.elasticsearch.license.plugin.core.LicensesService; -import org.elasticsearch.node.Node; -import org.elasticsearch.node.internal.InternalNode; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; diff --git a/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java b/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java index fba01c3aae4..2cc6578e945 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java @@ -14,7 +14,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.util.*; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; /** * Contains metadata about registered licenses diff --git a/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java index 113fc18f981..49a4cedb229 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java @@ -6,16 +6,9 @@ package org.elasticsearch.license.plugin.core.trial; import org.apache.commons.codec.binary.Base64; -import org.elasticsearch.common.collect.ImmutableMap; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.license.core.ESLicenses; +import org.elasticsearch.license.core.LicensesCharset; -import java.io.IOException; import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.util.HashSet; -import java.util.Map; import java.util.Set; import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.trialLicensesBuilder; @@ -29,17 +22,17 @@ public class TrialLicenseUtils { int uidLen = byteBuffer.getInt(); byte[] uidBytes = new byte[uidLen]; byteBuffer.get(uidBytes); - String uid = new String(uidBytes, Charset.forName("UTF-8")); + String uid = new String(uidBytes, LicensesCharset.UTF_8); int issuedToLen = byteBuffer.getInt(); byte[] issuedToBytes = new byte[issuedToLen]; byteBuffer.get(issuedToBytes); - String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8")); + String issuedTo = new String(issuedToBytes, LicensesCharset.UTF_8); int featureLen = byteBuffer.getInt(); byte[] featureBytes = new byte[featureLen]; byteBuffer.get(featureBytes); - String feature = new String(featureBytes, Charset.forName("UTF-8")); + String feature = new String(featureBytes, LicensesCharset.UTF_8); int maxNodes = byteBuffer.getInt(); long issueDate = byteBuffer.getLong(); @@ -56,9 +49,9 @@ public class TrialLicenseUtils { } public static String toEncodedTrialLicense(TrialLicenses.TrialLicense trialLicense) { - byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8")); - byte[] featureBytes = trialLicense.feature().getBytes(Charset.forName("UTF-8")); - byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8")); + byte[] uidBytes = trialLicense.uid().getBytes(LicensesCharset.UTF_8); + byte[] featureBytes = trialLicense.feature().getBytes(LicensesCharset.UTF_8); + byte[] issuedToBytes = trialLicense.issuedTo().getBytes(LicensesCharset.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; diff --git a/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java index 336d035b540..56039ce9367 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java @@ -9,7 +9,9 @@ import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.ESLicense; -import java.util.*; +import java.util.Collection; +import java.util.Iterator; +import java.util.UUID; import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense; diff --git a/src/test/java/org/elasticsearch/license/TestUtils.java b/src/test/java/org/elasticsearch/license/TestUtils.java index e4bf26131d7..010d4488b6b 100644 --- a/src/test/java/org/elasticsearch/license/TestUtils.java +++ b/src/test/java/org/elasticsearch/license/TestUtils.java @@ -15,7 +15,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.ParseException; -import java.util.HashMap; import java.util.Map; import java.util.Set; diff --git a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java index f0dfce1e319..17d57850a42 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicenseTransportTests.java @@ -11,7 +11,8 @@ import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.TestUtils; -import org.elasticsearch.license.core.*; +import org.elasticsearch.license.core.ESLicense; +import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.manager.Utils; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequestBuilder; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse; diff --git a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java index 75aff11d21a..66391e16f5c 100644 --- a/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java +++ b/src/test/java/org/elasticsearch/license/plugin/LicensesServiceTests.java @@ -15,7 +15,8 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.TestUtils; -import org.elasticsearch.license.core.*; +import org.elasticsearch.license.core.ESLicense; +import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.manager.ESLicenseManager; import org.elasticsearch.license.manager.Utils; import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;