diff --git a/drivers/sshj/src/main/java/org/jclouds/sshj/SSHClientConnection.java b/drivers/sshj/src/main/java/org/jclouds/sshj/SSHClientConnection.java index 68754e9c4f..5a4895d22c 100644 --- a/drivers/sshj/src/main/java/org/jclouds/sshj/SSHClientConnection.java +++ b/drivers/sshj/src/main/java/org/jclouds/sshj/SSHClientConnection.java @@ -157,12 +157,12 @@ public class SSHClientConnection implements Connection { ssh.setTimeout(sessionTimeout); } ssh.connect(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(22)); - if (loginCredentials.getOptionalPassword().isPresent()) { - ssh.authPassword(loginCredentials.getUser(), loginCredentials.getOptionalPassword().get()); - } else if (loginCredentials.hasUnencryptedPrivateKey()) { + if (loginCredentials.hasUnencryptedPrivateKey()) { OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(loginCredentials.getOptionalPrivateKey().get(), null); ssh.authPublickey(loginCredentials.getUser(), key); + } else if (loginCredentials.getOptionalPassword().isPresent()) { + ssh.authPassword(loginCredentials.getUser(), loginCredentials.getOptionalPassword().get()); } else if (agentConnector.isPresent()) { AgentProxy proxy = new AgentProxy(agentConnector.get()); ssh.auth(loginCredentials.getUser(), getAuthMethods(proxy));