Created
February 10, 2016 04:26
-
-
Save smclauch/213c6c2155a993f3aec7 to your computer and use it in GitHub Desktop.
Git pre-commit script to check for AWS keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
EMPTY_TREE=$(git hash-object -t tree /dev/null) | |
against=$EMPTY_TREE | |
fi | |
# Redirect output to stderr. | |
exec 1>&2 | |
# Check changed files for an AWS keys | |
FILES=$(git diff --cached --name-only $against) | |
if [ -n "$FILES" ]; then | |
KEY_ID=$(grep -E --line-number '\b[A-Z0-9]{20}\b' $FILES) | |
KEY=$(grep -E --line-number '[^A-Za-z0-9/+=][A-Za-z0-9/+=]{40}(\b|[^A-Za-z0-9/+=])' $FILES) | |
if [ -n "$KEY_ID" ] || [ -n "$KEY" ]; then | |
exec < /dev/tty # Capture input | |
echo "=========== Possible AWS Access Key IDs ===========" | |
echo "${KEY_ID}" | |
echo "" | |
echo "=========== Possible AWS Secret Access Keys ===========" | |
echo "${KEY}" | |
echo "" | |
while true; do | |
read -p "[AWS Key Check] Possible AWS keys found. Commit files anyway? (y/N) " yn | |
if [ "$yn" = "" ]; then | |
yn='N' | |
fi | |
case $yn in | |
[Yy] ) exit 0;; | |
[Nn] ) exit 1;; | |
* ) echo "Please answer y or n for yes or no.";; | |
esac | |
done | |
exec <&- # Release input | |
fi | |
fi | |
# Normal exit | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on (i.e. almost completely ripped off from) https://gist.github.com/czardoz/b8bb58ad10f4063209bd with improved regular expressions in the key detection grep statements. To use this script copy it to
$REPOSITORY_DIR/.git/hooks/pre-commit
and make it executable.