threads / patch / 53292

patchssh: add 'ssh.keyfile' option

Subject: [PATCH] ssh: add 'ssh.keyfile' option

## tl;dr

9 messages between Apr 23, 2020 and Apr 25, 2020. Diffs are folded; open one to read it.

replies: 8people: 4as markdown or json

Raymond E. Pasco· Apr 23, 2020, 06:41 UTC · lore

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(-)
Show changes to connect.c +28 −4
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
Raymond E. Pasco· Apr 23, 2020, 11:21 UTC · re: Raymond E. Pasco · lore

[PATCH v2] ssh: add 'ssh.keyfile' option

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(-)
Show changes to connect.c +29 −4
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
Johannes Sixt· Apr 23, 2020, 17:24 UTC · re: Raymond E. Pasco · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

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
Junio C Hamano· Apr 23, 2020, 19:32 UTC · re: Johannes Sixt · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

Johannes Sixt <j6t@kdbg.org> writes:
Show 19 quoted lines
> 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.

Raymond E. Pasco· Apr 24, 2020, 03:24 UTC · re: Johannes Sixt · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

On Thu Apr 23, 2020 at 7:24 PM, Johannes Sixt wrote:
Show 7 quoted lines
> 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
Matthias Aßhauer· Apr 23, 2020, 18:03 UTC · re: Raymond E. Pasco · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

>+		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
Junio C Hamano· Apr 23, 2020, 19:38 UTC · re: Matthias Aßhauer · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

Matthias Aßhauer <mha1993@live.de> writes:
Show 7 quoted lines
>>+		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(-)
Show changes to connect.c +1 −1
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:
Matthias Aßhauer· Apr 24, 2020, 04:33 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] ssh: add 'ssh.keyfile' option

Am 23.04.2020 um 21:38 schrieb Junio C Hamano:
Show 25 quoted lines
> 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.

Matthias Aßhauer· Apr 25, 2020, 09:43 UTC · re: Matthias Aßhauer · lore

[PATCH] connect.c: clarify BUG() messages in push_ssh_options

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(-)
Show changes to connect.c +3 −3
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

← back to recent threads