Skip to content

Commit b3022c2

Browse files
committed
rolesync start
1 parent 2df9a3e commit b3022c2

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

coderd/idpsync/idpsync.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/coderd/runtimeconfig"
1717
"github.com/coder/coder/v2/codersdk"
1818
"github.com/coder/coder/v2/site"
19+
"github.com/coder/serpent"
1920
)
2021

2122
// IDPSync is an interface, so we can implement this as AGPL and as enterprise,
@@ -75,6 +76,11 @@ type DeploymentSyncSettings struct {
7576
GroupAllowList map[string]struct{}
7677
// Legacy deployment settings that only apply to the default org.
7778
Legacy DefaultOrgLegacySettings
79+
80+
// SiteRoleField syncs a user's site wide roles from an IDP.
81+
SiteRoleField string
82+
SiteRoleMapping serpent.Struct[map[string][]string]
83+
SiteDefaultRoles []string
7884
}
7985

8086
type DefaultOrgLegacySettings struct {
@@ -111,6 +117,7 @@ type SyncSettings struct {
111117
DeploymentSyncSettings
112118

113119
Group runtimeconfig.RuntimeEntry[*GroupSyncSettings]
120+
Role runtimeconfig.RuntimeEntry[*RoleSyncSettings]
114121
}
115122

116123
func NewAGPLSync(logger slog.Logger, manager *runtimeconfig.Manager, settings DeploymentSyncSettings) *AGPLIDPSync {

coderd/idpsync/role.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package idpsync
2+
3+
import (
4+
"context"
5+
6+
"github.com/golang-jwt/jwt/v4"
7+
8+
"github.com/coder/coder/v2/coderd/database"
9+
"github.com/coder/coder/v2/coderd/database/dbauthz"
10+
"github.com/coder/coder/v2/coderd/runtimeconfig"
11+
)
12+
13+
type RoleParams struct {
14+
// SyncEnabled if false will skip syncing the user's roles
15+
SyncEnabled bool
16+
MergedClaims jwt.MapClaims
17+
}
18+
19+
func (AGPLIDPSync) RoleSyncEnabled() bool {
20+
// AGPL does not support syncing groups.
21+
return false
22+
}
23+
func (s AGPLIDPSync) RoleSyncSettings() runtimeconfig.RuntimeEntry[*RoleSyncSettings] {
24+
return s.Role
25+
}
26+
27+
func (s AGPLIDPSync) ParseRoleClaims(_ context.Context, _ jwt.MapClaims) (RoleParams, *HTTPError) {
28+
return RoleParams{
29+
SyncEnabled: s.RoleSyncEnabled(),
30+
}, nil
31+
}
32+
33+
func (s AGPLIDPSync) SyncRoles(ctx context.Context, db database.Store, user database.User, params RoleParams) error {
34+
// Nothing happens if sync is not enabled
35+
if !params.SyncEnabled {
36+
return nil
37+
}
38+
39+
// nolint:gocritic // all syncing is done as a system user
40+
ctx = dbauthz.AsSystemRestricted(ctx)
41+
42+
return nil
43+
}
44+
45+
type RoleSyncSettings struct {
46+
// Field selects the claim field to be used as the created user's
47+
// groups. If the group field is the empty string, then no group updates
48+
// will ever come from the OIDC provider.
49+
Field string `json:"field"`
50+
// Mapping maps from an OIDC group --> Coder organization role
51+
Mapping map[string][]string `json:"mapping"`
52+
}

0 commit comments

Comments
 (0)