Example usage for org.eclipse.jgit.transport SshTransport setSshSessionFactory

List of usage examples for org.eclipse.jgit.transport SshTransport setSshSessionFactory

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport SshTransport setSshSessionFactory.

Prototype

public void setSshSessionFactory(SshSessionFactory factory) 

Source Link

Document

Set SSH session factory instead of the default one for this instance of the transport.

Usage

From source file:jetbrains.buildServer.buildTriggers.vcs.git.TransportFactoryImpl.java

License:Apache License

public Transport createTransport(@NotNull final Repository r, @NotNull final URIish url,
        @NotNull final AuthSettings authSettings, final int timeoutSeconds)
        throws NotSupportedException, VcsException {
    try {/*from w ww .  j  a va 2  s  .c  o  m*/
        checkUrl(url);
        URIish preparedURI = prepareURI(url);
        final Transport t = Transport.open(r, preparedURI);
        t.setCredentialsProvider(authSettings.toCredentialsProvider());
        if (t instanceof SshTransport) {
            SshTransport ssh = (SshTransport) t;
            ssh.setSshSessionFactory(getSshSessionFactory(authSettings, url));
        }
        t.setTimeout(timeoutSeconds);
        return t;
    } catch (TransportException e) {
        throw new VcsException("Cannot create transport", e);
    }
}

From source file:org.apache.maven.scm.provider.git.jgit.command.JGitTransportConfigCallback.java

License:Apache License

@Override
public void configure(Transport transport) {
    if (transport instanceof SshTransport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }// w ww  .  ja va 2 s .c om
}

From source file:org.apache.oozie.action.hadoop.GitOperations.java

License:Apache License

/**
 * Clones a Git repository/*from   w  ww .j a  va 2  s.c o m*/
 * @param outputDir location in which to clone the Git repository
 * @throws GitOperationsException if the Git clone fails
 */
private void cloneRepo(final File outputDir) throws GitOperationsException {
    final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(final OpenSshConfig.Host host, final Session session) {
            // nop
        }

        @Override
        protected JSch createDefaultJSch(final FS fs) throws JSchException {
            JSch.setConfig("StrictHostKeyChecking", "no");
            final JSch defaultJSch = super.createDefaultJSch(fs);

            if (credentialFile != null) {
                defaultJSch.addIdentity(credentialFile.toString());
            }

            return defaultJSch;
        }
    };

    final CloneCommand cloneCommand = Git.cloneRepository();
    cloneCommand.setURI(srcURL.toString());

    if (srcURL.getScheme().toLowerCase().equals("ssh")) {
        cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
            @Override
            public void configure(final Transport transport) {
                final SshTransport sshTransport = (SshTransport) transport;
                sshTransport.setSshSessionFactory(sshSessionFactory);
            }
        });
    }

    cloneCommand.setDirectory(outputDir);
    // set our branch identifier
    if (branch != null) {
        cloneCommand.setBranchesToClone(Arrays.asList("refs/heads/" + branch));
    }

    try {
        cloneCommand.call();
    } catch (final GitAPIException e) {
        throw new GitOperationsException("Unable to clone Git repo: ", e);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

/**
 * Execute remote jgit command.//from   w  w w.  j a va 2 s  . com
 *
 * @param remoteUrl
 *         remote url
 * @param command
 *         command to execute
 * @return executed command
 * @throws GitException
 * @throws GitAPIException
 * @throws UnauthorizedException
 */
private Object executeRemoteCommand(String remoteUrl, TransportCommand command)
        throws GitException, GitAPIException, UnauthorizedException {
    String sshKeyDirectoryPath = "";
    try {
        if (GitUrlUtils.isSSH(remoteUrl)) {
            File keyDirectory = Files.createTempDir();
            sshKeyDirectoryPath = keyDirectory.getPath();
            File sshKey = writePrivateKeyFile(remoteUrl, keyDirectory);

            SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
                @Override
                protected void configure(OpenSshConfig.Host host, Session session) {
                    session.setConfig("StrictHostKeyChecking", "no");
                }

                @Override
                protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
                    JSch jsch = super.getJSch(hc, fs);
                    jsch.removeAllIdentity();
                    jsch.addIdentity(sshKey.getAbsolutePath());
                    return jsch;
                }
            };
            command.setTransportConfigCallback(new TransportConfigCallback() {
                @Override
                public void configure(Transport transport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }
            });
        } else {
            UserCredential credentials = credentialsLoader.getUserCredential(remoteUrl);
            if (credentials != null) {
                command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
                        credentials.getUserName(), credentials.getPassword()));
            }
        }
        return command.call();
    } catch (GitException | TransportException exception) {
        if ("Unable get private ssh key".equals(exception.getMessage())
                || exception.getMessage().contains(ERROR_AUTHENTICATION_REQUIRED)) {
            throw new UnauthorizedException(exception.getMessage());
        } else {
            throw exception;
        }
    } finally {
        if (!sshKeyDirectoryPath.isEmpty()) {
            try {
                FileUtils.delete(new File(sshKeyDirectoryPath), FileUtils.RECURSIVE);
            } catch (IOException exception) {
                throw new GitException("Can't remove SSH key directory", exception);
            }
        }
    }
}

From source file:org.jabylon.team.git.GitTeamProvider.java

License:Open Source License

private TransportConfigCallback createTransportConfigCallback(Project project) {

    Preferences node = PreferencesUtil.scopeFor(project);
    //        String username = node.get(GitConstants.KEY_USERNAME, "");
    final String password = node.get(GitConstants.KEY_PASSWORD, "");
    String privateKey = node.get(GitConstants.KEY_PRIVATE_KEY, null);
    File tempFile = null;//from   w  w  w  .  j  av a  2 s .co  m
    if (privateKey != null) {
        try {
            // store it somewhere temporary
            File tempDir = new File(new File(ServerConstants.WORKING_DIR), "temp");
            tempDir.mkdirs();
            tempFile = File.createTempFile("temp", ".key", tempDir);
            Files.copy(new ByteArrayInputStream(privateKey.getBytes("UTF-8")), tempFile.toPath(),
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            LOGGER.error("Could not store private key to " + tempFile.getAbsolutePath(), e);
        }

    }
    final File keyFile = tempFile;
    final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

        AtomicInteger sessionCounter = new AtomicInteger();

        @Override
        protected void configure(Host host, Session session) {

            // do nothing
        }

        protected JSch createDefaultJSch(FS fs) throws JSchException {
            JSch defaultJSch = super.createDefaultJSch(fs);
            if (keyFile != null)
                defaultJSch.addIdentity(keyFile.getAbsolutePath(), password);
            return defaultJSch;
        }

        @Override
        protected Session createSession(Host hc, String user, String host, int port, FS fs)
                throws JSchException {
            sessionCounter.getAndIncrement();
            return super.createSession(hc, user, host, port, fs);
        }

        @Override
        public void releaseSession(RemoteSession session) {
            if (sessionCounter.decrementAndGet() == 0) {
                LOGGER.debug("Connection closing, deleting keyfile");
                keyFile.delete();
            }
            super.releaseSession(session);
        }

    };
    return new TransportConfigCallback() {

        @Override
        public void configure(Transport transport) {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    };
}

From source file:org.springframework.cloud.config.server.ssh.PropertiesBasedSshTransportConfigCallback.java

License:Apache License

@Override
public void configure(Transport transport) {
    if (transport instanceof SshTransport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(new PropertyBasedSshSessionFactory(
                new SshUriPropertyProcessor(sshUriProperties).getSshKeysByHostname(), new JSch()));
    }//from   w  w  w  . j ava  2s  .c om
}