All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestra.plugin.git.services.SshTransportConfigCallback Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
package io.kestra.plugin.git.services;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.AllArgsConstructor;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.ssh.jsch.JschConfigSessionFactory;
import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig;
import org.eclipse.jgit.util.FS;

import java.io.File;
import java.nio.charset.StandardCharsets;

@AllArgsConstructor
public class SshTransportConfigCallback implements TransportConfigCallback {
    private byte[] privateKey;
    private String passphrase;

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host hc, 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();

                if (passphrase != null) {
                    jsch.addIdentity(
                        "privateKey",
                        privateKey,
                        null,
                        passphrase.getBytes(StandardCharsets.UTF_8)
                    );
                } else {
                    jsch.addIdentity("privateKey", privateKey, null, null);
                }

                return jsch;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy