From e9c8570803dc87af08eba2afb29bdcbc3c968669 Mon Sep 17 00:00:00 2001 From: Kaveh Shamsi Date: Wed, 14 Jun 2023 20:08:44 +0200 Subject: [PATCH] Configures SshSessionFactory only if there is at least one git uri with ssh scheme (#2288) --- .../FileBasedSshTransportConfigCallback.java | 18 ++++- ...ertiesBasedSshTransportConfigCallback.java | 20 ++++-- ...ransportConfigurationIntegrationTests.java | 66 +++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/FileBasedSshTransportConfigCallback.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/FileBasedSshTransportConfigCallback.java index fa408c64..b78c4ef5 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/FileBasedSshTransportConfigCallback.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/FileBasedSshTransportConfigCallback.java @@ -16,10 +16,13 @@ package org.springframework.cloud.config.server.ssh; +import java.util.Map; + import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.Transport; +import org.springframework.cloud.config.server.environment.JGitEnvironmentProperties; import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties; /** @@ -32,8 +35,19 @@ public class FileBasedSshTransportConfigCallback implements TransportConfigCallb private final MultipleJGitEnvironmentProperties sshUriProperties; + private final FileBasedSshSessionFactory sshdSessionFactory; + public FileBasedSshTransportConfigCallback(MultipleJGitEnvironmentProperties sshUriProperties) { this.sshUriProperties = sshUriProperties; + Map sshKeysByHostname = new SshUriPropertyProcessor(this.sshUriProperties) + .getSshKeysByHostname(); + if (sshKeysByHostname.isEmpty()) { + this.sshdSessionFactory = null; + } + else { + this.sshdSessionFactory = new FileBasedSshSessionFactory(sshUriProperties); + } + } public MultipleJGitEnvironmentProperties getSshUriProperties() { @@ -42,8 +56,8 @@ public class FileBasedSshTransportConfigCallback implements TransportConfigCallb @Override public void configure(Transport transport) { - if (transport instanceof SshTransport) { - ((SshTransport) transport).setSshSessionFactory(new FileBasedSshSessionFactory(sshUriProperties)); + if (this.sshdSessionFactory != null && transport instanceof SshTransport) { + ((SshTransport) transport).setSshSessionFactory(this.sshdSessionFactory); } } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/PropertiesBasedSshTransportConfigCallback.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/PropertiesBasedSshTransportConfigCallback.java index f07545cf..12514006 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/PropertiesBasedSshTransportConfigCallback.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/PropertiesBasedSshTransportConfigCallback.java @@ -16,10 +16,13 @@ package org.springframework.cloud.config.server.ssh; +import java.util.Map; + import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.Transport; +import org.springframework.cloud.config.server.environment.JGitEnvironmentProperties; import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties; /** @@ -30,10 +33,20 @@ import org.springframework.cloud.config.server.environment.MultipleJGitEnvironme */ public class PropertiesBasedSshTransportConfigCallback implements TransportConfigCallback { - private MultipleJGitEnvironmentProperties sshUriProperties; + private final MultipleJGitEnvironmentProperties sshUriProperties; + + private final PropertyBasedSshSessionFactory sshdSessionFactory; public PropertiesBasedSshTransportConfigCallback(MultipleJGitEnvironmentProperties sshUriProperties) { this.sshUriProperties = sshUriProperties; + Map sshKeysByHostname = new SshUriPropertyProcessor(this.sshUriProperties) + .getSshKeysByHostname(); + if (sshKeysByHostname.isEmpty()) { + this.sshdSessionFactory = null; + } + else { + this.sshdSessionFactory = new PropertyBasedSshSessionFactory(sshKeysByHostname); + } } public MultipleJGitEnvironmentProperties getSshUriProperties() { @@ -42,9 +55,8 @@ public class PropertiesBasedSshTransportConfigCallback implements TransportConfi @Override public void configure(Transport transport) { - if (transport instanceof SshTransport) { - ((SshTransport) transport).setSshSessionFactory(new PropertyBasedSshSessionFactory( - new SshUriPropertyProcessor(this.sshUriProperties).getSshKeysByHostname())); + if (this.sshdSessionFactory != null && transport instanceof SshTransport) { + ((SshTransport) transport).setSshSessionFactory(this.sshdSessionFactory); } } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/TransportConfigurationIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/TransportConfigurationIntegrationTests.java index 40fcf444..c2f01b92 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/TransportConfigurationIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/TransportConfigurationIntegrationTests.java @@ -24,6 +24,7 @@ import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.transport.FetchConnection; import org.eclipse.jgit.transport.PushConnection; import org.eclipse.jgit.transport.SshConfigStore; +import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.SshTransport; import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.sshd.SshdSessionFactory; @@ -339,6 +340,71 @@ public class TransportConfigurationIntegrationTests { } + public static class CallbackWithHttpUrlsOnly { + + @RunWith(SpringRunner.class) + @SpringBootTest(classes = { TestConfigServerApplication.class, SshPropertyValidator.class }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { "spring.cloud.config.server.git.uri=https://gitserver.com/team/repo.git" }) + @ActiveProfiles({ "test", "git" }) + public static class StaticTest { + + @Autowired + private MultipleJGitEnvironmentRepository jGitEnvironmentRepository; + + @Test + public void sshTransportCallbackIsConfigured() { + TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository + .getTransportConfigCallback(); + assertThat(transportConfigCallback).isNotNull(); + } + + @Test + public void noSessionFactoryIsConfiguredForSshTransports() throws Exception { + this.jGitEnvironmentRepository.afterPropertiesSet(); + + SshTransport sshTransport = DummySshTransport.newInstance(); + SshSessionFactory defaultSessionFactory = sshTransport.getSshSessionFactory(); + this.jGitEnvironmentRepository.getTransportConfigCallback().configure(sshTransport); + + assertThat(sshTransport.getSshSessionFactory()).isSameAs(defaultSessionFactory); + } + + } + + @RunWith(SpringRunner.class) + @SpringBootTest(classes = { TestConfigServerApplication.class, SshPropertyValidator.class }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { "spring.cloud.config.server.composite[0].type=git", + "spring.cloud.config.server.composite[0].uri=https://gitserver.com/team/repo.git" }) + @ActiveProfiles({ "test", "composite" }) + public static class ListTest { + + @Autowired + private MultipleJGitEnvironmentRepository jGitEnvironmentRepository; + + @Test + public void sshTransportCallbackIsConfigured() { + TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository + .getTransportConfigCallback(); + assertThat(transportConfigCallback).isNotNull(); + } + + @Test + public void noSessionFactoryIsConfiguredForSshTransports() throws Exception { + this.jGitEnvironmentRepository.afterPropertiesSet(); + + SshTransport sshTransport = DummySshTransport.newInstance(); + SshSessionFactory defaultSessionFactory = sshTransport.getSshSessionFactory(); + this.jGitEnvironmentRepository.getTransportConfigCallback().configure(sshTransport); + + assertThat(sshTransport.getSshSessionFactory()).isSameAs(defaultSessionFactory); + } + + } + + } + private static class DummySshTransport extends SshTransport { DummySshTransport(String uri) throws URISyntaxException {