SSH key pair management.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / ssh / AbstractSsh.java
index f42ae205cfe6cb24a5cbce137b90d3d0de0cc977..88b28b525290a113f94250b90458df06ef94bade 100644 (file)
@@ -14,7 +14,7 @@ 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.SftpFileSystemProvider;
+import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
 import org.apache.sshd.common.config.keys.FilePasswordProvider;
 
 abstract class AbstractSsh {
@@ -26,6 +26,8 @@ abstract class AbstractSsh {
        private boolean passwordSet = false;
        private ClientSession session;
 
+       private SshKeyPair sshKeyPair;
+
        synchronized SshClient getSshClient() {
                if (sshClient == null) {
                        long begin = System.currentTimeMillis();
@@ -48,21 +50,26 @@ abstract class AbstractSsh {
 
        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: ");
+                                               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) {
@@ -79,13 +86,13 @@ abstract class AbstractSsh {
        }
 
        void loadKey(String password, String keyPath) {
-               try {
-                       KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
-                                       FilePasswordProvider.of(password));
-                       session.addPublicKeyIdentity(keyPair);
-               } catch (IOException | GeneralSecurityException e) {
-                       throw new IllegalStateException(e);
-               }
+//             try {
+//                     KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
+//                                     FilePasswordProvider.of(password));
+//                     session.addPublicKeyIdentity(keyPair);
+//             } catch (IOException | GeneralSecurityException e) {
+//                     throw new IllegalStateException(e);
+//             }
        }
 
        void openSession(URI uri) {