fix: configure ssh connect timeout value (#2394)

Modify `PropertyBasedSshSessionFactory` and `FileBasedSshSessionFactory` to configure `SshConstants.CONNECT_TIMEOUT` based on the supplied timeout property
This commit is contained in:
Pearson Radu
2024-03-28 14:03:32 -04:00
committed by GitHub
parent f96aab5bd1
commit de0c0e14c7
4 changed files with 26 additions and 0 deletions

View File

@@ -52,6 +52,8 @@ public class FileBasedSshSessionFactory extends SshdSessionFactory {
hostEntry.setValue(SshConstants.STRICT_HOST_KEY_CHECKING,
sshProperties.isStrictHostKeyChecking() ? SshConstants.YES : SshConstants.NO);
hostEntry.setValue(SshConstants.CONNECT_TIMEOUT, String.valueOf(sshProperties.getTimeout()));
return hostEntry;
}
};

View File

@@ -97,6 +97,8 @@ public class PropertyBasedSshSessionFactory extends SshdSessionFactory {
private OpenSshConfigFile.HostEntry updateIfNeeded(OpenSshConfigFile.HostEntry hostEntry, String hostName) {
JGitEnvironmentProperties sshProperties = sshKeysByHostname.get(hostName);
hostEntry.setValue(SshConstants.CONNECT_TIMEOUT, String.valueOf(sshProperties.getTimeout()));
if (sshProperties.getHostKey() == null || !sshProperties.isStrictHostKeyChecking()) {
hostEntry.setValue(SshConstants.STRICT_HOST_KEY_CHECKING, SshConstants.NO);
}

View File

@@ -82,6 +82,17 @@ public class FileBasedSshSessionFactoryTest {
assertThat(configStore).isNull();
}
@Test
public void connectTimeoutIsUsed() {
JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties();
sshKey.setUri("ssh://gitlab.example.local:3322/somerepo.git");
setupSessionFactory(sshKey);
SshConfigStore.HostConfig sshConfig = getSshHostConfig("gitlab.example.local");
assertThat(sshConfig.getValue("ConnectTimeout")).isEqualTo("5");
}
private SshConfigStore.HostConfig getSshHostConfig(String hostName) {
return factory.createSshConfigStore(new File("dummy"), new File("dummy"), "localUserName").lookup(hostName, 22,
"userName");

View File

@@ -312,6 +312,17 @@ public class PropertyBasedSshSessionFactoryTest {
assertThat(proxyData.getProxy().address().toString()).containsPattern("host\\.domain.*:8080");
}
@Test
public void connectTimeoutIsUsed() {
JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties();
sshKey.setUri("ssh://gitlab.example.local:3322/somerepo.git");
setupSessionFactory(sshKey);
SshConfigStore.HostConfig sshConfig = getSshHostConfig("gitlab.example.local");
assertThat(sshConfig.getValue("ConnectTimeout")).isEqualTo("5");
}
@Test
public void sshConfigFileIsNotUsed() {
setupSessionFactory(new JGitEnvironmentProperties());