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
handle legacy params
  • Loading branch information
Emyrk committed Sep 9, 2024
commit 86c0f6f52eb79c142920a9abce15b6d40e4f315c
2 changes: 1 addition & 1 deletion coderd/database/queries.sql.go

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

2 changes: 1 addition & 1 deletion coderd/database/queries/groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ WHERE
ELSE true
END
AND CASE WHEN array_length(@group_names :: text[], 1) > 0 THEN
name = ANY(@group_names)
groups.name = ANY(@group_names)
ELSE true
END
;
Expand Down
34 changes: 34 additions & 0 deletions coderd/idpsync/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return nil
}

// Only care about the default org for deployment settings if the
// legacy deployment settings exist.
defaultOrgID := uuid.Nil
// Default organization is configured via legacy deployment values
if s.DeploymentSyncSettings.Legacy.GroupField != "" {
defaultOrganization, err := db.GetDefaultOrganization(ctx)
if err != nil {
return xerrors.Errorf("get default organization: %w", err)
}
defaultOrgID = defaultOrganization.ID
}
Comment on lines +51 to +61
Copy link
Member Author

Choose a reason for hiding this comment

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

Startup group sync config has to be pulled for the default org 😢. Backwards compatibility.


// nolint:gocritic // all syncing is done as a system user
ctx = dbauthz.AsSystemRestricted(ctx)

Expand Down Expand Up @@ -66,6 +78,16 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
return xerrors.Errorf("resolve group sync settings: %w", err)
}
orgSettings[orgID] = *settings

// Legacy deployment settings will override empty settings.
if orgID == defaultOrgID && settings.GroupField == "" {
settings = &GroupSyncSettings{
GroupField: s.Legacy.GroupField,
LegacyGroupNameMapping: s.Legacy.GroupMapping,
RegexFilter: s.Legacy.GroupFilter,
AutoCreateMissingGroups: s.Legacy.CreateMissingGroups,
}
}
}

// collect all diffs to do 1 sql update for all orgs
Expand Down Expand Up @@ -175,6 +197,12 @@ type GroupSyncSettings struct {
GroupMapping map[string][]uuid.UUID `json:"mapping"`
RegexFilter *regexp.Regexp `json:"regex_filter"`
AutoCreateMissingGroups bool `json:"auto_create_missing_groups"`
// LegacyGroupNameMapping is deprecated. It remaps an IDP group name to
// a Coder group name. Since configuration is now done at runtime,
// group IDs are used to account for group renames.
// For legacy configurations, this config option has to remain.
// Deprecated: Use GroupMapping instead.
LegacyGroupNameMapping map[string]string
}

func (s *GroupSyncSettings) Set(v string) error {
Expand Down Expand Up @@ -232,6 +260,12 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
}
continue
}

mappedGroupName, ok := s.LegacyGroupNameMapping[group]
if ok {
groups = append(groups, ExpectedGroup{GroupName: &mappedGroupName})
continue
}
group := group
groups = append(groups, ExpectedGroup{GroupName: &group})
}
Expand Down
24 changes: 17 additions & 7 deletions coderd/idpsync/idpsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package idpsync
import (
"context"
"net/http"
"regexp"
"strings"

"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -69,6 +70,15 @@ type DeploymentSyncSettings struct {
// have at least one group in this list.
// A map representation is used for easier lookup.
GroupAllowList map[string]struct{}
// Legacy deployment settings that only apply to the default org.
Legacy DefaultOrgLegacySettings
}

type DefaultOrgLegacySettings struct {
GroupField string
GroupMapping map[string]string
GroupFilter *regexp.Regexp
CreateMissingGroups bool
}

func FromDeploymentValues(dv *codersdk.DeploymentValues) DeploymentSyncSettings {
Expand All @@ -80,8 +90,15 @@ func FromDeploymentValues(dv *codersdk.DeploymentValues) DeploymentSyncSettings
OrganizationMapping: dv.OIDC.OrganizationMapping.Value,
OrganizationAssignDefault: dv.OIDC.OrganizationAssignDefault.Value(),

// TODO: Separate group field for allow list from default org
GroupField: dv.OIDC.GroupField.Value(),
GroupAllowList: ConvertAllowList(dv.OIDC.GroupAllowList.Value()),
Legacy: DefaultOrgLegacySettings{
GroupField: dv.OIDC.GroupField.Value(),
GroupMapping: dv.OIDC.GroupMapping.Value,
GroupFilter: dv.OIDC.GroupRegexFilter.Value(),
CreateMissingGroups: dv.OIDC.GroupAutoCreate.Value(),
},
}

}
Expand All @@ -90,13 +107,6 @@ type SyncSettings struct {
DeploymentSyncSettings

Group runtimeconfig.RuntimeEntry[*GroupSyncSettings]

//// Group options here are set by the deployment config and only apply to
//// the default organization.
//GroupField string
//CreateMissingGroups bool
//GroupMapping map[string]string
//GroupFilter *regexp.Regexp
}

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