Issue 79: ssh key support

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2232 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-09 03:24:01 +00:00
parent faf79d4fa3
commit 8fab264dd4
5 changed files with 50 additions and 8 deletions

View File

@ -184,8 +184,13 @@ public class GaeHttpCommandExecutorServiceTest {
private void testHoot(HttpRequest request) throws IOException { private void testHoot(HttpRequest request) throws IOException {
request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "text/plain"); request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "text/plain");
HTTPRequest gaeRequest = client.convert(request); HTTPRequest gaeRequest = client.convert(request);
assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.CONTENT_TYPE); try {
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "text/plain"); assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.CONTENT_TYPE);
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "text/plain");
} catch (AssertionError e) {
assertEquals(gaeRequest.getHeaders().get(1).getName(), HttpHeaders.CONTENT_TYPE);
assertEquals(gaeRequest.getHeaders().get(1).getValue(), "text/plain");
}
assertEquals(new String(gaeRequest.getPayload()), "hoot!"); assertEquals(new String(gaeRequest.getPayload()), "hoot!");
} }
} }

View File

@ -79,9 +79,12 @@ public class JschSshClient implements SshClient {
private final int port; private final int port;
private final String username; private final String username;
private final String password; private final String password;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private Session session; private Session session;
private final byte[] privateKey;
final byte[] emptyPassPhrase = new byte[0];
@Inject @Inject
public JschSshClient(InetSocketAddress socket, String username, String password) { public JschSshClient(InetSocketAddress socket, String username, String password) {
@ -90,6 +93,17 @@ public class JschSshClient implements SshClient {
this.port = socket.getPort(); this.port = socket.getPort();
this.username = checkNotNull(username, "username"); this.username = checkNotNull(username, "username");
this.password = checkNotNull(password, "password"); this.password = checkNotNull(password, "password");
this.privateKey = null;
}
@Inject
public JschSshClient(InetSocketAddress socket, String username, byte[] privateKey) {
this.host = checkNotNull(socket, "socket").getAddress();
checkArgument(socket.getPort() > 0, "ssh port must be greater then zero" + socket.getPort());
this.port = socket.getPort();
this.username = checkNotNull(username, "username");
this.password = null;
this.privateKey = checkNotNull(privateKey, "privateKey");
} }
public InputStream get(String path) { public InputStream get(String path) {
@ -125,12 +139,16 @@ public class JschSshClient implements SshClient {
session = null; session = null;
try { try {
session = jsch.getSession(username, host.getHostAddress(), port); session = jsch.getSession(username, host.getHostAddress(), port);
logger.debug("%s@%s:%d: Session created.", username, host.getHostAddress(), port);
if (password != null) {
session.setPassword(password);
} else {
jsch.addIdentity(username, privateKey, null, emptyPassPhrase);
}
} catch (JSchException e) { } catch (JSchException e) {
throw new SshException(String.format("%s@%s:%d: Error creating session.", username, host throw new SshException(String.format("%s@%s:%d: Error creating session.", username, host
.getHostAddress(), port), e); .getHostAddress(), port), e);
} }
logger.debug("%s@%s:%d: Session created.", username, host.getHostAddress(), port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties(); java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no"); config.put("StrictHostKeyChecking", "no");
session.setConfig(config); session.setConfig(config);

View File

@ -49,5 +49,9 @@ public class JschSshClientModule extends AbstractModule {
return new JschSshClient(socket, username, password); return new JschSshClient(socket, username, password);
} }
public SshClient create(InetSocketAddress socket, String username, byte[] privateKey) {
return new JschSshClient(socket, username, privateKey);
}
} }
} }

View File

@ -25,6 +25,8 @@ package org.jclouds.ssh.jsch;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
@ -53,14 +55,16 @@ public class JschSshClientLiveTest {
protected static final String sshPort = System.getProperty("jclouds.test.ssh.port"); protected static final String sshPort = System.getProperty("jclouds.test.ssh.port");
protected static final String sshUser = System.getProperty("jclouds.test.ssh.username"); protected static final String sshUser = System.getProperty("jclouds.test.ssh.username");
protected static final String sshPass = System.getProperty("jclouds.test.ssh.password"); protected static final String sshPass = System.getProperty("jclouds.test.ssh.password");
protected static final String sshKeyFile = System.getProperty("jclouds.test.ssh.keyfile");
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = { "live" })
public SshClient setupClient() throws NumberFormatException, UnknownHostException { public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
int port = (sshPort != null) ? Integer.parseInt(sshPort) : 22; int port = (sshPort != null) ? Integer.parseInt(sshPort) : 22;
InetAddress host = (sshHost != null) ? InetAddress.getByName(sshHost) : InetAddress InetAddress host = (sshHost != null) ? InetAddress.getByName(sshHost) : InetAddress
.getLocalHost(); .getLocalHost();
if (sshUser == null || sshPass == null || sshUser.trim().equals("") if (sshUser == null
|| sshPass.trim().equals("")) { || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile
.trim().equals(""))) || sshUser.trim().equals("")) {
System.err.println("ssh credentials not present. Tests will be lame"); System.err.println("ssh credentials not present. Tests will be lame");
return new SshClient() { return new SshClient() {
@ -92,7 +96,13 @@ public class JschSshClientLiveTest {
} else { } else {
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 InetSocketAddress(host, port), sshUser, sshPass); SshClient connection;
if (sshKeyFile != null && !sshKeyFile.trim().equals("")) {
connection = factory.create(new InetSocketAddress(host, port), sshUser, Utils
.toStringAndClose(new FileInputStream(sshKeyFile)).getBytes());
} else {
connection = factory.create(new InetSocketAddress(host, port), sshUser, sshPass);
}
connection.connect(); connection.connect();
return connection; return connection;
} }

View File

@ -44,6 +44,7 @@
<jclouds.test.ssh.port>22</jclouds.test.ssh.port> <jclouds.test.ssh.port>22</jclouds.test.ssh.port>
<jclouds.test.ssh.username /> <jclouds.test.ssh.username />
<jclouds.test.ssh.password /> <jclouds.test.ssh.password />
<jclouds.test.ssh.keyfile />
</properties> </properties>
<profiles> <profiles>
<profile> <profile>
@ -89,6 +90,10 @@
<name>jclouds.test.ssh.username</name> <name>jclouds.test.ssh.username</name>
<value>${jclouds.test.ssh.username}</value> <value>${jclouds.test.ssh.username}</value>
</property> </property>
<property>
<name>jclouds.test.ssh.keyfile</name>
<value>${jclouds.test.ssh.keyfile}</value>
</property>
<property> <property>
<name>jclouds.test.ssh.password</name> <name>jclouds.test.ssh.password</name>
<value>${jclouds.test.ssh.password}</value> <value>${jclouds.test.ssh.password}</value>