Skip to content

Commit e2fc5de

Browse files
Merge pull request #152 from drone-plugins/CI-10849
feat: [CI-10849]: add git-leaks support
2 parents bac4fe7 + 324283f commit e2fc5de

File tree

8 files changed

+141
-0
lines changed

8 files changed

+141
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Drone plugin to publish files and artifacts to Amazon S3 or Minio. For the
1212
usage information and a listing of the available options please take a look at
1313
[the docs](http://plugins.drone.io/drone-plugins/drone-s3/).
1414

15+
Run the following script to install git-leaks support to this repo.
16+
```
17+
chmod +x ./git-hooks/install.sh
18+
./git-hooks/install.sh
19+
```
20+
1521
## Build
1622

1723
Build the binary with the following commands:

git-hooks/.gitleaksignore

Whitespace-only changes.

git-hooks/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This document explains on how to install certain git hooks globally for all repositories in your machine.
2+
3+
Step 1: git clone https://github.com/drone-plugins/drone-s3.git
4+
Step 2: cd git-hooks
5+
Step 3: Run install.sh
6+
7+
"install.sh" script will create .git_template in the user directory and will put the git hook and its dependent scripts in it. Along with the .git_template folder, it will add 2 sections "init" and "hooks boolean" in the .gitconfig file in the same user's root directory.
8+
After running "install.sh" if you create/clone a new git repository then all the hooks will get install automatically for the git repository. In case of existing git repository copy the contents of ~/.git_template/hooks into the .git/hooks directory of existing git repository.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
#Helper script to be used as a pre-commit hook.
4+
5+
echo "This hook checks for any secrets getting pushed as part of commit. If you feel that scan is false positive. \
6+
Then add the exclusion in .gitleaksignore file. For more info visit: https://github.com/zricethezav/gitleaks"
7+
8+
GIT_LEAKS_PRE_COMMIT=s$(git config --bool hook.pre-commit.gitleak)
9+
10+
echo "INFO: Scanning Commits information for any GIT LEAKS"
11+
gitleaks protect --staged -v --exit-code=100
12+
STATUS=$?
13+
if [ $STATUS = 100 ]; then
14+
echo "WARNING: GIT LEAKS has detected sensitive information in your changes. Please remove them or add them (IF NON-SENSITIVE) in .gitleaksignore file."
15+
else
16+
exit 0
17+
fi

git-hooks/hooks/git-leaks.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
#Helper script to be used as a pre-commit hook.
4+
5+
echo "This hook checks for any secrets getting pushed as part of commit. If you feel that scan is false positive. \
6+
Then add the exclusion in .gitleaksignore file. For more info visit: https://github.com/zricethezav/gitleaks"
7+
8+
GIT_LEAKS=$(git config --bool hook.pre-push.gitleaks)
9+
10+
echo "INFO: Scanning Commits information for any GIT LEAKS"
11+
gitleaks detect -s ./ --log-level=debug --log-opts=-1 -v
12+
STATUS=$?
13+
if [ $STATUS != 0 ]; then
14+
echo "WARNING: GIT LEAKS has detected sensitive information in your changes. Please remove them or add them (IF NON-SENSITIVE) in .gitleaksignore file."
15+
exit $STATUS
16+
else
17+
exit 0
18+
fi

git-hooks/hooks/pre-commit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
GL_SCRIPT_PATH="$HOME/.git_template/hooks/git-leaks-pre-commit.sh"
4+
5+
pushd `dirname $0` > /dev/null && cd ../.. && BASEDIR=$(pwd -L) && popd > /dev/null
6+
BASENAME=`basename $0`
7+
8+
if git rev-parse --verify HEAD >/dev/null 2>&1
9+
then
10+
against=HEAD
11+
else
12+
#Initial commit : diff against an empty tree object
13+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
14+
fi
15+
16+
GIT_LEAKS_PRE_COMMIT=hook.pre-commit.gitleaks
17+
if [ "`git config $GIT_LEAKS_PRE_COMMIT`" == "false" ]
18+
then
19+
echo -e '\033[0;31m' checking git leaks is disabled - to enable: '\033[0;37m'git config --unset $GIT_LEAKS_PRE_COMMIT '\033[0m'
20+
echo -e '\033[0;34m' checking git leaks ... to enable: '\033[0;37m'git config --add $GIT_LEAKS_PRE_COMMIT true '\033[0m'
21+
else
22+
echo -e '\033[0;34m' checking for git leaks...
23+
[ -f "${GL_SCRIPT_PATH}" ] && . ${GL_SCRIPT_PATH} || echo "ERROR: Hook Script Not Found..." && exit 404
24+
fi

git-hooks/hooks/pre-push

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
GL_SCRIPT_PATH="$HOME/.git_template/hooks/git-leaks.sh"
4+
5+
pushd `dirname $0` > /dev/null && cd ../.. && BASEDIR=$(pwd -L) && popd > /dev/null
6+
BASENAME=`basename $0`
7+
8+
if git rev-parse --verify HEAD >/dev/null 2>&1
9+
then
10+
against=HEAD
11+
else
12+
#Initial commit : diff against an empty tree object
13+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
14+
fi
15+
16+
GIT_LEAKS=hook.pre-push.gitleaks
17+
if [ "`git config $GIT_LEAKS`" == "false" ]
18+
then
19+
echo -e '\033[0;31m' checking git leaks is disabled - to enable: '\033[0;37m'git config --unset $GIT_LEAKS '\033[0m'
20+
echo -e '\033[0;34m' checking git leaks ... to enable: '\033[0;37m'git config --add $GIT_LEAKS true '\033[0m'
21+
else
22+
echo -e '\033[0;34m' checking for git leaks...
23+
[ -f "${GL_SCRIPT_PATH}" ] && . ${GL_SCRIPT_PATH} || echo "ERROR: Hook Script Not Found..." && exit 404
24+
fi

git-hooks/install.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
#Function to check if package is installed or not
4+
#args: $1: Name of the Package
5+
function check_package_installed() {
6+
LOCAL_PACKAGE_NAME=$1
7+
echo "Checking if $LOCAL_PACKAGE_NAME is installed or not..."
8+
brew list $LOCAL_PACKAGE_NAME
9+
if [ "$?" -eq 1 ];then
10+
echo "Installing $LOCAL_PACKAGE_NAME package..."
11+
brew install $LOCAL_PACKAGE_NAME
12+
fi
13+
}
14+
15+
function create_git_template() {
16+
cd $BASEDIR
17+
mkdir -p ~/.git_template/hooks
18+
git config --global init.templatedir ${GIT_TEMPLATE}
19+
git config --global --add $GIT_LEAKS true
20+
git config --global --add $GIT_LEAKS_PRE_COMMIT true
21+
find hooks/ -type f -exec cp "{}" ~/.git_template/hooks \;
22+
#cp -f hooks/* ~/.git_template/hooks
23+
cat ~/.gitconfig
24+
}
25+
26+
GIT_TEMPLATE="~/.git_template"
27+
GIT_LEAKS=hook.pre-push.gitleaks
28+
GIT_LEAKS_PRE_COMMIT=hook.pre-commit.gitleaks
29+
30+
pushd `dirname $0` && BASEDIR=$(pwd -L) && popd
31+
32+
echo This script will install hooks that run scripts that could be updated without notice.
33+
34+
while true; do
35+
read -p "Do you wish to install these hooks?" yn
36+
case $yn in
37+
[Yy]* ) check_package_installed "gitleaks";
38+
break;;
39+
[Nn]* ) exit;;
40+
* ) echo "Please answer yes or no.";;
41+
esac
42+
done
43+
44+
create_git_template

0 commit comments

Comments
 (0)