Search in sources :

Example 1 with JschConfigSessionFactory

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

the class GitUtil method initSsh.

public static void initSsh(final TestAccount a) {
    final Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch.setConfig(config);
    // register a JschConfigSessionFactory that adds the private key as identity
    // to the JSch instance of JGit so that SSH communication via JGit can
    // succeed
    SshSessionFactory.setInstance(new JschConfigSessionFactory() {

        @Override
        protected void configure(Host hc, Session session) {
            try {
                final JSch jsch = getJSch(hc, FS.DETECTED);
                jsch.addIdentity("KeyPair", a.privateKey(), a.sshKey.getPublicKeyBlob(), null);
            } catch (JSchException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : JSchException(com.jcraft.jsch.JSchException) Host(org.eclipse.jgit.transport.OpenSshConfig.Host) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 2 with JschConfigSessionFactory

use of org.eclipse.jgit.transport.JschConfigSessionFactory in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepository method initialize.

private void initialize() {
    if (!this.initialized) {
        SshSessionFactory.setInstance(new JschConfigSessionFactory() {

            @Override
            protected void configure(Host hc, Session session) {
                session.setConfig("StrictHostKeyChecking", isStrictHostKeyChecking() ? "yes" : "no");
            }
        });
        this.initialized = true;
    }
}
Also used : Host(org.eclipse.jgit.transport.OpenSshConfig.Host) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) Session(com.jcraft.jsch.Session)

Example 3 with JschConfigSessionFactory

use of org.eclipse.jgit.transport.JschConfigSessionFactory in project Android-Password-Store by zeapo.

the class GitOperation method setAuthentication.

/**
 * Sets the authentication using ssh-key scheme
 *
 * @param sshKey     the ssh-key file
 * @param username   the username
 * @param passphrase the passphrase
 * @return the current object
 */
GitOperation setAuthentication(File sshKey, String username, String passphrase) {
    JschConfigSessionFactory sessionFactory = new SshConfigSessionFactory(sshKey.getAbsolutePath(), username, passphrase);
    SshSessionFactory.setInstance(sessionFactory);
    this.provider = null;
    return this;
}
Also used : SshConfigSessionFactory(com.zeapo.pwdstore.git.config.SshConfigSessionFactory) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory)

Example 4 with JschConfigSessionFactory

use of org.eclipse.jgit.transport.JschConfigSessionFactory in project che by eclipse.

the class JGitConnection method executeRemoteCommand.

/**
     * Execute remote jgit command.
     *
     * @param remoteUrl
     *         remote url
     * @param command
     *         command to execute
     * @return executed command
     * @throws GitException
     * @throws GitAPIException
     * @throws UnauthorizedException
     */
@VisibleForTesting
Object executeRemoteCommand(String remoteUrl, TransportCommand command, @Nullable String username, @Nullable String password) throws GitException, GitAPIException, UnauthorizedException {
    File keyDirectory = null;
    UserCredential credentials = null;
    try {
        if (GitUrlUtils.isSSH(remoteUrl)) {
            keyDirectory = Files.createTempDir();
            final 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(transport -> {
                if (transport instanceof SshTransport) {
                    ((SshTransport) transport).setSshSessionFactory(sshSessionFactory);
                }
            });
        } else {
            if (remoteUrl != null && GIT_URL_WITH_CREDENTIALS_PATTERN.matcher(remoteUrl).matches()) {
                username = remoteUrl.substring(remoteUrl.indexOf("://") + 3, remoteUrl.lastIndexOf(":"));
                password = remoteUrl.substring(remoteUrl.lastIndexOf(":") + 1, remoteUrl.indexOf("@"));
                command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
            } else {
                if (username != null && password != null) {
                    command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
                } else {
                    credentials = credentialsLoader.getUserCredential(remoteUrl);
                    if (credentials != null) {
                        command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(credentials.getUserName(), credentials.getPassword()));
                    }
                }
            }
        }
        ProxyAuthenticator.initAuthenticator(remoteUrl);
        return command.call();
    } catch (GitException | TransportException exception) {
        if ("Unable get private ssh key".equals(exception.getMessage())) {
            throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY);
        } else if (exception.getMessage().contains(ERROR_AUTHENTICATION_REQUIRED)) {
            final ProviderInfo info = credentialsLoader.getProviderInfo(remoteUrl);
            if (info != null) {
                throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION, ImmutableMap.of(PROVIDER_NAME, info.getProviderName(), AUTHENTICATE_URL, info.getAuthenticateUrl(), "authenticated", Boolean.toString(credentials != null)));
            }
            throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION);
        } else {
            throw exception;
        }
    } finally {
        if (keyDirectory != null && keyDirectory.exists()) {
            try {
                FileUtils.delete(keyDirectory, FileUtils.RECURSIVE);
            } catch (IOException exception) {
                throw new GitException("Can't remove SSH key directory", exception);
            }
        }
        ProxyAuthenticator.resetAuthenticator();
    }
}
Also used : UserCredential(org.eclipse.che.api.git.UserCredential) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) TransportException(org.eclipse.jgit.api.errors.TransportException) ProviderInfo(org.eclipse.che.api.git.shared.ProviderInfo) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with JschConfigSessionFactory

use of org.eclipse.jgit.transport.JschConfigSessionFactory in project fabric8 by fabric8io.

the class GitUtils method configureCommand.

/**
 * Configures the transport of the command to deal with things like SSH
 */
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);
                }
            }
        });
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfo(com.jcraft.jsch.UserInfo) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) CredentialsProviderUserInfo(org.eclipse.jgit.transport.CredentialsProviderUserInfo) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport) Session(com.jcraft.jsch.Session) SSLSession(javax.net.ssl.SSLSession)

Aggregations

JschConfigSessionFactory (org.eclipse.jgit.transport.JschConfigSessionFactory)7 Session (com.jcraft.jsch.Session)5 JSch (com.jcraft.jsch.JSch)4 SshTransport (org.eclipse.jgit.transport.SshTransport)4 JSchException (com.jcraft.jsch.JSchException)3 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)3 SshSessionFactory (org.eclipse.jgit.transport.SshSessionFactory)3 FS (org.eclipse.jgit.util.FS)3 IOException (java.io.IOException)2 TransportException (org.eclipse.jgit.api.errors.TransportException)2 OpenSshConfig (org.eclipse.jgit.transport.OpenSshConfig)2 Host (org.eclipse.jgit.transport.OpenSshConfig.Host)2 Transport (org.eclipse.jgit.transport.Transport)2 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)1 CredentialsMatchers (com.cloudbees.plugins.credentials.CredentialsMatchers)1 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 URIRequirementBuilder (com.cloudbees.plugins.credentials.domains.URIRequirementBuilder)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 KeyPair (com.jcraft.jsch.KeyPair)1