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
work on batching removal by name or id
  • Loading branch information
Emyrk committed Sep 9, 2024
commit 2a1769c7fdcd34b5c7a82f335d7f1e4f05b696ce
41 changes: 29 additions & 12 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,17 @@ func (q *FakeQuerier) getWorkspaceResourcesByJobIDNoLock(_ context.Context, jobI
return resources, nil
}

func (q *FakeQuerier) getGroupByNameNoLock(arg database.NameOrganizationPair) (database.Group, error) {
for _, group := range q.groups {
if group.OrganizationID == arg.OrganizationID &&
group.Name == arg.Name {
return group, nil
}
}

return database.Group{}, sql.ErrNoRows
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm very curious what this is necessary for

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I originally was using it in another query. Wanted to reuse the same code. Now it's not necessary to extract to it's own function 🤷‍♂️. I'll put it back.


func (q *FakeQuerier) getGroupByIDNoLock(_ context.Context, id uuid.UUID) (database.Group, error) {
for _, group := range q.groups {
if group.ID == id {
Expand Down Expand Up @@ -2613,14 +2624,10 @@ func (q *FakeQuerier) GetGroupByOrgAndName(_ context.Context, arg database.GetGr
q.mutex.RLock()
defer q.mutex.RUnlock()

for _, group := range q.groups {
if group.OrganizationID == arg.OrganizationID &&
group.Name == arg.Name {
return group, nil
}
}

return database.Group{}, sql.ErrNoRows
return q.getGroupByNameNoLock(database.NameOrganizationPair{
Name: arg.Name,
OrganizationID: arg.OrganizationID,
})
}

func (q *FakeQuerier) GetGroupMembers(ctx context.Context) ([]database.GroupMember, error) {
Expand Down Expand Up @@ -7648,14 +7655,24 @@ func (q *FakeQuerier) RemoveUserFromGroups(_ context.Context, arg database.Remov

removed := make([]uuid.UUID, 0)
q.data.groupMembers = slices.DeleteFunc(q.data.groupMembers, func(groupMember database.GroupMemberTable) bool {
// Delete all group members that match the arguments.
if groupMember.UserID != arg.UserID {
// Not the right user, ignore.
return false
}
if !slices.Contains(arg.GroupIds, groupMember.GroupID) {
return false

matchesByID := slices.Contains(arg.GroupIds, groupMember.GroupID)
matchesByName := slices.ContainsFunc(arg.GroupNames, func(name database.NameOrganizationPair) bool {
_, err := q.getGroupByNameNoLock(name)
return err == nil
})

if matchesByName || matchesByID {
removed = append(removed, groupMember.GroupID)
return true
}
removed = append(removed, groupMember.GroupID)
return true

return false
})

return removed, nil
Expand Down
19 changes: 15 additions & 4 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion coderd/database/queries/groupmembers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ WHERE
-- name: RemoveUserFromGroups :many
DELETE FROM
group_members
USING groups
WHERE
group_members.group_id = groups.id AND
user_id = @user_id AND
group_id = ANY(@group_ids :: uuid [])
(
CASE WHEN array_length(@group_names :: name_organization_pair[], 1) > 0 THEN
-- Using 'coalesce' to avoid troubles with null literals being an empty string.
(groups.name, coalesce(groups.organization_id, '00000000-0000-0000-0000-000000000000' ::uuid)) = ANY (@group_names::name_organization_pair[])
ELSE false
END
OR
group_id = ANY (@group_ids :: uuid[])
)
RETURNING group_id;

-- name: InsertGroupMember :exec
Expand Down
108 changes: 71 additions & 37 deletions coderd/idpsync/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
}

// collect all diffs to do 1 sql update for all orgs
groupsToAdd := make([]uuid.UUID, 0)
groupsToRemove := make([]uuid.UUID, 0)
groupIDsToAdd := make([]uuid.UUID, 0)
groupsToRemove := make([]ExpectedGroup, 0)
// For each org, determine which groups the user should land in
for orgID, settings := range orgSettings {
if settings.GroupField == "" {
Expand All @@ -112,7 +112,8 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
}
// Everyone group is always implied.
expectedGroups = append(expectedGroups, ExpectedGroup{
GroupID: &orgID,
OrganizationID: orgID,
GroupID: &orgID,
})

// Now we know what groups the user should be in for a given org,
Expand All @@ -121,8 +122,9 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
existingGroups := userOrgs[orgID]
existingGroupsTyped := db2sdk.List(existingGroups, func(f database.GetGroupsRow) ExpectedGroup {
return ExpectedGroup{
GroupID: &f.Group.ID,
GroupName: &f.Group.Name,
OrganizationID: orgID,
GroupID: &f.Group.ID,
GroupName: &f.Group.Name,
}
})
add, remove := slice.SymmetricDifferenceFunc(existingGroupsTyped, expectedGroups, func(a, b ExpectedGroup) bool {
Expand All @@ -144,52 +146,75 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return xerrors.Errorf("handle missing groups: %w", err)
}

for _, removeGroup := range remove {
// This should always be the case.
// TODO: make sure this is always the case
if removeGroup.GroupID != nil {
groupsToRemove = append(groupsToRemove, *removeGroup.GroupID)
}
}
groupsToRemove = append(groupsToRemove, remove...)
groupIDsToAdd = append(groupIDsToAdd, assignGroups...)
}

groupsToAdd = append(groupsToAdd, assignGroups...)
err = s.applyGroupDifference(ctx, tx, user, groupIDsToAdd, groupsToRemove)
if err != nil {
return xerrors.Errorf("apply group difference: %w", err)
}

assignedGroupIDs, err := tx.InsertUserGroupsByID(ctx, database.InsertUserGroupsByIDParams{
UserID: user.ID,
GroupIds: groupsToAdd,
return nil
}, nil)

if err != nil {
return err
}

return nil
}

func (s AGPLIDPSync) applyGroupDifference(ctx context.Context, tx database.Store, user database.User, add []uuid.UUID, remove []ExpectedGroup) error {
// Always do group removal before group add. This way if there is an error,
// we error on the underprivileged side.
removeIDs := make([]uuid.UUID, 0)
removeNames := make([]database.NameOrganizationPair, 0)
for _, r := range remove {
if r.GroupID != nil {
removeIDs = append(removeIDs, *r.GroupID)
} else if r.GroupName != nil {
removeNames = append(removeNames, database.NameOrganizationPair{
Name: *r.GroupName,
OrganizationID: r.OrganizationID,
})
}
}

// If there is something to remove, do it.
if len(removeIDs) > 0 || len(removeNames) > 0 {
removedGroupIDs, err := tx.RemoveUserFromGroups(ctx, database.RemoveUserFromGroupsParams{
UserID: user.ID,
GroupNames: removeNames,
GroupIds: removeIDs,
})
if err != nil {
return xerrors.Errorf("insert user into %d groups: %w", len(groupsToAdd), err)
return xerrors.Errorf("remove user from %d groups: %w", len(removeIDs), err)
}
if len(assignedGroupIDs) != len(groupsToAdd) {
s.Logger.Debug(ctx, "failed to assign all groups to user",
if len(removedGroupIDs) != len(removeIDs) {
s.Logger.Debug(ctx, "failed to remove user from all groups",
slog.F("user_id", user.ID),
slog.F("groups_assigned_count", len(assignedGroupIDs)),
slog.F("expected_count", len(groupsToAdd)),
slog.F("groups_removed_count", len(removedGroupIDs)),
slog.F("expected_count", len(removeIDs)),
)
}
}

removedGroupIDs, err := tx.RemoveUserFromGroups(ctx, database.RemoveUserFromGroupsParams{
if len(add) > 0 {
assignedGroupIDs, err := tx.InsertUserGroupsByID(ctx, database.InsertUserGroupsByIDParams{
UserID: user.ID,
GroupIds: groupsToRemove,
GroupIds: add,
})
if err != nil {
return xerrors.Errorf("remove user from %d groups: %w", len(groupsToRemove), err)
return xerrors.Errorf("insert user into %d groups: %w", len(add), err)
}
if len(removedGroupIDs) != len(groupsToRemove) {
s.Logger.Debug(ctx, "failed to remove user from all groups",
if len(assignedGroupIDs) != len(add) {
s.Logger.Debug(ctx, "failed to assign all groups to user",
slog.F("user_id", user.ID),
slog.F("groups_removed_count", len(removedGroupIDs)),
slog.F("expected_count", len(groupsToRemove)),
slog.F("groups_assigned_count", len(assignedGroupIDs)),
slog.F("expected_count", len(add)),
)
}

return nil
}, nil)

if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -226,8 +251,9 @@ func (s *GroupSyncSettings) Type() string {
}

type ExpectedGroup struct {
GroupID *uuid.UUID
GroupName *string
OrganizationID uuid.UUID
GroupID *uuid.UUID
GroupName *string
}

// ParseClaims will take the merged claims from the IDP and return the groups
Expand Down Expand Up @@ -280,20 +306,28 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
return groups, nil
}

// HandleMissingGroups ensures all ExpectedGroups convert to uuids.
// Groups can be referenced by name via legacy params or IDP group names.
// These group names are converted to IDs for easier assignment.
// Missing groups are created if AutoCreate is enabled.
// TODO: Batching this would be better, as this is 1 or 2 db calls per organization.
func (s GroupSyncSettings) HandleMissingGroups(ctx context.Context, tx database.Store, orgID uuid.UUID, add []ExpectedGroup) ([]uuid.UUID, error) {
if !s.AutoCreateMissingGroups {
// construct the list of groups to search by name to see if they exist.
// If we are not creating groups, then just construct a db lookup for
// all groups by name.
var lookups []string
filter := make([]uuid.UUID, 0)
for _, expected := range add {
if expected.GroupID != nil {
// Groups with IDs are easy!
filter = append(filter, *expected.GroupID)
} else if expected.GroupName != nil {
lookups = append(lookups, *expected.GroupName)
}
}

if len(lookups) > 0 {
// Do name lookups for all groups that are missing IDs.
newGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
OrganizationID: uuid.UUID{},
HasMemberID: uuid.UUID{},
Expand Down
5 changes: 4 additions & 1 deletion coderd/idpsync/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ func TestGroupSyncTable(t *testing.T) {
},
AutoCreateMissingGroups: true,
},
Groups: map[uuid.UUID]bool{},
Groups: map[uuid.UUID]bool{
ids.ID("lg-foo"): true,
},
GroupNames: map[string]bool{
"legacy-foo": false,
"extra": true,
},
ExpectedGroupNames: []string{
"legacy-bar",
Expand Down