Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 2a6dd1f

Browse files
[Backport 5.1] [permissions] Revert default behaviour for explicit permissions (#54439)
With the switch to the `user_repo_permissions` table, the default behaviour for explicit permissions changed. Previously, if explicit permissions API was enabled, then all repositories would be treated as restricted, regardless of code host connection configurations. The `user_repo_permissions` table allows us to run permissions syncs and explicit permissions in parallel, but the default behaviour was to have code host connections be marked as unrestricted, unless authorization is enabled. However, not all code hosts support authorization, leading to connections that are always left unrestricted, even if explicit permissions are being used. This PR reverts the default behaviour back to treating a repository as restricted if explicit permissions are enabled. ## Test plan Added unit tests for the new Unrestricted logic. Also tested locally with an &quot;Other Git code host&quot; connection. &lt;!-- All pull requests REQUIRE a test plan: https://docs.sourcegraph.com/dev/background-information/testing_principles --&gt; <br> Backport 5ad2474 from #54419 Co-authored-by: Petri-Johan Last <[email protected]>
1 parent f8ae03d commit 2a6dd1f

File tree

11 files changed

+76
-11
lines changed

11 files changed

+76
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ All notable changes to Sourcegraph are documented in this file.
2626

2727
### Fixed
2828

29-
-
29+
- Fixed the default behaviour when the explicit permissions API is enabled. Repositories are no longer marked as unrestricted by default. [#54419](https://github.com/sourcegraph/sourcegraph/pull/54419)
3030

3131
### Removed
3232

doc/admin/permissions/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ of `alice` are the following union set: [`horsegraph/global`, `horsegraph/hay-v1
112112
1. Go to **Site Admin > Migrations** page. There is a migration called `Migrate data from user_permissions table to unified user_repo_permissions.`.
113113
Make sure that it finished migrating all the data (it reports as 100%). Contact support if the migration does not seem to complete for a long time (multiple days).
114114

115-
1. Enable the experimental feature in the [site configuration](../config/site_config.md):
115+
1. (Not required for Sourcegraph 5.1+) Enable the experimental feature in the [site configuration](../config/site_config.md):
116116
```json
117117
{
118118
"experimentalFeatures": {
@@ -122,8 +122,6 @@ Make sure that it finished migrating all the data (it reports as 100%). Contact
122122
}
123123
```
124124
1. Continue [configuring the explicit permissions API](api.md#configuration) as you would before.
125-
Both mechanisms work at the same time, thanks to a new behind the scenes data model that allows
126-
what was previously impossible.
127125

128126
### Permission updates
129127

internal/database/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/external_services.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/sourcegraph/log"
1919

2020
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
21+
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
2122
"github.com/sourcegraph/sourcegraph/internal/conf"
2223
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
2324
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
@@ -1578,8 +1579,12 @@ func (e *externalServiceStore) recalculateFields(es *types.ExternalService, rawC
15781579
// For existing auth providers, this is forwards compatible. While at the same time if they also
15791580
// wanted to get on the `enforcePermissions` pattern, this change is backwards compatible.
15801581
enforcePermissions := gjson.Get(rawConfig, "enforcePermissions")
1581-
if !envvar.SourcegraphDotComMode() && enforcePermissions.Exists() {
1582-
es.Unrestricted = !enforcePermissions.Bool()
1582+
if !envvar.SourcegraphDotComMode() {
1583+
if globals.PermissionsUserMapping().Enabled {
1584+
es.Unrestricted = false
1585+
} else if enforcePermissions.Exists() {
1586+
es.Unrestricted = !enforcePermissions.Bool()
1587+
}
15831588
}
15841589

15851590
hasWebhooks := false

internal/database/external_services_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
"github.com/sourcegraph/log"
2222

2323
"github.com/sourcegraph/sourcegraph/internal/api"
24+
"github.com/sourcegraph/sourcegraph/internal/jsonc"
2425
"github.com/sourcegraph/sourcegraph/internal/timeutil"
2526
"github.com/sourcegraph/sourcegraph/lib/errors"
2627
"github.com/sourcegraph/sourcegraph/lib/pointers"
2728

2829
"github.com/sourcegraph/log/logtest"
2930

3031
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
32+
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
3133
"github.com/sourcegraph/sourcegraph/internal/actor"
3234
"github.com/sourcegraph/sourcegraph/internal/conf"
3335
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
@@ -2389,6 +2391,61 @@ func TestConfigurationHasWebhooks(t *testing.T) {
23892391
})
23902392
}
23912393

2394+
func TestExternalServiceStore_recalculateFields(t *testing.T) {
2395+
tests := map[string]struct {
2396+
explicitPermsEnabled bool
2397+
authorizationSet bool
2398+
expectUnrestricted bool
2399+
}{
2400+
"default state": {
2401+
expectUnrestricted: true,
2402+
},
2403+
"explicit perms set": {
2404+
explicitPermsEnabled: true,
2405+
expectUnrestricted: false,
2406+
},
2407+
"authorization set": {
2408+
authorizationSet: true,
2409+
expectUnrestricted: false,
2410+
},
2411+
"authorization and explicit perms set": {
2412+
explicitPermsEnabled: true,
2413+
authorizationSet: true,
2414+
expectUnrestricted: false,
2415+
},
2416+
}
2417+
2418+
e := &externalServiceStore{logger: logtest.NoOp(t)}
2419+
2420+
for name, tc := range tests {
2421+
t.Run(name, func(t *testing.T) {
2422+
pmu := globals.PermissionsUserMapping()
2423+
t.Cleanup(func() {
2424+
globals.SetPermissionsUserMapping(pmu)
2425+
})
2426+
2427+
es := &types.ExternalService{}
2428+
2429+
if tc.explicitPermsEnabled {
2430+
globals.SetPermissionsUserMapping(&schema.PermissionsUserMapping{
2431+
BindID: "email",
2432+
Enabled: true,
2433+
})
2434+
}
2435+
rawConfig := "{}"
2436+
var err error
2437+
if tc.authorizationSet {
2438+
rawConfig, err = jsonc.Edit(rawConfig, struct{}{}, "authorization")
2439+
require.NoError(t, err)
2440+
}
2441+
2442+
require.NoError(t, e.recalculateFields(es, rawConfig))
2443+
2444+
require.Equal(t, es.Unrestricted, tc.expectUnrestricted)
2445+
})
2446+
}
2447+
}
2448+
23922449
func TestExternalServiceStore_ListRepos(t *testing.T) {
23932450
if testing.Short() {
23942451
t.Skip()

internal/repos/awscodecommit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (s *AWSCodeCommitSource) makeRepo(r *awscodecommit.Repository) *types.Repo
147147
},
148148
},
149149
Metadata: r,
150+
Private: !s.svc.Unrestricted,
150151
}
151152
}
152153

internal/repos/gitolite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,6 @@ func (s GitoliteSource) makeRepo(repo *gitolite.Repo) *types.Repo {
123123
},
124124
},
125125
Metadata: repo,
126+
Private: !s.svc.Unrestricted,
126127
}
127128
}

internal/repos/other.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ func (s OtherSource) otherRepoFromCloneURL(urn string, u *url.URL) (*types.Repo,
198198
Metadata: &extsvc.OtherRepoMetadata{
199199
RelativePath: strings.TrimPrefix(repoURL, serviceID),
200200
},
201+
Private: !s.svc.Unrestricted,
201202
}, nil
202203
}
203204

internal/repos/phabricator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (s *PhabricatorSource) makeRepo(repo *phabricator.Repo) (*types.Repo, error
168168
},
169169
},
170170
Metadata: repo,
171+
Private: !s.svc.Unrestricted,
171172
}, nil
172173
}
173174

schema/schema.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)