Last active
February 20, 2020 10:22
-
-
Save hijak/972a996974d5edd4bdb6aef89e37908c to your computer and use it in GitHub Desktop.
Vault Password Rotation - Linux User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
source /home/$USER/.bashrc | |
echo "Renew lease for" | |
curl -sS --fail -X POST -H "X-Vault-Token: $VAULT_TOKEN" ${VAULT_ADDR}/v1/auth/token/renew-self | jq .auth.lease_duration | |
retval=$? | |
if [[ $retval -ne 0 ]]; then | |
echo "Error renewing Vault token lease." | |
fi | |
ROTATION_KV="rotation-kv" | |
echo "Rotating $USER password" | |
echo "{\"data\":{\"password\":\"$(openssl rand -rand /etc/machine-id:/etc/ssl/certs/ca-certificates.crt -base64 33)\"}}" \ | |
| curl -sX POST -H "X-Vault-Token: $VAULT_TOKEN" ${VAULT_ADDR}/v1/${ROTATION_KV}/data/$(hostname) -d @- | jq .data.version || echo "fail" | |
NEWPASSWORD=$(curl -sX GET -H "X-Vault-Token: $VAULT_TOKEN" ${VAULT_ADDR}/v1/${ROTATION_KV}/data/$(hostname) | jq .data.data.password | sed 's/"//g') | |
echo "$USER:$NEWPASSWORD" | sudo chpasswd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment