apache / mina-sshd

Apache MINA sshd is a comprehensive Java library for client- and server-side SSH.
https://mina.apache.org/sshd-project/
Apache License 2.0
847 stars 353 forks source link

Multiple connection to remote sftp using same user throwing FileSystemAlreadyExistsException #521

Open rashmimaharana opened 1 week ago

rashmimaharana commented 1 week ago

Description

We did the basic setup for apache mina sshd server and set the FileSystemFactory of sshd (Ex: sshd.setFileSystemFactory(new SftpClientFileSystemFactory());) to connect to remote sftp server. Its working fine with one connection but when we tried to attempt multiple connection using same user it throws FileSystemAlreadyExistsException.

Sample code from custom SftpClientFileSystemFactory

public class SftpClientFileSystemFactory implements FileSystemFactory { private static final Logger logger = LoggerFactory.getLogger(SftpClientFileSystemFactory.class.getName());

public SftpClientFileSystemFactory() {
}

public Path getUserHomeDir(SessionContext session) throws IOException {  
    return null;
}

public FileSystem createFileSystem(SessionContext session) throws IOException {
    String username = session.getUsername();
    FileSystem fs = null;
    URI uri = SftpFileSystemProvider.createFileSystemURI("sftp.remote.test", 2022, "user", "pass"); //Example
    try {        
        fs = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());         
    } catch (IOException e) {
        logger.error("An error occurred while creating the file system or walking the file tree", e);
    }       
    return fs;
}

}

sshd server config
SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(22); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); sshd.setPasswordAuthenticator((username, password, session) -> { return true; });

sshd.setPublickeyAuthenticator((username, key, session) -> { return true; }); sshd.setFileSystemFactory(new SftpClientFileSystemFactory()); sshd.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));
sshd.start();

Motivation

Wanted to check if this functionality exists or its not valid.

Alternatives considered

No response

Additional context

No response