incorporate first round of feedback; minor cleanup & fixes

Original commit: elastic/x-pack-elasticsearch@1058049d44
This commit is contained in:
Areek Zillur 2014-10-01 15:53:46 -04:00
parent 67d776f30a
commit 4dc2344bb0
6 changed files with 35 additions and 110 deletions

14
pom.xml
View File

@ -145,20 +145,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.elasticsearch.license.licensor.tools.LicenseGeneratorTool</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
-->
</plugins> </plugins>
</build> </build>

View File

@ -13,13 +13,13 @@ import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
public class DateUtils { public class DateUtils {
public static final DateFormat DATE_FORMAT;
public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");
static { private static DateFormat getDateFormat() {
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
DATE_FORMAT.setTimeZone(DateUtils.TIME_ZONE); dateFormat.setTimeZone(TIME_ZONE);
DATE_FORMAT.setLenient(false); dateFormat.setLenient(false);
return dateFormat;
} }
public static long longExpiryDateFromDate(long date) { public static long longExpiryDateFromDate(long date) {
@ -38,7 +38,7 @@ public class DateUtils {
} }
public static long longFromDateString(String dateStr) throws ParseException { public static long longFromDateString(String dateStr) throws ParseException {
Date dateObj = DATE_FORMAT.parse(dateStr); Date dateObj = getDateFormat().parse(dateStr);
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.clear(); calendar.clear();
calendar.setTimeZone(TIME_ZONE); calendar.setTimeZone(TIME_ZONE);
@ -56,6 +56,6 @@ public class DateUtils {
calendar.clear(); calendar.clear();
calendar.setTimeZone(TIME_ZONE); calendar.setTimeZone(TIME_ZONE);
calendar.setTimeInMillis(dateObj.getTime()); calendar.setTimeInMillis(dateObj.getTime());
return DATE_FORMAT.format(calendar.getTime()); return getDateFormat().format(calendar.getTime());
} }
} }

View File

@ -36,15 +36,13 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
* Enum for License Type * Enum for License Type
*/ */
public enum Type { public enum Type {
TRIAL((byte) 0, "trial"), TRIAL("trial"),
SUBSCRIPTION((byte) 1, "subscription"), SUBSCRIPTION("subscription"),
INTERNAL((byte) 2, "internal"); INTERNAL("internal");
private final byte id;
private final String name; private final String name;
private Type(byte id, String name) { private Type(String name) {
this.id = id;
this.name = name; this.name = name;
} }
@ -52,23 +50,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
return name; return name;
} }
public byte id() {
return id;
}
public static Type fromId(byte id) {
switch (id) {
case 0:
return TRIAL;
case 1:
return SUBSCRIPTION;
case 2:
return INTERNAL;
default:
throw new IllegalArgumentException("Invalid Type id=" + id);
}
}
public static Type fromString(String type) { public static Type fromString(String type) {
if (type.equalsIgnoreCase(TRIAL.string())) { if (type.equalsIgnoreCase(TRIAL.string())) {
return TRIAL; return TRIAL;
@ -87,19 +68,17 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
* Enum for License Subscription Type * Enum for License Subscription Type
*/ */
public enum SubscriptionType { public enum SubscriptionType {
NONE((byte) 0, "none"), NONE("none"),
DEVELOPMENT((byte) 1, "development"), DEVELOPMENT("development"),
SILVER((byte) 2, "silver"), SILVER("silver"),
GOLD((byte) 3, "gold"), GOLD("gold"),
PLATINUM((byte) 4, "platinum"); PLATINUM("platinum");
public static SubscriptionType DEFAULT = NONE; public static SubscriptionType DEFAULT = NONE;
private final byte id;
private final String name; private final String name;
private SubscriptionType(byte id, String name) { private SubscriptionType(String name) {
this.id = id;
this.name = name; this.name = name;
} }
@ -107,28 +86,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
return name; return name;
} }
public byte id() {
return id;
}
public static SubscriptionType fromId(byte id) {
switch (id) {
case 0:
return NONE;
case 1:
return DEVELOPMENT;
case 2:
return SILVER;
case 3:
return GOLD;
case 4:
return PLATINUM;
default:
throw new IllegalArgumentException("Invalid SubscriptionType id=" + id);
}
}
public static SubscriptionType fromString(String subscriptionType) { public static SubscriptionType fromString(String subscriptionType) {
if (subscriptionType.equalsIgnoreCase(NONE.string())) { if (subscriptionType.equalsIgnoreCase(NONE.string())) {
return NONE; return NONE;
@ -150,15 +107,12 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
* Enum for License FeatureType * Enum for License FeatureType
*/ */
public enum FeatureType { public enum FeatureType {
SHIELD((byte) 0, "shield"), SHIELD("shield"),
MARVEL((byte) 1, "marvel"); MARVEL("marvel");
private final byte id;
private final String name; private final String name;
private FeatureType(byte id, String name) { private FeatureType(String name) {
this.id = id;
this.name = name; this.name = name;
} }
@ -166,21 +120,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
return name; return name;
} }
public byte id() {
return id;
}
public static FeatureType fromId(byte id) {
switch (id) {
case 0:
return SHIELD;
case 1:
return MARVEL;
default:
throw new IllegalArgumentException("Invalid FeatureType id=" + id);
}
}
public static FeatureType fromString(String featureType) { public static FeatureType fromString(String featureType) {
if (featureType.equalsIgnoreCase(SHIELD.string())) { if (featureType.equalsIgnoreCase(SHIELD.string())) {
return SHIELD; return SHIELD;

View File

@ -13,14 +13,15 @@ import net.nicholaswilliams.java.licensing.exception.KeyNotFoundException;
import net.nicholaswilliams.java.licensing.licensor.LicenseCreator; import net.nicholaswilliams.java.licensing.licensor.LicenseCreator;
import net.nicholaswilliams.java.licensing.licensor.LicenseCreatorProperties; import net.nicholaswilliams.java.licensing.licensor.LicenseCreatorProperties;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
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 java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random; import java.util.Random;
import static org.elasticsearch.license.core.ESLicenses.ESLicense; import static org.elasticsearch.license.core.ESLicenses.ESLicense;
@ -51,10 +52,10 @@ public class ESLicenseSigner {
LicenseCreatorProperties.setPrivateKeyDataProvider(new PrivateKeyDataProvider() { LicenseCreatorProperties.setPrivateKeyDataProvider(new PrivateKeyDataProvider() {
@Override @Override
public byte[] getEncryptedPrivateKeyData() throws KeyNotFoundException { public byte[] getEncryptedPrivateKeyData() throws KeyNotFoundException {
File privateKeyFile = new File(options.privateKeyPath); Path privateKeyFile = Paths.get(options.privateKeyPath);
assert privateKeyFile.exists(); assert privateKeyFile.toFile().exists();
try { try {
return FileUtils.readFileToByteArray(privateKeyFile); return Files.readAllBytes(privateKeyFile);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -107,7 +108,7 @@ public class ESLicenseSigner {
random.nextBytes(magic); random.nextBytes(magic);
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(
FileUtils.readFileToByteArray(new File(options.publicKeyPath))) Files.readAllBytes(Paths.get(options.publicKeyPath)))
).getBytes(Charset.forName("UTF-8")); ).getBytes(Charset.forName("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

@ -11,10 +11,7 @@ import net.nicholaswilliams.java.licensing.exception.InappropriateKeyException;
import net.nicholaswilliams.java.licensing.exception.InappropriateKeySpecificationException; import net.nicholaswilliams.java.licensing.exception.InappropriateKeySpecificationException;
import net.nicholaswilliams.java.licensing.exception.RSA2048NotSupportedException; import net.nicholaswilliams.java.licensing.exception.RSA2048NotSupportedException;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.KeyPair; import java.security.KeyPair;
public class KeyPairGeneratorTool { public class KeyPairGeneratorTool {
@ -69,7 +66,7 @@ public class KeyPairGeneratorTool {
} }
public static void run(String[] args, OutputStream out) throws IOException { public static void run(String[] args, OutputStream out) throws IOException {
PrintWriter printWriter = new PrintWriter(out); PrintStream printStream = new PrintStream(out);
Options options = parse(args); Options options = parse(args);
@ -81,7 +78,8 @@ public class KeyPairGeneratorTool {
KeyPair keyPair = generateKeyPair(options.privateKeyFilePath, options.publicKeyFilePath, options.keyPass); KeyPair keyPair = generateKeyPair(options.privateKeyFilePath, options.publicKeyFilePath, options.keyPass);
if (keyPair != null) { if (keyPair != null) {
printWriter.println("Successfully generated new keyPair [publicKey: " + options.publicKeyFilePath + ", privateKey: " + options.privateKeyFilePath + "]"); printStream.println("Successfully generated new keyPair [publicKey: " + options.publicKeyFilePath + ", privateKey: " + options.privateKeyFilePath + "]");
printStream.flush();
} }
} }
@ -97,7 +95,7 @@ public class KeyPairGeneratorTool {
try { try {
keyPair = generator.generateKeyPair(); keyPair = generator.generateKeyPair();
} catch (RSA2048NotSupportedException e) { } catch (RSA2048NotSupportedException e) {
return null; throw new IllegalStateException(e);
} }
try { try {

View File

@ -12,7 +12,6 @@ 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.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
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 sun.reflect.generics.reflectiveObjects.NotImplementedException;
@ -20,6 +19,8 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@ -103,7 +104,7 @@ public class ESLicenseManager {
byteBuffer.get(hash); byteBuffer.get(hash);
final byte[] computedHash = Hasher.hash(Base64.encodeBase64String( final byte[] computedHash = Hasher.hash(Base64.encodeBase64String(
FileUtils.readFileToByteArray(publicKeyDataProvider.getPublicKeyFile())) Files.readAllBytes(Paths.get(publicKeyDataProvider.getPublicKeyFile().getAbsolutePath())))
).getBytes(Charset.forName("UTF-8")); ).getBytes(Charset.forName("UTF-8"));
if (!Arrays.equals(hash, computedHash)) { if (!Arrays.equals(hash, computedHash)) {