Skip to content

Instantly share code, notes, and snippets.

@v6
Created August 9, 2018 19:03
Show Gist options
  • Save v6/f4683336eb1c4a6a98a0f3cf21e62df2 to your computer and use it in GitHub Desktop.
Save v6/f4683336eb1c4a6a98a0f3cf21e62df2 to your computer and use it in GitHub Desktop.
// , convert_hcl_vault_policy_to_json.sh converts a Vault Policy in HashiCorp Language format to the Single line JSON format.
#!/usr/bin/env bash
## tr -d '\n' |
##
## Behold.
##
## ______ _____ _ _ _____ _ ______
## | ___ \ ___| | | | _ | | | _ \
## | |_/ / |__ | |_| | | | | | | | | |
## | ___ \ __|| _ | | | | | | | | |
## | |_/ / |___| | | \ \_/ / |___| |/ /
## \____/\____/\_| |_/\___/\_____/___/ (_)
##
##
##
##
## It is hcl2json, Policy Weirdness Edition™
## This removes comments from a .hcl file, serializes
## its contents into a string compatible with JSON, and
## outputs JSON with that string assigned to the
## key "policy".
## Created this based on my experiments documented here:
## https://groups.google.com/forum/#!topic/vault-tool/VslgAkDsEtc
printf "{
\"policy\": \""
cat $1 | sed '/^[[:blank:]]*#/d;s/#.*//' | sed 's/\"/\\\"/g' | tr -d '\n' ## Remove comments and serialize string
printf "\"
}"
@noahehall
Copy link

4 years later and still working perfectly

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