USERAUTH fail using JGit to access git repo securely for PullCommand()

前端 未结 1 975
栀梦
栀梦 2020-12-20 10:35

I am trying to do a git pull using JGit\'s API with the following code

public class gitHubTest {
    JSch jsch = new JSch();

    // Defining the private key         


        
相关标签:
1条回答
  • 2020-12-20 11:14

    I could figure out some of the issues I was facing. Here's solution to it:

    1. if auth has to be done without the passphrase, generate the id_rsa without passphrase

    2. We needed to override getJSch(final OpenSshConfig.Host hc, FS fs) making use of addIdentity in the configure of SshSessionfactory:

      SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
          @Override
          protected void configure(OpenSshConfig.Host host, Session sess ion) {
              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("/path/to/private/key");
              return jsch;
          }
      };
      
    3. We need to call needs to be instantiated differently:

      PullCommand pull = git.pull().setTransportConfigCallback(new TransportConfigCallback() {
      
          @Override
          public void configure(Transport transport) {
              SshTransport sshTransport = (SshTransport) transport;
              sshTransport.setSshSessionFactory(sshSessionFactory);
          }
      });
      

    And then call pull instance:

    PullResult pullResult = pull.call();
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题