fix(ssh-agent): add support for any OpenSSH key

The ssh-agent plugin now adds all files in `~/.ssh/` that start with
`-----BEGIN OPENSSH PRIVATE KEY-----`, regardless of their name.

This fixes an issue where the plugin was only adding keys with hardcoded names.

This change ensures that any valid OpenSSH private key will be added
to the ssh-agent, improving compatibility and flexibility for users
with custom key names.
This commit is contained in:
mikhailde 2024-10-10 14:13:25 +03:00
commit 2e746384bf

View file

@ -39,13 +39,16 @@ function _add_identities() {
return return
fi fi
# add default keys if no identities were set up via zstyle # If no keys specified in zstyle, add default keys.
# this is to mimic the call to ssh-add with no identities # Mimics calling ssh-add with no arguments.
if [[ ${#identities} -eq 0 ]]; then if [[ ${#identities[@]} -eq 0 ]]; then
# key list found on `ssh-add` man page's DESCRIPTION section # Iterate over files in .ssh folder.
for id in id_rsa id_dsa id_ecdsa id_ed25519 id_ed25519_sk identity; do for file in "$HOME/.ssh"/*; do
# check if file exists # Check if file is a regular file and starts with "-----BEGIN OPENSSH PRIVATE KEY-----".
[[ -f "$HOME/.ssh/$id" ]] && identities+=($id) if [[ -f "$file" && $(head -n 1 "$file") =~ ^-----BEGIN\ OPENSSH\ PRIVATE\ KEY----- ]]; then
# Add filename (without path) to identities array.
identities+=("${file##*/}")
fi
done done
fi fi