Polishing.
Extract duplicate code into VaultConfigurationUtil.
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.cloud.vault.config;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
@@ -36,14 +35,12 @@ import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.authentication.ClientAuthentication;
|
||||
import org.springframework.vault.authentication.LifecycleAwareSessionManager;
|
||||
import org.springframework.vault.authentication.SessionManager;
|
||||
import org.springframework.vault.authentication.SimpleSessionManager;
|
||||
import org.springframework.vault.client.SimpleVaultEndpointProvider;
|
||||
import org.springframework.vault.client.VaultClients;
|
||||
import org.springframework.vault.client.VaultEndpoint;
|
||||
import org.springframework.vault.client.VaultEndpointProvider;
|
||||
import org.springframework.vault.config.ClientHttpRequestFactoryFactory;
|
||||
import org.springframework.vault.config.AbstractVaultConfiguration.ClientFactoryWrapper;
|
||||
@@ -51,7 +48,6 @@ import org.springframework.vault.core.VaultOperations;
|
||||
import org.springframework.vault.core.VaultTemplate;
|
||||
import org.springframework.vault.support.ClientOptions;
|
||||
import org.springframework.vault.support.SslConfiguration;
|
||||
import org.springframework.vault.support.SslConfiguration.KeyStoreConfiguration;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
|
||||
/**
|
||||
@@ -84,27 +80,13 @@ public class VaultBootstrapConfiguration implements InitializingBean {
|
||||
VaultEndpointProvider provider = endpointProvider.getIfAvailable();
|
||||
|
||||
if (provider == null) {
|
||||
provider = SimpleVaultEndpointProvider.of(getVaultEndpoint(vaultProperties));
|
||||
provider = SimpleVaultEndpointProvider.of(VaultConfigurationUtil
|
||||
.createVaultEndpoint(vaultProperties));
|
||||
}
|
||||
|
||||
this.endpointProvider = provider;
|
||||
}
|
||||
|
||||
private static VaultEndpoint getVaultEndpoint(VaultProperties vaultProperties) {
|
||||
|
||||
if (StringUtils.hasText(vaultProperties.getUri())) {
|
||||
return VaultEndpoint.from(URI.create(vaultProperties.getUri()));
|
||||
}
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
vaultEndpoint.setHost(vaultProperties.getHost());
|
||||
vaultEndpoint.setPort(vaultProperties.getPort());
|
||||
vaultEndpoint.setScheme(vaultProperties.getScheme());
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void afterPropertiesSet() {
|
||||
@@ -134,39 +116,8 @@ public class VaultBootstrapConfiguration implements InitializingBean {
|
||||
.getConnectionTimeout()), Duration.ofMillis(vaultProperties
|
||||
.getReadTimeout()));
|
||||
|
||||
VaultProperties.Ssl ssl = vaultProperties.getSsl();
|
||||
SslConfiguration sslConfiguration;
|
||||
if (ssl != null) {
|
||||
|
||||
KeyStoreConfiguration keyStore = KeyStoreConfiguration.unconfigured();
|
||||
KeyStoreConfiguration trustStore = KeyStoreConfiguration.unconfigured();
|
||||
|
||||
if (ssl.getKeyStore() != null) {
|
||||
if (StringUtils.hasText(ssl.getKeyStorePassword())) {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore(), ssl
|
||||
.getKeyStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore());
|
||||
}
|
||||
}
|
||||
|
||||
if (ssl.getTrustStore() != null) {
|
||||
|
||||
if (StringUtils.hasText(ssl.getTrustStorePassword())) {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore(), ssl
|
||||
.getTrustStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore());
|
||||
}
|
||||
}
|
||||
|
||||
sslConfiguration = new SslConfiguration(keyStore, trustStore);
|
||||
}
|
||||
else {
|
||||
sslConfiguration = SslConfiguration.unconfigured();
|
||||
}
|
||||
SslConfiguration sslConfiguration = VaultConfigurationUtil
|
||||
.createSslConfiguration(vaultProperties.getSsl());
|
||||
|
||||
return new ClientFactoryWrapper(ClientHttpRequestFactoryFactory.create(
|
||||
clientOptions, sslConfiguration));
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.vault.config;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.cloud.vault.config.VaultProperties.Ssl;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.client.VaultEndpoint;
|
||||
import org.springframework.vault.support.SslConfiguration;
|
||||
import org.springframework.vault.support.SslConfiguration.KeyStoreConfiguration;
|
||||
|
||||
/**
|
||||
* Support class for Vault configuration providing utility methods.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.1
|
||||
*/
|
||||
class VaultConfigurationUtil {
|
||||
|
||||
/**
|
||||
* Create a {@link SslConfiguration} given {@link Ssl SSL properties}.
|
||||
*
|
||||
* @param ssl
|
||||
* @return
|
||||
*/
|
||||
static SslConfiguration createSslConfiguration(Ssl ssl) {
|
||||
|
||||
if (ssl == null) {
|
||||
return SslConfiguration.unconfigured();
|
||||
}
|
||||
|
||||
KeyStoreConfiguration keyStore = KeyStoreConfiguration.unconfigured();
|
||||
KeyStoreConfiguration trustStore = KeyStoreConfiguration.unconfigured();
|
||||
|
||||
if (ssl.getKeyStore() != null) {
|
||||
if (StringUtils.hasText(ssl.getKeyStorePassword())) {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore(), ssl
|
||||
.getKeyStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore());
|
||||
}
|
||||
}
|
||||
|
||||
if (ssl.getTrustStore() != null) {
|
||||
|
||||
if (StringUtils.hasText(ssl.getTrustStorePassword())) {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore(), ssl
|
||||
.getTrustStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore());
|
||||
}
|
||||
}
|
||||
|
||||
return new SslConfiguration(keyStore, trustStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link VaultEndpoint} given {@link VaultProperties}.
|
||||
*
|
||||
* @param vaultProperties
|
||||
* @return
|
||||
*/
|
||||
static VaultEndpoint createVaultEndpoint(VaultProperties vaultProperties) {
|
||||
|
||||
if (StringUtils.hasText(vaultProperties.getUri())) {
|
||||
return VaultEndpoint.from(URI.create(vaultProperties.getUri()));
|
||||
}
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
vaultEndpoint.setHost(vaultProperties.getHost());
|
||||
vaultEndpoint.setPort(vaultProperties.getPort());
|
||||
vaultEndpoint.setScheme(vaultProperties.getScheme());
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.cloud.vault.config;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -38,7 +37,6 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.authentication.AuthenticationStepsFactory;
|
||||
import org.springframework.vault.authentication.AuthenticationStepsOperator;
|
||||
import org.springframework.vault.authentication.CachingVaultTokenSupplier;
|
||||
@@ -55,7 +53,6 @@ import org.springframework.vault.core.ReactiveVaultOperations;
|
||||
import org.springframework.vault.core.ReactiveVaultTemplate;
|
||||
import org.springframework.vault.support.ClientOptions;
|
||||
import org.springframework.vault.support.SslConfiguration;
|
||||
import org.springframework.vault.support.SslConfiguration.KeyStoreConfiguration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
@@ -85,24 +82,10 @@ public class VaultReactiveBootstrapConfiguration {
|
||||
public VaultReactiveBootstrapConfiguration(VaultProperties vaultProperties) {
|
||||
|
||||
this.vaultProperties = vaultProperties;
|
||||
this.vaultEndpoint = getVaultEndpoint(vaultProperties);
|
||||
this.vaultEndpoint = VaultConfigurationUtil.createVaultEndpoint(vaultProperties);
|
||||
this.clientHttpConnector = createConnector(this.vaultProperties);
|
||||
}
|
||||
|
||||
private static VaultEndpoint getVaultEndpoint(VaultProperties vaultProperties) {
|
||||
|
||||
if (StringUtils.hasText(vaultProperties.getUri())) {
|
||||
return VaultEndpoint.from(URI.create(vaultProperties.getUri()));
|
||||
}
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
vaultEndpoint.setHost(vaultProperties.getHost());
|
||||
vaultEndpoint.setPort(vaultProperties.getPort());
|
||||
vaultEndpoint.setScheme(vaultProperties.getScheme());
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link ClientHttpConnector} configured with {@link ClientOptions} and
|
||||
* {@link SslConfiguration} which are not necessarily applicable for the whole
|
||||
@@ -116,39 +99,8 @@ public class VaultReactiveBootstrapConfiguration {
|
||||
.getConnectionTimeout()), Duration.ofMillis(vaultProperties
|
||||
.getReadTimeout()));
|
||||
|
||||
VaultProperties.Ssl ssl = vaultProperties.getSsl();
|
||||
SslConfiguration sslConfiguration;
|
||||
if (ssl != null) {
|
||||
|
||||
KeyStoreConfiguration keyStore = KeyStoreConfiguration.unconfigured();
|
||||
KeyStoreConfiguration trustStore = KeyStoreConfiguration.unconfigured();
|
||||
|
||||
if (ssl.getKeyStore() != null) {
|
||||
if (StringUtils.hasText(ssl.getKeyStorePassword())) {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore(), ssl
|
||||
.getKeyStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
keyStore = KeyStoreConfiguration.of(ssl.getKeyStore());
|
||||
}
|
||||
}
|
||||
|
||||
if (ssl.getTrustStore() != null) {
|
||||
|
||||
if (StringUtils.hasText(ssl.getTrustStorePassword())) {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore(), ssl
|
||||
.getTrustStorePassword().toCharArray());
|
||||
}
|
||||
else {
|
||||
trustStore = KeyStoreConfiguration.of(ssl.getTrustStore());
|
||||
}
|
||||
}
|
||||
|
||||
sslConfiguration = new SslConfiguration(keyStore, trustStore);
|
||||
}
|
||||
else {
|
||||
sslConfiguration = SslConfiguration.unconfigured();
|
||||
}
|
||||
SslConfiguration sslConfiguration = VaultConfigurationUtil
|
||||
.createSslConfiguration(vaultProperties.getSsl());
|
||||
|
||||
return ClientHttpConnectorFactory.create(clientOptions, sslConfiguration);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user