From 43c1c29f88f3fe54844d4dd05a007f86f8f57950 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 25 May 2022 09:52:25 +0200 Subject: [PATCH] Move SSH support to Argeo Commons. --- .../build.properties | 4 +- .../src/org/argeo/ssh/AbstractSsh.java | 188 ------------------ .../src/org/argeo/ssh/BasicSshServer.java | 105 ---------- .../src/org/argeo/ssh/Sftp.java | 42 ---- .../src/org/argeo/ssh/Ssh.java | 81 -------- .../src/org/argeo/ssh/SshKeyPair.java | 182 ----------------- .../src/org/argeo/ssh/SshSync.java | 134 ------------- .../src/org/argeo/ssh/package-info.java | 2 - 8 files changed, 3 insertions(+), 735 deletions(-) delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/AbstractSsh.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/BasicSshServer.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/Ssh.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/SshKeyPair.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/SshSync.java delete mode 100644 cms/org.argeo.cms.integration/src/org/argeo/ssh/package-info.java diff --git a/cms/org.argeo.cms.integration/build.properties b/cms/org.argeo.cms.integration/build.properties index 4e0e742e8..bd854f7a3 100644 --- a/cms/org.argeo.cms.integration/build.properties +++ b/cms/org.argeo.cms.integration/build.properties @@ -3,4 +3,6 @@ output.. = bin/ bin.includes = META-INF/,\ . additional.bundles = org.apache.sshd.common,\ -org.apache.sshd.core + org.apache.sshd.core,\ + org.slf4j.api,\ + org.argeo.ext.slf4j diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/AbstractSsh.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/AbstractSsh.java deleted file mode 100644 index 6bdb9bc99..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/AbstractSsh.java +++ /dev/null @@ -1,188 +0,0 @@ -package org.argeo.ssh; - -import java.io.Console; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Scanner; -import java.util.Set; - -import org.apache.sshd.client.SshClient; -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.argeo.api.cms.CmsLog; - -@SuppressWarnings("restriction") -abstract class AbstractSsh { - private final static CmsLog log = CmsLog.getLog(AbstractSsh.class); - - private static SshClient sshClient; - private static SftpFileSystemProvider sftpFileSystemProvider; - - private boolean passwordSet = false; - private ClientSession session; - - private SshKeyPair sshKeyPair; - - synchronized SshClient getSshClient() { - if (sshClient == null) { - long begin = System.currentTimeMillis(); - sshClient = SshClient.setUpDefaultClient(); - sshClient.start(); - long duration = System.currentTimeMillis() - begin; - if (log.isDebugEnabled()) - log.debug("SSH client started in " + duration + " ms"); - Runtime.getRuntime().addShutdownHook(new Thread(() -> sshClient.stop(), "Stop SSH client")); - } - return sshClient; - } - - synchronized SftpFileSystemProvider getSftpFileSystemProvider() { - if (sftpFileSystemProvider == null) { - sftpFileSystemProvider = new SftpFileSystemProvider(sshClient); - } - return sftpFileSystemProvider; - } - - void authenticate() { - try { - 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.auth().verify(1000l); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - - void addPassword(String password) { - session.addPasswordIdentity(password); - } - - void loadKey(String password) { - loadKey(password, System.getProperty("user.home") + "/.ssh/id_rsa"); - } - - 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); -// } - } - - void openSession(URI uri) { - openSession(uri.getUserInfo(), uri.getHost(), uri.getPort() > 0 ? uri.getPort() : null); - } - - void openSession(String login, String host, Integer port) { - if (session != null) - throw new IllegalStateException("Session is already open"); - - if (host == null) - host = "localhost"; - if (port == null) - port = 22; - if (login == null) - login = System.getProperty("user.name"); - String password = null; - int sepIndex = login.indexOf(':'); - if (sepIndex > 0) - if (sepIndex + 1 < login.length()) { - password = login.substring(sepIndex + 1); - login = login.substring(0, sepIndex); - } else { - throw new IllegalArgumentException("Illegal authority: " + login); - } - try { - ConnectFuture connectFuture = getSshClient().connect(login, host, port); - connectFuture.await(); - ClientSession session = connectFuture.getSession(); - if (password != null) { - session.addPasswordIdentity(password); - passwordSet = true; - } - this.session = session; - } catch (IOException e) { - throw new IllegalStateException("Cannot connect to " + host + ":" + port); - } - } - - void closeSession() { - if (session == null) - throw new IllegalStateException("No session is open"); - try { - session.close(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - session = null; - } - } - - ClientSession getSession() { - return session; - } - - public void setSshKeyPair(SshKeyPair sshKeyPair) { - this.sshKeyPair = sshKeyPair; - } - - public static void openShell(ClientSession session) { - try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) { - channel.setIn(new NoCloseInputStream(System.in)); - channel.setOut(new NoCloseOutputStream(System.out)); - channel.setErr(new NoCloseOutputStream(System.err)); - channel.open(); - - Set events = new HashSet<>(); - events.add(ClientChannelEvent.CLOSED); - channel.waitFor(events, 0); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - session.close(false); - } - } - - static URI toUri(String username, String host, int port) { - try { - if (username == null) - username = "root"; - return new URI("ssh://" + username + "@" + host + ":" + port); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Cannot generate SSH URI to " + host + ":" + port + " for " + username, - e); - } - } - -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/BasicSshServer.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/BasicSshServer.java deleted file mode 100644 index 2a4d8f472..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/BasicSshServer.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.argeo.ssh; - -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.apache.sshd.server.SshServer; -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; - -/** A simple SSH server with some defaults. Supports SCP. */ -@SuppressWarnings("restriction") -public class BasicSshServer { - private Integer port; - private Path hostKeyPath; - - private SshServer sshd = null; - - public BasicSshServer(Integer port, Path hostKeyPath) { - this.port = port; - this.hostKeyPath = hostKeyPath; - } - - public void init() { - try { - sshd = SshServer.setUpDefaultServer(); - sshd.setPort(port); - if (hostKeyPath == null) - throw new IllegalStateException("An SSH server key must be set"); - sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath)); - // sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", - // "-l" })); - String[] shellCommand = OS.LOCAL.getDefaultShellCommand(); - // FIXME transfer args -// sshd.setShellFactory(new ProcessShellFactory(shellCommand)); -// sshd.setShellFactory(new ProcessShellFactory(shellCommand[0], shellCommand)); - sshd.setCommandFactory(new ScpCommandFactory()); - sshd.start(); - } catch (Exception e) { - throw new RuntimeException("Cannot start SSH server on port " + port, e); - } - } - - public void destroy() { - try { - sshd.stop(); - } catch (IOException e) { - throw new RuntimeException("Cannot stop SSH server on port " + port, e); - } - } - - public Integer getPort() { - return port; - } - - public void setPort(Integer port) { - this.port = port; - } - - public Path getHostKeyPath() { - return hostKeyPath; - } - - public void setHostKeyPath(Path hostKeyPath) { - this.hostKeyPath = hostKeyPath; - } - - public static void main(String[] args) { - int port = 2222; - Path hostKeyPath = Paths.get("hostkey.ser"); - try { - if (args.length > 0) - port = Integer.parseInt(args[0]); - if (args.length > 1) - hostKeyPath = Paths.get(args[1]); - } catch (Exception e1) { - printUsage(); - } - - BasicSshServer sshServer = new BasicSshServer(port, hostKeyPath); - sshServer.init(); - Runtime.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") { - - @Override - public void run() { - sshServer.destroy(); - } - }); - try { - synchronized (sshServer) { - sshServer.wait(); - } - } catch (InterruptedException e) { - sshServer.destroy(); - } - - } - - public static void printUsage() { - System.out.println("java " + BasicSshServer.class.getName() + " [port] [server key path]"); - } - -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java deleted file mode 100644 index da10b961f..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.argeo.ssh; - -import java.io.IOException; -import java.net.URI; -import java.nio.file.FileSystem; -import java.nio.file.Path; - -import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem; - -/** Create an SFTP {@link FileSystem}. */ -public class Sftp extends AbstractSsh { - private URI uri; - - private SftpFileSystem fileSystem; - - public Sftp(String username, String host, int port) { - this(AbstractSsh.toUri(username, host, port)); - } - - public Sftp(URI uri) { - this.uri = uri; - openSession(uri); - } - - public FileSystem getFileSystem() { - if (fileSystem == null) { - try { - authenticate(); - fileSystem = getSftpFileSystemProvider().newFileSystem(getSession()); - } catch (IOException e) { - throw new IllegalStateException(e); - } - } - return fileSystem; - } - - public Path getBasePath() { - String p = uri.getPath() != null ? uri.getPath() : "/"; - return getFileSystem().getPath(p); - } - -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/Ssh.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/Ssh.java deleted file mode 100644 index 68dd912ec..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/Ssh.java +++ /dev/null @@ -1,81 +0,0 @@ -package org.argeo.ssh; - -import java.net.URI; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; - -/** Create an SSH shell. */ -public class Ssh extends AbstractSsh { - private final URI uri; - - public Ssh(String username, String host, int port) { - this(AbstractSsh.toUri(username, host, port)); - } - - public Ssh(URI uri) { - this.uri = uri; - openSession(uri); - } - - public static void main(String[] args) { - Options options = getOptions(); - CommandLineParser parser = new DefaultParser(); - try { - CommandLine line = parser.parse(options, args); - List remaining = line.getArgList(); - if (remaining.size() == 0) { - System.err.println("There must be at least one argument"); - printHelp(options); - System.exit(1); - } - URI uri = new URI("ssh://" + remaining.get(0)); - List command = new ArrayList<>(); - if (remaining.size() > 1) { - for (int i = 1; i < remaining.size(); i++) { - command.add(remaining.get(i)); - } - } - - // auth - Ssh ssh = new Ssh(uri); - ssh.authenticate(); - - if (command.size() == 0) {// shell - AbstractSsh.openShell(ssh.getSession()); - } else {// execute command - - } - ssh.closeSession(); - } catch (Exception exp) { - exp.printStackTrace(); - printHelp(options); - System.exit(1); - } finally { - - } - } - - public URI getUri() { - return uri; - } - - public static Options getOptions() { - Options options = new Options(); -// options.addOption("p", true, "port"); - options.addOption(Option.builder("p").hasArg().argName("port").desc("port of the SSH server").build()); - - return options; - } - - public static void printHelp(Options options) { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp("ssh [username@]hostname", options, true); - } -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshKeyPair.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshKeyPair.java deleted file mode 100644 index f9b348598..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshKeyPair.java +++ /dev/null @@ -1,182 +0,0 @@ -package org.argeo.ssh; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.GeneralSecurityException; -import java.security.KeyFactory; -import java.security.KeyPair; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.interfaces.RSAPrivateCrtKey; -import java.security.spec.RSAPublicKeySpec; - -import org.apache.sshd.common.config.keys.KeyUtils; -import org.apache.sshd.common.config.keys.PublicKeyEntry; -import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; -import org.bouncycastle.openssl.PEMKeyPair; -import org.bouncycastle.openssl.PEMParser; -import org.bouncycastle.openssl.PKCS8Generator; -import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; -import org.bouncycastle.openssl.jcajce.JcaPEMWriter; -import org.bouncycastle.openssl.jcajce.JcaPKCS8Generator; -import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder; -import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8EncryptorBuilder; -import org.bouncycastle.operator.InputDecryptorProvider; -import org.bouncycastle.operator.OutputEncryptor; -import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; - -@SuppressWarnings("restriction") -public class SshKeyPair { - public final static String RSA_KEY_TYPE = "ssh-rsa"; - - private PublicKey publicKey; - private PrivateKey privateKey; - private KeyPair keyPair; - - public SshKeyPair(KeyPair keyPair) { - super(); - this.publicKey = keyPair.getPublic(); - this.privateKey = keyPair.getPrivate(); - this.keyPair = keyPair; - } - - public SshKeyPair(PublicKey publicKey, PrivateKey privateKey) { - super(); - this.publicKey = publicKey; - this.privateKey = privateKey; - this.keyPair = new KeyPair(publicKey, privateKey); - } - - public KeyPair asKeyPair() { - return keyPair; - } - - public String getPublicKeyAsOpenSshString() { - return PublicKeyEntry.toString(publicKey); - } - - public String getPrivateKeyAsPemString(char[] password) { - try { - Object obj; - - if (password != null) { - JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder( - PKCS8Generator.PBE_SHA1_3DES); - encryptorBuilder.setPasssword(password); - OutputEncryptor oe = encryptorBuilder.build(); - JcaPKCS8Generator gen = new JcaPKCS8Generator(privateKey, oe); - obj = gen.generate(); - } else { - obj = privateKey; - } - - StringWriter sw = new StringWriter(); - JcaPEMWriter pemWrt = new JcaPEMWriter(sw); - pemWrt.writeObject(obj); - pemWrt.close(); - return sw.toString(); - } catch (Exception e) { - throw new RuntimeException("Cannot convert private key", e); - } - } - - public static SshKeyPair loadOrGenerate(Path privateKeyPath, int size, char[] password) { - try { - SshKeyPair sshKeyPair; - if (Files.exists(privateKeyPath)) { -// String privateKeyStr = new String(Files.readAllBytes(privateKeyPath), StandardCharsets.US_ASCII); - sshKeyPair = load( - new InputStreamReader(Files.newInputStream(privateKeyPath), StandardCharsets.US_ASCII), - password); - // TOD make sure public key is consistemt - } else { - sshKeyPair = generate(size); - Files.write(privateKeyPath, - sshKeyPair.getPrivateKeyAsPemString(password).getBytes(StandardCharsets.US_ASCII)); - Path publicKeyPath = privateKeyPath.resolveSibling(privateKeyPath.getFileName() + ".pub"); - Files.write(publicKeyPath, - sshKeyPair.getPublicKeyAsOpenSshString().getBytes(StandardCharsets.US_ASCII)); - } - return sshKeyPair; - } catch (IOException e) { - throw new RuntimeException("Cannot read or write private key " + privateKeyPath, e); - } - } - - public static SshKeyPair generate(int size) { - return generate(RSA_KEY_TYPE, size); - } - - public static SshKeyPair generate(String keyType, int size) { - try { - KeyPair keyPair = KeyUtils.generateKeyPair(keyType, size); - PublicKey publicKey = keyPair.getPublic(); - PrivateKey privateKey = keyPair.getPrivate(); - return new SshKeyPair(publicKey, privateKey); - } catch (GeneralSecurityException e) { - throw new RuntimeException("Cannot generate SSH key", e); - } - } - - public static SshKeyPair load(Reader reader, char[] password) { - try (PEMParser pemParser = new PEMParser(reader)) { - Object object = pemParser.readObject(); - JcaPEMKeyConverter converter = new JcaPEMKeyConverter();// .setProvider("BC"); - KeyPair kp; - if (object instanceof PKCS8EncryptedPrivateKeyInfo) { - // Encrypted key - we will use provided password - PKCS8EncryptedPrivateKeyInfo ckp = (PKCS8EncryptedPrivateKeyInfo) object; -// PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(password); - InputDecryptorProvider inputDecryptorProvider = new JceOpenSSLPKCS8DecryptorProviderBuilder() - .build(password); - PrivateKeyInfo pkInfo = ckp.decryptPrivateKeyInfo(inputDecryptorProvider); - PrivateKey privateKey = converter.getPrivateKey(pkInfo); - - // generate public key - RSAPrivateCrtKey privk = (RSAPrivateCrtKey) privateKey; - RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(privk.getModulus(), - privk.getPublicExponent()); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); - - kp = new KeyPair(publicKey, privateKey); - } else { - // Unencrypted key - no password needed -// PKCS8EncryptedPrivateKeyInfo ukp = (PKCS8EncryptedPrivateKeyInfo) object; - PEMKeyPair pemKp = (PEMKeyPair) object; - kp = converter.getKeyPair(pemKp); - } - return new SshKeyPair(kp); - } catch (Exception e) { - throw new RuntimeException("Cannot load private key", e); - } - } - - public static void main(String args[]) { - Path privateKeyPath = Paths.get(System.getProperty("user.dir") + "/id_rsa"); - SshKeyPair skp = SshKeyPair.loadOrGenerate(privateKeyPath, 1024, null); - System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString()); - System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null)); - System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray())); - - StringReader reader = new StringReader(skp.getPrivateKeyAsPemString(null)); - skp = SshKeyPair.load(reader, null); - System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString()); - System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null)); - System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray())); - - reader = new StringReader(skp.getPrivateKeyAsPemString("demo".toCharArray())); - skp = SshKeyPair.load(reader, "demo".toCharArray()); - System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString()); - System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null)); - System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray())); - } - -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshSync.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshSync.java deleted file mode 100644 index b9f1e91d0..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/SshSync.java +++ /dev/null @@ -1,134 +0,0 @@ -package org.argeo.ssh; - -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.util.Map; -import java.util.Scanner; - -import org.apache.commons.io.IOUtils; -import org.apache.sshd.agent.SshAgent; -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.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.argeo.api.cms.CmsLog; - -public class SshSync { - private final static CmsLog log = CmsLog.getLog(SshSync.class); - - public static void main(String[] args) { - - try (SshClient client = SshClient.setUpDefaultClient()) { - client.start(); - boolean osAgent = true; - SshAgentFactory agentFactory = osAgent ? new UnixAgentFactory() : new LocalAgentFactory(); - // SshAgentFactory agentFactory = new LocalAgentFactory(); - client.setAgentFactory(agentFactory); - SshAgent sshAgent = agentFactory.createClient(client); - - String login = System.getProperty("user.name"); - String host = "localhost"; - int port = 22; - - 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"); - } - -// List> identities = sshAgent.getIdentities(); -// for (Map.Entry entry : identities) { -// System.out.println(entry.getValue() + " : " + entry.getKey()); -// } - - ConnectFuture connectFuture = client.connect(login, host, port); - connectFuture.await(); - ClientSession session = connectFuture.getSession(); - - try { - -// session.addPasswordIdentity(new String(password)); - session.auth().verify(1000l); - - SftpFileSystemProvider fsProvider = new SftpFileSystemProvider(client); - - SftpFileSystem fs = fsProvider.newFileSystem(session); - Path testPath = fs.getPath("/home/" + login + "/tmp"); - Files.list(testPath).forEach(System.out::println); - test(testPath); - - } finally { - client.stop(); - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - static void test(Path testBase) { - try { - Path testPath = testBase.resolve("ssh-test.txt"); - Files.createFile(testPath); - log.debug("Created file " + testPath); - Files.delete(testPath); - log.debug("Deleted " + testPath); - String txt = "TEST\nTEST2\n"; - byte[] arr = txt.getBytes(); - Files.write(testPath, arr); - log.debug("Wrote " + testPath); - byte[] read = Files.readAllBytes(testPath); - log.debug("Read " + testPath); - Path testDir = testBase.resolve("testDir"); - log.debug("Resolved " + testDir); - // Copy - Files.createDirectory(testDir); - log.debug("Created directory " + testDir); - Path subsubdir = Files.createDirectories(testDir.resolve("subdir/subsubdir")); - log.debug("Created sub directories " + subsubdir); - Path copiedFile = testDir.resolve("copiedFile.txt"); - log.debug("Resolved " + copiedFile); - Path relativeCopiedFile = testDir.relativize(copiedFile); - log.debug("Relative copied file " + relativeCopiedFile); - try (OutputStream out = Files.newOutputStream(copiedFile); - InputStream in = Files.newInputStream(testPath)) { - IOUtils.copy(in, out); - } - log.debug("Copied " + testPath + " to " + copiedFile); - Files.delete(testPath); - log.debug("Deleted " + testPath); - byte[] copiedRead = Files.readAllBytes(copiedFile); - log.debug("Read " + copiedFile); - // Browse directories - DirectoryStream files = Files.newDirectoryStream(testDir); - int fileCount = 0; - Path listedFile = null; - for (Path file : files) { - fileCount++; - if (!Files.isDirectory(file)) - listedFile = file; - } - log.debug("Listed " + testDir); - // Generic attributes - Map attrs = Files.readAttributes(copiedFile, "*"); - log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - -} diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/package-info.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/package-info.java deleted file mode 100644 index 8324a7a88..000000000 --- a/cms/org.argeo.cms.integration/src/org/argeo/ssh/package-info.java +++ /dev/null @@ -1,2 +0,0 @@ -/** SSH support. */ -package org.argeo.ssh; \ No newline at end of file -- 2.30.2