Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the configuration for jschconfigsessionfactory for jgit so that pull and push work?

Tags:

java

jsch

jgit

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.

like image 608
bmillare Avatar asked Sep 19 '12 20:09

bmillare


2 Answers

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);
like image 135
Stephan Windmüller Avatar answered Oct 26 '22 05:10

Stephan Windmüller


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

like image 24
bmillare Avatar answered Oct 26 '22 05:10

bmillare