Last active
January 16, 2018 16:37
-
-
Save brockoffdev/9c282de01151396571eb2ab1a832df25 to your computer and use it in GitHub Desktop.
Example Versioning in Hashicorp Vault
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 | |
VAULT_PATH=/dev/single | |
DATETIME=$(date -u +"%Y%m%d%H%M%S") | |
# Loop through passed variables, add them to json | |
for VAR in "${@}"; do | |
VAR=$(echo $VAR | sed -E -e 's/\\/\\\\/g' -e "s/([^=]+)=(.*)/\"\1\"=\"\2\"/") | |
JQ="${JQ} | .${VAR}" | |
done | |
# Create tempfiles | |
CURRENTTMPFILE=`mktemp /tmp/we.XXXXXX` || exit 1 | |
NEWTMPFILE=`mktemp /tmp/we.XXXXXX` || exit 1 | |
# Read vault vars, record error and exit if so. | |
vault read -format=json $VAULT_PATH | \ | |
jq .data >> $CURRENTTMPFILE | |
CURRENT_EXIT=$? | |
# Perform the write to /versions, and edit of current config | |
if [ $CURRENT_EXIT == 0 ]; then | |
cat $CURRENTTMPFILE | jq ". ${JQ}" >> $NEWTMPFILE | |
echo "Saving current config state..." | |
vault write /versions$VAULT_PATH/$DATETIME \ | |
@$CURRENTTMPFILE > /dev/null | |
vault write /versions$VAULT_PATH/previous \ | |
@$CURRENTTMPFILE > /dev/null | |
vault write $VAULT_PATH @$NEWTMPFILE \ | |
&& rm $OLDTMPFILE $NEWTMPFILE | |
else | |
echo "Issue reading from Vault...do you have permissions?" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment