Skip to content

chore: support multi-org group sync with runtime configuration #14578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
99c97c2
wip
Emyrk Sep 3, 2024
bfddeb6
begin group sync main work
Emyrk Sep 3, 2024
f2857c6
initial implementation of group sync
Emyrk Sep 3, 2024
791a059
work on moving to the manager
Emyrk Sep 4, 2024
4326e9d
fixup compile issues
Emyrk Sep 4, 2024
6d3ed2e
fixup some tests
Emyrk Sep 4, 2024
0803619
handle allow list
Emyrk Sep 4, 2024
596e7b4
WIP unit test for group sync
Emyrk Sep 4, 2024
b9476ac
fixup tests, account for existing groups
Emyrk Sep 4, 2024
ee8e4e4
fix compile issues
Emyrk Sep 5, 2024
d5ff0f7
add comment for test helper
Emyrk Sep 5, 2024
86c0f6f
handle legacy params
Emyrk Sep 5, 2024
2f03e18
make gen
Emyrk Sep 5, 2024
ec8092d
cleanup
Emyrk Sep 5, 2024
d63727d
add unit test for legacy behavior
Emyrk Sep 5, 2024
2a1769c
work on batching removal by name or id
Emyrk Sep 5, 2024
640e86e
group sync adjustments
Emyrk Sep 5, 2024
c544a29
test legacy params
Emyrk Sep 5, 2024
476be45
add unit test for ApplyGroupDifference
Emyrk Sep 5, 2024
164aeac
chore: remove old group sync code
Emyrk Sep 5, 2024
986498d
switch oidc test config to deployment values
Emyrk Sep 5, 2024
290cfa5
fix err name
Emyrk Sep 6, 2024
c563b10
some linting cleanup
Emyrk Sep 6, 2024
d2c247f
dbauthz test for new query
Emyrk Sep 6, 2024
12685bd
fixup comments
Emyrk Sep 6, 2024
bf0d4ed
fixup compile issues from rebase
Emyrk Sep 6, 2024
f95128e
add test for disabled sync
Emyrk Sep 6, 2024
88b0ad9
linting
Emyrk Sep 6, 2024
6491f6a
chore: handle db conflicts gracefully
Emyrk Sep 6, 2024
bd23288
test expected group equality
Emyrk Sep 6, 2024
a390ec4
cleanup comments
Emyrk Sep 6, 2024
a0a1c53
spelling mistake
Emyrk Sep 6, 2024
a86ba83
linting:
Emyrk Sep 6, 2024
0df7f28
add interface method to allow api crud
Emyrk Sep 9, 2024
7a802a9
Remove testable example
Emyrk Sep 11, 2024
611f1e3
fix formatting of sql, add a comment
Emyrk Sep 11, 2024
7f28a53
remove function only used in 1 place
Emyrk Sep 11, 2024
41994d2
make fmt
Emyrk Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP unit test for group sync
  • Loading branch information
Emyrk committed Sep 9, 2024
commit 596e7b467feef913f10ff70768783bb6b5f826e5
21 changes: 21 additions & 0 deletions coderd/coderdtest/uuids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package coderdtest

import "github.com/google/uuid"

type DeterministicUUIDGenerator struct {
Named map[string]uuid.UUID
}

func NewDeterministicUUIDGenerator() *DeterministicUUIDGenerator {
return &DeterministicUUIDGenerator{
Named: make(map[string]uuid.UUID),
}
}

func (d *DeterministicUUIDGenerator) ID(name string) uuid.UUID {
if v, ok := d.Named[name]; ok {
return v
}
d.Named[name] = uuid.New()
return d.Named[name]
}
17 changes: 16 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7643,7 +7643,22 @@ func (q *FakeQuerier) RemoveUserFromGroups(ctx context.Context, arg database.Rem
return nil, err
}

panic("not implemented")
q.mutex.Lock()
defer q.mutex.Unlock()

removed := make([]uuid.UUID, 0)
q.data.groupMembers = slices.DeleteFunc(q.data.groupMembers, func(groupMember database.GroupMemberTable) bool {
if groupMember.UserID != arg.UserID {
return false
}
if !slices.Contains(arg.GroupIds, groupMember.GroupID) {
return false
}
removed = append(removed, groupMember.GroupID)
return true
})

return removed, nil
}

func (q *FakeQuerier) RevokeDBCryptKey(_ context.Context, activeKeyDigest string) error {
Expand Down
23 changes: 20 additions & 3 deletions coderd/idpsync/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package idpsync

import (
"context"
"encoding/json"
"regexp"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/runtimeconfig"
"github.com/coder/coder/v2/coderd/util/slice"
)

Expand All @@ -32,7 +34,6 @@ func (s AGPLIDPSync) ParseGroupClaims(_ context.Context, _ jwt.MapClaims) (Group
}, nil
}

// TODO: Group allowlist behavior should probably happen at this step.
func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user database.User, params GroupParams) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enterprise license code just handles the ParseGroupClaims. So AGPL will always be disabled.

I put the actual SyncGroups code in AGPL so that I can unit test and maintain the sync code all in 1 package. It is a convenience, while still making sure AGPL code cannot leverage this feature.

// Nothing happens if sync is not enabled
if !params.SyncEnabled {
Expand All @@ -43,6 +44,8 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
ctx = dbauthz.AsSystemRestricted(ctx)

db.InTx(func(tx database.Store) error {
manager := runtimeconfig.NewStoreManager(tx)

userGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
HasMemberID: user.ID,
})
Expand All @@ -60,12 +63,12 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
// For each org, we need to fetch the sync settings
orgSettings := make(map[uuid.UUID]GroupSyncSettings)
for orgID := range userOrgs {
orgResolver := s.Manager.Scoped(orgID.String())
orgResolver := manager.Scoped(orgID.String())
settings, err := s.SyncSettings.Group.Resolve(ctx, orgResolver)
if err != nil {
return xerrors.Errorf("resolve group sync settings: %w", err)
}
orgSettings[orgID] = settings.Value
orgSettings[orgID] = *settings
}

// collect all diffs to do 1 sql update for all orgs
Expand Down Expand Up @@ -177,6 +180,20 @@ type GroupSyncSettings struct {
AutoCreateMissingGroups bool `json:"auto_create_missing_groups"`
}

func (s *GroupSyncSettings) Set(v string) error {
return json.Unmarshal([]byte(v), s)
}
func (s *GroupSyncSettings) String() string {
v, err := json.Marshal(s)
if err != nil {
return "decode failed: " + err.Error()
}
return string(v)
}
func (s *GroupSyncSettings) Type() string {
return "GroupSyncSettings"
}

type ExpectedGroup struct {
GroupID *uuid.UUID
GroupName *string
Expand Down
Loading