Clean up CredHubTemplateFactory API.
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.credhub.configuration.CredHubTemplateFactory;
|
||||
import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations;
|
||||
import org.springframework.credhub.core.CredHubOperations;
|
||||
import org.springframework.credhub.core.CredHubProperties;
|
||||
import org.springframework.credhub.support.ClientOptions;
|
||||
import org.springframework.credhub.support.ServicesData;
|
||||
|
||||
/**
|
||||
@@ -46,16 +47,16 @@ public class CredHubInterpolationServiceDataPostProcessor
|
||||
*/
|
||||
public CredHubInterpolationServiceDataPostProcessor() {
|
||||
try {
|
||||
CredHubTemplateFactory credHubTemplateFactroy = new CredHubTemplateFactory();
|
||||
CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory();
|
||||
CredHubProperties credHubProperties = new CredHubProperties();
|
||||
|
||||
credHubProperties.setUrl(System.getProperty("spring.credhub.url"));
|
||||
|
||||
if (credHubProperties.getUrl() != null
|
||||
&& !credHubProperties.getUrl().isEmpty()) {
|
||||
credHubOperations = credHubTemplateFactroy.credHubTemplate(
|
||||
credHubOperations = credHubTemplateFactory.credHubTemplate(
|
||||
credHubProperties,
|
||||
credHubTemplateFactroy.clientHttpRequestFactoryWrapper())
|
||||
new ClientOptions())
|
||||
.interpolation();
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -40,12 +40,12 @@ public class CredHubTemplateFactory {
|
||||
* Create a {@link CredHubTemplate} for interaction with a CredHub server.
|
||||
*
|
||||
* @param credHubProperties connection properties
|
||||
* @param clientHttpRequestFactory a factory for HTTP connections
|
||||
* @param clientOptions connection options
|
||||
* @return a {@code CredHubTemplate}
|
||||
*/
|
||||
public CredHubTemplate credHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
return new CredHubTemplate(credHubProperties, clientHttpRequestFactory);
|
||||
ClientOptions clientOptions) {
|
||||
return new CredHubTemplate(credHubProperties, clientHttpRequestFactory(clientOptions));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,38 +53,29 @@ public class CredHubTemplateFactory {
|
||||
* using OAuth2 for authentication.
|
||||
*
|
||||
* @param credHubProperties connection properties
|
||||
* @param clientHttpRequestFactory a factory for HTTP connections
|
||||
* @param clientOptions connection options
|
||||
* @param clientRegistrationRepository a repository of OAuth2 client registrations
|
||||
* @param authorizedClientService a repository of authorized OAuth2 clients
|
||||
* @return a {@code CredHubTemplate}
|
||||
*/
|
||||
public CredHubTemplate credHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientHttpRequestFactory clientHttpRequestFactory,
|
||||
ClientOptions clientOptions,
|
||||
ClientRegistrationRepository clientRegistrationRepository,
|
||||
OAuth2AuthorizedClientService authorizedClientService) {
|
||||
return new CredHubTemplate(credHubProperties, clientHttpRequestFactory,
|
||||
return new CredHubTemplate(credHubProperties, clientHttpRequestFactory(clientOptions),
|
||||
clientRegistrationRepository, authorizedClientService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ClientHttpRequestFactory}.
|
||||
*
|
||||
* @return the {@link ClientHttpRequestFactory} instance.
|
||||
*/
|
||||
public ClientHttpRequestFactory clientHttpRequestFactoryWrapper() {
|
||||
return ClientHttpRequestFactoryFactory.create(new ClientOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ReactiveCredHubTemplate} for interaction with a CredHub server.
|
||||
*
|
||||
* @param credHubProperties connection properties
|
||||
* @param clientHttpConnector a factory for HTTP connections
|
||||
* @param clientOptions connection options
|
||||
* @return a {@code ReactiveCredHubTemplate}
|
||||
*/
|
||||
public ReactiveCredHubTemplate credHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientHttpConnector clientHttpConnector) {
|
||||
return new ReactiveCredHubTemplate(credHubProperties, clientHttpConnector);
|
||||
public ReactiveCredHubTemplate reactiveCredHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientOptions clientOptions) {
|
||||
return new ReactiveCredHubTemplate(credHubProperties, clientHttpConnector(clientOptions));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,16 +83,16 @@ public class CredHubTemplateFactory {
|
||||
* using OAuth2 for authentication.
|
||||
*
|
||||
* @param credHubProperties connection properties
|
||||
* @param clientHttpConnector a factory for HTTP connections
|
||||
* @param clientOptions connection options
|
||||
* @param clientRegistrationRepository a repository of OAuth2 client registrations
|
||||
* @param authorizedClientRepository a repository of OAuth2 client authorizations
|
||||
* @return a {@code ReactiveCredHubTemplate}
|
||||
*/
|
||||
public ReactiveCredHubOperations credHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientHttpConnector clientHttpConnector,
|
||||
ReactiveClientRegistrationRepository clientRegistrationRepository,
|
||||
ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
|
||||
return new ReactiveCredHubTemplate(credHubProperties, clientHttpConnector,
|
||||
public ReactiveCredHubOperations reactiveCredHubTemplate(CredHubProperties credHubProperties,
|
||||
ClientOptions clientOptions,
|
||||
ReactiveClientRegistrationRepository clientRegistrationRepository,
|
||||
ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
|
||||
return new ReactiveCredHubTemplate(credHubProperties, clientHttpConnector(clientOptions),
|
||||
clientRegistrationRepository, authorizedClientRepository);
|
||||
}
|
||||
|
||||
@@ -111,8 +102,8 @@ public class CredHubTemplateFactory {
|
||||
* @param clientOptions options for creating the client connection
|
||||
* @return the {@link ClientHttpRequestFactory} instance.
|
||||
*/
|
||||
public ClientHttpConnector clientHttpConnector(ClientOptions clientOptions) {
|
||||
return ClientHttpConnectorFactory.create(clientOptions);
|
||||
private ClientHttpRequestFactory clientHttpRequestFactory(ClientOptions clientOptions) {
|
||||
return ClientHttpRequestFactoryFactory.create(clientOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +112,7 @@ public class CredHubTemplateFactory {
|
||||
* @param clientOptions options for creating the client connection
|
||||
* @return the {@link ClientHttpRequestFactory} instance.
|
||||
*/
|
||||
public ClientHttpRequestFactory clientHttpRequestFactoryWrapper(ClientOptions clientOptions) {
|
||||
return ClientHttpRequestFactoryFactory.create(clientOptions);
|
||||
private ClientHttpConnector clientHttpConnector(ClientOptions clientOptions) {
|
||||
return ClientHttpConnectorFactory.create(clientOptions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,23 +16,15 @@
|
||||
|
||||
package org.springframework.credhub.autoconfig;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.credhub.configuration.CredHubTemplateFactory;
|
||||
import org.springframework.credhub.core.CredHubProperties;
|
||||
import org.springframework.credhub.support.ClientOptions;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring CredHub support beans.
|
||||
@@ -43,8 +35,6 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public class CredHubAutoConfiguration {
|
||||
private final CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory();
|
||||
|
||||
/**
|
||||
* Create a {@link CredHubProperties} bean and populate it from properties.
|
||||
*
|
||||
@@ -70,65 +60,4 @@ public class CredHubAutoConfiguration {
|
||||
public ClientOptions clientOptions() {
|
||||
return new ClientOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ClientFactoryWrapper} containing a
|
||||
* {@link ClientHttpRequestFactory}. {@link ClientHttpRequestFactory} is not exposed
|
||||
* as root bean because {@link ClientHttpRequestFactory} is configured with
|
||||
* {@link ClientOptions} which are not necessarily applicable for the whole
|
||||
* application.
|
||||
*
|
||||
* @param clientOptions the populated {@link ClientOptions} bean
|
||||
* @return the {@link ClientFactoryWrapper} to wrap a {@link ClientHttpRequestFactory}
|
||||
* instance
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(ClientOptions.class)
|
||||
public ClientFactoryWrapper clientHttpRequestFactoryWrapper(ClientOptions clientOptions) {
|
||||
return new ClientFactoryWrapper(
|
||||
credHubTemplateFactory.clientHttpRequestFactoryWrapper(clientOptions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ClientHttpConnector}.
|
||||
*
|
||||
* @param clientOptions the populated {@link ClientOptions} bean
|
||||
* @return the {@link ClientHttpConnector}
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(ClientOptions.class)
|
||||
@ConditionalOnClass(WebClient.class)
|
||||
public ClientHttpConnector clientHttpConnector(ClientOptions clientOptions) {
|
||||
return credHubTemplateFactory.clientHttpConnector(clientOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for {@link ClientHttpRequestFactory} to not expose the bean globally.
|
||||
*/
|
||||
public static class ClientFactoryWrapper implements InitializingBean, DisposableBean {
|
||||
|
||||
private final ClientHttpRequestFactory clientHttpRequestFactory;
|
||||
|
||||
ClientFactoryWrapper(ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
this.clientHttpRequestFactory = clientHttpRequestFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
if (clientHttpRequestFactory instanceof DisposableBean) {
|
||||
((DisposableBean) clientHttpRequestFactory).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (clientHttpRequestFactory instanceof InitializingBean) {
|
||||
((InitializingBean) clientHttpRequestFactory).afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
|
||||
ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||
return clientHttpRequestFactory;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,14 +26,13 @@ import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.Re
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration.ClientFactoryWrapper;
|
||||
import org.springframework.credhub.configuration.CredHubTemplateFactory;
|
||||
import org.springframework.credhub.core.CredHubOperations;
|
||||
import org.springframework.credhub.core.CredHubProperties;
|
||||
import org.springframework.credhub.core.CredHubTemplate;
|
||||
import org.springframework.credhub.core.ReactiveCredHubOperations;
|
||||
import org.springframework.credhub.core.ReactiveCredHubTemplate;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.credhub.support.ClientOptions;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
||||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
||||
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
|
||||
@@ -60,8 +59,7 @@ public class CredHubTemplateAutoConfiguration {
|
||||
* with CredHub.
|
||||
*
|
||||
* @param credHubProperties {@link CredHubProperties} for CredHub
|
||||
* @param clientFactoryWrapper a {@link ClientFactoryWrapper}
|
||||
* to customize CredHub HTTP requests
|
||||
* @param clientOptions client connection options
|
||||
* @param clientRegistrationRepository a repository of OAuth2 client registrations
|
||||
* @param authorizedClientService a repository of authorized OAuth2 clients
|
||||
* @return the {@link CredHubOperations} bean
|
||||
@@ -69,24 +67,20 @@ public class CredHubTemplateAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CredHubOperations credHubTemplate(
|
||||
CredHubProperties credHubProperties,
|
||||
ClientFactoryWrapper clientFactoryWrapper,
|
||||
CredHubProperties credHubProperties, ClientOptions clientOptions,
|
||||
@Autowired(required = false) ClientRegistrationRepository clientRegistrationRepository,
|
||||
@Autowired(required = false) OAuth2AuthorizedClientService authorizedClientService) {
|
||||
|
||||
if (credHubProperties.getOauth2() == null || credHubProperties.getOauth2().getRegistrationId() == null) {
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties,
|
||||
clientFactoryWrapper.getClientHttpRequestFactory());
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientOptions);
|
||||
}
|
||||
|
||||
if (clientRegistrationRepository == null || authorizedClientService == null) {
|
||||
throw misconfiguredException();
|
||||
}
|
||||
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties,
|
||||
clientFactoryWrapper.getClientHttpRequestFactory(),
|
||||
clientRegistrationRepository,
|
||||
authorizedClientService);
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientOptions,
|
||||
clientRegistrationRepository, authorizedClientService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,8 +88,7 @@ public class CredHubTemplateAutoConfiguration {
|
||||
* with CredHub.
|
||||
*
|
||||
* @param credHubProperties {@link CredHubProperties} for CredHub
|
||||
* @param clientHttpConnector a {@link ClientHttpConnector} to customize CredHub
|
||||
* HTTP requests
|
||||
* @param clientOptions client connection options
|
||||
* @param clientRegistrationRepository a repository of OAuth2 client registrations
|
||||
* @param authorizedClientRepository a repository of OAuth2 authorized clients
|
||||
* @return the {@link CredHubTemplate} bean
|
||||
@@ -104,19 +97,18 @@ public class CredHubTemplateAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(WebClient.class)
|
||||
public ReactiveCredHubOperations reactiveCredHubTemplate(
|
||||
CredHubProperties credHubProperties,
|
||||
ClientHttpConnector clientHttpConnector,
|
||||
CredHubProperties credHubProperties, ClientOptions clientOptions,
|
||||
@Autowired(required = false) ReactiveClientRegistrationRepository clientRegistrationRepository,
|
||||
@Autowired(required = false) ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
|
||||
|
||||
if (credHubProperties.getOauth2() == null || credHubProperties.getOauth2().getRegistrationId() == null) {
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector);
|
||||
return credHubTemplateFactory.reactiveCredHubTemplate(credHubProperties, clientOptions);
|
||||
}
|
||||
|
||||
if (clientRegistrationRepository == null || authorizedClientRepository == null) {
|
||||
throw misconfiguredException();
|
||||
}
|
||||
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector,
|
||||
return credHubTemplateFactory.reactiveCredHubTemplate(credHubProperties, clientOptions,
|
||||
clientRegistrationRepository, authorizedClientRepository);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration.ClientFactoryWrapper;
|
||||
import org.springframework.credhub.core.CredHubProperties;
|
||||
import org.springframework.credhub.support.ClientOptions;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
@@ -91,9 +90,6 @@ public class CredHubAutoConfigurationTests {
|
||||
ClientOptions options = context.getBean(ClientOptions.class);
|
||||
assertThat(options.getConnectionTimeout()).isEqualTo(Duration.ofMillis(30));
|
||||
assertThat(options.getReadTimeout()).isEqualTo(Duration.ofMillis(60));
|
||||
|
||||
assertThat(context).hasSingleBean(ClientFactoryWrapper.class);
|
||||
assertThat(context).hasSingleBean(ClientHttpConnector.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
Reference in New Issue
Block a user