Search code examples
jakarta-eessh-keysapache-vfs

Apache VFS2 with Private Key but without a key file


I am wanting to use VFS2 to connect to an SFTP server using a private key.

However, the application will be running using Java EE on an application server; and so I don't want to store the private key in a file - I want to store it (encrypted) in a database instead; and then read the key and use it to connect.

However, all the instructions I can find (e.g. How to use apache vfs2 for sftp with public-private-key and without password) use a private key file, and apache VFS2 seems to require a file rather than a string in its identity provider object. (e.g. IdentityInfo myIdentityInfo = new IdentityInfo(new File(openSSHPrivateKey));)

I guess I could read the DB, save to a temporary file, then delete the temp file afterwards, but ideally I wouldn't use the local file system on the application server. Is there a way around this?


Solution

  • Instead of using SftpFileSystemConfigBuilder.getInstance().setIdentityInfo you can (and should) use SftpFileSystemConfigBuilder.getInstance().setIdentityProvider. This class allows you to use IdentityInfo if you want to use a file; but it also allows the option of using a [BytesIdentityInfo][1] object in its place, if your key is in a string (.getBytes()) rather than a File.