Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x a9b9e5497 -> fbdc3b376


[KARAF-2789] Upgrade to SSHD 0.10.1

Conflicts:
        pom.xml
        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/18c92d74
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/18c92d74
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/18c92d74

Branch: refs/heads/karaf-3.0.x
Commit: 18c92d74dd078f8e5b9ab4ebdb777bfed4d783bc
Parents: a9b9e54
Author: Guillaume Nodet <[email protected]>
Authored: Thu Mar 6 09:03:10 2014 +0100
Committer: Jonathan Anstey <[email protected]>
Committed: Fri Jun 20 17:14:15 2014 -0230

----------------------------------------------------------------------
 .../main/java/org/apache/karaf/client/Main.java | 59 +++++++++---------
 pom.xml                                         |  2 +-
 .../karaf/shell/ssh/KarafAgentFactory.java      | 11 ++--
 .../karaf/shell/ssh/KarafJaasAuthenticator.java |  3 -
 .../karaf/shell/ssh/KnownHostsManager.java      |  2 +-
 .../org/apache/karaf/shell/ssh/SshAction.java   | 65 +++++++-------------
 6 files changed, 60 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/18c92d74/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 1c1f7b9..05f567f 100644
--- a/client/src/main/java/org/apache/karaf/client/Main.java
+++ b/client/src/main/java/org/apache/karaf/client/Main.java
@@ -34,8 +34,8 @@ 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.AuthFuture;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.common.RuntimeSshException;
 import org.fusesource.jansi.AnsiConsole;
@@ -76,40 +76,41 @@ public class Main {
         Terminal terminal = null;
         int exitStatus = 0;
         try {
+            final Console console = System.console();
             client = SshClient.setUpDefaultClient();
             setupAgent(config.getUser(), client);
-            client.start();
-            ClientSession session = connectWithRetries(client, config);
-            Console console = System.console();
-            if (console != null) {
-                console.printf("Logging in as %s\n", config.getUser());
-            }
-            if (!session.authAgent(config.getUser()).await().isSuccess()) {
-                AuthFuture authFuture;
-                boolean useDefault = config.getPassword() != null;
-                do {
-                    String password;
-                    if (useDefault) {
-                        password = config.getPassword();
-                        useDefault = false;
-                    } else {
-                        if (console != null) {
-                            char[] readPassword = 
console.readPassword("Password: ");
-                            if (readPassword != null) {
-                                password = new String(readPassword);
-                            } else {
-                                return;
+            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] + " "));
+                                }
                             }
-                        } else {
-                            throw new Exception("Unable to prompt password: 
could not get system console");
                         }
+                    } catch (IOError e) {
                     }
-                    authFuture = session.authPassword(config.getUser(), 
password);
-                } while (authFuture.await().isFailure());
-                if (!authFuture.isSuccess()) {
-                    throw new Exception("Authentication failure");
+                    return answers;
                 }
+            });
+            client.start();
+            if (console != null) {
+                console.printf("Logging in as %s\n", config.getUser());
             }
+            ClientSession session = connectWithRetries(client, config);
+            if (config.getPassword() != null) {
+                session.addPasswordIdentity(config.getPassword());
+            }
+            session.auth().verify();
+
             ClientChannel channel;
             if (config.getCommand().length() > 0) {
                 channel = session.createChannel("exec", config.getCommand() + 
"\n");
@@ -172,7 +173,7 @@ public class Main {
         ClientSession session = null;
         int retries = 0;
         do {
-            ConnectFuture future = client.connect(config.getHost(), 
config.getPort());
+            ConnectFuture future = client.connect(config.getUser(), 
config.getHost(), config.getPort());
             future.await();
             try {
                 session = future.getSession();

http://git-wip-us.apache.org/repos/asf/karaf/blob/18c92d74/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 4d06bef..2a2dc6d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -243,7 +243,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/18c92d74/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 45d1f36..2a0c5c5 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/18c92d74/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafJaasAuthenticator.java
----------------------------------------------------------------------
diff --git 
a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafJaasAuthenticator.java
 
b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafJaasAuthenticator.java
index 0562bb2..222cf0f 100644
--- 
a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafJaasAuthenticator.java
+++ 
b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KarafJaasAuthenticator.java
@@ -19,7 +19,6 @@
 package org.apache.karaf.shell.ssh;
 
 import java.io.IOException;
-import java.security.Principal;
 import java.security.PublicKey;
 
 import javax.security.auth.Subject;
@@ -28,10 +27,8 @@ import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.NameCallback;
 import javax.security.auth.callback.PasswordCallback;
 import javax.security.auth.callback.UnsupportedCallbackException;
-import javax.security.auth.login.FailedLoginException;
 import javax.security.auth.login.LoginContext;
 
-import org.apache.karaf.jaas.boot.principal.RolePrincipal;
 import org.apache.karaf.jaas.modules.publickey.PublickeyCallback;
 import org.apache.sshd.common.Session;
 import org.apache.sshd.server.PasswordAuthenticator;

http://git-wip-us.apache.org/repos/asf/karaf/blob/18c92d74/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java
----------------------------------------------------------------------
diff --git 
a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java 
b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java
index 31434a1..0c9389d 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/KnownHostsManager.java
@@ -33,7 +33,7 @@ import java.security.PublicKey;
 import java.security.spec.InvalidKeySpecException;
 import java.security.spec.X509EncodedKeySpec;
 
-import org.apache.mina.util.Base64;
+import org.apache.sshd.common.util.Base64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/18c92d74/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 ed59460..ef4886a 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
@@ -34,8 +34,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.slf4j.Logger;
@@ -117,54 +117,31 @@ public class SshAction extends OsgiCommandSupport {
             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(SessionProperties.IGNORE_INTERRUPTS);
 
             try {
-
-                boolean authed = false;
-                if (agentSocket != null) {
-                    try {
-                        sshSession.authAgent(username);
-                    } catch (IllegalStateException ise) {
-                        System.err.println(keyChangedMessage);
-                        return null;
-                    }
-                    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: ");
-                    } else {
-                        log.debug("Password provided using command line 
option");
-                    }
-                    try {
-                        sshSession.authPassword(username, password);
-                    } catch (IllegalStateException ise) {
-                        System.err.println(keyChangedMessage);
-                        return null;
-                    }
-                    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();
 
                 System.out.println("Connected");
                 this.session.put( SessionProperties.IGNORE_INTERRUPTS, 
Boolean.TRUE );
@@ -196,7 +173,7 @@ public class SshAction extends OsgiCommandSupport {
                 }
                 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( SessionProperties.IGNORE_INTERRUPTS, 
oldIgnoreInterrupts );

Reply via email to