public key authentication with passphrase

464 views
Skip to first unread message

Jason Zwolak

unread,
May 19, 2015, 2:38:40 PM5/19/15
to sshj-...@googlegroups.com
How can I use an openssh key file for public key authentication without prompting me for the passphrase?

We have a special user designated for deployment and we store the passphrase in a properties file on developers' computers who are authorized to make deployments.  For automated deployments this reads the passphrase and we need to then supply it to sshj to connect.

Thanks!

Jason Zwolak

unread,
May 19, 2015, 3:28:43 PM5/19/15
to sshj-...@googlegroups.com
Here's the source code that does the trick for me.  This is Groovy code and I copied it out of a running Gradle build system, so it won't run until you modify it for your needs:

import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.SFTPClient
import net.schmizz.sshj.userauth.keyprovider.KeyProvider
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile
import net.schmizz.sshj.userauth.password.PasswordFinder

final SSHClient ssh = new SSHClient()
ssh.loadKnownHosts(new File("path/to/known_hosts"))
ssh.connect("server")
try {
    OpenSSHKeyFile keyProvider = new OpenSSHKeyFile()
    keyProvider.init(
        new File("path/to/key/file"),
        new StaticPasswordFinder("secretpassphrase")
    )
    ssh.authPublickey("username",keyProvider)
    final SFTPClient sftp = ssh.newSFTPClient()
    try {
        sftp.ls("some/remote/dir").each {
            // do stuff
        }
    } finally {
        sftp.close()
    }
} finally {
    ssh.disconnect()
}


class StaticPasswordFinder implements PasswordFinder {
    private char[] password;
    public StaticPasswordFinder( String password ) {
        this.password = password.toCharArray()
    }
    public char[] reqPassword(net.schmizz.sshj.userauth.password.Resource<?> resource) {
        return password;
    }
    public boolean shouldRetry(net.schmizz.sshj.userauth.password.Resource<?> resource) {
        return false;
    }
}

Reply all
Reply to author
Forward
0 new messages