Re: [PATCH v6 3/9] ssh signing: retrieve a default key from ssh-agent
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jul 28, 2021, 21:29 UTC
- Message-ID
- <xmqqim0u86w2.fsf@gitster.g>
- In-Reply-To
- <071e6173d8e418349d94fea97624e8cee9f1dde5.1627501009.git.gitgitgadget@gmail.com>
"Fabian Stelzer via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 9 quoted lines
> From: Fabian Stelzer <fs@gigacodes.de> > > if user.signingkey is not set and a ssh signature is requested we call > ssh-add -L and use the first key we get > > Signed-off-by: Fabian Stelzer <fs@gigacodes.de> > --- > gpg-interface.c | 26 +++++++++++++++++++++++++- > 1 file changed, 25 insertions(+), 1 deletion(-)
I would have expected that this also would become a method call into *use_format object (instead of dispatching on use_format->name), but let's not go overboard. I think this is good enough for now.
Show 41 quoted lines
> diff --git a/gpg-interface.c b/gpg-interface.c
> index c131977b347..3afacb48900 100644
> --- a/gpg-interface.c
> +++ b/gpg-interface.c
> @@ -470,11 +470,35 @@ int git_gpg_config(const char *var, const char *value, void *cb)
> return 0;
> }
>
> +/* Returns the first public key from an ssh-agent to use for signing */
> +static char *get_default_ssh_signing_key(void)
> +{
> + struct child_process ssh_add = CHILD_PROCESS_INIT;
> + int ret = -1;
> + struct strbuf key_stdout = STRBUF_INIT;
> + struct strbuf **keys;
> +
> + strvec_pushl(&ssh_add.args, "ssh-add", "-L", NULL);
> + ret = pipe_command(&ssh_add, NULL, 0, &key_stdout, 0, NULL, 0);
> + if (!ret) {
> + keys = strbuf_split_max(&key_stdout, '\n', 2);
> + if (keys[0])
> + return strbuf_detach(keys[0], NULL);
> + }
> +
> + strbuf_release(&key_stdout);
> + return "";
> +}
> +
> const char *get_signing_key(void)
> {
> if (configured_signing_key)
> return configured_signing_key;
> - return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
> + if (!strcmp(use_format->name, "ssh")) {
> + return get_default_ssh_signing_key();
> + } else {
> + return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
> + }
> }
>
> int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)