Refactor string literals by enum.
Signed-off-by: Max Lambrecht <maxlambrecht@gmail.com>
This commit is contained in:
parent
48aa4e6308
commit
96d660ad3a
|
|
@ -36,6 +36,8 @@ import java.util.stream.Collectors;
|
||||||
import static io.spiffe.internal.KeyUsage.CRL_SIGN;
|
import static io.spiffe.internal.KeyUsage.CRL_SIGN;
|
||||||
import static io.spiffe.internal.KeyUsage.DIGITAL_SIGNATURE;
|
import static io.spiffe.internal.KeyUsage.DIGITAL_SIGNATURE;
|
||||||
import static io.spiffe.internal.KeyUsage.KEY_CERT_SIGN;
|
import static io.spiffe.internal.KeyUsage.KEY_CERT_SIGN;
|
||||||
|
import static io.spiffe.internal.PrivateKeyAlgorithm.EC;
|
||||||
|
import static io.spiffe.internal.PrivateKeyAlgorithm.RSA;
|
||||||
import static org.apache.commons.lang3.StringUtils.startsWith;
|
import static org.apache.commons.lang3.StringUtils.startsWith;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -233,10 +235,10 @@ public class CertificateUtils {
|
||||||
PrivateKey privateKey;
|
PrivateKey privateKey;
|
||||||
switch (algorithm) {
|
switch (algorithm) {
|
||||||
case EC:
|
case EC:
|
||||||
privateKey = KeyFactory.getInstance("EC").generatePrivate(keySpec);
|
privateKey = KeyFactory.getInstance(EC.value()).generatePrivate(keySpec);
|
||||||
break;
|
break;
|
||||||
case RSA:
|
case RSA:
|
||||||
privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
|
privateKey = KeyFactory.getInstance(RSA.value()).generatePrivate(keySpec);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new NoSuchAlgorithmException(String.format("Private Key algorithm is not supported: %s", algorithm));
|
throw new NoSuchAlgorithmException(String.format("Private Key algorithm is not supported: %s", algorithm));
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,19 @@
|
||||||
package io.spiffe.internal;
|
package io.spiffe.internal;
|
||||||
|
|
||||||
public enum PrivateKeyAlgorithm {
|
public enum PrivateKeyAlgorithm {
|
||||||
RSA,
|
|
||||||
EC;
|
RSA("RSA"),
|
||||||
|
EC("EC");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
PrivateKeyAlgorithm(final String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String value() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
public static PrivateKeyAlgorithm parse(String a) {
|
public static PrivateKeyAlgorithm parse(String a) {
|
||||||
if ("RSA".equalsIgnoreCase(a)) {
|
if ("RSA".equalsIgnoreCase(a)) {
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import java.security.spec.InvalidKeySpecException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static io.spiffe.internal.PrivateKeyAlgorithm.RSA;
|
||||||
import static io.spiffe.utils.X509CertificateTestUtils.createCertificate;
|
import static io.spiffe.utils.X509CertificateTestUtils.createCertificate;
|
||||||
import static io.spiffe.utils.X509CertificateTestUtils.createRootCA;
|
import static io.spiffe.utils.X509CertificateTestUtils.createRootCA;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
@ -73,9 +74,9 @@ public class CertificateUtilsTest {
|
||||||
val keyBytes = Files.readAllBytes(keyPath);
|
val keyBytes = Files.readAllBytes(keyPath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PrivateKey privateKey = CertificateUtils.generatePrivateKey(keyBytes, PrivateKeyAlgorithm.RSA, KeyFileFormat.PEM);
|
PrivateKey privateKey = CertificateUtils.generatePrivateKey(keyBytes, RSA, KeyFileFormat.PEM);
|
||||||
assertNotNull(privateKey);
|
assertNotNull(privateKey);
|
||||||
assertEquals("RSA", privateKey.getAlgorithm());
|
assertEquals(RSA.value(), privateKey.getAlgorithm());
|
||||||
} catch (InvalidKeySpecException | InvalidKeyException | NoSuchAlgorithmException e) {
|
} catch (InvalidKeySpecException | InvalidKeyException | NoSuchAlgorithmException e) {
|
||||||
fail("Should have generated key", e);
|
fail("Should have generated key", e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue