Skip to content

Instantly share code, notes, and snippets.

@melchoy
Created July 28, 2024 04:18
Show Gist options
  • Save melchoy/1573a7f250004a8eb4b52d349eecdd2e to your computer and use it in GitHub Desktop.
Save melchoy/1573a7f250004a8eb4b52d349eecdd2e to your computer and use it in GitHub Desktop.
Pre-commit hook to check for ansible encryption
#!/bin/bash
VAULT_DIR="secrets"
EXIT_CODE=0
for file in $(git diff --cached --name-only | grep "^$VAULT_DIR/"); do
if [ ! -f "$file" ]; then
echo "Warning: $file does not exist. Skipping."
continue
fi
if ! grep -q "\$ANSIBLE_VAULT;" "$file"; then
echo "Error: $file is not encrypted. Please encrypt the file before committing."
echo "1. Remove the unencrypted secrets from the Git history:"
echo " git reset HEAD"
echo ""
echo "2. Encrypt the file(s) using Ansible Vault:"
echo " ansible-vault encrypt <file> --vault-password-file <VAULT_PASS_SCRIPT>"
echo " git add <file>"
echo " git commit -m 'Encrypt secrets'"
EXIT_CODE=1
fi
done
exit $EXIT_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment