From 17feb07e9436e6ab5f40ed9af3f4a71f87d11914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 26 Apr 2024 14:36:02 +0200 Subject: [PATCH] change ftp password to string type --- .../org/xbib/io/ftp/fs/FTPEnvironment.java | 3 +- .../org/apache/sshd/fs/SftpFileSystem.java | 7 +++-- ...ftpFileSystemClientSessionInitializer.java | 31 ++++++++++++++----- .../sshd/fs/SftpFileSystemProvider.java | 4 ++- .../sshd/fs/test/SFTPFileSystemTest.java | 4 ++- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/files-ftp-fs/src/main/java/org/xbib/io/ftp/fs/FTPEnvironment.java b/files-ftp-fs/src/main/java/org/xbib/io/ftp/fs/FTPEnvironment.java index 7e10fd5..4d62750 100644 --- a/files-ftp-fs/src/main/java/org/xbib/io/ftp/fs/FTPEnvironment.java +++ b/files-ftp-fs/src/main/java/org/xbib/io/ftp/fs/FTPEnvironment.java @@ -734,8 +734,7 @@ public class FTPEnvironment implements Map, Cloneable { } String username = getUsername(); - char[] passwordChars = FileSystemProviderSupport.getValue(this, PASSWORD, char[].class, null); - String password = passwordChars != null ? new String(passwordChars) : null; + String password = FileSystemProviderSupport.getValue(this, PASSWORD, String.class, null); String account = FileSystemProviderSupport.getValue(this, ACCOUNT, String.class, null); if (account != null) { if (!client.login(username, password, account)) { diff --git a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystem.java b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystem.java index 2698457..7ffdf51 100644 --- a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystem.java +++ b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystem.java @@ -76,8 +76,11 @@ public class SftpFileSystem private int writeBufferSize; private final List stores; - public SftpFileSystem(SftpFileSystemProvider provider, String id, ClientSession session, - SftpClientFactory factory, SftpVersionSelector selector) throws IOException { + public SftpFileSystem(SftpFileSystemProvider provider, + String id, + ClientSession session, + SftpClientFactory factory, + SftpVersionSelector selector) throws IOException { super(provider); this.id = id; this.clientSession = Objects.requireNonNull(session, "No client session"); diff --git a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemClientSessionInitializer.java b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemClientSessionInitializer.java index f07718c..1835737 100644 --- a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemClientSessionInitializer.java +++ b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemClientSessionInitializer.java @@ -22,6 +22,7 @@ package org.apache.sshd.fs; import java.io.IOException; import java.util.Map; +import org.apache.sshd.client.config.hosts.HostConfigEntry; import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.client.session.ClientSessionCreator; import org.apache.sshd.common.auth.PasswordHolder; @@ -50,13 +51,28 @@ public interface SftpFileSystemClientSessionInitializer { * @return The created {@link ClientSession} * @throws IOException If failed to connect */ - default ClientSession createClientSession( - SftpFileSystemProvider provider, SftpFileSystemInitializationContext context) + default ClientSession createClientSession(SftpFileSystemProvider provider, + SftpFileSystemInitializationContext context) throws IOException { UsernameHolder user = context.getCredentials(); ClientSessionCreator client = provider.getClientInstance(); - return client.connect(user.getUsername(), context.getHost(), context.getPort()) - .verifyDuration(context.getMaxConnectTime()).getSession(); + // proxy? + String proxy = (String) context.getEnvironment().get("proxy"); + if (proxy != null) { + HostConfigEntry entry = new HostConfigEntry(); + entry.setProxyJump(proxy); + entry.setUsername(user.getUsername()); + entry.setHostName(context.getHost()); + entry.setHost(context.getHost()); + entry.setPort(context.getPort()); + return client.connect(entry) + .verifyDuration(context.getMaxConnectTime()) + .getSession(); + } else { + return client.connect(user.getUsername(), context.getHost(), context.getPort()) + .verifyDuration(context.getMaxConnectTime()) + .getSession(); + } } /** @@ -90,9 +106,10 @@ public interface SftpFileSystemClientSessionInitializer { * @return The created {@link SftpFileSystem} * @throws IOException If failed to create the file-system */ - default SftpFileSystem createSftpFileSystem( - SftpFileSystemProvider provider, SftpFileSystemInitializationContext context, ClientSession session, - SftpVersionSelector selector) + default SftpFileSystem createSftpFileSystem(SftpFileSystemProvider provider, + SftpFileSystemInitializationContext context, + ClientSession session, + SftpVersionSelector selector) throws IOException { return new SftpFileSystem(provider, context.getId(), session, provider.getSftpClientFactory(), selector); } diff --git a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemProvider.java b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemProvider.java index d4c0d68..b0e241c 100644 --- a/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemProvider.java +++ b/files-sftp-fs/src/main/java/org/apache/sshd/fs/SftpFileSystemProvider.java @@ -155,7 +155,9 @@ public class SftpFileSystemProvider extends FileSystemProvider { this(client, null, selector); } - public SftpFileSystemProvider(SshClient client, SftpClientFactory factory, SftpVersionSelector selector) { + public SftpFileSystemProvider(SshClient client, + SftpClientFactory factory, + SftpVersionSelector selector) { this.factory = factory; this.versionSelector = selector; if (client == null) { diff --git a/files-sftp-fs/src/test/java/org/apache/sshd/fs/test/SFTPFileSystemTest.java b/files-sftp-fs/src/test/java/org/apache/sshd/fs/test/SFTPFileSystemTest.java index 5ceda1b..4f2b7b8 100644 --- a/files-sftp-fs/src/test/java/org/apache/sshd/fs/test/SFTPFileSystemTest.java +++ b/files-sftp-fs/src/test/java/org/apache/sshd/fs/test/SFTPFileSystemTest.java @@ -3,6 +3,8 @@ package org.apache.sshd.fs.test; import java.nio.file.Files; import org.apache.sshd.client.ClientBuilder; import org.apache.sshd.client.SshClient; +import org.apache.sshd.client.config.hosts.HostConfigEntry; +import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.fs.SftpFileSystem; import org.apache.sshd.fs.SftpFileSystemProvider; import org.junit.jupiter.api.Test; @@ -20,7 +22,7 @@ public class SFTPFileSystemTest { private static final Logger logger = Logger.getLogger(SFTPFileSystemTest.class.getName()); @Test - public void init() throws Exception { + public void testXbib() throws Exception { Map env = new HashMap<>(); env.put("username", "joerg"); URI uri = URI.create("sftp://xbib.org");