diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java index 3934e71d9b..da50831a72 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java @@ -28,11 +28,13 @@ import java.util.concurrent.locks.ReentrantLock; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.auth.keyboard.UserInteraction; import org.apache.sshd.client.auth.password.PasswordIdentityProvider; +import org.apache.sshd.client.channel.ChannelSubsystem; import org.apache.sshd.client.config.hosts.HostConfigEntry; import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier; import org.apache.sshd.client.keyverifier.RejectAllServerKeyVerifier; import org.apache.sshd.client.keyverifier.ServerKeyVerifier; import org.apache.sshd.client.session.ClientSession; +import org.apache.sshd.common.PropertyResolverUtils; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.config.keys.FilePasswordProvider; import org.apache.sshd.common.keyprovider.KeyIdentityProvider; @@ -44,9 +46,11 @@ import org.apache.sshd.common.util.security.SecurityUtils; import org.apache.sshd.sftp.client.SftpClient; import org.apache.sshd.sftp.client.SftpErrorDataHandler; import org.apache.sshd.sftp.client.SftpVersionSelector; +import org.apache.sshd.sftp.client.impl.AbstractSftpClient; import org.apache.sshd.sftp.client.impl.DefaultSftpClient; import org.springframework.core.io.Resource; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.file.remote.session.SharedSessionCapable; import org.springframework.util.Assert; @@ -107,7 +111,7 @@ public class DefaultSftpSessionFactory implements SessionFactory0, which means, - * that no timeout will occur. - * @param timeout The timeout. + * the default connection timeout. Defaults to {@code 30 seconds}. + * Setting to {@code 0} means no timeout; to {@code null} - infinite wait. + * @param timeout the timeout. * @see org.apache.sshd.client.future.ConnectFuture#verify(Duration, org.apache.sshd.common.future.CancelOption...) */ public void setTimeout(Integer timeout) { @@ -420,8 +424,10 @@ public class DefaultSftpSessionFactory implements SessionFactory + * Also sets the provided {@link #timeout} as a {@link AbstractSftpClient#SFTP_CLIENT_CMD_TIMEOUT} property. */ - protected static class ConcurrentSftpClient extends DefaultSftpClient { + protected class ConcurrentSftpClient extends DefaultSftpClient { private final Lock sendLock = new ReentrantLock(); @@ -442,6 +448,14 @@ public class DefaultSftpSessionFactory implements SessionFactory true); + server.setPort(0); + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); + server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); + server.start(); + + DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); + sftpSessionFactory.setHost("localhost"); + sftpSessionFactory.setPort(server.getPort()); + sftpSessionFactory.setUser("user"); + sftpSessionFactory.setPassword("pass"); + sftpSessionFactory.setAllowUnknownKeys(true); + sftpSessionFactory.setTimeout(15_000); + + ClientChannel clientChannel = sftpSessionFactory.getSession().getClientInstance().getClientChannel(); + + assertThat(AbstractSftpClient.SFTP_CLIENT_CMD_TIMEOUT.getRequired(clientChannel)).hasSeconds(15); + } + } + } diff --git a/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc b/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc index 9045f489bc..2e11d8cc0a 100644 --- a/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc +++ b/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc @@ -97,7 +97,8 @@ The passphrase is obtained from that object. Optional. `timeout`::The timeout property is used as the socket timeout parameter, as well as the default connection timeout. -Defaults to `0`, which means, that no timeout will occur. +Defaults to `30 seconds`. +Setting to `0` means no timeout; to `null` - infinite wait. [[sftp-unk-keys]] `allowUnknownKeys`::Set to `true` to allow connections to hosts with unknown (or changed) keys.