From 4d6cf75452612ee05cb95c8fcb935a19b5ce7bf1 Mon Sep 17 00:00:00 2001 From: FWinkler79 <52044081+FWinkler79@users.noreply.github.com> Date: Wed, 15 Jan 2020 19:06:25 +0100 Subject: [PATCH] Allows overriding of configTokenProvider. Made sure configTokenProvider default bean will be overridden by more specific versions (e.g. vault configTokenProvider, etc.) Fixes gh-1485 Fixes gh-1537 --- .../EnvironmentRepositoryConfiguration.java | 19 ++---- ...vironmentRepositoryConfigurationTests.java | 64 +++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java index f67836c9..38262fa0 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java @@ -125,6 +125,13 @@ public class EnvironmentRepositoryConfiguration { return new MultipleJGitEnvironmentProperties(); } + @Bean + @ConditionalOnMissingBean(ConfigTokenProvider.class) + public ConfigTokenProvider defaultConfigTokenProvider( + ObjectProvider httpRequest) { + return new HttpRequestConfigTokenProvider(httpRequest); + } + @Configuration(proxyBeanMethods = false) @ConditionalOnProperty("spring.cloud.config.server.consul.watch.enabled") protected static class ConsulEnvironmentWatchConfiguration { @@ -147,18 +154,6 @@ public class EnvironmentRepositoryConfiguration { } - @Configuration(proxyBeanMethods = false) - @ConditionalOnMissingBean(ConfigTokenProvider.class) - protected static class DefaultConfigTokenProvider { - - @Bean - public ConfigTokenProvider defaultConfigTokenProvider( - ObjectProvider httpRequest) { - return new HttpRequestConfigTokenProvider(httpRequest); - } - - } - @Configuration(proxyBeanMethods = false) @ConditionalOnClass(TransportConfigCallback.class) static class JGitFactoryConfig { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java new file mode 100644 index 00000000..f9555273 --- /dev/null +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2018-2019 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 + * + * https://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.config.server.config; + +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.config.server.environment.ConfigTokenProvider; +import org.springframework.cloud.config.server.environment.EnvironmentConfigTokenProvider; +import org.springframework.context.annotation.Bean; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EnvironmentRepositoryConfigurationTests { + + @Test + public void configTokenProviderCanBeOverridden() { + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations + .of(EnvironmentRepositoryConfiguration.class, TestBeans.class)) + .withPropertyValues("spring.profiles.active=composite", + "spring.cloud.config.server.vault.authentication=TOKEN", + "spring.cloud.config.server.vault.token=testTokenValue", + "spring.cloud.config.server.composite[0].type=vault", + "spring.cloud.config.server.composite[1].type=git", + "spring.cloud.config.server.composite[1].uri=https://test.com/Some-Test-Repo.git") + .run((context) -> { + assertThat(context.getBean(ConfigTokenProvider.class)).isNotNull(); + assertThat(context.getBean(ConfigTokenProvider.class)) + .isInstanceOf(EnvironmentConfigTokenProvider.class); + EnvironmentConfigTokenProvider tokenProvider = context + .getBean(EnvironmentConfigTokenProvider.class); + assertThat(tokenProvider.getToken()).isEqualTo("testTokenValue"); + }); + } + + @TestConfiguration + public static class TestBeans { + + @Bean + public ConfigServerProperties vaultConfigServerProperties() { + ConfigServerProperties configServerProperties = new ConfigServerProperties(); + return configServerProperties; + } + + } + +}