@@ -12,8 +12,15 @@ import (
12
12
"github.com/stretchr/testify/assert"
13
13
)
14
14
15
+ const (
16
+ ignoreRuleId = "ignoreRuleId.yaml"
17
+ ignoreResultId = "ignoreResultId.yaml"
18
+ ignoreFiles = "excludeFile.yaml"
19
+ ignoreFolder = "excludeFolder.yaml"
20
+ )
21
+
15
22
func TestPreReceive_PushSecrets (t * testing.T ) {
16
- workDir , cleanUp := setUpPreReceiveHookDir (t )
23
+ workDir , cleanUp := setUpPreReceiveHookDir (t , "" )
17
24
defer cleanUp ()
18
25
assert .NoError (t , os .Chdir (workDir ))
19
26
setGlobalGitAccount (t , workDir )
@@ -47,7 +54,7 @@ func TestPreReceive_PushSecrets(t *testing.T) {
47
54
}
48
55
49
56
func TestPreReceive_PushWithoutSecrets (t * testing.T ) {
50
- workDir , cleanUp := setUpPreReceiveHookDir (t )
57
+ workDir , cleanUp := setUpPreReceiveHookDir (t , "" )
51
58
defer cleanUp ()
52
59
assert .NoError (t , os .Chdir (workDir ))
53
60
setGlobalGitAccount (t , workDir )
@@ -80,7 +87,7 @@ func TestPreReceive_PushWithoutSecrets(t *testing.T) {
80
87
}
81
88
82
89
func TestPreReceive_PushSecrets_and_NoSecretsFile (t * testing.T ) {
83
- workDir , cleanUp := setUpPreReceiveHookDir (t )
90
+ workDir , cleanUp := setUpPreReceiveHookDir (t , "" )
84
91
defer cleanUp ()
85
92
assert .NoError (t , os .Chdir (workDir ))
86
93
setGlobalGitAccount (t , workDir )
@@ -115,6 +122,154 @@ func TestPreReceive_PushSecrets_and_NoSecretsFile(t *testing.T) {
115
122
assert .Contains (t , outputString , "Detected 1 secret across 1 commit" )
116
123
}
117
124
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
+
118
273
func setGlobalGitAccount (t * testing.T , repoName string ) {
119
274
// Set global git config
120
275
username := os .Getenv ("GITHUB_ACTOR" )
@@ -123,22 +278,29 @@ func setGlobalGitAccount(t *testing.T, repoName string) {
123
278
assert .NoError (t , err )
124
279
}
125
280
126
- func setUpPreReceiveHookDir (t * testing.T ) (workdir string , cleanup func ()) {
281
+ func setUpPreReceiveHookDir (t * testing.T , fileName string ) (workdir string , cleanup func ()) {
127
282
orgWorkDir , err := os .Getwd ()
128
283
assert .NoError (t , err )
129
284
tempDir := t .TempDir ()
130
- fmt .Println ("the current dir" + orgWorkDir )
131
285
132
286
//Init a bare repo
133
287
134
288
err = exec .Command ("git" , "init" , "--bare" , filepath .Join (tempDir , "server" )).Run ()
135
289
assert .NoError (t , err )
136
290
cxPath := filepath .Join (orgWorkDir , ".." , ".." , "bin" , "cx" )
291
+ yamlPath := filepath .Join (orgWorkDir , "data" , "pre-receive-data" , fileName )
292
+ fmt .Println ("yaml path" + yamlPath )
137
293
fmt .Println ("the current dir" + cxPath )
138
294
139
295
preReceivePath := filepath .Join (tempDir , "server" , "hooks" , "pre-receive" )
296
+ configFlags := ""
297
+ if fileName != "" {
298
+ configFlags = fmt .Sprintf (` --config "%s"` , yamlPath )
299
+ }
300
+
140
301
script := fmt .Sprintf (`#!/bin/bash
141
- "%s" hooks pre-receive secrets-scan` , cxPath )
302
+ "%s" hooks pre-receive secrets-scan%s` , cxPath , configFlags )
303
+
142
304
err = os .WriteFile (preReceivePath , []byte (script ), 0755 )
143
305
assert .NoError (t , err )
144
306
0 commit comments