1
1
package v1
2
2
3
3
import (
4
- "context"
5
4
"testing"
6
5
7
6
"github.com/google/go-cmp/cmp"
8
-
9
- "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
10
- "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
11
- )
12
-
13
- var (
14
- validPattern = `
15
- azure credentials:
16
- azureClientID: clientid9304d5df4-aac1-6117-552c-7f70c89a40d9
17
- azureTenant: tenant_idid9304d5df4-aac1-6117-552c-7f70c89a40d9
18
- azureClientSecret: clientsecretY_0w|[cGpan41k6ng.ol414sp4ccw2v_rkfmbs537i
19
- `
20
- invalidPattern = `
21
- azure credentials:
22
- azureClientID: 9304d5df4-aac1-6117-552c-7f70c89a
23
- azureTenant: id9304d5df4-aac1-6117-55-7f70c89a40d9
24
- azureClientSecret: Y_0w|[cGpan41k6ng.
25
- `
26
7
)
27
8
28
- func TestAzure_Pattern (t * testing.T ) {
29
- d := Scanner {}
30
- ahoCorasickCore := ahocorasick .NewAhoCorasickCore ([]detectors.Detector {d })
31
-
32
- tests := []struct {
33
- name string
34
- input string
35
- want []string
36
- }{
37
- {
38
- name : "valid pattern" ,
39
- input : validPattern ,
40
- want : []string {"304d5df4-aac1-6117-552c-7f70c89a40d9cGpan41k6ng.ol414sp4ccw2v_rkfmbs53304d5df4-aac1-6117-552c-7f70c89a40d9" },
41
- },
42
- {
43
- name : "invalid pattern" ,
44
- input : invalidPattern ,
45
- want : nil ,
46
- },
47
- }
48
-
49
- for _ , test := range tests {
50
- t .Run (test .name , func (t * testing.T ) {
51
- matchedDetectors := ahoCorasickCore .FindDetectorMatches ([]byte (test .input ))
52
- if len (matchedDetectors ) == 0 {
53
- t .Errorf ("keywords '%v' not matched by: %s" , d .Keywords (), test .input )
54
- return
55
- }
56
-
57
- results , err := d .FromData (context .Background (), false , []byte (test .input ))
58
- if err != nil {
59
- t .Errorf ("error = %v" , err )
60
- return
61
- }
62
-
63
- if len (results ) != len (test .want ) {
64
- if len (results ) == 0 {
65
- t .Errorf ("did not receive result" )
66
- } else {
67
- t .Errorf ("expected %d results, only received %d" , len (test .want ), len (results ))
68
- }
69
- return
70
- }
71
-
72
- actual := make (map [string ]struct {}, len (results ))
73
- for _ , r := range results {
74
- if len (r .RawV2 ) > 0 {
75
- actual [string (r .RawV2 )] = struct {}{}
76
- } else {
77
- actual [string (r .Raw )] = struct {}{}
78
- }
79
- }
80
- expected := make (map [string ]struct {}, len (test .want ))
81
- for _ , v := range test .want {
82
- expected [v ] = struct {}{}
83
- }
84
-
85
- if diff := cmp .Diff (expected , actual ); diff != "" {
86
- t .Errorf ("%s diff: (-want +got)\n %s" , test .name , diff )
87
- }
88
- })
89
- }
90
- }
91
-
92
9
type testCase struct {
93
10
Input string
94
11
Expected map [string ]struct {}
@@ -155,10 +72,10 @@ configs = {"fs.azure.account.auth.type": "OAuth"`,
155
72
Input : ` "AZUREAD-AKS-APPID-SECRET": "8w__IGsaY.6g6jUxb1.pPGK262._pgX.q-",` ,
156
73
Expected : map [string ]struct {}{"8w__IGsaY.6g6jUxb1.pPGK262._pgX.q-" : {}},
157
74
},
158
- //"client_secret6": {
75
+ // "client_secret6": {
159
76
// Input: ``,
160
77
// Expected: map[string]struct{}{"": {}},
161
- //},
78
+ // },
162
79
163
80
"password" : {
164
81
Input : `# Login using Service Principal
@@ -175,12 +92,12 @@ $Credential = New-Object -TypeName System.Management.Automation.PSCredential -Ar
175
92
az login --service-principal --username {{http://azure-cli-service-principal}} --password {{secret}} --tenant {{someone.onmicrosoft.com}}` ,
176
93
Expected : nil ,
177
94
},
178
- //"client_secret3": {
95
+ // "client_secret3": {
179
96
// Input: ``,
180
97
// Expected: map[string]struct{}{
181
98
// "": {},
182
99
// },
183
- //},
100
+ // },
184
101
}
185
102
186
103
for name , test := range cases {
0 commit comments