Skip to content

Commit de0c0e1

Browse files
authored
fix: configure ssh connect timeout value (#2394)
Modify `PropertyBasedSshSessionFactory` and `FileBasedSshSessionFactory` to configure `SshConstants.CONNECT_TIMEOUT` based on the supplied timeout property
1 parent f96aab5 commit de0c0e1

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/FileBasedSshSessionFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public HostEntry lookup(@NonNull String hostName, int port, String userName) {
5252
hostEntry.setValue(SshConstants.STRICT_HOST_KEY_CHECKING,
5353
sshProperties.isStrictHostKeyChecking() ? SshConstants.YES : SshConstants.NO);
5454

55+
hostEntry.setValue(SshConstants.CONNECT_TIMEOUT, String.valueOf(sshProperties.getTimeout()));
56+
5557
return hostEntry;
5658
}
5759
};

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public HostConfig lookupDefault(String hostName, int port, String userName) {
9797
private OpenSshConfigFile.HostEntry updateIfNeeded(OpenSshConfigFile.HostEntry hostEntry, String hostName) {
9898
JGitEnvironmentProperties sshProperties = sshKeysByHostname.get(hostName);
9999

100+
hostEntry.setValue(SshConstants.CONNECT_TIMEOUT, String.valueOf(sshProperties.getTimeout()));
101+
100102
if (sshProperties.getHostKey() == null || !sshProperties.isStrictHostKeyChecking()) {
101103
hostEntry.setValue(SshConstants.STRICT_HOST_KEY_CHECKING, SshConstants.NO);
102104
}

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/FileBasedSshSessionFactoryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public void handlesNullConfigFile() {
8282
assertThat(configStore).isNull();
8383
}
8484

85+
@Test
86+
public void connectTimeoutIsUsed() {
87+
JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties();
88+
sshKey.setUri("ssh://gitlab.example.local:3322/somerepo.git");
89+
setupSessionFactory(sshKey);
90+
91+
SshConfigStore.HostConfig sshConfig = getSshHostConfig("gitlab.example.local");
92+
93+
assertThat(sshConfig.getValue("ConnectTimeout")).isEqualTo("5");
94+
}
95+
8596
private SshConfigStore.HostConfig getSshHostConfig(String hostName) {
8697
return factory.createSshConfigStore(new File("dummy"), new File("dummy"), "localUserName").lookup(hostName, 22,
8798
"userName");

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactoryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ public void proxySettingsIsUsedWithRepoIp() {
312312
assertThat(proxyData.getProxy().address().toString()).containsPattern("host\\.domain.*:8080");
313313
}
314314

315+
@Test
316+
public void connectTimeoutIsUsed() {
317+
JGitEnvironmentProperties sshKey = new JGitEnvironmentProperties();
318+
sshKey.setUri("ssh://gitlab.example.local:3322/somerepo.git");
319+
setupSessionFactory(sshKey);
320+
321+
SshConfigStore.HostConfig sshConfig = getSshHostConfig("gitlab.example.local");
322+
323+
assertThat(sshConfig.getValue("ConnectTimeout")).isEqualTo("5");
324+
}
325+
315326
@Test
316327
public void sshConfigFileIsNotUsed() {
317328
setupSessionFactory(new JGitEnvironmentProperties());

0 commit comments

Comments
 (0)