Allow customization of the keystore/truststore type.

SslConfiguration now supports configuration of the keystore/truststore type defaulting to the system default.

Closes gh-78.
This commit is contained in:
Mark Paluch
2017-04-19 16:07:36 +02:00
parent 6f63dab52e
commit 0927f67f2f
2 changed files with 76 additions and 19 deletions

View File

@@ -136,12 +136,14 @@ public class ClientHttpRequestFactoryFactory {
throws GeneralSecurityException, IOException {
KeyManager[] keyManagers = sslConfiguration.getKeyStore() != null ? createKeyManagerFactory(
sslConfiguration.getKeyStore(), sslConfiguration.getKeyStorePassword())
.getKeyManagers() : null;
sslConfiguration.getKeyStore(), sslConfiguration.getKeyStorePassword(),
sslConfiguration.getKeyStoreType()).getKeyManagers()
: null;
TrustManager[] trustManagers = sslConfiguration.getTrustStore() != null ? createTrustManagerFactory(
sslConfiguration.getTrustStore(),
sslConfiguration.getTrustStorePassword()).getTrustManagers()
sslConfiguration.getTrustStorePassword(),
sslConfiguration.getTrustStoreType()).getTrustManagers()
: null;
SSLContext sslContext = SSLContext.getInstance("TLS");
@@ -151,9 +153,12 @@ public class ClientHttpRequestFactoryFactory {
}
private static KeyManagerFactory createKeyManagerFactory(Resource keystoreFile,
String storePassword) throws GeneralSecurityException, IOException {
String storePassword, String storeType) throws GeneralSecurityException,
IOException {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
KeyStore keyStore = KeyStore
.getInstance(StringUtils.hasText(storeType) ? storeType : KeyStore
.getDefaultType());
loadKeyStore(keystoreFile, storePassword, keyStore);
@@ -167,9 +172,12 @@ public class ClientHttpRequestFactoryFactory {
}
private static TrustManagerFactory createTrustManagerFactory(Resource trustFile,
String storePassword) throws GeneralSecurityException, IOException {
String storePassword, String storeType) throws GeneralSecurityException,
IOException {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
KeyStore trustStore = KeyStore
.getInstance(StringUtils.hasText(storeType) ? storeType : KeyStore
.getDefaultType());
loadKeyStore(trustFile, storePassword, trustStore);
@@ -325,13 +333,15 @@ public class ClientHttpRequestFactoryFactory {
if (sslConfiguration.getTrustStore() != null) {
sslContextBuilder.trustManager(createTrustManagerFactory(
sslConfiguration.getTrustStore(),
sslConfiguration.getTrustStorePassword()));
sslConfiguration.getTrustStorePassword(),
sslConfiguration.getTrustStoreType()));
}
if (sslConfiguration.getKeyStore() != null) {
sslContextBuilder.keyManager(createKeyManagerFactory(
sslConfiguration.getKeyStore(),
sslConfiguration.getKeyStorePassword()));
sslConfiguration.getKeyStorePassword(),
sslConfiguration.getKeyStoreType()));
}
requestFactory.setSslContext(sslContextBuilder.sslProvider(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.vault.support;
import java.security.KeyStore;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@@ -49,6 +51,11 @@ public class SslConfiguration {
*/
private final String keyStorePassword;
/**
* Keystore type.
*/
private final String keyStoreType;
/**
* Trust store that holds SSL certificates.
*/
@@ -60,7 +67,12 @@ public class SslConfiguration {
private final String trustStorePassword;
/**
* Create a new {@link SslConfiguration}.
* Truststore type.
*/
private final String trustStoreType;
/**
* Create a new {@link SslConfiguration} with the default {@link KeyStore} type.
*
* @param keyStore the keystore resource.
* @param keyStorePassword the keystore password.
@@ -69,15 +81,34 @@ public class SslConfiguration {
*/
public SslConfiguration(Resource keyStore, String keyStorePassword,
Resource trustStore, String trustStorePassword) {
this.keyStore = keyStore;
this.keyStorePassword = keyStorePassword;
this.trustStore = trustStore;
this.trustStorePassword = trustStorePassword;
this(keyStore, keyStorePassword, KeyStore.getDefaultType(), trustStore,
trustStorePassword, KeyStore.getDefaultType());
}
/**
* Create a new {@link SslConfiguration} for the given trust store.
* Create a new {@link SslConfiguration}.
*
* @param keyStore the keystore resource.
* @param keyStorePassword the keystore password.
* @param trustStore the truststore resource.
* @param trustStorePassword the truststore password.
* @since 1.1
*/
public SslConfiguration(Resource keyStore, String keyStorePassword,
String keyStoreType, Resource trustStore, String trustStorePassword,
String trustStoreType) {
this.keyStore = keyStore;
this.keyStorePassword = keyStorePassword;
this.keyStoreType = keyStoreType;
this.trustStore = trustStore;
this.trustStorePassword = trustStorePassword;
this.trustStoreType = trustStoreType;
}
/**
* Create a new {@link SslConfiguration} for the given trust store with the default
* {@link KeyStore} type.
*
* @param trustStore resource pointing to an existing trust store, must not be
* {@literal null}.
@@ -96,7 +127,8 @@ public class SslConfiguration {
}
/**
* Create a new {@link SslConfiguration} for the given key store.
* Create a new {@link SslConfiguration} for the given key store with the default
* {@link KeyStore} type.
*
* @param keyStore resource pointing to an existing key store, must not be
* {@literal null}.
@@ -114,7 +146,8 @@ public class SslConfiguration {
}
/**
* Create a new {@link SslConfiguration} for the given truststore.
* Create a new {@link SslConfiguration} for the given truststore with the default
* {@link KeyStore} type.
*
* @param keyStore resource pointing to an existing keystore, must not be
* {@literal null}.
@@ -155,6 +188,13 @@ public class SslConfiguration {
return keyStorePassword;
}
/**
* @return the key store type or {@literal null} if not configured.
*/
public String getKeyStoreType() {
return keyStoreType;
}
/**
* @return the {@link java.security.KeyStore key store} resource or {@literal null} if
* not configured.
@@ -169,4 +209,11 @@ public class SslConfiguration {
public String getTrustStorePassword() {
return trustStorePassword;
}
/**
* @return the trust store type or {@literal null} if not configured.
*/
public String getTrustStoreType() {
return trustStoreType;
}
}