-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathartifactoryreferencetoken_test.go
More file actions
225 lines (212 loc) · 6.72 KB
/
artifactoryreferencetoken_test.go
File metadata and controls
225 lines (212 loc) · 6.72 KB
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
package artifactoryreferencetoken
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
func TestArtifactoryReferenceToken_Pattern(t *testing.T) {
d := Scanner{}
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
tests := []struct {
name string
input string
cloudEndpoint string
useCloudEndpoint bool
useFoundEndpoint bool
want []string
}{
{
name: "valid pattern - environment variable",
input: `
[INFO] Connecting to Artifactory
[DEBUG] Using reference token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] Connected to trufflehog.jfrog.io
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEtrufflehog.jfrog.io",
},
},
{
name: "valid pattern - config file",
input: `
artifactory:
url: https://trufflehog.jfrog.io
reference_token: cmVmdGtuOjAxOjE3NjkxNjY0NjE6RE2ZeXpdsU1sOENUUG1RqXqDawNeMrJaTapu
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: []string{
"cmVmdGtuOjAxOjE3NjkxNjY0NjE6RE2ZeXpdsU1sOENUUG1RqXqDawNeMrJaTaputrufflehog.jfrog.io",
},
},
{
name: "valid pattern - curl command",
input: `
curl -H "Authorization: Bearer cmVmdGtuOjAxOjE3NzE0OTkzNzY6RG9OS0QxOHVLduRyyUtNrneMwqt6a33TNUZV" \
https://trufflehog.jfrog.io/artifactory/api/system/ping
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: []string{
"cmVmdGtuOjAxOjE3NzE0OTkzNzY6RG9OS0QxOHVLduRyyUtNrneMwqt6a33TNUZVtrufflehog.jfrog.io",
},
},
{
name: "valid pattern - with cloud endpoint",
input: `
[INFO] Connecting to Artifactory
[DEBUG] Using reference token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] Response received: 200 OK
`,
cloudEndpoint: "cloudendpoint.jfrog.io",
useCloudEndpoint: true,
useFoundEndpoint: false,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEcloudendpoint.jfrog.io",
},
},
{
name: "valid pattern - with cloud and found endpoints",
input: `
[INFO] Connecting to Artifactory
[DEBUG] Using reference token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] trufflehog.jfrog.io
[INFO] Response received: 200 OK
`,
cloudEndpoint: "cloudendpoint.jfrog.io",
useCloudEndpoint: true,
useFoundEndpoint: true,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEcloudendpoint.jfrog.io",
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEtrufflehog.jfrog.io",
},
},
{
name: "valid pattern - with disabled found endpoints",
input: `
[INFO] Connecting to Artifactory
[DEBUG] Using reference token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] trufflehog.jfrog.io
[INFO] Response received: 200 OK
`,
cloudEndpoint: "cloudendpoint.jfrog.io",
useCloudEndpoint: true,
useFoundEndpoint: false,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEcloudendpoint.jfrog.io",
},
},
{
name: "valid pattern - with https in configured endpoint",
input: `
[INFO] Connecting to Artifactory
[DEBUG] Using reference token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] Response received: 200 OK
`,
cloudEndpoint: "https://cloudendpoint.jfrog.io",
useCloudEndpoint: true,
useFoundEndpoint: false,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEcloudendpoint.jfrog.io",
},
},
{
name: "finds multiple tokens",
input: `
# Primary token
export ARTIFACTORY_TOKEN=cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
# Backup token
export ARTIFACTORY_TOKEN_BACKUP=cmVmdGtuOjAxOjE3NjkxNjY0NjE6RE2ZeXpdsU1sOENUUG1RqXqDawNeMrJaTapu
export ARTIFACTORY_URL=https://trufflehog.jfrog.io
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: []string{
"cmVmdGtuOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtEtrufflehog.jfrog.io",
"cmVmdGtuOjAxOjE3NjkxNjY0NjE6RE2ZeXpdsU1sOENUUG1RqXqDawNeMrJaTaputrufflehog.jfrog.io",
},
},
{
name: "invalid pattern - too short",
input: `
[DEBUG] Using token: cmVmdGtuOjAxOjAwMDAwMDAwMDA6SHORT
[INFO] URL: trufflehog.jfrog.io
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: nil,
},
{
name: "invalid pattern - wrong prefix",
input: `
[DEBUG] Using token: aBcDeFgHOjAxOjAwMDAwMDAwMDA6awJQVlZkdEVyWXJ2cVNSemAABVQ1bwaBSWtE
[INFO] URL: trufflehog.jfrog.io
`,
useCloudEndpoint: false,
useFoundEndpoint: true,
want: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Configure endpoint customizer based on test case
d.UseFoundEndpoints(test.useFoundEndpoint)
d.UseCloudEndpoint(test.useCloudEndpoint)
if test.useCloudEndpoint && test.cloudEndpoint != "" {
d.SetCloudEndpoint(test.cloudEndpoint)
}
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
if len(matchedDetectors) == 0 && len(test.want) > 0 {
t.Errorf("keywords were not matched: %v", d.Keywords())
return
}
results, err := d.FromData(context.Background(), false, []byte(test.input))
require.NoError(t, err)
if len(results) != len(test.want) {
t.Errorf("expected %d results, got %d", len(test.want), len(results))
for _, r := range results {
t.Logf("got: %s", string(r.RawV2))
}
return
}
actual := make(map[string]struct{}, len(results))
for _, r := range results {
if len(r.RawV2) > 0 {
actual[string(r.RawV2)] = struct{}{}
} else {
actual[string(r.Raw)] = struct{}{}
}
}
expected := make(map[string]struct{}, len(test.want))
for _, v := range test.want {
expected[v] = struct{}{}
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
}
})
}
}
func TestArtifactoryreferencetoken_Endpoint_Contains_CustomEndpoint(t *testing.T) {
appURL := "example.com"
s := Scanner{}
s.UseFoundEndpoints(true)
err := s.SetConfiguredEndpoints(appURL)
if err != nil {
t.Fatal("Error in setting configured endpoint")
}
configuredEndpoints := s.Endpoints()
if len(configuredEndpoints) == 0 {
t.Fatal("No Confiured endpoint found")
}
for _, ep := range configuredEndpoints {
if ep != appURL {
t.Fatalf("expected endpoint %s to be present in endpoints list, got %s", appURL, ep)
}
}
}