Adapt SSH support.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 25 May 2022 08:30:42 +0000 (10:30 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 25 May 2022 08:30:42 +0000 (10:30 +0200)
org.argeo.cms.ssh/src/org/argeo/cms/ssh/AbstractSsh.java
org.argeo.cms.ssh/src/org/argeo/cms/ssh/BasicSshServer.java
org.argeo.cms.ssh/src/org/argeo/cms/ssh/Sftp.java
org.argeo.cms.ssh/src/org/argeo/cms/ssh/SshSync.java

index cb53f7c1e782962ef7797972a0c1265636e732d8..2d195de9f3d0f4ce3e9644218d840abc6cdc1d02 100644 (file)
@@ -14,9 +14,9 @@ import org.apache.sshd.client.channel.ClientChannel;
 import org.apache.sshd.client.channel.ClientChannelEvent;
 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.util.io.NoCloseInputStream;
-import org.apache.sshd.common.util.io.NoCloseOutputStream;
+import org.apache.sshd.common.util.io.input.NoCloseInputStream;
+import org.apache.sshd.common.util.io.output.NoCloseOutputStream;
+import org.apache.sshd.sftp.client.fs.SftpFileSystemProvider;
 import org.argeo.api.cms.CmsLog;
 
 @SuppressWarnings("restriction")
index 3e2f9aafbed8a645197f9fa05b4ec2cabe1a5aac..9e9389342903deeeb3d68baa2da83a4d5535b6d8 100644 (file)
@@ -4,10 +4,10 @@ import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
+import org.apache.sshd.scp.server.ScpCommandFactory;
 import org.apache.sshd.server.SshServer;
 import org.apache.sshd.server.config.keys.DefaultAuthorizedKeysAuthenticator;
 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
-import org.apache.sshd.server.scp.ScpCommandFactory;
 import org.apache.sshd.server.shell.ProcessShellFactory;
 import org.argeo.util.OS;
 
index b272547137f00dcc97c28b9aa866b712b52479d4..f6f44741887e703421bccdf3938d264a4111522b 100644 (file)
@@ -5,7 +5,7 @@ import java.net.URI;
 import java.nio.file.FileSystem;
 import java.nio.file.Path;
 
-import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem;
+import org.apache.sshd.sftp.client.fs.SftpFileSystem;
 
 /** Create an SFTP {@link FileSystem}. */
 public class Sftp extends AbstractSsh {
index 8c048f76f5cfa351715d7f8dd0e647381a43e042..71b6365761f7ee5a7752747f554d230a1b5b69fc 100644 (file)
@@ -1,11 +1,13 @@
 package org.argeo.cms.ssh;
 
+import java.io.Console;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.security.KeyPair;
 import java.util.Map;
 import java.util.Scanner;
 
@@ -15,10 +17,13 @@ import org.apache.sshd.agent.SshAgentFactory;
 import org.apache.sshd.agent.local.LocalAgentFactory;
 import org.apache.sshd.agent.unix.UnixAgentFactory;
 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.SftpFileSystem;
-import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
+import org.apache.sshd.common.NamedResource;
+import org.apache.sshd.common.config.keys.FilePasswordProvider;
+import org.apache.sshd.sftp.client.fs.SftpFileSystem;
+import org.apache.sshd.sftp.client.fs.SftpFileSystemProvider;
 import org.argeo.api.cms.CmsLog;
 
 public class SshSync {
@@ -28,11 +33,11 @@ public class SshSync {
 
                try (SshClient client = SshClient.setUpDefaultClient()) {
                        client.start();
-                       boolean osAgent = true;
+                       boolean osAgent = false;
                        SshAgentFactory agentFactory = osAgent ? new UnixAgentFactory() : new LocalAgentFactory();
                        // SshAgentFactory agentFactory = new LocalAgentFactory();
                        client.setAgentFactory(agentFactory);
-                       SshAgent sshAgent = agentFactory.createClient(client);
+                       SshAgent sshAgent = agentFactory.createClient(null, client);
 
                        String login = System.getProperty("user.name");
                        String host = "localhost";
@@ -40,12 +45,27 @@ public class SshSync {
 
                        if (!osAgent) {
                                String keyPath = "/home/" + login + "/.ssh/id_rsa";
-                               System.out.print(keyPath + ": ");
-                               Scanner s = new Scanner(System.in);
-                               String password = s.next();
-//                             KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
-//                                             FilePasswordProvider.of(password));
-//                             sshAgent.addIdentity(keyPair, "NO COMMENT");
+
+                               String password;
+                               Console console = System.console();
+                               if (console != null) {
+                                       password = new String(console.readPassword(keyPath + ": "));
+                               } else {
+                                       System.out.print(keyPath + ": ");
+                                       try (Scanner s = new Scanner(System.in)) {
+                                               password = s.next();
+                                       }
+                               }
+                               NamedResource namedResource = new NamedResource() {
+
+                                       @Override
+                                       public String getName() {
+                                               return keyPath;
+                                       }
+                               };
+                               KeyPair keyPair = ClientIdentityLoader.DEFAULT
+                                               .loadClientIdentities(null, namedResource, FilePasswordProvider.of(password)).iterator().next();
+                               sshAgent.addIdentity(keyPair, "NO COMMENT");
                        }
 
 //                     List<? extends Map.Entry<PublicKey, String>> identities = sshAgent.getIdentities();