4

I am using JGit to connect to a remote Git repository. I tried below to perform same.

public class Main{
    public static void main(String args[]) throws Exception{
        JschConfigSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
              @Override
              protected void configure( Host host, Session session ) {
                session.setPassword( "XXXX" );
              }
            };

            String REMOTE_URL="ssh://git@url/test.git";
            CloneCommand cloneCommand = Git.cloneRepository();
            cloneCommand.setURI( "ssh://git@url/test.git" );
            cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
              @Override
              public void configure( Transport transport ) {
                SshTransport sshTransport = ( SshTransport )transport;
                sshTransport.setSshSessionFactory( sshSessionFactory );
              }
            } );

            System.out.println("Listing remote repository " + REMOTE_URL);
            Collection<Ref> refs = Git.lsRemoteRepository()
                    .setHeads(true)
                    .setTags(true)
                    .setRemote(REMOTE_URL)
                    .call();

            for (Ref ref : refs) {
                System.out.println("Ref: " + ref);
            }
    }
}

ERROR:

Listing remote repository ssh://git@url/test.git
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at org.eclipse.jgit.api.LsRemoteCommand.execute(LsRemoteCommand.java:223)
    at org.eclipse.jgit.api.LsRemoteCommand.call(LsRemoteCommand.java:159)
    at Jgit.Jgit.Main.main(Main.java:94)
Caused by: org.eclipse.jgit.errors.TransportException: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:159)
    at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:136)
    at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:262)
    at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:161)
    at org.eclipse.jgit.api.LsRemoteCommand.execute(LsRemoteCommand.java:202)
    ... 2 more
Caused by: com.jcraft.jsch.JSchException: UnknownHostKey: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at com.jcraft.jsch.Session.checkHost(Session.java:797)
    at com.jcraft.jsch.Session.connect(Session.java:342)
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116)
    ... 6 more

I looked over internet; and found that, below needs to be added:

com.jcraft.jsch.JSch.setConfig ( "StrictHostKeyChecking", "no" );

But, where to fit it in above code snippet. Is there any sample working code snippet available.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
shankar
  • 93
  • 2
  • 8
  • Did you try to change the host checking setting within your implementation of `JschConfigSessionFactory`, i.e. `session.setConfig( "StrictHostKeyChecking", "no" );`? – Rüdiger Herrmann Jan 12 '16 at 09:23
  • Hi Rudiger, i had put `session.setconfig ( "StrictHostKeyChecking", "no" );` the ssh key error is gone. I am getting "Auth Error" – shankar Jan 13 '16 at 14:02
  • Hi Rudiger, Please find the code / research below – shankar Jan 13 '16 at 14:11
  • You need of course provide suitable credentials for the remote repository. See here for more information: http://www.codeaffine.com/2014/12/09/jgit-authentication/ – Rüdiger Herrmann Jan 13 '16 at 15:43

1 Answers1

1

You can call setConfig("","") method using Session object as shown below:-

JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();

Hope that helps.

Sudheep
  • 51
  • 2