4

I am using gitlab of version GitLab Community Edition 9.1.1 d3123f6.

I am able to push my code to gitlab with http protocol but when I am trying with git protocol it is showing following error.

Exception in thread "main" 
org.eclipse.jgit.errors.UnsupportedCredentialItem: 
ssh://git@192......: 
org.eclipse.jgit.transport.CredentialItem$YesNoType:The authenticity 
of host '192......' can't be established.

RSA key fingerprint is 
99:...............
Are you sure you want to continue connecting?
at org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:119)
at org.eclipse.jgit.transport.CredentialsProviderUserInfo.promptYesNo(CredentialsProviderUserInfo.java:124)
at com.jcraft.jsch.Session.checkHost(Session.java:785)
at com.jcraft.jsch.Session.connect(Session.java:342)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:117)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:264)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:162)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1201)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:203)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:136)
at org.dstadler.jgit.unfinished.PushToRemoteRepository.main(PushToRemoteRepository.java:100)

and my code is

private static final String REMOTE_URL = "git@192.....:ash/test.git";
File localPath = new File("/home/user/Git", "");
localPath.getParentFile().mkdirs();
localPath.createNewFile();
if(!localPath.delete()) {
    throw new IOException("Could not delete temporary file " + localPath);
}

Git git = Git.cloneRepository()
        .setURI( REMOTE_URL )
        .setDirectory(localPath)
        .setCredentialsProvider( cp )
        .call();
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);


System.out.println("please edit the file and then press \"Enter\" here to push ");
System.in.read();


File file = new File( git.getRepository().getWorkTree(), "file" + new Object().hashCode() );

git.add().addFilepattern( file.getName() ).call();

git.commit().setAll(true).setMessage("a message").call();

git.push() .setCredentialsProvider( cp ) .call();

System.out.println("Pushed from repository: " + localPath + " to remote repository at " + REMOTE_URL);
Nayan
  • 1,521
  • 2
  • 13
  • 27
Ash
  • 149
  • 1
  • 1
  • 13

1 Answers1

2

Try to establish a connection from command line first:

ssh git@...

That should ask you the same question: answer 'y'.

Once that is done, meaning once the ~/.ssh/known_hosts has been updated by the key scan of the remote server, you can try your same Java JGit program again.

I posted before (5 years ago) a solution, but for EGit, not JGit.

See more at "JGit Authentication Explained"

JGit provides an abstract JSchConfigSessionFactory that uses JSch to establish SSH connections and requires its configure() to be overridden

The OP Ash adds in the comments:

The problem was in .ssh/config file where needed lines are commented including private key identity.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried the same steps still it is showing same error, my code looks like this `private static final String REMOTE_URL = "git@192.16...1..1..:ashwath/test.git"; static SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure( Host host, Session session ) { // do nothing } };` – Ash Jun 04 '17 at 06:08
  • `public static void main(String[] args) throws IOException, GitAPIException { UsernamePasswordCredentialsProvider cp = new UsernamePasswordCredentialsProvider("ashwath", "abcd"); CloneCommand cloneCommand = Git.cloneRepository(); cloneCommand.setTransportConfigCallback(new TransportConfigCallback(){ @Override public void configure( Transport transport ) { SshTransport sshTransport = ( SshTransport )transport; sshTransport.setSshSessionFactory( sshSessionFactory ); } } );` – Ash Jun 04 '17 at 06:10
  • I am able to do the same in command prompt, yes it was asking to type yes or no first time..I wonder what can be the error? – Ash Jun 04 '17 at 06:16
  • But first, did you do the manual step `ssh git@...`? Do you see a `~/.ssh/known_hosts` updated? – VonC Jun 04 '17 at 06:18
  • yes when I run that command it is showing `/home/user/.ssh/known_hosts: line 1: syntax error near unexpected token `|' /home/user/.ssh/known_hosts: line 1: `|1|6GHFDDs4eE+Tj6t+8FBeYVQwaGo=|CHLmBzUxFKYIGpiUNv1KBYsIgNc= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIb........` – Ash Jun 04 '17 at 06:23
  • @Ash then rename that `/home/user/.ssh/known_hosts`, and try again the ssh command: it will recreate it. – VonC Jun 04 '17 at 06:25
  • I can do git clone git@192....but if I do ssh git@192.169.1.:ashwath./test.git It is showing "could not resolve host name 192.... name or service not known – Ash Jun 04 '17 at 06:26
  • 1
    @Ash Yes, the ssh command to do is only the first part: `ssh git@192...` (without `:xxx`): it is only to be done one time, to update the `known_hosts` file. After that, as long as your Java program is executed with the same user account, it should reuse the information set in `known_hosts`. – VonC Jun 04 '17 at 06:29
  • @Ash So , no more syntax error? Now, your java program should work, at at the very least, not trigger the same `The authenticity of host` error message. – VonC Jun 04 '17 at 06:40
  • @Ash If it still does, set that known_host file for JSch used by JGit: https://stackoverflow.com/a/15645473/6309 – VonC Jun 04 '17 at 06:42
  • I am getting error unknown host key..rsa finger print is 99:4f......the finger print which I copied to gitlab is 19:7c...... should both match? – Ash Jun 08 '17 at 04:50
  • @Ash they should match yes. – VonC Jun 08 '17 at 05:32
  • The problem was in ssh.config file where needed lines are commented including private key identity:) – Ash Jun 09 '17 at 17:14
  • 1
    @Ash OK. I have included your comment in the answer for more visibility. – VonC Jun 09 '17 at 17:17