Java源码示例:org.eclipse.jgit.api.TransportCommand

示例1
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;
}
 
示例2
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);
}
 
示例3
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;
            }

        });
    });
}
 
示例4
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);
}
 
示例5
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;
            }

        });
    });
}
 
示例6
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;
}
 
示例7
/**
 * 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);
    }
}
 
示例8
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);
	}
}
 
示例9
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);
}
 
示例10
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;
}
 
示例11
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);
}
 
示例12
/**
 * 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());
        }
}
 
示例13
private static <T extends TransportCommand<?, ?>> void configureHttp(T cmd, PasswordMirrorCredential cred) {
    cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(cred.username(), cred.password()));
}
 
示例14
private void addAuth(TransportCommand command) {
    command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(oAuthToken, ""));
}
 
示例15
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;
}
 
示例16
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;
}
 
示例17
private TransportCommand addCredentials(TransportCommand command) {
    if (credentials != null) command.setCredentialsProvider(credentials);
    return command;
}