adapted ssh factory to accept credentials arg

This commit is contained in:
Adrian Cole 2010-10-26 11:35:23 -07:00
parent 26eb27fdd0
commit c7c6e9eae1
6 changed files with 41 additions and 12 deletions

View File

@ -23,6 +23,7 @@
[org.jclouds.ssh SshClient ExecResponse] [org.jclouds.ssh SshClient ExecResponse]
com.google.inject.Module com.google.inject.Module
com.google.common.collect.ImmutableSet com.google.common.collect.ImmutableSet
org.jclouds.domain.Credentials
org.jclouds.net.IPSocket org.jclouds.net.IPSocket
[org.jclouds.compute ComputeService ComputeServiceContextFactory StandaloneComputeServiceContextSpec] [org.jclouds.compute ComputeService ComputeServiceContextFactory StandaloneComputeServiceContextSpec]
[java.util Set Map] [java.util Set Map]
@ -75,6 +76,9 @@
[ctor] [ctor]
(reify (reify
org.jclouds.ssh.SshClient$Factory org.jclouds.ssh.SshClient$Factory
(^org.jclouds.ssh.SshClient create
[_ ^IPSocket socket ^Credentials credentials]
(ctor socket credentials))
(^org.jclouds.ssh.SshClient create (^org.jclouds.ssh.SshClient create
[_ ^IPSocket socket ^String username ^String password-or-key] [_ ^IPSocket socket ^String username ^String password-or-key]
(ctor socket username password-or-key)) (ctor socket username password-or-key))

View File

@ -22,6 +22,7 @@ package org.jclouds.ssh;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
@ -31,9 +32,24 @@ import org.jclouds.net.IPSocket;
public interface SshClient { public interface SshClient {
interface Factory { interface Factory {
/**
* please use {@link Factory#create(IPSocket, Credentials)}
*
* @return
*/
@Deprecated
SshClient create(IPSocket socket, String username, String password); 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, String username, byte[] privateKey);
SshClient create(IPSocket socket, Credentials credentials);
} }
String getUsername(); String getUsername();

View File

@ -19,16 +19,19 @@
package org.jclouds.ssh.jsch.config; package org.jclouds.ssh.jsch.config;
import static org.jclouds.util.Utils.isPrivateKeyCredential;
import javax.inject.Named; import javax.inject.Named;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.domain.Credentials;
import org.jclouds.http.handlers.BackoffLimitedRetryHandler; import org.jclouds.http.handlers.BackoffLimitedRetryHandler;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.SocketOpen; import org.jclouds.predicates.SocketOpen;
import org.jclouds.ssh.ConfiguresSshClient; import org.jclouds.ssh.ConfiguresSshClient;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.jsch.JschSshClient; import org.jclouds.ssh.jsch.JschSshClient;
import org.jclouds.ssh.jsch.predicates.InetSocketAddressConnect;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -74,5 +77,10 @@ public class JschSshClientModule extends AbstractModule {
return client; 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);
}
} }
} }

View File

@ -26,6 +26,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
@ -110,10 +111,10 @@ public class JschSshClientLiveTest {
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
SshClient connection; SshClient connection;
if (sshKeyFile != null && !sshKeyFile.trim().equals("")) { if (sshKeyFile != null && !sshKeyFile.trim().equals("")) {
connection = factory.create(new IPSocket(sshHost, port), sshUser, Utils.toStringAndClose( connection = factory.create(new IPSocket(sshHost, port),
new FileInputStream(sshKeyFile)).getBytes()); new Credentials(sshUser, Utils.toStringAndClose(new FileInputStream(sshKeyFile))));
} else { } else {
connection = factory.create(new IPSocket(sshHost, port), sshUser, sshPass); connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, sshPass));
} }
connection.connect(); connection.connect();
return connection; return connection;

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.jsch.config.JschSshClientModule; import org.jclouds.ssh.jsch.config.JschSshClientModule;
@ -51,8 +52,8 @@ public class JschSshClientTest {
protected JschSshClient createClient() throws UnknownHostException { protected JschSshClient createClient() throws UnknownHostException {
Injector i = Guice.createInjector(module()); Injector i = Guice.createInjector(module());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
JschSshClient ssh = JschSshClient.class.cast(factory.create(new IPSocket("localhost", 22), JschSshClient ssh = JschSshClient.class.cast(factory.create(new IPSocket("localhost", 22), new Credentials(
"username", "password")); "username", "password")));
return ssh; return ssh;
} }
@ -68,10 +69,8 @@ public class JschSshClientTest {
} }
public void testExceptionMessagesRetry() { public void testExceptionMessagesRetry() {
assert ssh.shouldRetry(new JSchException( assert ssh.shouldRetry(new JSchException("Session.connect: java.io.IOException: End of IO Stream Read"));
"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: invalid data"));
assert ssh.shouldRetry(new JSchException( assert ssh.shouldRetry(new JSchException("Session.connect: java.net.SocketException: Connection reset"));
"Session.connect: java.net.SocketException: Connection reset"));
} }
} }

View File

@ -21,6 +21,7 @@ package org.jclouds.ssh.jsch.config;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.jsch.JschSshClient; import org.jclouds.ssh.jsch.JschSshClient;
@ -41,7 +42,7 @@ public class JschSshClientModuleTest {
Injector i = Guice.createInjector(new JschSshClientModule()); Injector i = Guice.createInjector(new JschSshClientModule());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); 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; assert connection instanceof JschSshClient;
} }
} }