diff --git a/compute/src/main/clojure/org/jclouds/modules.clj b/compute/src/main/clojure/org/jclouds/modules.clj index d7f3aa1140..b9d524e265 100644 --- a/compute/src/main/clojure/org/jclouds/modules.clj +++ b/compute/src/main/clojure/org/jclouds/modules.clj @@ -23,6 +23,7 @@ [org.jclouds.ssh SshClient ExecResponse] com.google.inject.Module com.google.common.collect.ImmutableSet + org.jclouds.domain.Credentials org.jclouds.net.IPSocket [org.jclouds.compute ComputeService ComputeServiceContextFactory StandaloneComputeServiceContextSpec] [java.util Set Map] @@ -75,6 +76,9 @@ [ctor] (reify org.jclouds.ssh.SshClient$Factory + (^org.jclouds.ssh.SshClient create + [_ ^IPSocket socket ^Credentials credentials] + (ctor socket credentials)) (^org.jclouds.ssh.SshClient create [_ ^IPSocket socket ^String username ^String password-or-key] (ctor socket username password-or-key)) diff --git a/compute/src/main/java/org/jclouds/ssh/SshClient.java b/compute/src/main/java/org/jclouds/ssh/SshClient.java index ca9c82b99d..25ccc1f84e 100644 --- a/compute/src/main/java/org/jclouds/ssh/SshClient.java +++ b/compute/src/main/java/org/jclouds/ssh/SshClient.java @@ -22,6 +22,7 @@ package org.jclouds.ssh; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.jclouds.domain.Credentials; import org.jclouds.io.Payload; import org.jclouds.net.IPSocket; @@ -31,9 +32,24 @@ import org.jclouds.net.IPSocket; public interface SshClient { interface Factory { + /** + * please use {@link Factory#create(IPSocket, Credentials)} + * + * @return + */ + @Deprecated SshClient create(IPSocket socket, String username, String password); + /** + * please use {@link Factory#create(IPSocket, Credentials)} + * + * @return + */ + @Deprecated SshClient create(IPSocket socket, String username, byte[] privateKey); + + SshClient create(IPSocket socket, Credentials credentials); + } String getUsername(); diff --git a/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/config/JschSshClientModule.java b/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/config/JschSshClientModule.java index b0171402e9..a594f1cab7 100755 --- a/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/config/JschSshClientModule.java +++ b/extensions/ssh/jsch/src/main/java/org/jclouds/ssh/jsch/config/JschSshClientModule.java @@ -19,16 +19,19 @@ package org.jclouds.ssh.jsch.config; +import static org.jclouds.util.Utils.isPrivateKeyCredential; + import javax.inject.Named; import org.jclouds.Constants; +import org.jclouds.domain.Credentials; import org.jclouds.http.handlers.BackoffLimitedRetryHandler; import org.jclouds.net.IPSocket; +import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.SocketOpen; import org.jclouds.ssh.ConfiguresSshClient; import org.jclouds.ssh.SshClient; import org.jclouds.ssh.jsch.JschSshClient; -import org.jclouds.ssh.jsch.predicates.InetSocketAddressConnect; import com.google.inject.AbstractModule; import com.google.inject.Inject; @@ -74,5 +77,10 @@ public class JschSshClientModule extends AbstractModule { return client; } + @Override + public SshClient create(IPSocket socket, Credentials credentials) { + return isPrivateKeyCredential(credentials) ? create(socket, credentials.identity, + credentials.credential.getBytes()) : create(socket, credentials.identity, credentials.credential); + } } } \ No newline at end of file diff --git a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java index dd02cf693f..921f7a98e3 100644 --- a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java +++ b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java @@ -26,6 +26,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import org.jclouds.domain.Credentials; import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.net.IPSocket; @@ -101,7 +102,7 @@ public class JschSshClientLiveTest { @Override public void put(String path, String contents) { - + } }; @@ -110,10 +111,10 @@ public class JschSshClientLiveTest { SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient connection; if (sshKeyFile != null && !sshKeyFile.trim().equals("")) { - connection = factory.create(new IPSocket(sshHost, port), sshUser, Utils.toStringAndClose( - new FileInputStream(sshKeyFile)).getBytes()); + connection = factory.create(new IPSocket(sshHost, port), + new Credentials(sshUser, Utils.toStringAndClose(new FileInputStream(sshKeyFile)))); } else { - connection = factory.create(new IPSocket(sshHost, port), sshUser, sshPass); + connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, sshPass)); } connection.connect(); return connection; diff --git a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientTest.java b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientTest.java index fa97413d05..e40cf73b68 100644 --- a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientTest.java +++ b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.net.ConnectException; import java.net.UnknownHostException; +import org.jclouds.domain.Credentials; import org.jclouds.net.IPSocket; import org.jclouds.ssh.SshClient; import org.jclouds.ssh.jsch.config.JschSshClientModule; @@ -51,8 +52,8 @@ public class JschSshClientTest { protected JschSshClient createClient() throws UnknownHostException { Injector i = Guice.createInjector(module()); SshClient.Factory factory = i.getInstance(SshClient.Factory.class); - JschSshClient ssh = JschSshClient.class.cast(factory.create(new IPSocket("localhost", 22), - "username", "password")); + JschSshClient ssh = JschSshClient.class.cast(factory.create(new IPSocket("localhost", 22), new Credentials( + "username", "password"))); return ssh; } @@ -68,10 +69,8 @@ public class JschSshClientTest { } public void testExceptionMessagesRetry() { - assert ssh.shouldRetry(new JSchException( - "Session.connect: java.io.IOException: End of IO Stream Read")); + assert ssh.shouldRetry(new JSchException("Session.connect: java.io.IOException: End of IO Stream Read")); assert ssh.shouldRetry(new JSchException("Session.connect: invalid data")); - assert ssh.shouldRetry(new JSchException( - "Session.connect: java.net.SocketException: Connection reset")); + assert ssh.shouldRetry(new JSchException("Session.connect: java.net.SocketException: Connection reset")); } } \ No newline at end of file diff --git a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/config/JschSshClientModuleTest.java b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/config/JschSshClientModuleTest.java index 9e537e9fe5..1eb6deffe7 100644 --- a/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/config/JschSshClientModuleTest.java +++ b/extensions/ssh/jsch/src/test/java/org/jclouds/ssh/jsch/config/JschSshClientModuleTest.java @@ -21,6 +21,7 @@ package org.jclouds.ssh.jsch.config; import java.net.UnknownHostException; +import org.jclouds.domain.Credentials; import org.jclouds.net.IPSocket; import org.jclouds.ssh.SshClient; import org.jclouds.ssh.jsch.JschSshClient; @@ -41,7 +42,7 @@ public class JschSshClientModuleTest { Injector i = Guice.createInjector(new JschSshClientModule()); SshClient.Factory factory = i.getInstance(SshClient.Factory.class); - SshClient connection = factory.create(new IPSocket("localhost", 22), "username", "password"); + SshClient connection = factory.create(new IPSocket("localhost", 22), new Credentials("username", "password")); assert connection instanceof JschSshClient; } } \ No newline at end of file