Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Simplify variable names. Remove unused internal methods. Use CharSequence instead of String to accept keystore passwords.

Closes gh-711
See gh-708
  • Loading branch information
mp911de committed Jun 24, 2022
1 parent 09530fa commit 6fbd423
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ public String getPrivateKeyType() {
*/
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;
}

/**
Expand Down Expand Up @@ -188,9 +188,9 @@ public KeyStore createKeyStore(String keyAlias) {
* @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);
}

Expand All @@ -200,7 +200,7 @@ public KeyStore createKeyStore(String keyAlias, String password) {
* @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);
Expand All @@ -227,11 +227,18 @@ public KeyStore createKeyStore(String keyAlias, boolean includeCaChain) {
* 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);
}

/**
Expand All @@ -242,7 +249,7 @@ public KeyStore createKeyStore(String keyAlias, boolean includeCaChain, String p
* 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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
*
* @author Mark Paluch
* @author Alex Bremora
* @author Bogdan Cardos
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = VaultIntegrationTestConfiguration.class)
Expand Down

0 comments on commit 6fbd423

Please sign in to comment.