blob: 646b7f7a94f584dada26192a18ecf37d4a03c0c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# -*- mode: sh -*-
# op-ssh-add will add SSH keys to the agent, but will fetch the
# passphrase in 1Password rather than prompt you for it.
#
# To be found, keys must have the following three things:
#
# 1. Have the password field set to the passphrase of the key,
# 2. Have an `SSH' tag set, and
# 3. Have a `fingerprint' field set to the fingerprint of the key.
#
# You must also have the 1Password CLI tool (op) and jq installed.
if [[ ${@[1]} =~ '-[DdKLlTXx]' ]]; then
$(which -p ssh-add) $@
return $?
fi
local fp prv pub
for prv (${@[-1]} ~/.ssh/id_{ecdsa,ed25519}{,_sk}); do
if [[ -f ${prv}.pub ]]; then
pub=${prv}.pub
fp=$(cut -d' ' -f2 <<< $(ssh-keygen -f ${pub} -l))
break
fi
done
if [[ -z ${pub} ]]; then
$(which -p ssh-add) $@
return $?
fi
local session=$(op signin --raw)
if [[ -z ${session} ]]; then
return 1
fi
zmodload zsh/zpty
zpty ssh $(which -p ssh-add) ${prv}
zpty -w ssh $(op list items --session ${session} --tags SSH \
| op get item \
--fields fingerprint,password \
--session ${session} - \
| jq -jr "select(.fingerprint == \"${fp}\") | .password")
zpty -r ssh
zpty -d ssh
op signout --session ${session}
|