Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for additional files for ssh-add #18

Closed
wants to merge 2 commits into from

Conversation

jankatins
Copy link

According to the docs, ssh-add only adds certain keyfiles.

ssh-add adds private key identities to the authentication agent, ssh-agent(1).
When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa,
~/.ssh/id_ecdsa and ~/.ssh/identity.

Sometimes there is the need to add more keyfiles.

This is now possible by setting the SSH_ADD_ADDITIONAL_KEYFILES environment
variable to the comma separated list of additional keyfiles in ~/.ssh/

set "SSH_ADD_ADDITIONAL_KEYFILES=keyfile1_rsa,keyfile2_rsa2"
start-ssh-agent

Report: cmderdev/cmder#1062

According to the docs, ssh-add only adds certain keyfiles.

> ssh-add adds private key identities to the authentication agent, ssh-agent(1).
> When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa,
> ~/.ssh/id_ecdsa and ~/.ssh/identity.

Sometimes there is the need to add more keyfiles.

This is now possible by setting the SSH_ADD_ADDITIONAL_KEYFILES environment
variable to the comma separated list of additional keyfiles in ~/.ssh/

```bat
set "SSH_ADD_ADDITIONAL_KEYFILES=keyfile1_rsa,keyfile2_rsa2"
start-ssh-agent
```

Signed-off-by: Jan Schulz <[email protected]>
@dscho
Copy link
Member

dscho commented Aug 8, 2016

Are we really the first ones to solve this problem? If not, how do others solve it?

@jankatins
Copy link
Author

cmder had an agent script which simply added all *_rsa keys in one go (=no default keys added, e.g. no id_dsa or identity, but all *_rsa): https://github.com/cmderdev/cmder/blob/4651201b6c78ad1834b431a9a1c15b631ad1b45e/bin/agent.cmd#L26-L43 (so our switch to the git provided one prompted the issue as a user complained about the regression)

The help at github only talks about using ssh-add directly: https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agent

googling for "ssh-add more than one key" also wasn't so helpful: Best I found was things like http://stackoverflow.com/a/26483359/1380673 which use ssh-add directly

Googling for ssh-agent wrapper and ssh-add wrapper resulted in this:

There are also a lot of bash snippets, e.g. http://stackoverflow.com/a/18915067/1380673, https://gist.github.com/rezlam/850855, https://gist.github.com/gxela/5883418. The last is actually interesting as it also loads all *.pem keyfiles.

So that doesn't look like there is a "default" for the "more than one key" usecase. I guess most users simply use ssh-add with only one key. And if they have more than one key, use some variant of the various bash snippets floating around, which are then trivial to add support for more than one key...

I could actually live with load every id_* or load every *_rsa solution (=without a special env variable)... Or letting start-ssh-agent take keyfiles as arguments (which would make the patch trivial: simply add %* after the ssh-add call -> if it is empty, the default behaviour will happen and if you add keyfiles they will be passed to the ssh-add call) ...

@jankatins
Copy link
Author

jankatins commented Aug 8, 2016

Actually the last idea works great:

λ start-ssh-agent.cmd
Found ssh-agent at 7812
Found ssh-agent socket at /tmp/ssh-PeY5lmKPqlf1/agent.4748
Starting ssh-agent:  done
Enter passphrase for /c/Users/jschulz/.ssh/id_rsa:
Identity added: /c/Users/jschulz/.ssh/id_rsa (/c/Users/jschulz/.ssh/id_rsa)

# here I'm killing the ssh-agent process ...

λ start-ssh-agent.cmd /c/Users/jschulz/.ssh/id_rsa c:\Users\jschulz\.ssh\id_rsa3
Found ssh-agent at 7812
Found ssh-agent socket at /tmp/ssh-PeY5lmKPqlf1/agent.4748
Starting ssh-agent:  done
Enter passphrase for /c/Users/jschulz/.ssh/id_rsa:
Identity added: /c/Users/jschulz/.ssh/id_rsa (/c/Users/jschulz/.ssh/id_rsa)
c:\Users\jschulz\.ssh\id_rsa3: No such file or directory

@jankatins
Copy link
Author

Change the implementation to the trivial one which needs keyfiles on the start-ssh-agent call :-)

@jankatins
Copy link
Author

jankatins commented Aug 8, 2016

Ok, it doesn't: the current start-ssh-agent script depends on %* beeing commands which are called in the end. -> reverted to the original state...

@jankatins jankatins force-pushed the more-keys branch 2 times, most recently from 51765fe to ea4dad1 Compare August 8, 2016 13:20
you can now also pass in a full path:

set "SSH_ADD_ADDITIONAL_KEYFILES=C:\Users\jschulz\.ssh\id_rsa2,id_rsa3"

ssh-add does a check if the file exist, so our check for existence now only
makes it easier for the user to specify a keyfile:

* if %USERPROFILE%\.ssh\<keyfile> exist, use that path
* else assume that <keyfile> is a full path

Signed-off-by: Jan Schulz <[email protected]>
@dscho
Copy link
Member

dscho commented Aug 8, 2016

I could actually live with load every id_* or load every *_rsa solution (=without a special env variable)...

Personally, I would prefer that... Dunno. Do you have any strong opinion?

@jankatins
Copy link
Author

I don't care. Or better: I actually have a potential use case for the *.pem files.

@svenluijten do you want to voice an opinion here?

@dscho
Copy link
Member

dscho commented Aug 25, 2016

Good news! We may not need any patch at all... Git for Windows 2.9.3(2) just shipped, including OpenSSH 7.3p1. This OpenSSH version most notably supports the AddKeysToAgent setting. From http://superuser.com/questions/325662/how-to-make-ssh-agent-automatically-add-the-key-on-demand/1114257#1114257:

ssh supports adding a key to the agent on first use (since version 7.2). You can enable that feature by putting the following into ~/.ssh/config

AddKeysToAgent yes

This also works when using derivative tools, such as git.

From the 7.2 changelog:

  • ssh(1): Add an AddKeysToAgent client option which can be set to
    'yes', 'no', 'ask', or 'confirm', and defaults to 'no'. When
    enabled, a private key that is used during authentication will be
    added to ssh-agent if it is running (with confirmation enabled if
    set to 'confirm').

@jankatins
Copy link
Author

@dscho Nice! Lets close this then :-)

@jankatins jankatins closed this Aug 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants