Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Ssh prompting unexpectedly depending on .ssh/config
Ssh prompting unexpectedly depending on .ssh/config [message #1780818] Sun, 28 January 2018 19:13 Go to next message
Dave Musicant is currently offline Dave MusicantFriend
Messages: 15
Registered: May 2016
Junior Member
I've written a program below that uses an SshSessionFactory to establish an authenticated connection with a remote Git repository, and then makes two calls to an lsRemoteRepositoryCommand. It works as expected; in particular, it only prompts me once for my ssh private key password, presumably because that password is pbeing remembered somewhere in the session. That's good. However, when I go to the entry in my .ssh/config file for this host, and add an IdentityFile line in the config file that points to the same private key that was used before I add it (i.e., the default one), then the below program prompts me twice for my password. I'd like to avoid this; in my bigger application that I'm writing, this is materializing as a bug where the user keeps getting prompted over and over for an ssh private key password only if the private key file is identified in the .ssh/config.

My program is below, as well as the output without an IdentityFile entry in my .ssh/config, and again with. Can anyone help me understand why I'm not getting prompted for the password repeatedly on the same SshSessionFactory when I don't have an IdentityFile. but I do get prompted repeatedly when I do have such an entry, despite the entry pointing it to the default private key it's using otherwise anyway?

package sshpractice;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.TransportCommand;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.TransportGitSsh;
import org.eclipse.jgit.util.FS;

import java.util.Scanner;

public class AuthenticationTests {

    private static SshSessionFactory setupSshSessionFactory() {
        return new JschConfigSessionFactory() {
            private String passphrase;
            private String password;
            @Override
            protected void configure(OpenSshConfig.Host host, Session session) {
                session.setUserInfo(new UserInfo() {
                    @Override
                    public String getPassphrase() {
                        return passphrase;
                    }

                    @Override
                    public String getPassword() {
                        return password;
                    }

                    @Override
                    public boolean promptPassword(String message) {
                        System.out.println(message);
                        System.out.print("Enter password: ");
                        Scanner scanner = new Scanner(System.in);
                        password = scanner.next();
                        return true;
                    }

                    @Override
                    public boolean promptPassphrase(String message) {
                        System.out.println(message);
                        System.out.print("Enter passphrase: ");
                        Scanner scanner = new Scanner(System.in);
                        passphrase = scanner.next();
                        return true;
                    }

                    @Override
                    public boolean promptYesNo(String message) {
                        return false;
                    }

                    @Override
                    public void showMessage(String message) {
                        System.out.println(message);
                    }
                });
            }};
    }

    public static void main(String[] args) throws Exception {

        TransportCommand command = Git.lsRemoteRepository()
            .setRemote("ssh://.... the host and repo I'm connecting to....");

        SshSessionFactory sshSessionFactory= setupSshSessionFactory();

        command.setTransportConfigCallback(
                transport -> {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                });

        System.out.println(command.call());
        System.out.println(command.call());

    }
}


When my .ssh/config does not have an IdentityFile entry for this host, the output of this program (where I've masked my password entry) looks like this:

Passphrase for /home/myusername/.ssh/id_rsa
Enter passphrase: ***************
[Ref[HEAD=a4abfb07e20091b630fd2b825da36005b6376243], Ref[refs/heads/master=a4abfb07e20091b630fd2b825da36005b6376243]]
[Ref[HEAD=a4abfb07e20091b630fd2b825da36005b6376243], Ref[refs/heads/master=a4abfb07e20091b630fd2b825da36005b6376243]]


But when I do have a line in my .ssh/config, associated with the same host, that looks like

 IdentityFile ~/.ssh/id_rsa


Then the output of the program looks like:

Passphrase for /home/myusername/.ssh/id_rsa
Enter passphrase: ***************
[Ref[HEAD=a4abfb07e20091b630fd2b825da36005b6376243], Ref[refs/heads/master=a4abfb07e20091b630fd2b825da36005b6376243]]
Passphrase for /home/myusername/.ssh/id_rsa
Enter passphrase: ***************
[Ref[HEAD=a4abfb07e20091b630fd2b825da36005b6376243], Ref[refs/heads/master=a4abfb07e20091b630fd2b825da36005b6376243]]
Re: Ssh prompting unexpectedly depending on .ssh/config [message #1780884 is a reply to message #1780818] Mon, 29 January 2018 14:50 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 80
Registered: August 2016
Member
That's bug 529173.

Use a newer JGit, for instance the one from the Egit nightly update site.

I don't know when a JGit 4.10.1 including the fix for this bug will be released.
Re: Ssh prompting unexpectedly depending on .ssh/config [message #1780885 is a reply to message #1780884] Mon, 29 January 2018 15:04 Go to previous message
Dave Musicant is currently offline Dave MusicantFriend
Messages: 15
Registered: May 2016
Junior Member
Thomas: that's awesome news. I'm glad to see that's been fixed; I'll upgrade. Thanks for the quick response.
Previous Topic:git-upload-pack not permitted when cloning
Next Topic:Git staging view falsely shows modified file
Goto Forum:
  


Current Time: Fri Sep 21 13:56:28 GMT 2018

Powered by FUDForum. Page generated in 0.01475 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top