Search in sources :

Example 1 with SshdSessionFactory

use of org.eclipse.jgit.transport.sshd.SshdSessionFactory in project gerrit by GerritCodeReview.

the class SshSessionMina method getMinaSession.

private SshdSession getMinaSession() throws Exception {
    if (session == null) {
        String username = getUsername();
        URIish uri = new URIish("ssh://" + username + "@" + addr.getAddress().getHostAddress() + ":" + addr.getPort());
        // TODO(davido): Switch to memory only key resolving mode.
        File userhome = createTempDirectory("home-").toFile();
        FS fs = FS.DETECTED.setUserHome(userhome);
        File sshDir = new File(userhome, ".ssh");
        sshDir.mkdir();
        OpenSSHKeyPairResourceWriter keyPairWriter = new OpenSSHKeyPairResourceWriter();
        try (OutputStream out = new FileOutputStream(new File(sshDir, "id_ecdsa"))) {
            keyPairWriter.writePrivateKey(sshKeys.getKeyPair(account), null, null, out);
        }
        // TODO(davido): Disable programmatically host key checking: "StrictHostKeyChecking: no" mode.
        CharSink configFile = Files.asCharSink(new File(sshDir, "config"), UTF_8);
        configFile.writeLines(Arrays.asList("Host *", "StrictHostKeyChecking no"));
        JGitKeyCache keyCache = new JGitKeyCache();
        try (SshdSessionFactory factory = new SshdSessionFactory(keyCache, new DefaultProxyDataFactory())) {
            factory.setHomeDirectory(userhome);
            factory.setSshDirectory(sshDir);
            session = factory.getSession(uri, null, fs, TIMEOUT);
            session.addCloseListener(future -> {
                try {
                    MoreFiles.deleteRecursively(userhome.toPath(), ALLOW_INSECURE);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Failed to cleanup userhome", e);
                }
            });
        }
    }
    return session;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) CharSink(com.google.common.io.CharSink) JGitKeyCache(org.eclipse.jgit.transport.sshd.JGitKeyCache) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultProxyDataFactory(org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory) IOException(java.io.IOException) FS(org.eclipse.jgit.util.FS) OpenSSHKeyPairResourceWriter(org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter) SshdSessionFactory(org.eclipse.jgit.transport.sshd.SshdSessionFactory) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with SshdSessionFactory

use of org.eclipse.jgit.transport.sshd.SshdSessionFactory in project gerrit by GerritCodeReview.

the class SshSessionFactoryInitializer method init.

public static void init() {
    SshdSessionFactory factory = new SshdSessionFactory(new JGitKeyCache(), new DefaultProxyDataFactory());
    factory.setHomeDirectory(FS.DETECTED.userHome());
    SshSessionFactory.setInstance(factory);
}
Also used : JGitKeyCache(org.eclipse.jgit.transport.sshd.JGitKeyCache) SshdSessionFactory(org.eclipse.jgit.transport.sshd.SshdSessionFactory) DefaultProxyDataFactory(org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory)

Example 3 with SshdSessionFactory

use of org.eclipse.jgit.transport.sshd.SshdSessionFactory in project gerrit by GerritCodeReview.

the class SshSessionMina method initClient.

public static void initClient() {
    JGitKeyCache keyCache = new JGitKeyCache();
    SshdSessionFactory factory = new SshdSessionFactory(keyCache, new DefaultProxyDataFactory());
    SshSessionFactory.setInstance(factory);
}
Also used : JGitKeyCache(org.eclipse.jgit.transport.sshd.JGitKeyCache) SshdSessionFactory(org.eclipse.jgit.transport.sshd.SshdSessionFactory) DefaultProxyDataFactory(org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory)

Aggregations

DefaultProxyDataFactory (org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory)3 JGitKeyCache (org.eclipse.jgit.transport.sshd.JGitKeyCache)3 SshdSessionFactory (org.eclipse.jgit.transport.sshd.SshdSessionFactory)3 CharSink (com.google.common.io.CharSink)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OpenSSHKeyPairResourceWriter (org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter)1 URIish (org.eclipse.jgit.transport.URIish)1 FS (org.eclipse.jgit.util.FS)1