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
group sync adjustments
  • Loading branch information
Emyrk committed Sep 9, 2024
commit 640e86e47d633feb767de942b7faa3a437c8bc03
18 changes: 8 additions & 10 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,10 @@ func (q *FakeQuerier) GetGroups(_ context.Context, arg database.GetGroupsParams)
continue
}

if len(arg.GroupNames) > 0 && !slices.Contains(arg.GroupNames, group.Name) {
continue
}

orgDetails, ok := orgDetailsCache[group.ID]
if !ok {
for _, org := range q.organizations {
Expand Down Expand Up @@ -7661,18 +7665,12 @@ func (q *FakeQuerier) RemoveUserFromGroups(_ context.Context, arg database.Remov
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
if !slices.Contains(arg.GroupIds, groupMember.GroupID) {
return false
}

return false
removed = append(removed, groupMember.GroupID)
return true
})

return removed, nil
Expand Down
19 changes: 4 additions & 15 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: 1 addition & 11 deletions coderd/database/queries/groupmembers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,9 @@ WHERE
-- name: RemoveUserFromGroups :many
DELETE FROM
group_members
USING groups
WHERE
group_members.group_id = groups.id AND
user_id = @user_id AND
(
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[])
)
group_id = ANY(@group_ids :: uuid [])
RETURNING group_id;

-- name: InsertGroupMember :exec
Expand Down
125 changes: 48 additions & 77 deletions coderd/idpsync/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package idpsync
import (
"context"
"encoding/json"
"fmt"
"regexp"

"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -92,15 +93,15 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat

// collect all diffs to do 1 sql update for all orgs
groupIDsToAdd := make([]uuid.UUID, 0)
groupsToRemove := make([]ExpectedGroup, 0)
groupIDsToRemove := make([]uuid.UUID, 0)
// For each org, determine which groups the user should land in
for orgID, settings := range orgSettings {
if settings.GroupField == "" {
// No group sync enabled for this org, so do nothing.
continue
}

expectedGroups, err := settings.ParseClaims(params.MergedClaims)
expectedGroups, err := settings.ParseClaims(orgID, params.MergedClaims)
if err != nil {
s.Logger.Debug(ctx, "failed to parse claims for groups",
slog.F("organization_field", s.GroupField),
Expand Down Expand Up @@ -128,6 +129,10 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
}
})
add, remove := slice.SymmetricDifferenceFunc(existingGroupsTyped, expectedGroups, func(a, b ExpectedGroup) bool {
// Must match
if a.OrganizationID != b.OrganizationID {
return false
}
// Only the name or the name needs to be checked, priority is given to the ID.
if a.GroupID != nil && b.GroupID != nil {
return *a.GroupID == *b.GroupID
Expand All @@ -138,6 +143,20 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return false
})

for _, r := range remove {
// This should never happen. All group removals come from the
// existing set, which come from the db. All groups from the
// database have IDs. This code is purely defensive.
if r.GroupID == nil {
detail := "user:" + user.Username
if r.GroupName != nil {
detail += fmt.Sprintf(" from group %s", *r.GroupName)
}
return xerrors.Errorf("removal group has nil ID, which should never happen: %s", detail)
}
groupIDsToRemove = append(groupIDsToRemove, *r.GroupID)
}

// HandleMissingGroups will add the new groups to the org if
// the settings specify. It will convert all group names into uuids
// for easier assignment.
Expand All @@ -146,11 +165,10 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return xerrors.Errorf("handle missing groups: %w", err)
}

groupsToRemove = append(groupsToRemove, remove...)
groupIDsToAdd = append(groupIDsToAdd, assignGroups...)
}

err = s.applyGroupDifference(ctx, tx, user, groupIDsToAdd, groupsToRemove)
err = s.applyGroupDifference(ctx, tx, user, groupIDsToAdd, groupIDsToRemove)
if err != nil {
return xerrors.Errorf("apply group difference: %w", err)
}
Expand All @@ -165,28 +183,13 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return nil
}

