Make GitCredentialsProviderFactory a bean. Fixes #1896 (#1926)

This commit is contained in:
Ryan Baxter
2021-07-08 11:28:41 -04:00
committed by GitHub
parent c6a07ed234
commit b7732ca4c5
3 changed files with 57 additions and 4 deletions

View File

@@ -75,6 +75,7 @@ import org.springframework.cloud.config.server.environment.VaultEnvironmentRepos
import org.springframework.cloud.config.server.environment.vault.SpringVaultClientConfiguration;
import org.springframework.cloud.config.server.environment.vault.SpringVaultEnvironmentRepository;
import org.springframework.cloud.config.server.environment.vault.SpringVaultEnvironmentRepositoryFactory;
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
import org.springframework.cloud.config.server.support.GoogleCloudSourceSupport;
import org.springframework.cloud.config.server.support.TransportConfigCallbackFactory;
import org.springframework.context.annotation.Bean;
@@ -163,11 +164,18 @@ public class EnvironmentRepositoryConfiguration {
ConfigurableEnvironment environment, ConfigServerProperties server,
Optional<ConfigurableHttpConnectionFactory> jgitHttpConnectionFactory,
Optional<TransportConfigCallback> customTransportConfigCallback,
Optional<GoogleCloudSourceSupport> googleCloudSourceSupport) {
Optional<GoogleCloudSourceSupport> googleCloudSourceSupport,
GitCredentialsProviderFactory gitCredentialsProviderFactory) {
final TransportConfigCallbackFactory transportConfigCallbackFactory = new TransportConfigCallbackFactory(
customTransportConfigCallback.orElse(null), googleCloudSourceSupport.orElse(null));
return new MultipleJGitEnvironmentRepositoryFactory(environment, server, jgitHttpConnectionFactory,
transportConfigCallbackFactory);
transportConfigCallbackFactory, gitCredentialsProviderFactory);
}
@Bean
@ConditionalOnMissingBean
public GitCredentialsProviderFactory gitCredentialsProviderFactory() {
return new GitCredentialsProviderFactory();
}
}

View File

@@ -21,6 +21,7 @@ import java.util.Optional;
import org.eclipse.jgit.transport.HttpTransport;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory;
import org.springframework.cloud.config.server.support.TransportConfigCallbackFactory;
import org.springframework.core.env.ConfigurableEnvironment;
@@ -38,19 +39,32 @@ public class MultipleJGitEnvironmentRepositoryFactory
private final TransportConfigCallbackFactory transportConfigCallbackFactory;
private final GitCredentialsProviderFactory gitCredentialsProviderFactory;
@Deprecated
public MultipleJGitEnvironmentRepositoryFactory(ConfigurableEnvironment environment, ConfigServerProperties server,
TransportConfigCallbackFactory transportConfigCallbackFactory) {
this(environment, server, Optional.empty(), transportConfigCallbackFactory);
this(environment, server, Optional.empty(), transportConfigCallbackFactory,
new GitCredentialsProviderFactory());
}
@Deprecated
public MultipleJGitEnvironmentRepositoryFactory(ConfigurableEnvironment environment, ConfigServerProperties server,
Optional<ConfigurableHttpConnectionFactory> connectionFactory,
TransportConfigCallbackFactory transportConfigCallbackFactory) {
this(environment, server, connectionFactory, transportConfigCallbackFactory,
new GitCredentialsProviderFactory());
}
public MultipleJGitEnvironmentRepositoryFactory(ConfigurableEnvironment environment, ConfigServerProperties server,
Optional<ConfigurableHttpConnectionFactory> connectionFactory,
TransportConfigCallbackFactory transportConfigCallbackFactory) {
TransportConfigCallbackFactory transportConfigCallbackFactory,
GitCredentialsProviderFactory gitCredentialsProviderFactory) {
this.environment = environment;
this.server = server;
this.connectionFactory = connectionFactory;
this.transportConfigCallbackFactory = transportConfigCallbackFactory;
this.gitCredentialsProviderFactory = gitCredentialsProviderFactory;
}
@Override
@@ -67,6 +81,7 @@ public class MultipleJGitEnvironmentRepositoryFactory
if (this.server.getDefaultLabel() != null) {
repository.setDefaultLabel(this.server.getDefaultLabel());
}
repository.setGitCredentialsProviderFactory(gitCredentialsProviderFactory);
return repository;
}

View File

@@ -19,10 +19,12 @@ package org.springframework.cloud.config.server.config;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
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.cloud.config.server.support.GitCredentialsProviderFactory;
import org.springframework.context.annotation.Bean;
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,6 +51,19 @@ public class EnvironmentRepositoryConfigurationTests {
});
}
@Test
public void customGitCredentialsProvider() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GitTestBeans.class, TestBeans.class,
EnvironmentRepositoryConfiguration.class))
.withPropertyValues("spring.profiles.active=git",
"spring.cloud.config.server.git.uri=http://github.com/user/test")
.run((context) -> {
assertThat(context.getBean(GitCredentialsProviderFactory.class))
.isInstanceOf(GitTestBeans.CustomGitCredentialsProviderFactory.class);
});
}
@TestConfiguration
public static class TestBeans {
@@ -60,4 +75,19 @@ public class EnvironmentRepositoryConfigurationTests {
}
@TestConfiguration
@AutoConfigureBefore(EnvironmentRepositoryConfiguration.class)
public static class GitTestBeans {
@Bean
public GitCredentialsProviderFactory customGitCredentialsProviderFactory() {
return new CustomGitCredentialsProviderFactory();
}
public static class CustomGitCredentialsProviderFactory extends GitCredentialsProviderFactory {
}
}
}