org.eclipse.jgit.api.TransportCommand Java Examples

The following examples show how to use org.eclipse.jgit.api.TransportCommand. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private <T extends TransportCommand<?, ?>> T configure(T command) {
    final MirrorCredential c = mirror.credential();
    switch (mirror.remoteRepoUri().getScheme()) {
        case SCHEME_GIT_HTTP:
        case SCHEME_GIT_HTTPS:
            if (c instanceof PasswordMirrorCredential) {
                configureHttp(command, (PasswordMirrorCredential) c);
            }
            break;
        case SCHEME_GIT_SSH:
            if (c instanceof PasswordMirrorCredential) {
                configureSsh(command, (PasswordMirrorCredential) c);
            } else if (c instanceof PublicKeyMirrorCredential) {
                configureSsh(command, (PublicKeyMirrorCredential) c);
            }
            break;
    }

    return command;
}
 
Example #2
Source File: StudioNodeSyncGlobalRepoTask.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
private <T extends TransportCommand> void configurePrivateKeyAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, final Path tempKey)
        throws CryptoException, IOException  {
    String privateKey = encryptor.decrypt(remoteNode.getGitPrivateKey());
    try {
        Files.write(tempKey, privateKey.getBytes());
    } catch (IOException e) {
        throw new IOException("Failed to write private key for SSH connection to temp location", e);
    }

    tempKey.toFile().deleteOnExit();

    gitCommand.setTransportConfigCallback(transport -> {
        SshTransport sshTransport = (SshTransport)transport;
        sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory() {

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = super.createDefaultJSch(fs);
                defaultJSch.addIdentity(tempKey.toAbsolutePath().toString());
                return defaultJSch;
            }

        });
    });
}
 
Example #3
Source File: StudioNodeSyncGlobalRepoTask.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
private <T extends TransportCommand> void configureBasicAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, boolean sshProtocol) throws CryptoException {
    String password = encryptor.decrypt(remoteNode.getGitPassword());
    UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(
            remoteNode.getGitUsername(), password);

    if (sshProtocol) {
        gitCommand.setTransportConfigCallback(transport -> {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory() {

                @Override
                protected void configure(OpenSshConfig.Host host, Session session) {
                    super.configure(host, session);
                    session.setPassword(password);
                }

            });
        });
    }

    gitCommand.setCredentialsProvider(credentialsProvider);
}
 
Example #4
Source File: StudioNodeSyncBaseTask.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
protected <T extends TransportCommand> void configurePrivateKeyAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, final Path tempKey)
        throws CryptoException, IOException  {
    String privateKey = encryptor.decrypt(remoteNode.getGitPrivateKey());
    try {
        Files.write(tempKey, privateKey.getBytes());
    } catch (IOException e) {
        throw new IOException("Failed to write private key for SSH connection to temp location", e);
    }

    tempKey.toFile().deleteOnExit();

    gitCommand.setTransportConfigCallback(transport -> {
        SshTransport sshTransport = (SshTransport)transport;
        sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory() {

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = super.createDefaultJSch(fs);
                defaultJSch.addIdentity(tempKey.toAbsolutePath().toString());
                return defaultJSch;
            }

        });
    });
}
 
Example #5
Source File: StudioNodeSyncBaseTask.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
protected <T extends TransportCommand> void configureBasicAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, boolean sshProtocol) throws CryptoException {
    String password = encryptor.decrypt(remoteNode.getGitPassword());
    UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(
            remoteNode.getGitUsername(), password);

    if (sshProtocol) {
        gitCommand.setTransportConfigCallback(transport -> {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory() {

                @Override
                protected void configure(OpenSshConfig.Host host, Session session) {
                    super.configure(host, session);
                    session.setPassword(password);
                }

            });
        });
    }

    gitCommand.setCredentialsProvider(credentialsProvider);
}
 
Example #6
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private void configureCommand(TransportCommand<?, ?> command) {
	command.setTimeout(this.timeout);
	if (this.transportConfigCallback != null) {
		command.setTransportConfigCallback(this.transportConfigCallback);
	}
	CredentialsProvider credentialsProvider = getCredentialsProvider();
	if (credentialsProvider != null) {
		command.setCredentialsProvider(credentialsProvider);
	}
}
 
Example #7
Source File: StudioNodeSyncBaseTask.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected <T extends TransportCommand> void configureTokenAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, boolean sshProtocol) throws CryptoException {
    UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(
            encryptor.decrypt(remoteNode.getGitToken()), StringUtils.EMPTY);

    if (sshProtocol) {
        gitCommand.setTransportConfigCallback(transport -> {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory());
        });
    }

    gitCommand.setCredentialsProvider(credentialsProvider);
}
 
