Repository: karaf
Updated Branches:
refs/heads/karaf-2.x 1b92d7c78 -> 994d3cce3
[KARAF-2789] Upgrade to SSHD 0.10.1
Conflicts:
client/src/main/java/org/apache/karaf/client/Main.java
pom.xml
shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java
shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java
Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/531def5e
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/531def5e
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/531def5e
Branch: refs/heads/karaf-2.x
Commit: 531def5ef5b1d7d9153b80cbef97157b75f83389
Parents: 1b92d7c
Author: Guillaume Nodet <[email protected]>
Authored: Thu Mar 6 09:03:10 2014 +0100
Committer: Jonathan Anstey <[email protected]>
Committed: Fri Jun 20 16:53:00 2014 -0230
----------------------------------------------------------------------
.../main/java/org/apache/karaf/client/Main.java | 95 ++++++++++++++------
pom.xml | 2 +-
.../karaf/shell/ssh/KarafAgentFactory.java | 11 ++-
.../org/apache/karaf/shell/ssh/SshAction.java | 51 +++++------
4 files changed, 96 insertions(+), 63 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf/blob/531def5e/client/src/main/java/org/apache/karaf/client/Main.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/karaf/client/Main.java
b/client/src/main/java/org/apache/karaf/client/Main.java
index 27624e8..9fbf85c 100644
--- a/client/src/main/java/org/apache/karaf/client/Main.java
+++ b/client/src/main/java/org/apache/karaf/client/Main.java
@@ -34,6 +34,7 @@ import org.apache.sshd.SshClient;
import org.apache.sshd.agent.SshAgent;
import org.apache.sshd.agent.local.AgentImpl;
import org.apache.sshd.agent.local.LocalAgentFactory;
+import org.apache.sshd.client.UserInteraction;
import org.apache.sshd.client.channel.ChannelShell;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.common.RuntimeSshException;
@@ -142,17 +143,43 @@ public class Main {
SshAgent agent = null;
int exitStatus = 0;
try {
- agent = startAgent(user);
+
+ final Console console = System.console();
client = SshClient.setUpDefaultClient();
- client.setAgentFactory(new LocalAgentFactory(agent));
- client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME,
"local");
+ setupAgent(user, client);
+ client.setUserInteraction(new UserInteraction() {
+ public void welcome(String banner) {
+ System.out.println(banner);
+ }
+
+ public String[] interactive(String destination, String name,
String instruction, String[] prompt, boolean[] echo) {
+ String[] answers = new String[prompt.length];
+ try {
+ for (int i = 0; i < prompt.length; i++) {
+ if (console != null) {
+ if (echo[i]) {
+ answers[i] = console.readLine(prompt[i] +
" ");
+ } else {
+ answers[i] = new
String(console.readPassword(prompt[i] + " "));
+ }
+ }
+ }
+ } catch (IOError e) {
+ }
+ return answers;
+ }
+ });
client.start();
- int retries = 0;
+ if (console != null) {
+ console.printf("Logging in as %s\n", user);
+ }
+
ClientSession session = null;
+ int retries = 0;
do {
- ConnectFuture future = client.connect(host, port);
+ ConnectFuture future = client.connect(user, host, port);
future.await();
- try {
+ try {
session = future.getSession();
} catch (RuntimeSshException ex) {
if (retries++ < retryAttempts) {
@@ -163,22 +190,13 @@ public class Main {
}
}
} while (session == null);
- if (!session.authAgent(user).await().isSuccess()) {
- if (password == null) {
- Console console = System.console();
- if (console != null) {
- char[] readPassword = console.readPassword("Password:
");
- if (readPassword != null) {
- password = new String(readPassword);
- }
- } else {
- throw new Exception("Unable to prompt password: could
not get system console");
- }
- }
- if (!session.authPassword(user, password).await().isSuccess())
{
- throw new Exception("Authentication failure");
- }
+
+
+ if (password != null) {
+ session.addPasswordIdentity(password);
}
+ session.auth().verify();
+
ClientChannel channel;
if (command.length() > 0) {
channel = session.createChannel("exec",
command.append("\n").toString());
@@ -226,6 +244,7 @@ public class Main {
System.exit(exitStatus);
}
+
private static Properties loadProps(File file) {
Properties props = new Properties();
FileInputStream is = null;
@@ -248,21 +267,41 @@ public class Main {
return props;
}
- protected static SshAgent startAgent(String user) {
+ private static void setupAgent(String user, SshClient client) {
+ SshAgent agent;
+ URL builtInPrivateKey =
Main.class.getClassLoader().getResource("karaf.key");
+ agent = startAgent(user, builtInPrivateKey);
+ client.setAgentFactory(new LocalAgentFactory(agent));
+ client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
+ }
+
+ private static SshAgent startAgent(String user, URL privateKeyUrl) {
+ InputStream is = null;
try {
- SshAgent local = new AgentImpl();
- URL url = Main.class.getClassLoader().getResource("karaf.key");
- InputStream is = url.openStream();
+ SshAgent agent = new AgentImpl();
+ is = privateKeyUrl.openStream();
ObjectInputStream r = new ObjectInputStream(is);
KeyPair keyPair = (KeyPair) r.readObject();
- local.addIdentity(keyPair, "karaf");
- return local;
+ is.close();
+ agent.addIdentity(keyPair, user);
+ return agent;
} catch (Throwable e) {
+ close(is);
System.err.println("Error starting ssh agent for: " +
e.getMessage());
return null;
}
}
-
+
+ private static void close(Closeable is) {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e1) {
+ // Ignore
+ }
+ }
+ }
+
public static String readLine(String msg) throws IOException {
StringBuffer sb = new StringBuffer();
System.err.print(msg);
http://git-wip-us.apache.org/repos/asf/karaf/blob/531def5e/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index fe6f3fa..ed48a26 100644
--- a/pom.xml
+++ b/pom.xml
@@ -222,7 +222,7 @@
<spring.security31.version>3.1.4.RELEASE</spring.security31.version>
<directory-version>2.0.0-M16</directory-version>
- <sshd.version>0.9.0</sshd.version>
+ <sshd.version>0.10.1</sshd.version>
<struts.bundle.version>1.3.10_1</struts.bundle.version>
<xbean.version>3.16</xbean.version>
<xerces.version>2.11.0</xerces.version>
http://git-wip-us.apache.org/repos/asf/karaf/blob/531def5e/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafAgentFactory.java
----------------------------------------------------------------------
diff --git
a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafAgentFactory.java
b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafAgentFactory.java
index 9c6c41f..4a11960 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafAgentFactory.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafAgentFactory.java
@@ -35,8 +35,10 @@ import org.apache.sshd.agent.local.AgentImpl;
import org.apache.sshd.agent.local.AgentServerProxy;
import org.apache.sshd.agent.local.ChannelAgentForwarding;
import org.apache.sshd.common.Channel;
+import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.Session;
+import org.apache.sshd.common.session.ConnectionService;
import org.apache.sshd.server.session.ServerSession;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
@@ -63,8 +65,8 @@ public class KarafAgentFactory implements SshAgentFactory {
return new ChannelAgentForwarding.Factory();
}
- public SshAgent createClient(Session session) throws IOException {
- String proxyId =
session.getFactoryManager().getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
+ public SshAgent createClient(FactoryManager manager) throws IOException {
+ String proxyId =
manager.getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
if (proxyId == null) {
throw new IllegalStateException("No " +
SshAgent.SSH_AUTHSOCKET_ENV_NAME + " environment variable set");
}
@@ -79,11 +81,12 @@ public class KarafAgentFactory implements SshAgentFactory {
throw new IllegalStateException("No ssh agent found");
}
- public SshAgentServer createServer(Session session) throws IOException {
+ public SshAgentServer createServer(ConnectionService service) throws
IOException {
+ Session session = service.getSession();
if (!(session instanceof ServerSession)) {
throw new IllegalStateException("The session used to create an
agent server proxy must be a server session");
}
- final AgentServerProxy proxy = new AgentServerProxy((ServerSession)
session);
+ final AgentServerProxy proxy = new AgentServerProxy(service);
proxies.put(proxy.getId(), proxy);
return new SshAgentServer() {
public String getId() {
http://git-wip-us.apache.org/repos/asf/karaf/blob/531def5e/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
index 6174080..7e74a14 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/SshAction.java
@@ -35,8 +35,8 @@ import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
import org.apache.sshd.agent.SshAgent;
+import org.apache.sshd.client.UserInteraction;
import org.apache.sshd.client.channel.ChannelShell;
-import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.common.util.NoCloseInputStream;
import org.apache.sshd.common.util.NoCloseOutputStream;
import org.osgi.service.blueprint.container.BlueprintContainer;
@@ -114,43 +114,34 @@ public class SshAction extends OsgiCommandSupport
implements BlueprintContainerA
agentSocket =
this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString();
client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME,agentSocket);
}
+ client.setUserInteraction(new UserInteraction() {
+ public void welcome(String banner) {
+ System.out.println(banner);
+ }
+ public String[] interactive(String destination, String name,
String instruction, String[] prompt, boolean[] echo) {
+ String[] answers = new String[prompt.length];
+ try {
+ for (int i = 0; i < prompt.length; i++) {
+ answers[i] = readLine(prompt[i] + " ", echo[i] ? null
: '*');
+ }
+ } catch (IOException e) {
+ }
+ return answers;
+ }
+ });
try {
- ConnectFuture future = client.connect(hostname, port);
- future.await();
- sshSession = future.getSession();
+ ClientSession sshSession = client.connect(username, hostname,
port).await().getSession();
Object oldIgnoreInterrupts =
this.session.get(Console.IGNORE_INTERRUPTS);
try {
System.out.println("Connected");
- boolean authed = false;
- if (agentSocket != null) {
- sshSession.authAgent(username);
- int ret = sshSession.waitFor(ClientSession.WAIT_AUTH |
ClientSession.CLOSED | ClientSession.AUTHED, 0);
- if ((ret & ClientSession.AUTHED) == 0) {
- System.err.println("Agent authentication failed,
falling back to password authentication.");
- } else {
- authed = true;
- }
- }
- if (!authed) {
- if (password == null) {
- log.debug("Prompting user for password");
- password = readLine("Password: ");
- }
- sshSession.authPassword(username, password);
- int ret = sshSession.waitFor(ClientSession.WAIT_AUTH |
ClientSession.CLOSED | ClientSession.AUTHED, 0);
- if ((ret & ClientSession.AUTHED) == 0) {
- System.err.println("Password authentication failed");
- } else {
- authed = true;
- }
- }
- if (!authed) {
- return null;
+ if (password != null) {
+ sshSession.addPasswordIdentity(password);
}
+ sshSession.auth().verify();
this.session.put( Console.IGNORE_INTERRUPTS, Boolean.TRUE );
@@ -181,7 +172,7 @@ public class SshAction extends OsgiCommandSupport
implements BlueprintContainerA
}
channel.setOut(new NoCloseOutputStream(System.out));
channel.setErr(new NoCloseOutputStream(System.err));
- channel.open();
+ channel.open().verify();
channel.waitFor(ClientChannel.CLOSED, 0);
} finally {
session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);