2018-10-29 11:11:18 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
source "${HOME}/bin/script_framework.sh"
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
keyname=${1:-}
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
[ -z "${keyname}" ] && fail "Key name not provided"
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
if [ ! -e "/tmp/${keyname}" ]; then
|
|
|
|
vault read -field=private "/secret/ssh-key/${keyname}" >"/tmp/${keyname}"
|
|
|
|
chmod 0600 \
|
|
|
|
"/tmp/${keyname}"
|
2016-07-21 13:48:49 +00:00
|
|
|
fi
|
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
function cleanup() {
|
|
|
|
rm -f \
|
|
|
|
"/tmp/${keyname}"
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
OLDPASS=$(vault read -field=passphrase "/secret/ssh-key/${keyname}") || fail "Unable to retrieve old passphrase"
|
|
|
|
NEWPASS=$(password get -l 64) || fail "Unable to generate a new passphrase"
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
[ -z "${NEWPASS}" ] && fail "Unable to generate a new passphrase"
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-10-29 11:11:18 +00:00
|
|
|
ssh-keygen -p -P "${OLDPASS}" -N "${NEWPASS}" -f "/tmp/${keyname}" || fail "Was not able to modify key with new passphrase"
|
|
|
|
vault-patch --log-level=warn "/secret/ssh-key/${keyname}" \
|
|
|
|
passphrase="${NEWPASS}" \
|
|
|
|
private=@/tmp/${keyname} \
|
|
|
|
passphrase_changed=$(date +%Y-%m-%dT%H:%M:%S%z)
|
2016-07-21 13:48:49 +00:00
|
|
|
|
|
|
|
echo "Everything was fine, key has been changed."
|