From 6fbd42349ee2aef0d8f3a2b78d671097fd26d60b Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 24 Jun 2022 15:09:53 +0200 Subject: [PATCH] Polishing. Simplify variable names. Remove unused internal methods. Use CharSequence instead of String to accept keystore passwords. Closes gh-711 See gh-708 --- .../vault/support/CertificateBundle.java | 29 +++++++++----- .../vault/support/KeystoreUtil.java | 40 ------------------- .../VaultPkiTemplateIntegrationTests.java | 1 + 3 files changed, 19 insertions(+), 51 deletions(-) diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java index 93ba650d..382b9034 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java @@ -148,13 +148,13 @@ public class CertificateBundle extends Certificate { */ public String getRequiredPrivateKeyType() { - String requiredPrivateKeyType = getPrivateKeyType(); + String type = getPrivateKeyType(); - if (requiredPrivateKeyType == null) { + if (type == null) { throw new IllegalStateException("Private key type is not set"); } - return requiredPrivateKeyType; + return type; } /** @@ -188,9 +188,9 @@ public class CertificateBundle extends Certificate { * @param keyAlias the key alias to use. * @param password the password to use. * @return the {@link KeyStore} containing the private key and certificate chain. - * @since 3.0.0 + * @since 2.4 */ - public KeyStore createKeyStore(String keyAlias, String password) { + public KeyStore createKeyStore(String keyAlias, CharSequence password) { return createKeyStore(keyAlias, false, password); } @@ -200,7 +200,7 @@ public class CertificateBundle extends Certificate { * @param keyAlias the key alias to use. * @param password the password to use. * @return the {@link KeyStore} containing the private key and certificate chain. - * @since 3.0.0 + * @since 2.4 */ public KeyStore createKeyStore(String keyAlias, char[] password) { return createKeyStore(keyAlias, false, password); @@ -227,11 +227,18 @@ public class CertificateBundle extends Certificate { * just the issuer certificate. * @param password the password to use. * @return the {@link KeyStore} containing the private key and certificate chain. - * @since 3.0.0 + * @since 2.4 */ - public KeyStore createKeyStore(String keyAlias, boolean includeCaChain, String password) { - Assert.hasText(password, "Password must not be empty"); - return createKeyStore(keyAlias, includeCaChain, password.toCharArray()); + public KeyStore createKeyStore(String keyAlias, boolean includeCaChain, CharSequence password) { + + Assert.notNull(password, "Password must not be null"); + + char[] passwordChars = new char[password.length()]; + for (int i = 0; i < passwordChars.length; i++) { + passwordChars[i] = password.charAt(i); + } + + return createKeyStore(keyAlias, includeCaChain, passwordChars); } /** @@ -242,7 +249,7 @@ public class CertificateBundle extends Certificate { * just the issuer certificate. * @param password the password to use. * @return the {@link KeyStore} containing the private key and certificate chain. - * @since 3.0.0 + * @since 2.4 */ public KeyStore createKeyStore(String keyAlias, boolean includeCaChain, char[] password) { diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/KeystoreUtil.java b/spring-vault-core/src/main/java/org/springframework/vault/support/KeystoreUtil.java index b5a3456c..26a0e2ee 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/KeystoreUtil.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/KeystoreUtil.java @@ -73,46 +73,6 @@ class KeystoreUtil { } } - /** - * Create a {@link KeyStore} containing the {@link KeySpec} and {@link X509Certificate - * certificates} using the given {@code keyAlias}. - * @param keyAlias the key alias to use. - * @param privateKeySpec the private key to use. - * @param certificates the certificate chain to use. - * @return the {@link KeyStore} containing the private key and certificate chain. - * @throws GeneralSecurityException if exception occur when creating the instance of - * the {@link KeyStore} - * @throws IOException if there is an I/O or format problem with the keystore data, if - * a password is required but not given, or if the given password was incorrect. If - * the error is due to a wrong password, the {@link Throwable#getCause cause} of the - * {@code IOException} should be an {@code UnrecoverableKeyException} - */ - static KeyStore createKeyStore(String keyAlias, KeySpec privateKeySpec, X509Certificate... certificates) - throws GeneralSecurityException, IOException { - return createKeyStore(keyAlias, privateKeySpec, new char[0], certificates); - } - - /** - * Create a {@link KeyStore} containing the {@link KeySpec} and {@link X509Certificate - * certificates} using the given {@code keyAlias} and {@code keyPassword}. - * @param keyAlias the key alias to use. - * @param privateKeySpec the private key to use. - * @param keyPassword the password to use. - * @param certificates the certificate chain to use. - * @return the {@link KeyStore} containing the private key and certificate chain. - * @throws GeneralSecurityException if exception occur when creating the instance of - * the {@link KeyStore} - * @throws IOException if there is an I/O or format problem with the keystore data, if - * a password is required but not given, or if the given password was incorrect. If - * the error is due to a wrong password, the {@link Throwable#getCause cause} of the - * {@code IOException} should be an {@code UnrecoverableKeyException} - */ - static KeyStore createKeyStore(String keyAlias, KeySpec privateKeySpec, String keyPassword, - X509Certificate... certificates) throws GeneralSecurityException, IOException { - Assert.hasText(keyPassword, "keyPassword must not be empty"); - return createKeyStore(keyAlias, privateKeySpec, keyPassword.toCharArray(), certificates); - } - /** * Create a {@link KeyStore} containing the {@link KeySpec} and {@link X509Certificate * certificates} using the given {@code keyAlias} and {@code keyPassword}. diff --git a/spring-vault-core/src/test/java/org/springframework/vault/core/VaultPkiTemplateIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/core/VaultPkiTemplateIntegrationTests.java index 5d7305b0..c93635d6 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/core/VaultPkiTemplateIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/core/VaultPkiTemplateIntegrationTests.java @@ -65,6 +65,7 @@ import static org.springframework.vault.util.Settings.*; * * @author Mark Paluch * @author Alex Bremora + * @author Bogdan Cardos */ @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = VaultIntegrationTestConfiguration.class)