Skip to content

Commit 993c6b2

Browse files
Added additional tests for coverage
1 parent 3203be1 commit 993c6b2

File tree

5 files changed

+177
-6
lines changed

5 files changed

+177
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude_path:
2+
- "*.txt"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude_path:
2+
- "integration/*"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ignore_result_id:
2+
- "986e5d5a73bcc5972e2d697ec0846db79b03dd75"
3+
- "976014fdd1ef77be2c2ff1c1b6a3fe63d2b237f4"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore_rule_id:
2+
- "github-pat"

test/integration/pre-receive_test.go

Lines changed: 168 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
const (
16+
ignoreRuleId = "ignoreRuleId.yaml"
17+
ignoreResultId = "ignoreResultId.yaml"
18+
ignoreFiles = "excludeFile.yaml"
19+
ignoreFolder = "excludeFolder.yaml"
20+
)
21+
1522
func TestPreReceive_PushSecrets(t *testing.T) {
16-
workDir, cleanUp := setUpPreReceiveHookDir(t)
23+
workDir, cleanUp := setUpPreReceiveHookDir(t, "")
1724
defer cleanUp()
1825
assert.NoError(t, os.Chdir(workDir))
1926
setGlobalGitAccount(t, workDir)
@@ -47,7 +54,7 @@ func TestPreReceive_PushSecrets(t *testing.T) {
4754
}
4855

4956
func TestPreReceive_PushWithoutSecrets(t *testing.T) {
50-
workDir, cleanUp := setUpPreReceiveHookDir(t)
57+
workDir, cleanUp := setUpPreReceiveHookDir(t, "")
5158
defer cleanUp()
5259
assert.NoError(t, os.Chdir(workDir))
5360
setGlobalGitAccount(t, workDir)
@@ -80,7 +87,7 @@ func TestPreReceive_PushWithoutSecrets(t *testing.T) {
8087
}
8188

8289
func TestPreReceive_PushSecrets_and_NoSecretsFile(t *testing.T) {
83-
workDir, cleanUp := setUpPreReceiveHookDir(t)
90+
workDir, cleanUp := setUpPreReceiveHookDir(t, "")
8491
defer cleanUp()
8592
assert.NoError(t, os.Chdir(workDir))
8693
setGlobalGitAccount(t, workDir)
@@ -115,6 +122,154 @@ func TestPreReceive_PushSecrets_and_NoSecretsFile(t *testing.T) {
115122
assert.Contains(t, outputString, "Detected 1 secret across 1 commit")
116123
}
117124

125+
func TestPreReceive_IgnoreRuleId_ConfigFile(t *testing.T) {
126+
configFileName := ignoreRuleId
127+
workDir, cleanUp := setUpPreReceiveHookDir(t, configFileName)
128+
defer cleanUp()
129+
assert.NoError(t, os.Chdir(workDir))
130+
setGlobalGitAccount(t, workDir)
131+
132+
//create a secret file
133+
secretFile := filepath.Join(workDir, "secret1.txt")
134+
err := os.WriteFile(secretFile, []byte("ghp_DDDDDDDDDDDDDDDDDDDDDDDDDDDADDADDDAD"), 0644)
135+
assert.NoError(t, err)
136+
// Git add
137+
outputCmd := exec.Command("git", "add", "secret1.txt")
138+
// making it workingDir
139+
outputCmd.Dir = workDir
140+
141+
output, err := outputCmd.CombinedOutput()
142+
assert.NoError(t, err, "failed to add files in staging :%s", string(output))
143+
144+
// Add commit
145+
commitCmd := exec.Command("git", "commit", "-m", "added without secrets file")
146+
commitCmd.Dir = workDir
147+
output, err = commitCmd.CombinedOutput()
148+
assert.NoError(t, err, "Filed to commit :%s", string(output))
149+
//Pushing
150+
cmdPush := exec.Command("git", "push")
151+
cmdPush.Dir = workDir
152+
output, err = cmdPush.CombinedOutput()
153+
outputString := string(output)
154+
// ignoring the secrets as per ruleId and successfully pushing
155+
assert.NotContains(t, outputString, "[remote rejected]")
156+
assert.NotContains(t, outputString, "(pre-receive hook declined)")
157+
assert.NotContains(t, outputString, "Detected 1 secret across 1 commit")
158+
}
159+
160+
func TestPreReceive_IgnoreResultId_ConfigFile(t *testing.T) {
161+
configFileName := ignoreResultId
162+
workDir, cleanUp := setUpPreReceiveHookDir(t, configFileName)
163+
defer cleanUp()
164+
assert.NoError(t, os.Chdir(workDir))
165+
setGlobalGitAccount(t, workDir)
166+
167+
//create a secret file
168+
file1 := filepath.Join(workDir, "secretsFile.txt")
169+
err := os.WriteFile(file1, []byte("ghp_DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"), 0644)
170+
assert.NoError(t, err)
171+
// Git add
172+
outputCmd := exec.Command("git", "add", "secretsFile.txt")
173+
// making it workingDir
174+
outputCmd.Dir = workDir
175+
176+
output, err := outputCmd.CombinedOutput()
177+
assert.NoError(t, err, "failed to add files in staging :%s", string(output))
178+
179+
// Add commit
180+
commitCmd := exec.Command("git", "commit", "-m", "added without secrets file")
181+
commitCmd.Dir = workDir
182+
output, err = commitCmd.CombinedOutput()
183+
assert.NoError(t, err, "Filed to commit :%s", string(output))
184+
//Pushing
185+
cmdPush := exec.Command("git", "push")
186+
cmdPush.Dir = workDir
187+
output, err = cmdPush.CombinedOutput()
188+
outputString := string(output)
189+
// ignoring the secrets as resultId matches in configFile and successfully pushing
190+
assert.NotContains(t, outputString, "[remote rejected]")
191+
assert.NotContains(t, outputString, "(pre-receive hook declined)")
192+
assert.NotContains(t, outputString, "Detected 1 secret across 1 commit")
193+
}
194+
195+
func TestPreReceive_IgnoreFileExclusion_ConfigFile(t *testing.T) {
196+
//Adding config file with file exclusion params
197+
configFileName := ignoreFiles
198+
workDir, cleanUp := setUpPreReceiveHookDir(t, configFileName)
199+
defer cleanUp()
200+
assert.NoError(t, os.Chdir(workDir))
201+
setGlobalGitAccount(t, workDir)
202+
203+
//create a secret file
204+
file1 := filepath.Join(workDir, "secretsFile.txt")
205+
err := os.WriteFile(file1, []byte("ghp_DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"), 0644)
206+
assert.NoError(t, err)
207+
// Git add
208+
outputCmd := exec.Command("git", "add", "secretsFile.txt")
209+
// making it workingDir
210+
outputCmd.Dir = workDir
211+
212+
output, err := outputCmd.CombinedOutput()
213+
assert.NoError(t, err, "failed to add files in staging :%s", string(output))
214+
215+
// Add commit
216+
commitCmd := exec.Command("git", "commit", "-m", "added without secrets file")
217+
commitCmd.Dir = workDir
218+
output, err = commitCmd.CombinedOutput()
219+
assert.NoError(t, err, "Filed to commit :%s", string(output))
220+
//Pushing
221+
cmdPush := exec.Command("git", "push")
222+
cmdPush.Dir = workDir
223+
output, err = cmdPush.CombinedOutput()
224+
outputString := string(output)
225+
// ignoring the secrets as resultId matches in configFile and successfully pushing
226+
assert.NotContains(t, outputString, "[remote rejected]")
227+
assert.NotContains(t, outputString, "(pre-receive hook declined)")
228+
assert.NotContains(t, outputString, "Detected 1 secret across 1 commit")
229+
assert.Contains(t, outputString, "No secrets detected by Cx Secret Scanner")
230+
231+
}
232+
233+
func TestPreReceive_IgnoreFolderExclusion_ConfigFile(t *testing.T) {
234+
//Adding config file with folder exclusion params
235+
configFileName := ignoreFolder
236+
workDir, cleanUp := setUpPreReceiveHookDir(t, configFileName)
237+
defer cleanUp()
238+
assert.NoError(t, os.Chdir(workDir))
239+
setGlobalGitAccount(t, workDir)
240+
241+
//create a secret file
242+
folderPath := filepath.Join(workDir, "integration")
243+
err := os.MkdirAll(folderPath, os.ModePerm)
244+
assert.NoError(t, err)
245+
file1 := filepath.Join(folderPath, "secretsFile.txt")
246+
err = os.WriteFile(file1, []byte("ghp_DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"), 0644)
247+
assert.NoError(t, err)
248+
// Git add
249+
outputCmd := exec.Command("git", "add", "integration/secretsFile.txt")
250+
// making it workingDir
251+
outputCmd.Dir = workDir
252+
253+
output, err := outputCmd.CombinedOutput()
254+
assert.NoError(t, err, "failed to add files in staging :%s", string(output))
255+
256+
// Add commit
257+
commitCmd := exec.Command("git", "commit", "-m", "added without secrets file")
258+
commitCmd.Dir = workDir
259+
output, err = commitCmd.CombinedOutput()
260+
assert.NoError(t, err, "Filed to commit :%s", string(output))
261+
//Pushing
262+
cmdPush := exec.Command("git", "push")
263+
cmdPush.Dir = workDir
264+
output, err = cmdPush.CombinedOutput()
265+
outputString := string(output)
266+
// ignoring the secrets as resultId matches in configFile and successfully pushing
267+
assert.NotContains(t, outputString, "[remote rejected]")
268+
assert.NotContains(t, outputString, "(pre-receive hook declined)")
269+
assert.NotContains(t, outputString, "Detected 1 secret across 1 commit")
270+
assert.Contains(t, outputString, "No secrets detected by Cx Secret Scanner")
271+
}
272+
118273
func setGlobalGitAccount(t *testing.T, repoName string) {
119274
// Set global git config
120275
username := os.Getenv("GITHUB_ACTOR")
@@ -123,22 +278,29 @@ func setGlobalGitAccount(t *testing.T, repoName string) {
123278
assert.NoError(t, err)
124279
}
125280

126-
func setUpPreReceiveHookDir(t *testing.T) (workdir string, cleanup func()) {
281+
func setUpPreReceiveHookDir(t *testing.T, fileName string) (workdir string, cleanup func()) {
127282
orgWorkDir, err := os.Getwd()
128283
assert.NoError(t, err)
129284
tempDir := t.TempDir()
130-
fmt.Println("the current dir" + orgWorkDir)
131285

132286
//Init a bare repo
133287

134288
err = exec.Command("git", "init", "--bare", filepath.Join(tempDir, "server")).Run()
135289
assert.NoError(t, err)
136290
cxPath := filepath.Join(orgWorkDir, "..", "..", "bin", "cx")
291+
yamlPath := filepath.Join(orgWorkDir, "data", "pre-receive-data", fileName)
292+
fmt.Println("yaml path" + yamlPath)
137293
fmt.Println("the current dir" + cxPath)
138294

139295
preReceivePath := filepath.Join(tempDir, "server", "hooks", "pre-receive")
296+
configFlags := ""
297+
if fileName != "" {
298+
configFlags = fmt.Sprintf(` --config "%s"`, yamlPath)
299+
}
300+
140301
script := fmt.Sprintf(`#!/bin/bash
141-
"%s" hooks pre-receive secrets-scan`, cxPath)
302+
"%s" hooks pre-receive secrets-scan%s`, cxPath, configFlags)
303+
142304
err = os.WriteFile(preReceivePath, []byte(script), 0755)
143305
assert.NoError(t, err)
144306

0 commit comments

Comments
 (0)