Skip to content

feat: add auto group create from OIDC #8884

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 22 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
fixup! sync missing groups
  • Loading branch information
Emyrk committed Aug 3, 2023
commit ec6ac573735a8814d511747b17b38e0b00033f75
2 changes: 2 additions & 0 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3526,6 +3526,7 @@ func (q *FakeQuerier) InsertGroup(_ context.Context, arg database.InsertGroupPar
OrganizationID: arg.OrganizationID,
AvatarURL: arg.AvatarURL,
QuotaAllowance: arg.QuotaAllowance,
Source: database.GroupSourceUser,
}

q.groups = append(q.groups, group)
Expand Down Expand Up @@ -3608,6 +3609,7 @@ func (q *FakeQuerier) InsertMissingGroups(ctx context.Context, arg database.Inse
AvatarURL: "",
QuotaAllowance: 0,
DisplayName: "",
Source: arg.Source,
}
q.groups = append(q.groups, g)
newGroups = append(newGroups, g)
Expand Down
10 changes: 9 additions & 1 deletion coderd/database/dump.sql

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

8 changes: 8 additions & 0 deletions coderd/database/migrations/000148_group_source.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN;

ALTER TABLE groups
DROP COLUMN source;

DROP TYPE group_source;

COMMIT;
15 changes: 15 additions & 0 deletions coderd/database/migrations/000148_group_source.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN;

CREATE TYPE group_source AS ENUM (
-- User created groups
'user',
-- Groups created by the system through oidc sync
'oidc'
);

ALTER TABLE groups
ADD COLUMN source group_source NOT NULL DEFAULT 'user';

COMMENT ON COLUMN groups.source IS 'Source indicates how the group was created. It can be created by a user manually, or through some system process like and OIDC group sync.';

COMMIT;
60 changes: 60 additions & 0 deletions coderd/database/models.go

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

36 changes: 23 additions & 13 deletions coderd/database/queries.sql.go

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

6 changes: 4 additions & 2 deletions coderd/database/queries/groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ VALUES
INSERT INTO groups (
id,
name,
organization_id
organization_id,
source
)
SELECT
gen_random_uuid(),
group_name,
@organization_id
@organization_id,
@source
FROM
UNNEST(@group_names :: text[]) AS group_name
-- If the name conflicts, do nothing.
Expand Down
2 changes: 1 addition & 1 deletion codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ when required by your organization's security policy.`,
},
{
Name: "OIDC Regex Group Filter",
Description: "If provided any group name not matching the regex is ignored. This allows for filtering out groups that are not needed.",
Description: "If provided any group name not matching the regex is ignored. This allows for filtering out groups that are not needed. This filter is applied after the group mapping.",
Flag: "oidc-group-regex-filter",
Env: "CODER_OIDC_GROUP_REGEX_FILTER",
Default: "",
Expand Down
22 changes: 15 additions & 7 deletions codersdk/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"golang.org/x/xerrors"
)

type GroupSource string

const (
GroupSourceUser GroupSource = "user"
GroupSourceOIDC GroupSource = "oidc"
)

type CreateGroupRequest struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Expand All @@ -18,13 +25,14 @@ type CreateGroupRequest struct {
}

type Group struct {
ID uuid.UUID `json:"id" format:"uuid"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
OrganizationID uuid.UUID `json:"organization_id" format:"uuid"`
Members []User `json:"members"`
AvatarURL string `json:"avatar_url"`
QuotaAllowance int `json:"quota_allowance"`
ID uuid.UUID `json:"id" format:"uuid"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
OrganizationID uuid.UUID `json:"organization_id" format:"uuid"`
Members []User `json:"members"`
AvatarURL string `json:"avatar_url"`
QuotaAllowance int `json:"quota_allowance"`
Source GroupSource `json:"source"`
}

func (c *Client) CreateGroup(ctx context.Context, orgID uuid.UUID, req CreateGroupRequest) (Group, error) {
Expand Down
1 change: 1 addition & 0 deletions enterprise/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var auditableResourcesTypes = map[any]map[string]Action{
"avatar_url": ActionTrack,
"quota_allowance": ActionTrack,
"members": ActionTrack,
"source": ActionIgnore,
},
&database.APIKey{}: {
"id": ActionIgnore,
Expand Down
1 change: 1 addition & 0 deletions enterprise/coderd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ func convertGroup(g database.Group, users []database.User) codersdk.Group {
AvatarURL: g.AvatarURL,
QuotaAllowance: int(g.QuotaAllowance),
Members: convertUsers(users, orgs),
Source: codersdk.GroupSource(g.Source),
}
}

Expand Down
1 change: 1 addition & 0 deletions enterprise/coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (api *API) setUserGroups(ctx context.Context, logger slog.Logger, db databa
created, err := tx.InsertMissingGroups(dbauthz.AsSystemRestricted(ctx), database.InsertMissingGroupsParams{
OrganizationID: orgs[0].ID,
GroupNames: groupNames,
Source: database.GroupSourceOidc,
})
if err != nil {
return xerrors.Errorf("insert missing groups: %w", err)
Expand Down
Loading