Skip to content

Instantly share code, notes, and snippets.

@daemswibowo
Last active July 4, 2022 15:50
Show Gist options
  • Save daemswibowo/42995eefb25a13da0dede80fe476040f to your computer and use it in GitHub Desktop.
Save daemswibowo/42995eefb25a13da0dede80fe476040f to your computer and use it in GitHub Desktop.
Bash generate .env file from vault
if [ -z "$TOKEN" ]; then
echo "Token not provided!"
else
if [ -z "$API_URL" ]; then
echo "Missing the API url, I don't know where the env is :/"
else
echo "Getting environment variable from $API_URL"
if [ -z "$OUTPUT" ]; then
# set default output name
OUTPUT=.env
fi
curl -X 'GET' \
"$API_URL" \
-H 'accept: */*' \
-H "X-Vault-Token: $TOKEN" | jq '.data.data' | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' | sed 's,.\(.*\).$,\1,g' > $OUTPUT
fi
fi
@daemswibowo
Copy link
Author

daemswibowo commented Jun 23, 2022

Generating ENV file from Vault

Requirements: jq => https://stedolan.github.io/jq/

Run this on your terminal:

API_URL=yourvaultsecretendpoint TOKEN=yourvaulttoken sh ./generate-env.sh

Disclaimer: This command tested only on Mac OS, not sure if it work or not for windows or linux

This will generate .env file if success getting the environment secret from vault.

Envronment Variables

Name Explanation Mandatory
API_URL A full path of your secret endpoint yes
TOKEN Your Vault token yes
OUTPUT Your desire output env file. Default .env no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment