This repository has been 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.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 5.1] Add feature flags to perms syncer context (#56501)
Add feature flags to perms syncer context (#56492) (cherry picked from commit a29b68f) Co-authored-by: Petri-Johan Last <[email protected]>
- Loading branch information
1 parent
27596b4
commit a08566b
Showing
7 changed files
with
427 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ import ( | |
"github.com/google/go-cmp/cmp" | ||
"github.com/grafana/regexp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/sourcegraph/log/logtest" | ||
|
||
"github.com/sourcegraph/sourcegraph/internal/actor" | ||
"github.com/sourcegraph/sourcegraph/internal/api" | ||
"github.com/sourcegraph/sourcegraph/internal/authz" | ||
authzGitHub "github.com/sourcegraph/sourcegraph/internal/authz/providers/github" | ||
authzGitLab "github.com/sourcegraph/sourcegraph/internal/authz/providers/gitlab" | ||
"github.com/sourcegraph/sourcegraph/internal/database" | ||
"github.com/sourcegraph/sourcegraph/internal/database/dbtest" | ||
"github.com/sourcegraph/sourcegraph/internal/extsvc" | ||
|
@@ -489,3 +491,151 @@ func TestIntegration_GitHubPermissions(t *testing.T) { | |
}) | ||
}) | ||
} | ||
|
||
func TestIntegration_GitLabPermissions(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip() | ||
} | ||
|
||
logger := logtest.Scoped(t) | ||
token := os.Getenv("GITLAB_TOKEN") | ||
|
||
spec := extsvc.AccountSpec{ | ||
ServiceType: extsvc.TypeGitLab, | ||
ServiceID: "https://gitlab.sgdev.org/", | ||
AccountID: "107564", | ||
} | ||
svc := types.ExternalService{ | ||
Kind: extsvc.KindGitLab, | ||
Config: extsvc.NewUnencryptedConfig(`{"url": "https://gitlab.sgdev.org", "authorization": {"identityProvider": {"type": "oauth"}}, "token": "abc", "projectQuery": [ "projects?membership=true&archived=no" ]}`), | ||
} | ||
uri, err := url.Parse("https://gitlab.sgdev.org") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
newUser := database.NewUser{ | ||
Email: "[email protected]", | ||
Username: "sourcegraph-vcr", | ||
EmailIsVerified: true, | ||
} | ||
|
||
// These tests require two repos to be set up: | ||
// Both schwifty2 and getschwifty are internal projects. | ||
// The user is an explicit collaborator on getschwifty, so | ||
// should have access to getschwifty regardless of the feature flag. | ||
// The user does not have explicit access to schwifty2, however | ||
// schwifty2 is configured so that anyone on the instance has read | ||
// access, so when the feature flag is enabled, the user should | ||
// see this repo as well. | ||
testRepos := []types.Repo{ | ||
{ | ||
Name: "gitlab.sgdev.org/petrissupercoolgroup/schwifty2", | ||
Private: true, | ||
URI: "gitlab.sgdev.org/petrissupercoolgroup/schwifty2", | ||
ExternalRepo: api.ExternalRepoSpec{ | ||
ID: "371335", | ||
ServiceType: extsvc.TypeGitLab, | ||
ServiceID: "https://gitlab.sgdev.org/", | ||
}, | ||
Sources: map[string]*types.SourceInfo{ | ||
svc.URN(): { | ||
ID: svc.URN(), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "gitlab.sgdev.org/petri.last/getschwifty", | ||
Private: true, | ||
URI: "gitlab.sgdev.org/petri.last/getschwifty", | ||
ExternalRepo: api.ExternalRepoSpec{ | ||
ID: "371334", | ||
ServiceType: extsvc.TypeGitLab, | ||
ServiceID: "https://gitlab.sgdev.org/", | ||
}, | ||
Sources: map[string]*types.SourceInfo{ | ||
svc.URN(): { | ||
ID: svc.URN(), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
authData := json.RawMessage(fmt.Sprintf(`{"access_token": "%s"}`, token)) | ||
|
||
// This integration tests performs a user-centric permissions syncing against | ||
// https://gitlab.sgdev.org, then check if permissions are correctly granted for the test | ||
// user "sourcegraph-vcr". | ||
t.Run("test gitLabProjectVisibilityExperimental feature flag", func(t *testing.T) { | ||
name := t.Name() | ||
|
||
cf, save := httptestutil.NewRecorderFactory(t, update(name), name) | ||
defer save() | ||
doer, err := cf.Doer() | ||
require.NoError(t, err) | ||
|
||
testDB := database.NewDB(logger, dbtest.NewDB(logger, t)) | ||
|
||
ctx := actor.WithInternalActor(context.Background()) | ||
|
||
reposStore := repos.NewStore(logtest.Scoped(t), testDB) | ||
|
||
err = reposStore.ExternalServiceStore().Upsert(ctx, &svc) | ||
require.NoError(t, err) | ||
|
||
provider := authzGitLab.NewOAuthProvider(authzGitLab.OAuthProviderOp{ | ||
BaseURL: uri, | ||
DB: testDB, | ||
CLI: doer, | ||
}) | ||
|
||
authz.SetProviders(false, []authz.Provider{provider}) | ||
defer authz.SetProviders(true, nil) | ||
for _, repo := range testRepos { | ||
err = reposStore.RepoStore().Create(ctx, &repo) | ||
require.NoError(t, err) | ||
} | ||
|
||
user, err := testDB.UserExternalAccounts().CreateUserAndSave(ctx, newUser, spec, extsvc.AccountData{ | ||
AuthData: extsvc.NewUnencryptedData(authData), | ||
}) | ||
require.NoError(t, err) | ||
|
||
permsStore := database.Perms(logger, testDB, timeutil.Now) | ||
syncer := NewPermsSyncer(logger, testDB, reposStore, permsStore, timeutil.Now) | ||
|
||
assertUserPermissions := func(t *testing.T, wantIDs []int32) { | ||
t.Helper() | ||
_, providerStates, err := syncer.syncUserPerms(ctx, user.ID, false, authz.FetchPermsOptions{}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, database.CodeHostStatusesSet{{ | ||
ProviderID: "https://gitlab.sgdev.org/", | ||
ProviderType: "gitlab", | ||
Status: database.CodeHostStatusSuccess, | ||
Message: "FetchUserPerms", | ||
}}, providerStates) | ||
|
||
p, err := permsStore.LoadUserPermissions(ctx, user.ID) | ||
require.NoError(t, err) | ||
|
||
gotIDs := make([]int32, len(p)) | ||
for i, perm := range p { | ||
gotIDs[i] = perm.RepoID | ||
} | ||
|
||
if diff := cmp.Diff(wantIDs, gotIDs); diff != "" { | ||
t.Fatalf("IDs mismatch (-want +got):\n%s", diff) | ||
} | ||
} | ||
|
||
// With the feature flag disabled (default state) the user should only have access to one repo | ||
assertUserPermissions(t, []int32{2}) | ||
|
||
// With the feature flag enabled the user should have access to both repositories | ||
_, err = testDB.FeatureFlags().CreateBool(ctx, "gitLabProjectVisibilityExperimental", true) | ||
require.NoError(t, err, "feature flag creation failed") | ||
|
||
assertUserPermissions(t, []int32{1, 2}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.