Example usage for org.eclipse.jgit.api TransportCommand setTransportConfigCallback

List of usage examples for org.eclipse.jgit.api TransportCommand setTransportConfigCallback

Introduction

In this page you can find the example usage for org.eclipse.jgit.api TransportCommand setTransportConfigCallback.

Prototype

public C setTransportConfigCallback(final TransportConfigCallback transportConfigCallback) 

Source Link

Document

Set the TransportConfigCallback.

Usage

From source file:bluej.groupwork.git.GitCommand.java

License:Open Source License

/**
 * Prepare a TransportCommand to be executed later.
 * This method checks if the command is using ssh. if it is,
 * then it will disable SSH's fingerprint detection and use
 * username and password authentication.
 * @param command The command to be configured.
 *//*from  w  w w  . j  a  v  a2 s .  co m*/
public void disableFingerprintCheck(TransportCommand command) {
    //check if the command is not null and conects via ssh.
    if (command != null && repository.getReposUrl().startsWith("ssh")) {
        //disable ssh host fingerprint check.
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host host, Session sn) {
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                sn.setConfig(config);
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                return super.createDefaultJSch(fs);
            }
        };

        command.setTransportConfigCallback((Transport t) -> {
            SshTransport sshTransport = (SshTransport) t;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        });
        command.setCredentialsProvider(getRepository().getCredentialsProvider());
    }
}

From source file:io.fabric8.collector.git.GitHelpers.java

License:Apache License

/**
 * Configures the transport of the command to deal with things like SSH
 *///www.  j a  va  2s . c  om
public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command,
        CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) {
    if (sshPrivateKey != null) {
        final CredentialsProvider provider = credentialsProvider;
        command.setTransportConfigCallback(new TransportConfigCallback() {
            @Override
            public void configure(Transport transport) {
                if (transport instanceof SshTransport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
                        @Override
                        protected void configure(OpenSshConfig.Host host, Session session) {
                            session.setConfig("StrictHostKeyChecking", "no");
                            UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
                            session.setUserInfo(userInfo);
                        }

                        @Override
                        protected JSch createDefaultJSch(FS fs) throws JSchException {
                            JSch jsch = super.createDefaultJSch(fs);
                            jsch.removeAllIdentity();
                            String absolutePath = sshPrivateKey.getAbsolutePath();
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: "
                                        + sshPublicKey);
                            }
                            if (sshPublicKey != null) {
                                jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null);
                            } else {
                                jsch.addIdentity(absolutePath);
                            }
                            return jsch;
                        }
                    };
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }
            }
        });
    }
}

From source file:io.fabric8.project.support.GitUtils.java

License:Apache License

/**
 * Configures the transport of the command to deal with things like SSH
 *//*from ww  w.  java 2 s  .c o m*/
public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command,
        CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) {
    LOG.info("Using " + credentialsProvider);
    if (sshPrivateKey != null) {
        final CredentialsProvider provider = credentialsProvider;
        command.setTransportConfigCallback(new TransportConfigCallback() {
            @Override
            public void configure(Transport transport) {
                if (transport instanceof SshTransport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
                        @Override
                        protected void configure(OpenSshConfig.Host host, Session session) {
                            session.setConfig("StrictHostKeyChecking", "no");
                            UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
                            session.setUserInfo(userInfo);
                        }

                        @Override
                        protected JSch createDefaultJSch(FS fs) throws JSchException {
                            JSch jsch = super.createDefaultJSch(fs);
                            jsch.removeAllIdentity();
                            String absolutePath = sshPrivateKey.getAbsolutePath();
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: "
                                        + sshPublicKey);
                            }
                            if (sshPublicKey != null) {
                                jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null);
                            } else {
                                jsch.addIdentity(absolutePath);
                            }
                            return jsch;
                        }
                    };
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }
            }
        });
    }
}

From source file:org.craftercms.commons.git.auth.AbstractSshAuthConfigurator.java

License:Open Source License

@Override
public void configureAuthentication(TransportCommand command) {
    SshSessionFactory sessionFactory = createSessionFactory();

    command.setTransportConfigCallback(
            transport -> ((SshTransport) transport).setSshSessionFactory(sessionFactory));
}

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

License:Open Source License

/**
 * Execute remote jgit command.//from  ww  w.j  a v a  2s.c  om
 *
 * @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);
            }
        }
    }
}