Allow custom configuration of properties.

This commit is contained in:
Scott Frederick
2018-12-04 16:06:23 -06:00
parent 968bc20bc3
commit c4a66c4f10
15 changed files with 428 additions and 398 deletions

View File

@@ -38,10 +38,11 @@ import org.springframework.web.client.RestTemplate;
/**
* Implements the main interaction with CredHub.
*
* @author Scott Frederick
* @author Scott Frederick
*/
public class CredHubTemplate implements CredHubOperations {
private final RestTemplate restTemplate;
private final boolean usingOAuth2;
/**
* Create a new {@link CredHubTemplate} using the provided {@link RestTemplate}.
@@ -53,6 +54,7 @@ public class CredHubTemplate implements CredHubOperations {
Assert.notNull(restTemplate, "restTemplate must not be null");
this.restTemplate = restTemplate;
this.usingOAuth2 = false;
}
/**
@@ -69,6 +71,7 @@ public class CredHubTemplate implements CredHubOperations {
this.restTemplate = CredHubRestTemplateFactory.createRestTemplate(properties,
clientHttpRequestFactory);
this.usingOAuth2 = false;
}
/**
@@ -91,6 +94,7 @@ public class CredHubTemplate implements CredHubOperations {
this.restTemplate = CredHubRestTemplateFactory.createRestTemplate(properties,
clientHttpRequestFactory, clientRegistrationRepository, authorizedClientService);
this.usingOAuth2 = true;
}
/**
@@ -172,4 +176,8 @@ public class CredHubTemplate implements CredHubOperations {
throw new CredHubException(e);
}
}
public boolean isUsingOAuth2() {
return this.usingOAuth2;
}
}

View File

@@ -42,10 +42,11 @@ import java.util.function.Function;
/**
* Implements the main interaction with CredHub.
*
* @author Scott Frederick
* @author Scott Frederick
*/
public class ReactiveCredHubTemplate implements ReactiveCredHubOperations {
private final WebClient webClient;
private final boolean usingOAuth2;
/**
* Create a new {@link ReactiveCredHubTemplate} using the provided {@link WebClient}.
@@ -57,6 +58,7 @@ public class ReactiveCredHubTemplate implements ReactiveCredHubOperations {
Assert.notNull(webClient, "webClient must not be null");
this.webClient = webClient;
this.usingOAuth2 = false;
}
/**
@@ -72,6 +74,7 @@ public class ReactiveCredHubTemplate implements ReactiveCredHubOperations {
Assert.notNull(clientHttpConnector, "clientHttpConnector must not be null");
this.webClient = CredHubWebClientFactory.createWebClient(credHubProperties, clientHttpConnector);
this.usingOAuth2 = false;
}
/**
@@ -94,6 +97,7 @@ public class ReactiveCredHubTemplate implements ReactiveCredHubOperations {
this.webClient = CredHubWebClientFactory.createWebClient(credHubProperties, clientHttpConnector,
clientRegistrationRepository, authorizedClientRepository);
this.usingOAuth2 = true;
}
/**
@@ -175,4 +179,8 @@ public class ReactiveCredHubTemplate implements ReactiveCredHubOperations {
throw new CredHubException(e);
}
}
public boolean isUsingOAuth2() {
return this.usingOAuth2;
}
}

View File

@@ -19,9 +19,6 @@ package org.springframework.credhub.integration;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration;
import org.springframework.credhub.core.ReactiveCredHubOperations;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.info.VersionInfo;
@@ -30,10 +27,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestApplication.class,
CredHubAutoConfiguration.class,
CredHubOAuth2TemplateAutoConfiguration.class,
CredHubTemplateAutoConfiguration.class})
@SpringBootTest(classes = {TestApplication.class})
@ActiveProfiles("test")
public abstract class ReactiveCredHubIntegrationTests {

View File

@@ -19,7 +19,9 @@ 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;
@@ -27,21 +29,19 @@ 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.core.CredHubTemplate;
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 {@link CredHubTemplate}.
* {@link EnableAutoConfiguration Auto-configuration} for Spring CredHub support beans.
*
* @author Scott Frederick
* @author Daniel Lavoie
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnProperty(value = "spring.credhub.url")
public class CredHubAutoConfiguration {
private final CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory();
@@ -51,6 +51,8 @@ public class CredHubAutoConfiguration {
* @return a {@link CredHubProperties} bean
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.credhub.url")
@ConfigurationProperties(prefix = "spring.credhub")
public CredHubProperties credHubProperties() {
return new CredHubProperties();
@@ -62,6 +64,8 @@ public class CredHubAutoConfiguration {
* @return a {@link ClientOptions} bean
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.credhub.url")
@ConfigurationProperties(prefix = "spring.credhub")
public ClientOptions clientOptions() {
return new ClientOptions();
@@ -79,6 +83,7 @@ public class CredHubAutoConfiguration {
* instance
*/
@Bean
@ConditionalOnBean(ClientOptions.class)
public ClientFactoryWrapper clientHttpRequestFactoryWrapper(ClientOptions clientOptions) {
return new ClientFactoryWrapper(
credHubTemplateFactory.clientHttpRequestFactoryWrapper(clientOptions));
@@ -91,6 +96,7 @@ public class CredHubAutoConfiguration {
* @return the {@link ClientHttpConnector}
*/
@Bean
@ConditionalOnBean(ClientOptions.class)
@ConditionalOnClass(WebClient.class)
public ClientHttpConnector clientHttpConnector(ClientOptions clientOptions) {
return credHubTemplateFactory.clientHttpConnector(clientOptions);
@@ -103,7 +109,7 @@ public class CredHubAutoConfiguration {
private final ClientHttpRequestFactory clientHttpRequestFactory;
public ClientFactoryWrapper(ClientHttpRequestFactory clientHttpRequestFactory) {
ClientFactoryWrapper(ClientHttpRequestFactory clientHttpRequestFactory) {
this.clientHttpRequestFactory = clientHttpRequestFactory;
}
@@ -121,7 +127,7 @@ public class CredHubAutoConfiguration {
}
}
public ClientHttpRequestFactory getClientHttpRequestFactory() {
ClientHttpRequestFactory getClientHttpRequestFactory() {
return clientHttpRequestFactory;
}
}

View File

@@ -1,13 +1,32 @@
/*
* 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.
* 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.credhub.autoconfig;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
@@ -20,10 +39,16 @@ import org.springframework.security.oauth2.client.web.server.UnAuthenticatedServ
import java.util.ArrayList;
import java.util.List;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring CredHub OAuth2 support beans.
*
* @author Scott Frederick
*/
@Configuration
@EnableConfigurationProperties(OAuth2ClientProperties.class)
@AutoConfigureBefore(ReactiveOAuth2ClientAutoConfiguration.class)
@ConditionalOnClass(ClientRegistration.class)
@Conditional(ClientsConfiguredCondition.class)
public class CredHubOAuth2AutoConfiguration {
private final OAuth2ClientProperties properties;

View File

@@ -1,99 +0,0 @@
/*
* 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.
* 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.credhub.autoconfig;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
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.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import org.springframework.web.reactive.function.client.WebClient;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link CredHubTemplate} with
* OAuth2 credentials.
*
* @author Daniel Lavoie
* @author Scott Frederick
*/
@Configuration
@AutoConfigureAfter({CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
OAuth2ClientAutoConfiguration.class,
ReactiveOAuth2ClientAutoConfiguration.class})
@ConditionalOnProperty("spring.credhub.oauth2.registration-id")
public class CredHubOAuth2TemplateAutoConfiguration {
private final CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory();
/**
* Create the {@link CredHubTemplate} that the application will use to interact
* with CredHub.
*
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientFactoryWrapper a {@link ClientFactoryWrapper}
* to customize CredHub http requests
* @param clientRegistrationRepository a repository of OAuth2 client registrations
* @param authorizedClientService a repository of authorized OAuth2 clients
* @return the {@link CredHubOperations} bean
*/
@Bean
public CredHubOperations oAuth2credHubTemplate(CredHubProperties credHubProperties,
ClientFactoryWrapper clientFactoryWrapper,
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientService authorizedClientService) {
return credHubTemplateFactory.credHubTemplate(credHubProperties,
clientFactoryWrapper.getClientHttpRequestFactory(),
clientRegistrationRepository,
authorizedClientService);
}
/**
* Create the {@link ReactiveCredHubTemplate} that the application will use to interact
* with CredHub.
*
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientHttpConnector a {@link ClientHttpConnector} to customize CredHub
* http requests
* @param clientRegistrationRepository a repository of OAuth2 client registrations
* @param authorizedClientRepository a repository of OAuth2 authorized clients
* @return the {@link CredHubTemplate} bean
*/
@Bean
@ConditionalOnClass(WebClient.class)
public ReactiveCredHubOperations oAuth2reactiveCredHubTemplate(CredHubProperties credHubProperties,
ClientHttpConnector clientHttpConnector,
ReactiveClientRegistrationRepository clientRegistrationRepository,
ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector,
clientRegistrationRepository, authorizedClientRepository);
}
}

View File

@@ -16,11 +16,14 @@
package org.springframework.credhub.autoconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
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;
@@ -31,17 +34,24 @@ 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.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import org.springframework.web.reactive.function.client.WebClient;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link CredHubTemplate}.
*
* @author Scott Frederick
*
* @author Daniel Lavoie
* @author Scott Frederick
*/
@Configuration
@AutoConfigureAfter({CredHubAutoConfiguration.class, CredHubOAuth2TemplateAutoConfiguration.class})
@ConditionalOnProperty(value = "spring.credhub.url")
@AutoConfigureAfter({CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
OAuth2ClientAutoConfiguration.class,
ReactiveOAuth2ClientAutoConfiguration.class})
@ConditionalOnBean(CredHubProperties.class)
public class CredHubTemplateAutoConfiguration {
private final CredHubTemplateFactory credHubTemplateFactory = new CredHubTemplateFactory();
@@ -49,34 +59,71 @@ public class CredHubTemplateAutoConfiguration {
* Create the {@link CredHubTemplate} that the application will use to interact
* with CredHub.
*
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientFactoryWrapper a {@link ClientFactoryWrapper} to customize CredHub
* HTTP requests
* @return the {@link CredHubTemplate} bean
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientFactoryWrapper a {@link ClientFactoryWrapper}
* to customize CredHub HTTP requests
* @param clientRegistrationRepository a repository of OAuth2 client registrations
* @param authorizedClientService a repository of authorized OAuth2 clients
* @return the {@link CredHubOperations} bean
*/
@Bean
@ConditionalOnMissingBean
public CredHubOperations credHubTemplate(CredHubProperties credHubProperties,
ClientFactoryWrapper clientFactoryWrapper) {
public CredHubOperations credHubTemplate(
CredHubProperties credHubProperties,
ClientFactoryWrapper clientFactoryWrapper,
@Autowired(required = false) ClientRegistrationRepository clientRegistrationRepository,
@Autowired(required = false) OAuth2AuthorizedClientService authorizedClientService) {
if (credHubProperties.getOauth2() == null || credHubProperties.getOauth2().getRegistrationId() == null) {
return credHubTemplateFactory.credHubTemplate(credHubProperties,
clientFactoryWrapper.getClientHttpRequestFactory());
}
if (clientRegistrationRepository == null || authorizedClientService == null) {
throw misconfiguredException();
}
return credHubTemplateFactory.credHubTemplate(credHubProperties,
clientFactoryWrapper.getClientHttpRequestFactory());
clientFactoryWrapper.getClientHttpRequestFactory(),
clientRegistrationRepository,
authorizedClientService);
}
/**
* Create the {@link ReactiveCredHubTemplate} that the application will use to interact
* with CredHub.
*
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientHttpConnector a {@link ClientHttpConnector} to customize CredHub
* HTTP requests
* @param credHubProperties {@link CredHubProperties} for CredHub
* @param clientHttpConnector a {@link ClientHttpConnector} to customize CredHub
* HTTP requests
* @param clientRegistrationRepository a repository of OAuth2 client registrations
* @param authorizedClientRepository a repository of OAuth2 authorized clients
* @return the {@link CredHubTemplate} bean
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(WebClient.class)
public ReactiveCredHubOperations reactiveCredHubTemplate(CredHubProperties credHubProperties,
ClientHttpConnector clientHttpConnector) {
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector);
public ReactiveCredHubOperations reactiveCredHubTemplate(
CredHubProperties credHubProperties,
ClientHttpConnector clientHttpConnector,
@Autowired(required = false) ReactiveClientRegistrationRepository clientRegistrationRepository,
@Autowired(required = false) ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
if (credHubProperties.getOauth2() == null || credHubProperties.getOauth2().getRegistrationId() == null) {
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector);
}
if (clientRegistrationRepository == null || authorizedClientRepository == null) {
throw misconfiguredException();
}
return credHubTemplateFactory.credHubTemplate(credHubProperties, clientHttpConnector,
clientRegistrationRepository, authorizedClientRepository);
}
private IllegalArgumentException misconfiguredException() {
return new IllegalArgumentException("A CredHub OAuth2 client registration is configured " +
"but Spring Security is not available or the Spring Security OAuth2 " +
"client registration is misconfigured");
}
}

View File

@@ -1,5 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.credhub.autoconfig.CredHubAutoConfiguration,\
org.springframework.credhub.autoconfig.CredHubOAuth2AutoConfiguration,\
org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration,\
org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration
org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration

View File

@@ -0,0 +1,114 @@
/*
* 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.
* 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.credhub.autoconfig;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
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;
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubAutoConfigurationTests {
private final ApplicationContextRunner context = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
CredHubAutoConfiguration.class
));
@Test
public void autoConfiguredWithDefaultProperties() {
context
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.oauth2.registration-id=test-client",
"spring.credhub.connection-timeout=30",
"spring.credhub.read-timeout=60",
"debug=true"
)
.run(this::assertPropertiesConfigured);
}
@Test
public void autoConfiguredWithCustomProperties() {
context
.withConfiguration(AutoConfigurations.of(CustomPropertiesConfiguration.class))
.withPropertyValues(
"my.custom.credhub.url=https://localhost",
"my.custom.credhub.oauth2.registration-id=test-client",
"my.custom.credhub.connection-timeout=30",
"my.custom.credhub.read-timeout=60"
)
.run(this::assertPropertiesConfigured);
}
@Test
public void webClientConnectorNotConfigured() {
context
.withClassLoader(new FilteredClassLoader(WebClient.class))
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.oauth2.registration-id=test-client",
"spring.credhub.connection-timeout=30",
"spring.credhub.read-timeout=60"
)
.run(context -> assertThat(context).doesNotHaveBean(ClientHttpConnector.class));
}
private void assertPropertiesConfigured(AssertableApplicationContext context) {
assertThat(context).hasSingleBean(CredHubProperties.class);
CredHubProperties properties = context.getBean(CredHubProperties.class);
assertThat(properties.getUrl()).isEqualTo("https://localhost");
assertThat(properties.getOauth2().getRegistrationId()).isEqualTo("test-client");
assertThat(context).hasSingleBean(ClientOptions.class);
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
@AutoConfigureBefore(CredHubAutoConfiguration.class)
public static class CustomPropertiesConfiguration {
@Bean
@ConfigurationProperties(prefix = "my.custom.credhub")
public CredHubProperties credHubProperties() {
return new CredHubProperties();
}
@Bean
@ConfigurationProperties(prefix = "my.custom.credhub")
public ClientOptions clientOptions() {
return new ClientOptions();
}
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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.
* 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.credhub.autoconfig;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubOAuth2AutoConfigurationTests {
private final ApplicationContextRunner context = new ApplicationContextRunner()
.withPropertyValues(
"debug=true"
)
.withConfiguration(AutoConfigurations.of(
CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
ReactiveOAuth2ClientAutoConfiguration.class
));
@Test
public void oauth2ContextConfigured() {
context
.withPropertyValues(
"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
"spring.security.oauth2.client.provider.uaa.token-uri=http://example.com/uaa/oauth/token"
)
.run(context -> {
assertThat(context).hasSingleBean(ClientRegistrationRepository.class);
assertThat(context).hasSingleBean(OAuth2AuthorizedClientService.class);
assertThat(context).hasSingleBean(ReactiveClientRegistrationRepository.class);
assertThat(context).hasSingleBean(ServerOAuth2AuthorizedClientRepository.class);
});
}
@Test
public void oauth2ContextNotConfiguredWithoutProperties() {
context
.run(this::assertNoOAuth2ContextConfigured);
}
@Test
public void oauth2ContextNotConfiguredWithoutSpringSecurity() {
context
.withClassLoader(new FilteredClassLoader(ClientRegistration.class))
.run(this::assertNoOAuth2ContextConfigured);
}
private void assertNoOAuth2ContextConfigured(AssertableApplicationContext context) {
assertThat(context).doesNotHaveBean(ClientRegistrationRepository.class);
assertThat(context).doesNotHaveBean(OAuth2AuthorizedClientService.class);
assertThat(context).doesNotHaveBean(ReactiveClientRegistrationRepository.class);
assertThat(context).doesNotHaveBean(ServerOAuth2AuthorizedClientRepository.class);
}
}

View File

@@ -0,0 +1,109 @@
/*
* 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.
* 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.credhub.autoconfig;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.credhub.core.CredHubTemplate;
import org.springframework.credhub.core.ReactiveCredHubTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubTemplateAutoConfigurationTests {
private final ApplicationContextRunner context = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
ReactiveOAuth2ClientAutoConfiguration.class,
CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
CredHubTemplateAutoConfiguration.class
))
.withPropertyValues(
"debug=true"
);
@Test
public void credHubTemplatesConfigured() {
context
.withPropertyValues(
"spring.credhub.url=https://localhost"
)
.run(context -> {
assertThat(context).hasSingleBean(CredHubTemplate.class);
CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
assertThat(credHubTemplate.isUsingOAuth2()).isFalse();
assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
ReactiveCredHubTemplate reactiveCredHubTemplate = context.getBean(ReactiveCredHubTemplate.class);
assertThat(reactiveCredHubTemplate.isUsingOAuth2()).isFalse();
});
}
@Test
public void reactiveCredHubTemplateNotConfiguredWithoutWebClient() {
context
.withPropertyValues(
"spring.credhub.url=https://localhost"
)
.withClassLoader(new FilteredClassLoader(WebClient.class))
.run(context -> {
assertThat(context).hasSingleBean(CredHubTemplate.class);
CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
assertThat(credHubTemplate.isUsingOAuth2()).isFalse();
assertThat(context).doesNotHaveBean(ReactiveCredHubTemplate.class);
});
}
@Test
public void credHubTemplatesConfiguredWithOAuth2() {
context
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.oauth2.registration-id=credhub-client",
"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
"spring.security.oauth2.client.provider.uaa.token-uri=http://example.com/uaa/oauth/token"
)
.run(context -> {
assertThat(context).hasSingleBean(CredHubTemplate.class);
CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
assertThat(credHubTemplate.isUsingOAuth2()).isTrue();
assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
ReactiveCredHubTemplate reactiveCredHubTemplate = context.getBean(ReactiveCredHubTemplate.class);
assertThat(reactiveCredHubTemplate.isUsingOAuth2()).isTrue();
});
}
@Test
public void credHubTemplatesNotConfiguredWithoutClientRegistration() {
context
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.oauth2.registration-id=credhub-client"
)
.run(context -> assertThat(context).hasFailed());
}
}

View File

@@ -1,57 +0,0 @@
/*
* 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.
* 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.credhub.configuration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration.ClientFactoryWrapper;
import org.springframework.credhub.core.CredHubProperties;
import org.springframework.credhub.support.ClientOptions;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubAutoConfigurationTests {
@Test
public void contextLoads() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CredHubAutoConfiguration.class))
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.connection-timeout=30",
"spring.credhub.read-timeout=60",
"spring.credhub.oauth2.registration-id=test-client"
)
.run((context) -> {
assertThat(context).hasSingleBean(CredHubProperties.class);
CredHubProperties properties = context.getBean(CredHubProperties.class);
assertThat(properties.getUrl()).isEqualTo("https://localhost");
assertThat(properties.getOauth2().getRegistrationId()).isEqualTo("test-client");
assertThat(context).hasSingleBean(ClientOptions.class);
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);
});
}
}

View File

@@ -1,58 +0,0 @@
/*
* 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.
* 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.credhub.configuration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubOAuth2AutoConfiguration;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubOAuth2AutoConfigurationTests {
private ApplicationContextRunner context = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
ReactiveOAuth2ClientAutoConfiguration.class))
.withPropertyValues(
"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
"spring.security.oauth2.client.provider.uaa.token-uri=http://example.com/uaa/oauth/token",
"debug=true"
);
@Test
public void oauth2ContextConfigured() {
context.run((context) -> {
assertThat(context).hasSingleBean(ClientRegistrationRepository.class);
assertThat(context).hasSingleBean(OAuth2AuthorizedClientService.class);
assertThat(context).hasSingleBean(ReactiveClientRegistrationRepository.class);
assertThat(context).hasSingleBean(ServerOAuth2AuthorizedClientRepository.class);
});
}
}

View File

@@ -1,80 +0,0 @@
/*
* 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.
* 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.credhub.configuration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubOAuth2AutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration;
import org.springframework.credhub.core.CredHubTemplate;
import org.springframework.credhub.core.ReactiveCredHubTemplate;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Daniel Lavoie
*/
public class CredHubOAuth2TemplateAutoConfigurationTests {
private ApplicationContextRunner context = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
ReactiveOAuth2ClientAutoConfiguration.class,
CredHubAutoConfiguration.class,
CredHubOAuth2AutoConfiguration.class,
CredHubOAuth2TemplateAutoConfiguration.class,
CredHubTemplateAutoConfiguration.class))
.withPropertyValues(
"spring.credhub.url=https://localhost",
"spring.credhub.oauth2.registration-id=credhub-client",
"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
"spring.security.oauth2.client.provider.uaa.token-uri=http://example.com/uaa/oauth/token",
"debug=true"
);
@Test
public void credHubTemplateConfiguredWithOAuth2() {
context.run((context) -> {
assertThat(context).hasSingleBean(CredHubTemplate.class);
assertThat(context).hasSingleBean(CredHubAutoConfiguration.ClientFactoryWrapper.class);
assertThat(context).hasSingleBean(ClientRegistrationRepository.class);
});
}
@Test
public void reactiveCredHubTemplateConfiguredWithOAuth2() {
context.run((context) -> {
assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
assertThat(context).hasSingleBean(ClientHttpConnector.class);
assertThat(context).hasSingleBean(ReactiveClientRegistrationRepository.class);
assertThat(context).hasSingleBean(ServerOAuth2AuthorizedClientRepository.class);
});
}
}

View File

@@ -1,70 +0,0 @@
/*
* 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.
* 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.credhub.configuration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.credhub.autoconfig.CredHubAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration;
import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration;
import org.springframework.credhub.core.CredHubTemplate;
import org.springframework.credhub.core.ReactiveCredHubTemplate;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
public class CredHubTemplateAutoConfigurationTests {
private final ApplicationContextRunner context = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CredHubAutoConfiguration.class,
CredHubOAuth2TemplateAutoConfiguration.class,
CredHubTemplateAutoConfiguration.class))
.withPropertyValues(
"spring.credhub.url=https://localhost",
"debug");
@Test
public void credHubTemplateConfigured() {
context.run((context) -> {
assertThat(context).hasSingleBean(CredHubTemplate.class);
});
}
@Test
public void reactiveCredHubTemplateConfigured() {
context.run((context) -> {
assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
assertThat(context).hasSingleBean(ClientHttpConnector.class);
assertThat(context).doesNotHaveBean(ReactiveClientRegistrationRepository.class);
assertThat(context).doesNotHaveBean(ServerOAuth2AuthorizedClientRepository.class);
});
}
@Test
public void reactiveCredHubTemplateNotConfiguredWithoutWebClient() {
context.withClassLoader(new FilteredClassLoader(WebClient.class))
.run((context) -> {
assertThat(context).doesNotHaveBean(ReactiveCredHubTemplate.class);
assertThat(context).doesNotHaveBean(ClientHttpConnector.class);
});
}
}