Example #8
Source File: GitUtils.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Tries to set proper credentials for the command
 * @param repo repo to test for url
 * @param command command that needs credentials
 * @param credential credential to use
 */
private static void addCredential(Repository repo, TransportCommand command, StandardCredentials credential) {
    if (isSshUrl(repo) && credential instanceof BasicSSHUserPrivateKey) {
        command.setTransportConfigCallback(getSSHKeyTransport((BasicSSHUserPrivateKey)credential));
    } else  if (credential != null) {
        SmartCredentialsProvider credentialsProvider = new SmartCredentialsProvider(null);
        credentialsProvider.addDefaultCredentials(credential);
        command.setCredentialsProvider(credentialsProvider);
    }
}
 
Example #9
Source File: StudioNodeSyncGlobalRepoTask.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
private <T extends TransportCommand> T configureAuthenticationForCommand(ClusterMember remoteNode, T gitCommand,
                                                                         final Path tempKey)
        throws CryptoException, IOException, ServiceLayerException {
    TextEncryptor encryptor = new PbkAesTextEncryptor(studioConfiguration.getProperty(SECURITY_CIPHER_KEY),
            studioConfiguration.getProperty(SECURITY_CIPHER_SALT));
    boolean sshProtocol = !remoteNode.getGitUrl().matches(NON_SSH_GIT_URL_REGEX);

    switch (remoteNode.getGitAuthType()) {
        case RemoteRepository.AuthenticationType.NONE:
            logger.debug("No authentication");
            break;
        case RemoteRepository.AuthenticationType.BASIC:
            logger.debug("Basic Authentication");
            configureBasicAuthentication(remoteNode, gitCommand, encryptor, sshProtocol);
            break;
        case RemoteRepository.AuthenticationType.TOKEN:
            logger.debug("Token based Authentication");
            configureTokenAuthentication(remoteNode, gitCommand, encryptor, sshProtocol);
            break;
        case RemoteRepository.AuthenticationType.PRIVATE_KEY:
            if (!sshProtocol) {
                throw new ServiceLayerException("Can't do private key authentication with non-ssh URLs");
            }

            logger.debug("Private Key Authentication");
            configurePrivateKeyAuthentication(remoteNode, gitCommand, encryptor, tempKey);
            break;
        default:
            throw new ServiceLayerException("Unsupported authentication type " + remoteNode.getGitAuthType());
    }

    return gitCommand;
}
 
Example #10
Source File: StudioNodeSyncGlobalRepoTask.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
private <T extends TransportCommand> void configureTokenAuthentication(
        ClusterMember remoteNode, T gitCommand, TextEncryptor encryptor, boolean sshProtocol) throws CryptoException {
    UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(
            encryptor.decrypt(remoteNode.getGitToken()), StringUtils.EMPTY);

    if (sshProtocol) {
        gitCommand.setTransportConfigCallback(transport -> {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(new StrictHostCheckingOffSshSessionFactory());
        });
    }

    gitCommand.setCredentialsProvider(credentialsProvider);
}
 
Example #11
Source File: JGitUtil.java    From milkman with MIT License 5 votes vote down vote up
public static<R extends TransportCommand<C, T>, C extends GitCommand<T>, T> R initWith(R cmd, GitSyncDetails sync){
	
	if (sync.isSsh()) {
		cmd.setTransportConfigCallback(new SshTransportConfigCallback(sync.getUsername(), sync.getPasswordOrToken()));
		if (StringUtils.isNotBlank(sync.getPasswordOrToken())) {
			cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(sync.getUsername(), sync.getPasswordOrToken()));
		}

	} else {
		cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(sync.getUsername(), sync.getPasswordOrToken()));
	}
	
	return cmd;
}
 
Example #12
Source File: AbstractGitTask.java    From ant-git-tasks with Apache License 2.0 5 votes vote down vote up
/**
 * Setups the Git credentials if specified and needed
 *
 * @param command The git command to configure
 */
@SuppressWarnings("rawtypes")
protected void setupCredentials(GitCommand<?> command) {
        GitSettings settings = lookupSettings();

        if (settings != null && command instanceof TransportCommand) {
                TransportCommand cmd = (TransportCommand) command;
                cmd.setCredentialsProvider(settings.getCredentials());
        }
}
 
