You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/03/06 09:47:15 UTC

[2/2] git commit: [KARAF-2789] Upgrade to SSHD 0.10.1

[KARAF-2789] Upgrade to SSHD 0.10.1


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/89bbd1d8
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/89bbd1d8
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/89bbd1d8

Branch: refs/heads/master
Commit: 89bbd1d8c0363a09c4c5109712b480685457eaa5
Parents: 7c8b5a7
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Thu Mar 6 09:03:10 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Thu Mar 6 09:46:57 2014 +0100

----------------------------------------------------------------------
 .../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   | 63 +++++++-------------
 .../shell/ssh/UserAuthFactoriesFactory.java     |  2 +-
 7 files changed, 61 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/89bbd1d8/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 81e0cf1..a2df5c5 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;
@@ -75,40 +75,41 @@ public class Main {
         SshClient client = null;
         Terminal terminal = null;
         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");
@@ -168,7 +169,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/89bbd1d8/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 938537e..ae3cabb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -241,7 +241,7 @@
         <spring32.version>3.2.4.RELEASE</spring32.version>
         <spring.security31.version>3.1.4.RELEASE</spring.security31.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/89bbd1d8/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 da4d43a..f54b9bf 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
@@ -34,8 +34,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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -51,8 +53,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");
         }
@@ -67,11 +69,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/89bbd1d8/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 6df591a..632cb98 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/89bbd1d8/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/89bbd1d8/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 0f2ae21..64bd6f3 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;
@@ -120,54 +120,33 @@ public class SshAction implements Action {
             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] = session.readLine(prompt[i] + " ", echo[i] ? null : '*');
+                    }
+                } catch (IOException e) {
+                }
+                return answers;
+            }
+        });
 
         try {
-            ConnectFuture future = client.connect(hostname, port);
-            future.await();
-            ClientSession sshSession = future.getSession();
+            ClientSession sshSession = client.connect(username, hostname, port).await().getSession();
 
             Object oldIgnoreInterrupts = this.session.get(Session.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 = session.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( Session.IGNORE_INTERRUPTS, Boolean.TRUE );
@@ -199,7 +178,7 @@ public class SshAction implements Action {
                 }
                 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( Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts );

http://git-wip-us.apache.org/repos/asf/karaf/blob/89bbd1d8/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java
index 94d71a3..a7bd297 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/UserAuthFactoriesFactory.java
@@ -53,7 +53,7 @@ public class UserAuthFactoriesFactory {
     private Set<String> methodSet;
     private List<NamedFactory<UserAuth>> factories;
 
-    public void setAuthMethods(String methods) {
+   public void setAuthMethods(String methods) {
         this.methodSet = new HashSet<String>();
         this.factories = new ArrayList<NamedFactory<UserAuth>>();
         String[] ams = methods.split(",");