All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Breck Yunits <breck7@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: Specifying a private key when connecting to a remote SSH repo
Date: Wed, 11 Sep 2013 21:39:55 -0700	[thread overview]
Message-ID: <xmqqeh8ur6uc.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <CAOgHByvTtNDho50B+pECMeXkq=3FF8EheFbP4iQbKYTiAPTwYw@mail.gmail.com> (Breck Yunits's message of "Wed, 11 Sep 2013 16:19:00 -0700")

Breck Yunits <breck7@gmail.com> writes:

> It would be very helpful if you could specify the path to the private
> key to use for ssh remotes just like in ssh.

You could add a support for the "remote.<name>.sshIdentityFile"
configuration variable, i.e.

	[remote "origin"]
		url = breck@example.com:projects/w.git
                sshIdentityFile = ~/.ssh/id_for_example_com

and then teach the codepath that leads to connect.c::git_connect()
to pass it down, and add "-o 'IdentityFile %s'" to the command line
that invokes "ssh".

But you would need the same number of configuration as you have
remotes that need such custom identity files, which happens to be
the same number of entries you would normally configure with the
standard ~/.ssh/config file, so I do not think it is worth it.

If the only thing you are interested in supporting is a one-shot
invocation, i.e. giving which identity file to use from the command
line when you run either "git push" or "git fetch", I suspect that
you could play with GIT_SSH environment variable, e.g.

    GIT_SSH_IDENTITY_FILE=$HOME/.ssh/id_for_example_com git push

and do something ugly like the attached, I suppose.

It also crossed my mind that you could (ab)use the credential helper
framework and ask it to return not the password but the identity
filename, and pass it down the callchain to git_connect(), but again
you will have to teach the credential helper as many settings as you
would need to make in ~/.ssh/config anyway, so I find it dubious how
it would be a win.

 connect.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/connect.c b/connect.c
index a80ebd3..87b7ceb 100644
--- a/connect.c
+++ b/connect.c
@@ -623,9 +623,10 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 		die("command line too long");
 
 	conn->in = conn->out = -1;
-	conn->argv = arg = xcalloc(7, sizeof(*arg));
+	conn->argv = arg = xcalloc(20, sizeof(*arg));
 	if (protocol == PROTO_SSH) {
 		const char *ssh = getenv("GIT_SSH");
+		const char *ssh_ident = getenv("GIT_SSH_IDENTITY_FILE");
 		int putty = ssh && strcasestr(ssh, "plink");
 		if (!ssh) ssh = "ssh";
 
@@ -637,6 +638,12 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 			*arg++ = putty ? "-P" : "-p";
 			*arg++ = port;
 		}
+		if (ssh_ident) {
+			char *ident_arg = xmalloc(strlen(ssh_ident) + 50);
+			sprintf(ident_arg, "IdentityFile %s", ssh_ident);
+			*arg++ = "-o";
+			*arg++ = ident_arg;
+		}
 		*arg++ = host;
 	}
 	else {

  reply	other threads:[~2013-09-12  4:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-11 23:19 Specifying a private key when connecting to a remote SSH repo Breck Yunits
2013-09-12  4:39 ` Junio C Hamano [this message]
2013-09-12  5:19   ` Jeff King
2013-09-12 15:43     ` Junio C Hamano
2013-09-12 17:48       ` Breck Yunits

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqeh8ur6uc.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=breck7@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.