Improve SSH client.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / ssh / AbstractSsh.java
index ae1d6a0d4acabf446948ccbfce60026140c1ef8b..588c16b01bd426a8a08e20aca5d051a8d9686cbb 100644 (file)
@@ -3,19 +3,15 @@ package org.argeo.ssh;
 import java.io.Console;
 import java.io.IOException;
 import java.net.URI;
-import java.security.GeneralSecurityException;
-import java.security.KeyPair;
 import java.util.Arrays;
 import java.util.Scanner;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.sshd.client.SshClient;
-import org.apache.sshd.client.config.keys.ClientIdentityLoader;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
-import org.apache.sshd.common.config.keys.FilePasswordProvider;
 
 abstract class AbstractSsh {
        private final static Log log = LogFactory.getLog(AbstractSsh.class);
@@ -26,6 +22,8 @@ abstract class AbstractSsh {
        private boolean passwordSet = false;
        private ClientSession session;
 
+       private SshKeyPair sshKeyPair;
+
        synchronized SshClient getSshClient() {
                if (sshClient == null) {
                        long begin = System.currentTimeMillis();
@@ -46,23 +44,30 @@ abstract class AbstractSsh {
                return sftpFileSystemProvider;
        }
 
+       @SuppressWarnings("restriction")
        void authenticate() {
                try {
-                       if (!passwordSet) {
-                               String password;
-                               Console console = System.console();
-                               if (console == null) {// IDE
-                                       System.out.print("Password: ");
-                                       Scanner s = new Scanner(System.in);
-                                       password = s.next();
-                               } else {
-                                       console.printf("Password: ");
-                                       char[] pwd = console.readPassword();
-                                       password = new String(pwd);
-                                       Arrays.fill(pwd, ' ');
+                       if (sshKeyPair != null) {
+                               session.addPublicKeyIdentity(sshKeyPair.asKeyPair());
+                       } else {
+
+                               if (!passwordSet) {
+                                       String password;
+                                       Console console = System.console();
+                                       if (console == null) {// IDE
+                                               System.out.print("Password: ");
+                                               try (Scanner s = new Scanner(System.in)) {
+                                                       password = s.next();
+                                               }
+                                       } else {
+                                               console.printf("Password: ");
+                                               char[] pwd = console.readPassword();
+                                               password = new String(pwd);
+                                               Arrays.fill(pwd, ' ');
+                                       }
+                                       session.addPasswordIdentity(password);
+                                       passwordSet = true;
                                }
-                               session.addPasswordIdentity(password);
-                               passwordSet = true;
                        }
                        session.auth().verify(1000l);
                } catch (IOException e) {
@@ -92,6 +97,7 @@ abstract class AbstractSsh {
                openSession(uri.getUserInfo(), uri.getHost(), uri.getPort() > 0 ? uri.getPort() : null);
        }
 
+       @SuppressWarnings("restriction")
        void openSession(String login, String host, Integer port) {
                if (session != null)
                        throw new IllegalStateException("Session is already open");
@@ -125,6 +131,7 @@ abstract class AbstractSsh {
                }
        }
 
+       @SuppressWarnings("restriction")
        void closeSession() {
                if (session == null)
                        throw new IllegalStateException("No session is open");