Example #13
Source File: GitRepositoryHelper.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
public <T extends TransportCommand> T setAuthenticationForCommand(T gitCommand, String authenticationType,
                                                                  String username, String password, String token,
                                                                  String privateKey, Path tempKey, boolean decrypt)
        throws CryptoException, ServiceLayerException {
    String passwordValue = password;
    String tokenValue = token;
    String privateKeyValue = privateKey;
    if (decrypt) {
        if (!StringUtils.isEmpty(password)) {
            passwordValue = encryptor.decrypt(password);
        }
        if (!StringUtils.isEmpty(token)) {
            tokenValue = encryptor.decrypt(token);
        }
        if (!StringUtils.isEmpty(privateKey)) {
            privateKeyValue = encryptor.decrypt(privateKey);
        }
    }
    final String pk = privateKeyValue;
    switch (authenticationType) {
        case RemoteRepository.AuthenticationType.NONE:
            logger.debug("No authentication");
            break;
        case RemoteRepository.AuthenticationType.BASIC:
            logger.debug("Basic authentication");
            UsernamePasswordCredentialsProvider credentialsProviderUP =
                    new UsernamePasswordCredentialsProvider(username, passwordValue);
            gitCommand.setCredentialsProvider(credentialsProviderUP);
            break;
        case RemoteRepository.AuthenticationType.TOKEN:
            logger.debug("Token based authentication");
            UsernamePasswordCredentialsProvider credentialsProvider =
                    new UsernamePasswordCredentialsProvider(tokenValue, StringUtils.EMPTY);
            gitCommand.setCredentialsProvider(credentialsProvider);
            break;
        case RemoteRepository.AuthenticationType.PRIVATE_KEY:
            logger.debug("Private key authentication");
            tempKey.toFile().deleteOnExit();
            gitCommand.setTransportConfigCallback(new TransportConfigCallback() {
                @Override
                public void configure(Transport transport) {
                    SshTransport sshTransport = (SshTransport)transport;
                    sshTransport.setSshSessionFactory(getSshSessionFactory(pk, tempKey));
                }
            });

            break;
        default:
            throw new ServiceLayerException("Unsupported authentication type " + authenticationType);
    }

    return gitCommand;
}
 
Example #14
Source File: StudioNodeSyncBaseTask.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
protected <T extends TransportCommand> T configureAuthenticationForCommand(ClusterMember remoteNode, T gitCommand,
                                                                           final Path tempKey)
        throws CryptoException, IOException, ServiceLayerException {
    TextEncryptor encryptor = new PbkAesTextEncryptor(studioConfiguration.getProperty(SECURITY_CIPHER_KEY),
                                                      studioConfiguration.getProperty(SECURITY_CIPHER_SALT));
    boolean sshProtocol = !remoteNode.getGitUrl().matches(NON_SSH_GIT_URL_REGEX);

    switch (remoteNode.getGitAuthType()) {
        case RemoteRepository.AuthenticationType.NONE:
            logger.debug("No authentication");
            break;
        case RemoteRepository.AuthenticationType.BASIC:
            logger.debug("Basic Authentication");
            if (StringUtils.isEmpty(remoteNode.getGitUsername()) ||
                    StringUtils.isEmpty(remoteNode.getGitPassword())) {
                throw new ServiceLayerException("Username or password empty for basic authentication for cluster " +
                        "node " + remoteNode.getLocalAddress());
            }
            configureBasicAuthentication(remoteNode, gitCommand, encryptor, sshProtocol);
            break;
        case RemoteRepository.AuthenticationType.TOKEN:
            logger.debug("Token based Authentication");
            if (StringUtils.isEmpty(remoteNode.getGitToken())) {
                throw new ServiceLayerException("Token is empty for token based authentication for cluster " +
                        "node " + remoteNode.getLocalAddress());
            }
            configureTokenAuthentication(remoteNode, gitCommand, encryptor, sshProtocol);
            break;
        case RemoteRepository.AuthenticationType.PRIVATE_KEY:
            if (!sshProtocol) {
                throw new ServiceLayerException("Can't do private key authentication with non-ssh URLs");
            }

            logger.debug("Private Key Authentication");
            if (StringUtils.isEmpty(remoteNode.getGitPrivateKey())) {
                throw new ServiceLayerException("Private key is empty for key based authentication for cluster " +
                        "node " + remoteNode.getLocalAddress());
            }
            configurePrivateKeyAuthentication(remoteNode, gitCommand, encryptor, tempKey);
            break;
        default:
            throw new ServiceLayerException("Unsupported authentication type " + remoteNode.getGitAuthType());
    }

    return gitCommand;
}
 
Example #15
Source File: GitHubConnector.java    From tool.accelerate.core with Apache License 2.0 4 votes vote down vote up
private void addAuth(TransportCommand command) {
    command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(oAuthToken, ""));
}
 
Example #16
Source File: GitWithAuth.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
private static <T extends TransportCommand<?, ?>> void configureHttp(T cmd, PasswordMirrorCredential cred) {
    cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(cred.username(), cred.password()));
}
 
Example #17
Source File: GitRepository.java    From CardinalPGM with MIT License 4 votes vote down vote up
private TransportCommand addCredentials(TransportCommand command) {
    if (credentials != null) command.setCredentialsProvider(credentials);
    return command;
}