func (s AGPLIDPSync) applyGroupDifference(ctx context.Context, tx database.Store, user database.User, add []uuid.UUID, remove []ExpectedGroup) error {
func (s AGPLIDPSync) applyGroupDifference(ctx context.Context, tx database.Store, user database.User, add []uuid.UUID, removeIDs []uuid.UUID) 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 {
if len(removeIDs) > 0 {
removedGroupIDs, err := tx.RemoveUserFromGroups(ctx, database.RemoveUserFromGroupsParams{
UserID: user.ID,
GroupNames: removeNames,
GroupIds: removeIDs,
UserID: user.ID,
GroupIds: removeIDs,
})
if err != nil {
return xerrors.Errorf("remove user from %d groups: %w", len(removeIDs), err)
Expand Down Expand Up @@ -264,7 +267,7 @@ type ExpectedGroup struct {
// the group "UUID 1234" is renamed, we want to maintain the mapping.
// We have to keep names because group sync supports syncing groups by name if
// the external IDP group name matches the Coder one.
func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGroup, error) {
func (s GroupSyncSettings) ParseClaims(orgID uuid.UUID, mergedClaims jwt.MapClaims) ([]ExpectedGroup, error) {
groupsRaw, ok := mergedClaims[s.GroupField]
if !ok {
return []ExpectedGroup{}, nil
Expand Down Expand Up @@ -294,13 +297,13 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
if ok {
for _, gid := range mappedGroupIDs {
gid := gid
groups = append(groups, ExpectedGroup{GroupID: &gid})
groups = append(groups, ExpectedGroup{OrganizationID: orgID, GroupID: &gid})
}
continue
}

group := group
groups = append(groups, ExpectedGroup{GroupName: &group})
groups = append(groups, ExpectedGroup{OrganizationID: orgID, GroupName: &group})
}

return groups, nil
Expand All @@ -312,38 +315,6 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
// 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 {
// 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{},
GroupNames: lookups,
})
if err != nil {
return nil, xerrors.Errorf("get groups by names: %w", err)
}
for _, g := range newGroups {
filter = append(filter, g.Group.ID)
}
}

return filter, nil
}

// All expected that are missing IDs means the group does not exist
// in the database. Either remove them, or create them if auto create is
// turned on.
Expand All @@ -359,33 +330,33 @@ func (s GroupSyncSettings) HandleMissingGroups(ctx context.Context, tx database.
}
}

createdMissingGroups, err := tx.InsertMissingGroups(ctx, database.InsertMissingGroupsParams{
OrganizationID: orgID,
Source: database.GroupSourceOidc,
GroupNames: missingGroups,
})
if err != nil {
return nil, xerrors.Errorf("insert missing groups: %w", err)
if s.AutoCreateMissingGroups && len(missingGroups) > 0 {
// Insert any missing groups. If the groups already exist, this is a noop.
_, err := tx.InsertMissingGroups(ctx, database.InsertMissingGroupsParams{
OrganizationID: orgID,
Source: database.GroupSourceOidc,
GroupNames: missingGroups,
})
if err != nil {
return nil, xerrors.Errorf("insert missing groups: %w", err)
}
}

if len(missingGroups) != len(createdMissingGroups) {
// This is unfortunate, but if legacy params are used, then some existing groups
// can come as params. So we need to fetch them
allGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
// Fetch any missing groups by name. If they exist, their IDs will be
// matched and returned.
if len(missingGroups) > 0 {
// Do name lookups for all groups that are missing IDs.
newGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
OrganizationID: orgID,
HasMemberID: uuid.UUID{},
GroupNames: missingGroups,
})
if err != nil {
return nil, xerrors.Errorf("get groups by names: %w", err)
}

createdMissingGroups = db2sdk.List(allGroups, func(g database.GetGroupsRow) database.Group {
return g.Group
})
}

for _, created := range createdMissingGroups {
addIDs = append(addIDs, created.ID)
for _, g := range newGroups {
addIDs = append(addIDs, g.Group.ID)
}
}

return addIDs, nil
Expand Down
2 changes: 2 additions & 0 deletions coderd/idpsync/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func TestGroupSyncTable(t *testing.T) {
LegacyGroupNameMapping: map[string]string{
"create-bar": "legacy-bar",
"foo": "legacy-foo",
"bop": "legacy-bop",
},
AutoCreateMissingGroups: true,
},
Expand All @@ -214,6 +215,7 @@ func TestGroupSyncTable(t *testing.T) {
GroupNames: map[string]bool{
"legacy-foo": false,
"extra": true,
"legacy-bop": true,
},
ExpectedGroupNames: []string{
"legacy-bar",
Expand Down