Skip to content

Instantly share code, notes, and snippets.

@brockoffdev
Last active January 16, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brockoffdev/9c282de01151396571eb2ab1a832df25 to your computer and use it in GitHub Desktop.
Save brockoffdev/9c282de01151396571eb2ab1a832df25 to your computer and use it in GitHub Desktop.
Example Versioning in Hashicorp Vault
#!/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