git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ssh: add 'ssh.keyfile' option
@ 2020-04-23  6:41 Raymond E. Pasco
  2020-04-23 11:21 ` [PATCH v2] " Raymond E. Pasco
  0 siblings, 1 reply; 9+ messages in thread
From: Raymond E. Pasco @ 2020-04-23  6:41 UTC (permalink / raw)
  To: git; +Cc: Raymond E. Pasco

When a specific private key needs to be used with a repository, manually
specifying it via 'core.sshCommand' is not ideal. This option allows a
keyfile to be specified in the local configuration. If a keyfile is
specified, SSH agents are disabled for the command.

Signed-off-by: Raymond E. Pasco <ray@ameretat.dev>
---
I've encountered the need to specify a specific SSH key to be used with
a repository, and overriding the whole ssh command isn't great. I have
only tested this with OpenSSH.

 connect.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/connect.c b/connect.c
index 23013c6344..dc7c75ead3 100644
--- a/connect.c
+++ b/connect.c
@@ -1104,8 +1104,9 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
  * `args` for running ssh in Git's SSH-tunneled transport.
  */
 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
-			     enum ssh_variant variant, const char *port,
-			     enum protocol_version version, int flags)
+			     enum ssh_variant variant, const char *keyfile,
+			     const char *port, enum protocol_version version,
+			     int flags)
 {
 	if (variant == VARIANT_SSH &&
 	    version > 0) {
@@ -1144,6 +1145,26 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
 	if (variant == VARIANT_TORTOISEPLINK)
 		argv_array_push(args, "-batch");
 
+	if (keyfile) {
+		switch (variant) {
+		case VARIANT_AUTO:
+			BUG("VARIANT_AUTO passed to push_ssh_options");
+		case VARIANT_SIMPLE:
+			die(_("ssh variant 'simple' does not support setting keyfiles"));
+		case VARIANT_SSH:
+			argv_array_push(args, "-a");
+			argv_array_push(args, "-i");
+			argv_array_push(args, keyfile);
+			break;
+		case VARIANT_PLINK:
+		case VARIANT_PUTTY:
+		case VARIANT_TORTOISEPLINK:
+			argv_array_push(args, "-noagent");
+			argv_array_push(args, "-i");
+			argv_array_push(args, keyfile);
+		}
+	}
+
 	if (port) {
 		switch (variant) {
 		case VARIANT_AUTO:
@@ -1169,6 +1190,7 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
 			  int flags)
 {
 	const char *ssh;
+	const char *keyfile;
 	enum ssh_variant variant;
 
 	if (looks_like_command_line_option(ssh_host))
@@ -1200,14 +1222,16 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
 		argv_array_push(&detect.args, ssh);
 		argv_array_push(&detect.args, "-G");
 		push_ssh_options(&detect.args, &detect.env_array,
-				 VARIANT_SSH, port, version, flags);
+				 VARIANT_SSH, keyfile, port, version, flags);
 		argv_array_push(&detect.args, ssh_host);
 
 		variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
 	}
 
+	git_config_get_string_const("ssh.keyfile", &keyfile);
+
 	argv_array_push(&conn->args, ssh);
-	push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
+	push_ssh_options(&conn->args, &conn->env_array, variant, keyfile, port, version, flags);
 	argv_array_push(&conn->args, ssh_host);
 }
 
-- 
2.26.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23  6:41 [PATCH] ssh: add 'ssh.keyfile' option Raymond E. Pasco
@ 2020-04-23 11:21 ` Raymond E. Pasco
  2020-04-23 17:24   ` Johannes Sixt
  2020-04-23 18:03   ` Matthias Aßhauer
  0 siblings, 2 replies; 9+ messages in thread
From: Raymond E. Pasco @ 2020-04-23 11:21 UTC (permalink / raw)
  To: git; +Cc: Raymond E. Pasco

When a specific private key needs to be used with a repository, manually
specifying it via 'core.sshCommand' is not ideal. This option allows a
keyfile to be specified in the local configuration. If a keyfile is
specified, SSH agents are disabled for the command.

Signed-off-by: Raymond E. Pasco <ray@ameretat.dev>
---
Retract the previous submission - these are the correct options to pass
to bypass the ssh agent. It had worked for me before, but I had killed
the agent earlier and forgotten about it...

 connect.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/connect.c b/connect.c
index 23013c6344..7759248194 100644
--- a/connect.c
+++ b/connect.c
@@ -1104,8 +1104,9 @@ static struct child_process *git_connect_git(int fd[2], char *hostandport,
  * `args` for running ssh in Git's SSH-tunneled transport.
  */
 static void push_ssh_options(struct argv_array *args, struct argv_array *env,
-			     enum ssh_variant variant, const char *port,
-			     enum protocol_version version, int flags)
+			     enum ssh_variant variant, const char *keyfile,
+			     const char *port, enum protocol_version version,
+			     int flags)
 {
 	if (variant == VARIANT_SSH &&
 	    version > 0) {
@@ -1144,6 +1145,27 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
 	if (variant == VARIANT_TORTOISEPLINK)
 		argv_array_push(args, "-batch");
 
+	if (keyfile) {
+		switch (variant) {
+		case VARIANT_AUTO:
+			BUG("VARIANT_AUTO passed to push_ssh_options");
+		case VARIANT_SIMPLE:
+			die(_("ssh variant 'simple' does not support setting keyfiles"));
+		case VARIANT_SSH:
+			argv_array_push(args, "-o");
+			argv_array_push(args, "IdentitiesOnly=yes");
+			argv_array_push(args, "-i");
+			argv_array_push(args, keyfile);
+			break;
+		case VARIANT_PLINK:
+		case VARIANT_PUTTY:
+		case VARIANT_TORTOISEPLINK:
+			argv_array_push(args, "-noagent");
+			argv_array_push(args, "-i");
+			argv_array_push(args, keyfile);
+		}
+	}
+
 	if (port) {
 		switch (variant) {
 		case VARIANT_AUTO:
@@ -1169,6 +1191,7 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
 			  int flags)
 {
 	const char *ssh;
+	const char *keyfile;
 	enum ssh_variant variant;
 
 	if (looks_like_command_line_option(ssh_host))
@@ -1200,14 +1223,16 @@ static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
 		argv_array_push(&detect.args, ssh);
 		argv_array_push(&detect.args, "-G");
 		push_ssh_options(&detect.args, &detect.env_array,
-				 VARIANT_SSH, port, version, flags);
+				 VARIANT_SSH, keyfile, port, version, flags);
 		argv_array_push(&detect.args, ssh_host);
 
 		variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
 	}
 
+	git_config_get_string_const("ssh.keyfile", &keyfile);
+
 	argv_array_push(&conn->args, ssh);
-	push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
+	push_ssh_options(&conn->args, &conn->env_array, variant, keyfile, port, version, flags);
 	argv_array_push(&conn->args, ssh_host);
 }
 
-- 
2.26.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 11:21 ` [PATCH v2] " Raymond E. Pasco
@ 2020-04-23 17:24   ` Johannes Sixt
  2020-04-23 19:32     ` Junio C Hamano
  2020-04-24  3:24     ` Raymond E. Pasco
  2020-04-23 18:03   ` Matthias Aßhauer
  1 sibling, 2 replies; 9+ messages in thread
From: Johannes Sixt @ 2020-04-23 17:24 UTC (permalink / raw)
  To: Raymond E. Pasco; +Cc: git

Am 23.04.20 um 13:21 schrieb Raymond E. Pasco:
> When a specific private key needs to be used with a repository, manually
> specifying it via 'core.sshCommand' is not ideal. This option allows a
> keyfile to be specified in the local configuration. If a keyfile is
> specified, SSH agents are disabled for the command.

You can do this without modifying Git. Say, your key file is
~/.ssh/id_other_ed25519, then do this:

Rename your remote to use an invented host name:

  git remote set-url origin git@other.github.com:other/repo

Then attach the invented name to the real host name and the identity in
your ~/.ssh/config:

  Host other.github.com
    Hostname github.com
    Identity ~/.ssh/id_other_ed25519

-- Hannes

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 11:21 ` [PATCH v2] " Raymond E. Pasco
  2020-04-23 17:24   ` Johannes Sixt
@ 2020-04-23 18:03   ` Matthias Aßhauer
  2020-04-23 19:38     ` Junio C Hamano
  1 sibling, 1 reply; 9+ messages in thread
From: Matthias Aßhauer @ 2020-04-23 18:03 UTC (permalink / raw)
  To: Raymond E. Pasco, git; +Cc: Jonathan Nieder, Stefan Beller, Junio C Hamano

>+		switch (variant) {
>+		case VARIANT_AUTO:
>+			BUG("VARIANT_AUTO passed to push_ssh_options");

Why do we keep replicating this all over push_ssh_options? The wording
very much sounds like it's a bug whenever VARIANT_AUTO gets passed to
push_ssh_options, no exceptions. In that case we should probably just
check for it once at the beginning of push_ssh_options instead of after
almost every single if. If there are valid cases where VARIANT_AUTO gets
passed to push_ssh_options we should probably clarify the wording as to
what parameter combinations are and aren't valid with VARIANT_AUTO.

I've included the Sign-Offs and Acks of the commits that introduced the
previous copies of this.[1][2] The previous discussion[3] on the mailing
list does not seem to mention the duplication.

[1] a3f5b66f: "ssh: 'simple' variant does not support -4/-6" 2017-11-20
[2] 3fa5e0d0: "ssh: 'simple' variant does not support --port" 2017-11-20
[3] https://lore.kernel.org/git/20170913215448.84674-1-bmwill@google.com/T/#u

Best regards

Matthias Aßhauer


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 17:24   ` Johannes Sixt
@ 2020-04-23 19:32     ` Junio C Hamano
  2020-04-24  3:24     ` Raymond E. Pasco
  1 sibling, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2020-04-23 19:32 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Raymond E. Pasco, git

Johannes Sixt <j6t@kdbg.org> writes:

> Am 23.04.20 um 13:21 schrieb Raymond E. Pasco:
>> When a specific private key needs to be used with a repository, manually
>> specifying it via 'core.sshCommand' is not ideal. This option allows a
>> keyfile to be specified in the local configuration. If a keyfile is
>> specified, SSH agents are disabled for the command.
>
> You can do this without modifying Git. Say, your key file is
> ~/.ssh/id_other_ed25519, then do this:
>
> Rename your remote to use an invented host name:
>
>   git remote set-url origin git@other.github.com:other/repo
>
> Then attach the invented name to the real host name and the identity in
> your ~/.ssh/config:
>
>   Host other.github.com
>     Hostname github.com
>     Identity ~/.ssh/id_other_ed25519

Nice.  I wonder if this answer (and answers to other "how would I
use .ssh/config to adjust Git to suite my use?" questions people may
often ask) can be put in some of our documentation.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 18:03   ` Matthias Aßhauer
@ 2020-04-23 19:38     ` Junio C Hamano
  2020-04-24  4:33       ` Matthias Aßhauer
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2020-04-23 19:38 UTC (permalink / raw)
  To: Matthias Aßhauer
  Cc: Raymond E. Pasco, git, Jonathan Nieder, Stefan Beller

Matthias Aßhauer <mha1993@live.de> writes:

>>+		switch (variant) {
>>+		case VARIANT_AUTO:
>>+			BUG("VARIANT_AUTO passed to push_ssh_options");
>
> Why do we keep replicating this all over push_ssh_options? The wording
> very much sounds like it's a bug whenever VARIANT_AUTO gets passed to
> push_ssh_options, no exceptions. In that case ...

My reading of the code tells me that it is *not* the case.  When the
caller asks to connect only over IPv4 with CONNECT_IPV4 bit set in
the flags, we cannot translate that to an option to underlying SSH
implementation without knowing the variant.  As long as the caller
does not specify IPV4/IPV6 or custom port, I think it is OK for the
caller to leave the variant AUTO.

So you probably want a patch along the lines of...

 connect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/connect.c b/connect.c
index b6451ab5e8..b02cf74c9a 100644
--- a/connect.c
+++ b/connect.c
@@ -1118,7 +1118,7 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
 	if (flags & CONNECT_IPV4) {
 		switch (variant) {
 		case VARIANT_AUTO:
-			BUG("VARIANT_AUTO passed to push_ssh_options");
+			BUG("VARIANT_AUTO and CONNECT_IPV4 used together");
 		case VARIANT_SIMPLE:
 			die(_("ssh variant 'simple' does not support -4"));
 		case VARIANT_SSH:

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 17:24   ` Johannes Sixt
  2020-04-23 19:32     ` Junio C Hamano
@ 2020-04-24  3:24     ` Raymond E. Pasco
  1 sibling, 0 replies; 9+ messages in thread
From: Raymond E. Pasco @ 2020-04-24  3:24 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: git

On Thu Apr 23, 2020 at 7:24 PM, Johannes Sixt wrote:
> Then attach the invented name to the real host name and the identity in
> your ~/.ssh/config:
>
> 
> Host other.github.com
> Hostname github.com
> Identity ~/.ssh/id_other_ed25519

This works for my purposes. The exact block I needed:

Host fake.host
  Hostname real.host
  IdentityFile ~/.ssh/alt_ed25519
  IdentitiesOnly yes

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] ssh: add 'ssh.keyfile' option
  2020-04-23 19:38     ` Junio C Hamano
@ 2020-04-24  4:33       ` Matthias Aßhauer
  2020-04-25  9:43         ` [PATCH] connect.c: clarify BUG() messages in push_ssh_options Matthias Aßhauer
  0 siblings, 1 reply; 9+ messages in thread
From: Matthias Aßhauer @ 2020-04-24  4:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Raymond E. Pasco, git, Jonathan Nieder, Stefan Beller

Am 23.04.2020 um 21:38 schrieb Junio C Hamano:

> My reading of the code tells me that it is *not* the case.  When the
> caller asks to connect only over IPv4 with CONNECT_IPV4 bit set in
> the flags, we cannot translate that to an option to underlying SSH
> implementation without knowing the variant.  As long as the caller
> does not specify IPV4/IPV6 or custom port, I think it is OK for the
> caller to leave the variant AUTO.
>
> So you probably want a patch along the lines of...
>
>   connect.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/connect.c b/connect.c
> index b6451ab5e8..b02cf74c9a 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -1118,7 +1118,7 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
>   	if (flags & CONNECT_IPV4) {
>   		switch (variant) {
>   		case VARIANT_AUTO:
> -			BUG("VARIANT_AUTO passed to push_ssh_options");
> +			BUG("VARIANT_AUTO and CONNECT_IPV4 used together");
>   		case VARIANT_SIMPLE:
>   			die(_("ssh variant 'simple' does not support -4"));
>   		case VARIANT_SSH:

Yes, that looks about right. I'll extend the patch to also cover CONNECT_IPV6 and a custom port,
give it a run through the test suite and send it back with a commit message. Thanks for taking a look
at the legitimate cases where VARIANT_AUTO gets passed to push_ssh_options.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH] connect.c: clarify BUG() messages in push_ssh_options
  2020-04-24  4:33       ` Matthias Aßhauer
@ 2020-04-25  9:43         ` Matthias Aßhauer
  0 siblings, 0 replies; 9+ messages in thread
From: Matthias Aßhauer @ 2020-04-25  9:43 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Raymond E. Pasco, git, Jonathan Nieder, Stefan Beller

The current BUG() messages in push_ssh_options imply that calling push_ssh_options and passing VARIANT_AUTO is always a bug
and we should check for it once at the top of push_ssh_options instead of multiple times in various if statements. That is
not actually the case. When the caller passes CONNECT_IPV4, CONNECT_IPV6 or a custom port alongside VARIANT_AUTO we cannot
translate that to an option to the underlying SSH implementation without knowing the variant. As long as the caller does
not specify IPV4/IPV6 or a custom port, it is ok for the caller to leave the variant AUTO. Let's explicitly state that the
bug is in the combination of parameters.

Signed-off-by: Matthias Aßhauer <mha1993@live.de>
---
  connect.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/connect.c b/connect.c
index 23013c6344..c15e60b13a 100644
--- a/connect.c
+++ b/connect.c
@@ -1118,7 +1118,7 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
  	if (flags & CONNECT_IPV4) {
  		switch (variant) {
  		case VARIANT_AUTO:
-			BUG("VARIANT_AUTO passed to push_ssh_options");
+			BUG("VARIANT_AUTO and CONNECT_IPV4 passed to push_ssh_options");
  		case VARIANT_SIMPLE:
  			die(_("ssh variant 'simple' does not support -4"));
  		case VARIANT_SSH:
@@ -1130,7 +1130,7 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
  	} else if (flags & CONNECT_IPV6) {
  		switch (variant) {
  		case VARIANT_AUTO:
-			BUG("VARIANT_AUTO passed to push_ssh_options");
+			BUG("VARIANT_AUTO and CONNECT_IPV6 passed to push_ssh_options");
  		case VARIANT_SIMPLE:
  			die(_("ssh variant 'simple' does not support -6"));
  		case VARIANT_SSH:
@@ -1147,7 +1147,7 @@ static void push_ssh_options(struct argv_array *args, struct argv_array *env,
  	if (port) {
  		switch (variant) {
  		case VARIANT_AUTO:
-			BUG("VARIANT_AUTO passed to push_ssh_options");
+			BUG("VARIANT_AUTO and a custom port passed to push_ssh_options");
  		case VARIANT_SIMPLE:
  			die(_("ssh variant 'simple' does not support setting port"));
  		case VARIANT_SSH:
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-25  9:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-23  6:41 [PATCH] ssh: add 'ssh.keyfile' option Raymond E. Pasco
2020-04-23 11:21 ` [PATCH v2] " Raymond E. Pasco
2020-04-23 17:24   ` Johannes Sixt
2020-04-23 19:32     ` Junio C Hamano
2020-04-24  3:24     ` Raymond E. Pasco
2020-04-23 18:03   ` Matthias Aßhauer
2020-04-23 19:38     ` Junio C Hamano
2020-04-24  4:33       ` Matthias Aßhauer
2020-04-25  9:43         ` [PATCH] connect.c: clarify BUG() messages in push_ssh_options Matthias Aßhauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).