Configures SshSessionFactory only if there is at least one git uri with ssh scheme (#2288)

This commit is contained in:
Kaveh Shamsi
2023-06-14 20:08:44 +02:00
committed by GitHub
parent 0febbc0a92
commit e9c8570803
3 changed files with 98 additions and 6 deletions

View File

@@ -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<String, JGitEnvironmentProperties> 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);
}
}

View File

@@ -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<String, JGitEnvironmentProperties> 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);
}
}

View File

@@ -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 {