-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathpersona_test.go
More file actions
216 lines (199 loc) · 6.29 KB
/
persona_test.go
File metadata and controls
216 lines (199 loc) · 6.29 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
package persona
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
func TestPersona_Pattern(t *testing.T) {
d := Scanner{}
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
tests := []struct {
name string
input string
want []string
}{
{
name: "valid sandbox key",
input: `persona_api_key = "persona_sandbox_550e8400-e29b-41d4-a716-446655440000"`,
want: []string{"persona_sandbox_550e8400-e29b-41d4-a716-446655440000"},
},
{
name: "valid production key",
input: `PERSONA_KEY=persona_production_abcdef01-2345-6789-abcd-ef0123456789`,
want: []string{"persona_production_abcdef01-2345-6789-abcd-ef0123456789"},
},
{
name: "both keys in same input",
input: `
sandbox: persona_sandbox_550e8400-e29b-41d4-a716-446655440000
production: persona_production_abcdef01-2345-6789-abcd-ef0123456789
`,
want: []string{
"persona_sandbox_550e8400-e29b-41d4-a716-446655440000",
"persona_production_abcdef01-2345-6789-abcd-ef0123456789",
},
},
{
name: "truncated UUID - invalid",
input: `key = persona_sandbox_550e8400-e29b-41d4-a716`,
want: nil,
},
{
name: "uppercase hex - invalid",
input: `key = persona_sandbox_550E8400-E29B-41D4-A716-446655440000`,
want: nil,
},
{
name: "wrong prefix - invalid",
input: `key = persona_staging_550e8400-e29b-41d4-a716-446655440000`,
want: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
if len(matchedDetectors) == 0 {
if len(test.want) > 0 {
t.Errorf("test %q failed: expected keywords %v to be found in the input", test.name, 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("mismatch in result count: expected %d, got %d", len(test.want), len(results))
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 TestPersona_Verification(t *testing.T) {
responseBody := `{"data":{"type":"api-key","id":"api_TvSRN76WJ6KsTzZFd4DEUDDF","attributes":{"name":"My Test Key","permissions":["account.read","inquiry.read","inquiry.write"],"expires_at":"2026-12-31T00:00:00.000Z"}}}`
tests := []struct {
name string
input string
client *http.Client
wantVerified bool
wantExtraData map[string]string
wantErr bool
}{
{
name: "verified with full response",
input: "persona_production_abcdef01-2345-6789-abcd-ef0123456789",
client: &http.Client{
Transport: common.FakeTransport{
CreateResponse: func(req *http.Request) (*http.Response, error) {
resp := &http.Response{
Request: req,
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(responseBody)),
Header: make(http.Header),
}
resp.Header.Set("Persona-Organization-Id", "org_abc123")
resp.Header.Set("Persona-Environment-Id", "env_xyz789")
return resp, nil
},
},
},
wantVerified: true,
wantExtraData: map[string]string{
"Type": "Production Key",
"ID": "api_TvSRN76WJ6KsTzZFd4DEUDDF",
"Organization": "org_abc123",
"Environment": "env_xyz789",
"Name": "My Test Key",
"Permissions": "account.read, inquiry.read, inquiry.write",
"Expires_At": "2026-12-31T00:00:00.000Z",
},
},
{
name: "verified sandbox key with minimal response",
input: "persona_sandbox_abcdef01-2345-6789-abcd-ef0123456789",
client: &http.Client{
Transport: common.FakeTransport{
CreateResponse: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Request: req,
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(`{"data":{"type":"api-key","id":"api_min123","attributes":{"permissions":["account.read"]}}}`)),
Header: make(http.Header),
}, nil
},
},
},
wantVerified: true,
wantExtraData: map[string]string{
"Type": "Sandbox Key",
"ID": "api_min123",
"Permissions": "account.read",
},
},
{
name: "unverified - 401",
input: "persona_production_abcdef01-2345-6789-abcd-ef0123456789",
client: common.ConstantResponseHttpClient(http.StatusUnauthorized, `{"errors":[{"title":"Must be authenticated"}]}`),
wantVerified: false,
wantExtraData: map[string]string{
"Type": "Production Key",
},
},
{
name: "unverified - 403",
input: "persona_production_abcdef01-2345-6789-abcd-ef0123456789",
client: common.ConstantResponseHttpClient(http.StatusForbidden, ""),
wantVerified: false,
wantExtraData: map[string]string{
"Type": "Production Key",
},
},
{
name: "error - unexpected status",
input: "persona_production_abcdef01-2345-6789-abcd-ef0123456789",
client: common.ConstantResponseHttpClient(http.StatusInternalServerError, ""),
wantVerified: false,
wantExtraData: map[string]string{
"Type": "Production Key",
},
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := Scanner{client: test.client}
results, err := d.FromData(context.Background(), true, []byte(test.input))
require.NoError(t, err)
require.Len(t, results, 1)
r := results[0]
assert.Equal(t, test.wantVerified, r.Verified)
assert.Equal(t, test.wantExtraData, r.ExtraData)
if test.wantErr {
assert.Error(t, r.VerificationError())
} else {
assert.NoError(t, r.VerificationError())
}
})
}
}