Load SSH-Keys from Vault instead of filesystem
This commit is contained in:
parent
67ced20929
commit
6a3e278967
1 changed files with 19 additions and 24 deletions
|
@ -28,49 +28,44 @@ if [ $# -eq 0 ]; then
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Create a helper script to send STDIN data to ssh-add
|
||||||
|
HELPER=$(tempfile -m 0700)
|
||||||
|
trap "rm ${HELPER}" EXIT
|
||||||
|
|
||||||
|
cat -s <<EOF > ${HELPER}
|
||||||
|
#!/bin/bash
|
||||||
|
vault read -field=private "/secret/ssh-key/\$1" | exec ssh-add -
|
||||||
|
EOF
|
||||||
|
|
||||||
for KEY_NAME in $@; do
|
for KEY_NAME in $@; do
|
||||||
KEYNAME_IN=${KEY_NAME}
|
|
||||||
|
|
||||||
# Try to find the passed key path / name
|
|
||||||
if ! [ -e "${KEY_NAME}" ]; then
|
|
||||||
if [ -e "${HOME}/.ssh/${KEY_NAME}" ]; then
|
|
||||||
KEY_NAME="${HOME}/.ssh/${KEY_NAME}"
|
|
||||||
else
|
|
||||||
error "[${KEYNAME_IN}] Could not find key file."
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If this key is already in the agent we don't need to do anything
|
# If this key is already in the agent we don't need to do anything
|
||||||
if ( ssh-add -l | grep -q "${KEY_NAME}" ); then
|
if ( ssh-add -l | grep -q "${KEY_NAME}" ); then
|
||||||
info "[${KEYNAME_IN}] Key already present."
|
info "[${KEY_NAME}] Key already present."
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Retrieve key from LastPass
|
# Retrieve key from LastPass
|
||||||
PWD=$(vault read -field=passphrase "/secret/ssh-key/$(basename ${KEY_NAME})")
|
PWD=$(vault read -field=passphrase "/secret/ssh-key/${KEY_NAME}")
|
||||||
|
|
||||||
# In case LastPass exitted non-zero we have no password
|
# In case LastPass exitted non-zero we have no password
|
||||||
if ! [ $? -eq 0 ]; then
|
if ! [ $? -eq 0 ]; then
|
||||||
error "[${KEYNAME_IN}] Unable to get password. Not trying to unlock."
|
error "[${KEY_NAME}] Unable to get password. Not trying to unlock."
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Fill password to ssh-add utility
|
# Fill password to ssh-add utility
|
||||||
expect <<EOF >/dev/null
|
expect <<EOF >/dev/null
|
||||||
spawn ssh-add ${KEY_NAME}
|
spawn ${HELPER} ${KEY_NAME}
|
||||||
|
|
||||||
expect "Enter passphrase"
|
expect "Enter passphrase"
|
||||||
send "$PWD\n"
|
send "$PWD\n"
|
||||||
expect eof
|
|
||||||
|
expect "added:" {exit 0} timeout {exit 1}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Check whether the key was added to the agent
|
if [ $? -eq 0 ]; then
|
||||||
if ( ssh-add -l | grep -q "${KEY_NAME}" ); then
|
info "[${KEY_NAME}] Should be loaded by now."
|
||||||
success "[${KEYNAME_IN}] Key successfully added."
|
|
||||||
continue
|
|
||||||
else
|
else
|
||||||
error "[${KEYNAME_IN}] Found passphrase but could not add key."
|
error "[${KEY_NAME}] Was not added successfully."
|
||||||
continue
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue