-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_helper.bash
240 lines (195 loc) · 5.84 KB
/
test_helper.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
# Bug Bounty and Hackerone Folks: No need to report this file. The
# apparent keys below are all test data used to
# ensure our leak prevention tools are working.
BATS_TMPDIR=${BATS_TMPDIR:-/tmp} # set default if sourcing from cli
REPO_PATH=$(mktemp -d "${BATS_TMPDIR}/gittest.XXXXXX")
setupGitRepo() {
mkdir -p "${REPO_PATH}"
(cd "$REPO_PATH" && git init .)
}
cleanGitRepo() {
rm -fr "${REPO_PATH}"
}
testCommit() {
filename=$1
echo "=== Git Status Before Add ===" >&2
(cd "${REPO_PATH}" && git status) >&2
echo "=== Git Add ===" >&2
(cd "${REPO_PATH}" && git add "${filename}")
echo "=== Git Status Before Commit ===" >&2
(cd "${REPO_PATH}" && git status) >&2
echo "=== Git Commit ===" >&2
(cd "${REPO_PATH}" && GIT_TRACE=1 git commit -m 'test commit')
local commit_status=$?
echo "=== Git Status After Commit ===" >&2
(cd "${REPO_PATH}" && git status) >&2
return $commit_status
}
testUnstagedCommit() {
filename=$1
(cd "${REPO_PATH}" && git commit -m 'test commit')
}
setup() {
load 'test/bats-support/load' # this is required by bats-assert!
load 'test/bats-assert/load'
setupGitRepo
}
teardown() {
cleanGitRepo
}
addFileWithNoSecrets() {
local filename="${REPO_PATH}/plainfile.md"
# Set up git config for test repo
(cd "${REPO_PATH}" && git config user.name "Test User")
(cd "${REPO_PATH}" && git config hooks.gitleaks true)
# Create and add file
touch "${filename}"
echo "Just a plain old file" >> "${filename}"
# Set test environment variables
export BATS_TEST_FILENAME="caulked.bats"
unset GIT_TRACE
unset GIT_TRACE_SETUP
# Try the commit
testCommit "$filename"
local commit_status=$?
# Clean up environment
unset BATS_TEST_FILENAME
return $commit_status
}
unstagedFileWithAwsSecrets() {
local secrets_file="${REPO_PATH}/unstaged-secretsfile.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
aws_secret_access_key = WT8ftNba7siVx5UOoGzJSyd82uNCZAC8LCllzcWp
END
testUnstagedCommit "$secrets_file"
}
addFileWithAwsSecrets() {
local secrets_file="${REPO_PATH}/secretsfile.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
aws_secret_access_key = WT8ftNba7siVx5UOoGzJSyd82uNCZAC8LCllzcWp
END
testCommit "$secrets_file"
}
addFileWithAwsAccessKey() {
local secrets_file="${REPO_PATH}/accessfile.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
AWS_ACCESS_KEY_ID: AKIAJLLCKKYFEWP5MWXA
END
testCommit "$secrets_file"
}
addFileWithSecretEmail() {
local secrets_file="${REPO_PATH}/emailfile.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
Email address like [email protected]
END
testCommit "$secrets_file"
}
addFileWithSlackAPIToken() {
local secrets_file="${REPO_PATH}/slacktokenfile.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
slack_api_token=xoxb-333649436676-799261852869-clFJVVIaoJahpORboa3Ba2al
END
testCommit "$secrets_file"
}
addFileWithIPv4() {
local secrets_file="${REPO_PATH}/ipv4file.md"
cat >"${secrets_file}" <<END
SHHHH... Secrets in this file
Host: 10.20.30.40
END
testCommit "$secrets_file"
}
yamlTest() {
local secrets_file="${REPO_PATH}/cloudgov.yml"
cat >"${secrets_file}" <<END
# Credentials
$1
END
testCommit "$secrets_file"
}
testLocalGitHook() {
# Create a test pre-commit hook that will run alongside gitleaks
local hook_dir="$HOME/.git-support/hooks"
local original_hook="$hook_dir/pre-commit"
local backup_hook="$hook_dir/pre-commit.backup"
# Backup existing hook if it exists
if [ -f "$original_hook" ]; then
cp "$original_hook" "$backup_hook"
fi
# Create new hook that includes both our test output and original functionality
cat >"$original_hook" <<'END'
#!/bin/bash
echo "foobar"
# Run gitleaks check if it exists
if [ -f "$HOME/.git-support/gitleaks.toml" ]; then
gitleaks protect --staged --config="$HOME/.git-support/gitleaks.toml" --verbose
fi
END
chmod 755 "$original_hook"
# Create and commit a test file
local test_file="${REPO_PATH}/test.txt"
echo "test content" > "$test_file"
testCommit "$test_file"
# Restore original hook
if [ -f "$backup_hook" ]; then
mv "$backup_hook" "$original_hook"
fi
}
##########################
# for development purposes
##########################
turnOffHooksGitleaks() {
(cd "$REPO_PATH" && git config --local hooks.gitleaks false)
./check_repos.sh "$REPO_PATH" check_hooks_gitleaks
}
changeGitHooksPath() {
(cd "$REPO_PATH" && git config --local core.hooksPath "foobar")
./check_repos.sh "$REPO_PATH" check_hooks_path
}
createPrecommitNoGitleaks() {
(cd "$REPO_PATH" && mv .git/hooks/pre-commit.sample .git/hooks/pre-commit)
}
createPrecommitCommentedGitleaks() {
cat >"$REPO_PATH"/.git/hooks/pre-commit <<END
# lets not run gitleaks
END
}
createPrecommitOKGitLeaks() {
cat >"$REPO_PATH"/.git/hooks/pre-commit <<END
#!/bin/sh
echo special stuff
$HOME/bin/gitleaks
END
}
addFileWithCGEmails() {
local secrets_file="${REPO_PATH}/cgemailfile.md"
cat >"${secrets_file}" <<END
No secrets in this file
Email addresses like [email protected] and [email protected]
END
testCommit "$secrets_file"
}
addFileWithGithubEmails() {
local secrets_file="${REPO_PATH}/ghemailfile.md"
cat >"${secrets_file}" <<END
No secrets in this file
Email address like [email protected] or [email protected]
END
testCommit "$secrets_file"
}
addFileWithInterpolatedYamlPassword() {
local secrets_file="${REPO_PATH}/ok_secret.yml"
cat >"${secrets_file}" <<END
No secrets in this file
database_password: ((database_password))
another_password: {{foo_pass}}
END
testCommit "$secrets_file"
}