9

I am trying to do a git pull/push using jgit's api with the following code

org.eclipse.jgit.api.Git.open(theRepoFile).pull().call()

but I am getting exceptions

JSchException Auth fail
com.jcraft.jsch.Session.connect (Session.java:461)
org.eclipse.jgit.transport.JschConfigSessionFactory.getSession (JschConfigSessionFactory.java:116)
org.eclipse.jgit.transport.SshTransport.getSession (SshTransport.java:121)
org.eclipse.jgit.transport.TransportGitSsh$SshPushConnection.<init> (TransportGitSsh.java:306)
org.eclipse.jgit.transport.TransportGitSsh.openPush (TransportGitSsh.java:152)
org.eclipse.jgit.transport.PushProcess.execute (PushProcess.java:130)
org.eclipse.jgit.transport.Transport.push (Transport.java:1127)
org.eclipse.jgit.api.PushCommand.call (PushCommand.java:153)

Even though using cgit pull and pushing works.

I tried checking SO for example code

Java git client using jgit

but the above question does not provide a complete coded example of what is necessary to do a git pull with a remote repo that is normally authenticated via ssh keys. There should be a way to get the credential information from ~/.ssh/ or the windows equivalent.

Community
  • 1
  • 1
bmillare
  • 4,183
  • 2
  • 23
  • 42

3 Answers3

12

Jsch will automatically detect your SSH keys but will fail if these are protected by a password. You need to specify the passphrase through a CredentialsProvider like this:

JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
    CredentialsProvider provider = new CredentialsProvider() {
        @Override
        public boolean isInteractive() {
            return false;
        }

        @Override
        public boolean supports(CredentialItem... items) {
            return true;
        }

        @Override
        public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
            for (CredentialItem item : items) {
                ((CredentialItem.StringType) item).setValue("yourpassphrase");
            }
            return true;
        }
    };
    UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
    session.setUserInfo(userInfo);
}
};
SshSessionFactory.setInstance(sessionFactory);
  • Thank you SO much for this - I was struggling with the SSH authentication for JGit, found this and it resolved my issue straight away. Many thanks. – Joe PP Sep 30 '15 at 16:38
  • @JoPintoPaul : I tried doing this using the similar snippet. Could please help . My question is : https://stackoverflow.com/questions/45889326/userauth-fail-using-jgit-to-access-git-repo-securely-for-pullcommand – mohanjot Aug 27 '17 at 17:44
3

The problem is Jsch does not support ssh-agents out of the box. One will need to configure https://github.com/ymnk/jsch-agent-proxy to get it to work.

An alternative is to make your own org.eclipse.jgit.transport.CredentialsProvider and set the org.eclipse.jgit.transport.CredentialItem to the correct values (by requesting them from the user or looking up a file). You can change the default CredentialsProvider with org.eclipse.jgit.transport.CredentialsProvider/setDefault

See my clojure library dj for details: https://github.com/bmillare/dj/blob/library/src/dj/git.clj

bmillare
  • 4,183
  • 2
  • 23
  • 42
  • so did you try that and it worked? Could you describe it in more details, what exactly is there to do? integrate the proxy in one's code? This will help the next developer having this problem. – Vince Sep 25 '12 at 09:19
  • I actually didn't use jsch-agent-proxy, I made my own CredentialsProvider that asked the user for the passphrase. It's actually written in clojure but the details are there. https://github.com/bmillare/dj/blob/library/src/dj/git.clj – bmillare Sep 25 '12 at 13:48
-1

I vaguely remember getting an error with JSch that blocked me for a while because the log was not very explicit. I can't tell for sure it's the same problem but I followed this page to solve my problem:

https://help.github.com/articles/generating-ssh-keys

(it was due to a wrong network configuration)

Vince
  • 1,570
  • 3
  • 27
  • 48
  • The link you posted has nothing jgit specific. I said that I am able to use normal git, which means the ssh keys are fine. One possibility is that I use gentoo keychain tools, which may something that jgit does no know how to deal with. – bmillare Sep 19 '12 at 21:51
  • if you don't want to listen to what people say, don't ask. "because the log was not very explicit" might not be clear enough: I lost days of work only looking in the same direction as you do, with Git working perfectly. I'm not sure what my answer is worth because it's a vague memory, but if you don't try, at least you're sure it will not work any better. And indeed it's not related to jGit buit if you find a page with written both jGit and ssh config, feel free to post it, the goal here is that the next reader finds useful information. – Vince Sep 20 '12 at 08:14
  • oh and overall, you'll learn it with time, but Git is way more tolerant with weird usages than jGit. – Vince Sep 20 '12 at 08:17
  • Please don't take it personal. I'm not attacking your efforts, but I am suggesting clarification. (Note the lack of a downvote.) Moving forward, your comments do clear things up. – bmillare Sep 21 '12 at 00:12