'Apache Mina SFTP: Mount Remote Sub-Directory instead of Filesystem Root

I would to use Apache SSHD to create an SFTP server and use SftpFileSystemProvider to mount a remote directory.

I successfully create the virtual file system with SftpFileSystemProvider following the documentation https://github.com/apache/mina-sshd/blob/master/docs/sftp.md#using-sftpfilesystemprovider-to-create-an-sftpfilesystem.

However I'm stuck when mouting remote directory even with the documentation https://github.com/apache/mina-sshd/blob/master/docs/sftp.md#configuring-the-sftpfilesystemprovider. It keeps mouting the root directory instead of the target one.

I tried:

  • adding the target directory into the sftp uri (not working)
  • getting new filesystem from path (not working)

Here is a quick example.

object Main:
  
  class Events extends SftpEventListener

  class Auth extends PasswordAuthenticator {
    override def authenticate(username: String, password: String, session: ServerSession): Boolean = {
      true
    }
  }

  class FilesSystem extends VirtualFileSystemFactory {

    override def createFileSystem(session: SessionContext): FileSystem = {
      val uri = new URI("sftp://xxx:yyy@host/plop")
//      val uri = SftpFileSystemProvider.createFileSystemURI("host", 22, "xxx", "yyy")
      val fs = Try(FileSystems.newFileSystem(uri, Collections.emptyMap[String, Object](), new SftpFileSystemProvider().getClass().getClassLoader())) match {
        case Failure(exception) =>
          println("Failed to mount bucket")
          println(exception.getMessage)
          throw exception
        case Success(filesSystem) =>
          println("Bucket mounted")
          filesSystem
      }
      //fs.getPath("plop").getFileSystem
      fs
    }
  }

  private val fs = new FilesSystem()

  private val sftpSubSystem = new SftpSubsystemFactory.Builder().build()
  sftpSubSystem.addSftpEventListener(new Events())
  private val sshd: SshServer = SshServer.setUpDefaultServer()
  sshd.setPort(22)
  sshd.setHost("0.0.0.0")
  sshd.setSubsystemFactories(Collections.singletonList(sftpSubSystem))
  sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("hostkey.ser")))
  sshd.setShellFactory(new InteractiveProcessShellFactory())
  sshd.setCommandFactory(new ScpCommandFactory())
  sshd.setFileSystemFactory(fs)
  sshd.setPasswordAuthenticator(new Auth())
  sshd.setSessionHeartbeat(HeartbeatType.IGNORE, Duration.ofSeconds(30L))

  @main def m() = {
    sshd.start()
    while (sshd.isStarted) {

    }
  }

end Main

Am I missing something ?

SSHD version 2.8.0, SFTP protocol version 3, Scala3, Java11



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source