ssh: add 'ssh.keyfile' option
PATCH 1/1 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.
---
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(-)
connect.c +28 -4
@@ -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); }
Signature · 1 line
2.26.1
PATCH v2 1/1 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.
---
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(-)
connect.c +29 -4
@@ -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); }
Signature · 1 line
2.26.1
Am 23.04.20 um 13:21 schrieb Raymond E. Pasco:
Quoted 4 lines
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 [email protected]: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
Quoted 11 lines · Johannes Sixt
Am 23.04.20 um 13:21 schrieb Raymond E. Pasco:
Quoted 4 lines
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:
Quoted 8 lines
git remote set-url origin [email protected]: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.
Quoted 7 lines · Johannes Sixt
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
connect.c static void push_ssh_options(struct argv_array *args, struct argv_array *env, · line 1151
Quoted 3 lines
connect.c +3
-0+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/[email protected]/T/#u
Best regards
Matthias Aßhauer
Quoted 7 lines · Matthias Aßhauer
Review
Quoted 3 lines
(quoted diff) +3
-0+ 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 wheneverVARIANT_AUTOgets passed topush_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(-)
connect.c +1 -1
@@ -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 :
Am 23.04.2020 um 21:38 schrieb Junio C Hamano:
Quoted 17 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_IPV4bit 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(-)connect.c +0
-0
connect.c static void push_ssh_options(struct argv_array *args, struct argv_array *env, · line 1124
Quoted 8 lines
connect.c +1
-1@@ -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.
PATCH 1/1 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.
--- connect.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
connect.c +3 -3
@@ -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 :
Signature · 1 line
2.17.1