This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsub_repo_permissions_test.go
More file actions
389 lines (354 loc) · 10.5 KB
/
sub_repo_permissions_test.go
File metadata and controls
389 lines (354 loc) · 10.5 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/sourcegraph/internal/gqltestutil"
"github.com/sourcegraph/sourcegraph/schema"
)
const (
perforceRepoName = "perforce/test-perms"
testPermsDepot = "test-perms"
aliceEmail = "alice@perforce.sgdev.org"
aliceUsername = "alice"
)
func TestSubRepoPermissionsPerforce(t *testing.T) {
checkPerforceEnvironment(t)
enableSubRepoPermissions(t)
cleanup := createPerforceExternalService(t, testPermsDepot, false)
t.Cleanup(cleanup)
userClient, repoName := createTestUserAndWaitForRepo(t)
// Test cases
// flaky test
t.Run("can read README.md", func(t *testing.T) {
t.Skip("skipping because flaky")
blob, err := userClient.GitBlob(repoName, "master", "README.md")
if err != nil {
t.Fatal(err)
}
wantBlob := `This depot is used to test user and group permissions.
`
if diff := cmp.Diff(wantBlob, blob); diff != "" {
t.Fatalf("Blob mismatch (-want +got):\n%s", diff)
}
})
t.Run("cannot read hack.sh", func(t *testing.T) {
// Should not be able to read hack.sh
blob, err := userClient.GitBlob(repoName, "master", "Security/hack.sh")
if err != nil {
t.Fatal(err)
}
// This is the desired behaviour at the moment, see where we check for
// os.IsNotExist error in GitCommitResolver.Blob
wantBlob := ``
if diff := cmp.Diff(wantBlob, blob); diff != "" {
t.Fatalf("Blob mismatch (-want +got):\n%s", diff)
}
})
// flaky test
t.Run("file list excludes excluded files", func(t *testing.T) {
t.Skip("skipping because flaky")
files, err := userClient.GitListFilenames(repoName, "master")
if err != nil {
t.Fatal(err)
}
// Notice that Security/hack.sh is excluded
wantFiles := []string{
"Backend/main.go",
"Frontend/app.ts",
"README.md",
}
if diff := cmp.Diff(wantFiles, files); diff != "" {
t.Fatalf("fileNames mismatch (-want +got):\n%s", diff)
}
})
}
func TestSubRepoPermissionsSearch(t *testing.T) {
checkPerforceEnvironment(t)
enableSubRepoPermissions(t)
cleanup := createPerforceExternalService(t, testPermsDepot, false)
t.Cleanup(cleanup)
userClient, _ := createTestUserAndWaitForRepo(t)
err := client.WaitForReposToBeIndexed(perforceRepoName)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
query string
zeroResult bool
minMatchCount int64
}{
{
name: "indexed search, nonzero result",
query: `index:only This depot is used to test`,
minMatchCount: 1,
},
{
name: "unindexed multiline search, nonzero result",
query: `index:no This depot is used to test`,
minMatchCount: 1,
},
{
name: "indexed search of restricted content",
query: `index:only uploading your secrets`,
zeroResult: true,
},
{
name: "unindexed search of restricted content",
query: `index:no uploading your secrets`,
zeroResult: true,
},
{
name: "structural, indexed search of restricted content",
query: `repo:^perforce/test-perms$ echo "..." index:only patterntype:structural`,
zeroResult: true,
},
{
name: "structural, unindexed search of restricted content",
query: `repo:^perforce/test-perms$ echo "..." index:no patterntype:structural`,
zeroResult: true,
},
{
name: "structural, indexed search, nonzero result",
query: `println(...) index:only patterntype:structural`,
minMatchCount: 1,
},
{
name: "structural, unindexed search, nonzero result",
query: `println(...) index:no patterntype:structural`,
minMatchCount: 1,
},
{
name: "filename search, nonzero result",
query: `repo:^perforce/test-perms$ type:path app`,
minMatchCount: 1,
},
{
name: "filename search of restricted content",
query: `repo:^perforce/test-perms$ type:path hack`,
zeroResult: true,
},
{
name: "content search, nonzero result",
query: `repo:^perforce/test-perms$ type:file let`,
minMatchCount: 1,
},
{
name: "content search of restricted content",
query: `repo:^perforce/test-perms$ type:file echo`,
zeroResult: true,
},
{
name: "diff search, nonzero result",
query: `repo:^perforce/test-perms$ type:diff let`,
minMatchCount: 1,
},
{
name: "diff search of restricted content",
query: `repo:^perforce/test-perms$ type:diff echo`,
zeroResult: true,
},
{
name: "symbol search, nonzero result",
query: `repo:^perforce/test-perms$ type:symbol main`,
minMatchCount: 1,
},
{
name: "symbol search of restricted content",
query: `repo:^perforce/test-perms$ type:symbol asdf`,
zeroResult: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
results, err := userClient.SearchFiles(test.query)
if err != nil {
t.Fatal(err)
}
if test.zeroResult {
if len(results.Results) > 0 {
t.Fatalf("Want zero result but got %d", len(results.Results))
}
} else {
if len(results.Results) == 0 {
t.Fatal("Want non-zero results but got 0")
}
}
if results.MatchCount < test.minMatchCount {
t.Fatalf("Want at least %d match count but got %d", test.minMatchCount, results.MatchCount)
}
})
}
t.Run("commit search admin", func(t *testing.T) {
results, err := client.SearchCommits(`repo:^perforce/test-perms$ type:commit`)
if err != nil {
t.Fatal(err)
}
// Admin should have access to ALL commits: there are 6 total
commitsNumber := len(results.Results)
expectedCommitsNumber := 6
if commitsNumber != expectedCommitsNumber {
t.Fatalf("Should have access to %d commits but got %d", expectedCommitsNumber, commitsNumber)
}
})
t.Run("commit search", func(t *testing.T) {
results, err := userClient.SearchCommits(`repo:^perforce/test-perms$ type:commit`)
if err != nil {
t.Fatal(err)
}
// Alice should have access to only 3 commits at the moment
commitsNumber := len(results.Results)
expectedCommitsNumber := 3
if commitsNumber != expectedCommitsNumber {
t.Fatalf("Should have access to %d commits but got %d", expectedCommitsNumber, commitsNumber)
}
})
commitAccessTests := []struct {
name string
revision string
hasAccess bool
}{
{
name: "direct access to inaccessible commit",
revision: "87440329a7bae580b90280aaaafdc14ee7c1f8ef",
},
{
name: "direct access to accessible commit",
revision: "36d7eda16b9a881ef153126a4036efc4f6afb0c1",
hasAccess: true,
},
}
for _, test := range commitAccessTests {
t.Run(test.name, func(t *testing.T) {
_, err := userClient.GitGetCommitMessage(perforceRepoName, test.revision)
if err != nil {
if test.hasAccess {
t.Fatal(err)
}
} else {
if !test.hasAccess {
t.Fatal("No error during accessing restricted commit")
}
}
})
}
t.Run("archive repo", func(t *testing.T) {
url := fmt.Sprintf("%s/%s/-/raw/", *baseURL, perforceRepoName)
response, err := userClient.GetWithHeaders(url, map[string][]string{"Accept": {"application/zip"}})
if err != nil {
t.Fatal(err)
}
if response.StatusCode == http.StatusOK {
t.Fatalf("Should not be able to get an archive of repo with enabled sub-repo perms")
}
})
t.Run("code intel search", func(t *testing.T) {
result, err := userClient.SearchFiles("context:global \\bhack1337\\b type:file patternType:regexp count:500 case:yes file:\\.(go)$ repo:^perforce/test-perms$@8574314b8de445ec652cab87cbaa1a8dbe6ba6c4")
if err != nil {
t.Fatal(err)
}
for _, file := range result.Results {
if strings.HasPrefix(file.File.Name, "hack") {
t.Fatal("Should not find references for restricted files")
}
}
})
}
func createTestUserAndWaitForRepo(t *testing.T) (*gqltestutil.Client, string) {
t.Helper()
// We need to create the `alice` user with a specific e-mail address. This user is
// configured on our dogfood perforce instance with limited access to the
// test-perms depot.
// Alice has access to root, Backend and Frontend directories. (there are .md, .ts and .go files)
// Alice doesn't have access to Security directory. (there is a .sh file)
alicePassword := "alicessupersecurepassword"
t.Log("Creating Alice")
userClient, err := gqltestutil.SignUpOrSignIn(*baseURL, aliceEmail, aliceUsername, alicePassword)
if err != nil {
t.Fatal(err)
}
aliceID := userClient.AuthenticatedUserID()
removeTestUserAfterTest(t, aliceID)
if err := client.SetUserEmailVerified(aliceID, aliceEmail, true); err != nil {
t.Fatal(err)
}
err = userClient.WaitForReposToBeCloned(perforceRepoName)
if err != nil {
t.Fatal(err)
}
syncUserPerms(t, aliceID, aliceUsername)
return userClient, perforceRepoName
}
func syncUserPerms(t *testing.T, userID, userName string) {
t.Helper()
err := client.ScheduleUserPermissionsSync(userID)
if err != nil {
t.Fatal(err)
}
// Wait up to 30 seconds for the user to have permissions synced
// from the code host at least once.
err = gqltestutil.Retry(30*time.Second, func() error {
userPermsInfo, err := client.UserPermissionsInfo(userName)
if err != nil {
t.Fatal(err)
}
if userPermsInfo != nil && !userPermsInfo.SyncedAt.IsZero() {
return nil
}
return gqltestutil.ErrContinueRetry
})
if err != nil {
t.Fatal("Waiting for user permissions to be synced:", err)
}
// Wait up to 30 seconds for Perforce to be added as an authz provider
err = gqltestutil.Retry(30*time.Second, func() error {
authzProviders, err := client.AuthzProviderTypes()
if err != nil {
t.Fatal("failed to fetch list of authz providers", err)
}
if len(authzProviders) != 0 {
for _, p := range authzProviders {
if p == "perforce" {
return nil
}
}
}
return gqltestutil.ErrContinueRetry
})
if err != nil {
t.Fatal("Waiting for authz providers to be added:", err)
}
}
func enableSubRepoPermissions(t *testing.T) {
t.Helper()
siteConfig, lastID, err := client.SiteConfiguration()
if err != nil {
t.Fatal(err)
}
oldSiteConfig := new(schema.SiteConfiguration)
*oldSiteConfig = *siteConfig
t.Cleanup(func() {
_, lastID, err := client.SiteConfiguration()
if err != nil {
t.Fatal(err)
}
err = client.UpdateSiteConfiguration(oldSiteConfig, lastID)
if err != nil {
t.Fatal(err)
}
})
siteConfig.ExperimentalFeatures = &schema.ExperimentalFeatures{
Perforce: "enabled",
SubRepoPermissions: &schema.SubRepoPermissions{
Enabled: true,
},
}
err = client.UpdateSiteConfiguration(siteConfig, lastID)
if err != nil {
t.Fatal(err)
}
}