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>
</executions>
</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>
</build>

View File

@ -13,13 +13,13 @@ import java.util.Date;
import java.util.TimeZone;
public class DateUtils {
public static final DateFormat DATE_FORMAT;
public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");
static {
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
DATE_FORMAT.setTimeZone(DateUtils.TIME_ZONE);
DATE_FORMAT.setLenient(false);
private static DateFormat getDateFormat() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setTimeZone(TIME_ZONE);
dateFormat.setLenient(false);
return dateFormat;
}
public static long longExpiryDateFromDate(long date) {
@ -38,7 +38,7 @@ public class DateUtils {
}
public static long longFromDateString(String dateStr) throws ParseException {
Date dateObj = DATE_FORMAT.parse(dateStr);
Date dateObj = getDateFormat().parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTimeZone(TIME_ZONE);
@ -56,6 +56,6 @@ public class DateUtils {
calendar.clear();
calendar.setTimeZone(TIME_ZONE);
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
*/
public enum Type {
TRIAL((byte) 0, "trial"),
SUBSCRIPTION((byte) 1, "subscription"),
INTERNAL((byte) 2, "internal");
TRIAL("trial"),
SUBSCRIPTION("subscription"),
INTERNAL("internal");
private final byte id;
private final String name;
private Type(byte id, String name) {
this.id = id;
private Type(String name) {
this.name = name;
}
@ -52,23 +50,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
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) {
if (type.equalsIgnoreCase(TRIAL.string())) {
return TRIAL;
@ -87,19 +68,17 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
* Enum for License Subscription Type
*/
public enum SubscriptionType {
NONE((byte) 0, "none"),
DEVELOPMENT((byte) 1, "development"),
SILVER((byte) 2, "silver"),
GOLD((byte) 3, "gold"),
PLATINUM((byte) 4, "platinum");
NONE("none"),
DEVELOPMENT("development"),
SILVER("silver"),
GOLD("gold"),
PLATINUM("platinum");
public static SubscriptionType DEFAULT = NONE;
private final byte id;
private final String name;
private SubscriptionType(byte id, String name) {
this.id = id;
private SubscriptionType(String name) {
this.name = name;
}
@ -107,28 +86,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
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) {
if (subscriptionType.equalsIgnoreCase(NONE.string())) {
return NONE;
@ -150,15 +107,12 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
* Enum for License FeatureType
*/
public enum FeatureType {
SHIELD((byte) 0, "shield"),
MARVEL((byte) 1, "marvel");
private final byte id;
SHIELD("shield"),
MARVEL("marvel");
private final String name;
private FeatureType(byte id, String name) {
this.id = id;
private FeatureType(String name) {
this.name = name;
}
@ -166,21 +120,6 @@ public interface ESLicenses extends Iterable<ESLicenses.ESLicense> {
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) {
if (featureType.equalsIgnoreCase(SHIELD.string())) {
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.LicenseCreatorProperties;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import java.io.File;
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;
import java.util.Random;
import static org.elasticsearch.license.core.ESLicenses.ESLicense;
@ -51,10 +52,10 @@ public class ESLicenseSigner {
LicenseCreatorProperties.setPrivateKeyDataProvider(new PrivateKeyDataProvider() {
@Override
public byte[] getEncryptedPrivateKeyData() throws KeyNotFoundException {
File privateKeyFile = new File(options.privateKeyPath);
assert privateKeyFile.exists();
Path privateKeyFile = Paths.get(options.privateKeyPath);
assert privateKeyFile.toFile().exists();
try {
return FileUtils.readFileToByteArray(privateKeyFile);
return Files.readAllBytes(privateKeyFile);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
@ -107,7 +108,7 @@ public class ESLicenseSigner {
random.nextBytes(magic);
final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license);
final byte[] hash = Hasher.hash(Base64.encodeBase64String(
FileUtils.readFileToByteArray(new File(options.publicKeyPath)))
Files.readAllBytes(Paths.get(options.publicKeyPath)))
).getBytes(Charset.forName("UTF-8"));
int headerLength = MAGIC_LENGTH + hash.length + 4 + 4;
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.RSA2048NotSupportedException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.*;
import java.security.KeyPair;
public class KeyPairGeneratorTool {
@ -69,7 +66,7 @@ public class KeyPairGeneratorTool {
}
public static void run(String[] args, OutputStream out) throws IOException {
PrintWriter printWriter = new PrintWriter(out);
PrintStream printStream = new PrintStream(out);
Options options = parse(args);
@ -81,7 +78,8 @@ public class KeyPairGeneratorTool {
KeyPair keyPair = generateKeyPair(options.privateKeyFilePath, options.publicKeyFilePath, options.keyPass);
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 {
keyPair = generator.generateKeyPair();
} catch (RSA2048NotSupportedException e) {
return null;
throw new IllegalStateException(e);
}
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.InvalidLicenseException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
@ -20,6 +19,8 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException;
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 java.util.Collections;
import java.util.Set;
@ -103,7 +104,7 @@ public class ESLicenseManager {
byteBuffer.get(hash);
final byte[] computedHash = Hasher.hash(Base64.encodeBase64String(
FileUtils.readFileToByteArray(publicKeyDataProvider.getPublicKeyFile()))
Files.readAllBytes(Paths.get(publicKeyDataProvider.getPublicKeyFile().getAbsolutePath())))
).getBytes(Charset.forName("UTF-8"));
if (!Arrays.equals(hash, computedHash)) {