From e3775e4c1ae95f41980bd86e51ec468a7b5b6cf9 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 8 Jun 2021 16:27:33 -0400 Subject: [PATCH] GH-171 Fix SFTP Supplier Related to: https://github.com/spring-cloud/stream-applications/issues/171 It might not be necessary in real world, but with `SshServer` we fail when we disconnect the session and cannot connect it back because that server decides to die for a strange reason like no connections. * Upgrade to SSHD 2.7.0 * Wrap default SFTP SF into a `CachingSessionFactory` to keep sessions opened and don't let the server to die because on connections from clients * Reenable SFTP Supplier tests --- common/function-test-support/pom.xml | 6 +++--- .../cloud/fn/test/support/sftp/SftpTestSupport.java | 4 ++-- .../fn/supplier/sftp/SftpSupplierFactoryConfiguration.java | 3 ++- .../fn/supplier/sftp/SftpSupplierApplicationTests.java | 4 ---- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/common/function-test-support/pom.xml b/common/function-test-support/pom.xml index 4ef1608b..ef009cf2 100644 --- a/common/function-test-support/pom.xml +++ b/common/function-test-support/pom.xml @@ -14,7 +14,7 @@ file consumer - 1.6.0 + 2.7.0 1.1.1 4.0.3 @@ -47,8 +47,8 @@ org.apache.sshd - sshd-core - ${sshd-core.version} + sshd-sftp + ${sshd-sftp.version} diff --git a/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/sftp/SftpTestSupport.java b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/sftp/SftpTestSupport.java index 9e83d95d..ead7a892 100644 --- a/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/sftp/SftpTestSupport.java +++ b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/sftp/SftpTestSupport.java @@ -29,7 +29,7 @@ import java.util.Collections; import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory; import org.apache.sshd.server.SshServer; import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; -import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; +import org.apache.sshd.sftp.server.SftpSubsystemFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -62,7 +62,7 @@ public class SftpTestSupport extends RemoteFileTestSupport { StringUtils.hasText(password) && !"badPassword".equals(password)); // fail if pub key validation failed server.setPublickeyAuthenticator((username, key, session) -> key.equals(decodePublicKey("id_rsa_pp.pub"))); server.setPort(0); - server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser"))); + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder)); server.start(); diff --git a/supplier/sftp-supplier/src/main/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierFactoryConfiguration.java b/supplier/sftp-supplier/src/main/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierFactoryConfiguration.java index d32a6013..868617fb 100644 --- a/supplier/sftp-supplier/src/main/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierFactoryConfiguration.java +++ b/supplier/sftp-supplier/src/main/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierFactoryConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.core.io.FileSystemResource; import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.file.remote.aop.StandardRotationPolicy; +import org.springframework.integration.file.remote.session.CachingSessionFactory; import org.springframework.integration.file.remote.session.DelegatingSessionFactory; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; @@ -87,7 +88,7 @@ public class SftpSupplierFactoryConfiguration { sftpSessionFactory.setKnownHostsResource(new FileSystemResource(path)); } - return sftpSessionFactory; + return new CachingSessionFactory<>(sftpSessionFactory); } public final static class DelegatingFactoryWrapper implements DisposableBean { diff --git a/supplier/sftp-supplier/src/test/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierApplicationTests.java b/supplier/sftp-supplier/src/test/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierApplicationTests.java index fdaa8039..5cfd6ffb 100644 --- a/supplier/sftp-supplier/src/test/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierApplicationTests.java +++ b/supplier/sftp-supplier/src/test/java/org/springframework/cloud/fn/supplier/sftp/SftpSupplierApplicationTests.java @@ -32,7 +32,6 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import reactor.test.StepVerifier; @@ -220,7 +219,6 @@ public class SftpSupplierApplicationTests extends SftpTestSupport { } @Test - @Disabled void deleteRemoteFiles() { defaultApplicationContextRunner .withPropertyValues( @@ -241,7 +239,6 @@ public class SftpSupplierApplicationTests extends SftpTestSupport { } @Test - @Disabled void renameRemoteFilesStream() { defaultApplicationContextRunner .withPropertyValues( @@ -252,7 +249,6 @@ public class SftpSupplierApplicationTests extends SftpTestSupport { } @Test - @Disabled void renameRemoteFiles() { defaultApplicationContextRunner .withPropertyValues(