Skip to content

Instantly share code, notes, and snippets.

@wnoguchi
Forked from DmZ/pre-commit
Created December 31, 2015 04:36
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 wnoguchi/78475cefdb1e00a6ba1e to your computer and use it in GitHub Desktop.
Save wnoguchi/78475cefdb1e00a6ba1e to your computer and use it in GitHub Desktop.
Git pre-commit hook to search for Amazon AWS API keys.
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# Check changed files for an AWS keys
KEY_ID=$(git diff --cached --name-only -z $against | xargs -0 cat | grep -c -E '[^A-Z0-9][A-Z0-9]{20}[^A-Z0-9]')
KEY=$(git diff --cached --name-only -z $against | xargs -0 cat | grep -c -E '[^A-Za-z0-9/+=][A-Za-z0-9/+=]{40}[^A-Za-z0-9/+=]')
if [ $KEY_ID -ne 0 -o $KEY -ne 0 ]; then
echo "Found patterns for AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY"
echo "Please check your code and remove API keys."
exit 1
fi
# Normal exit
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment