-
Notifications
You must be signed in to change notification settings - Fork 955
feat(enterprise/coderd): allow system users to be added to groups #18341
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
base: main
Are you sure you want to change the base?
Changes from all commits
6b3a731
4b63826
90b9078
af7c7cd
1cd68e3
4c0c4d4
93c46e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,7 +156,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) { | |
|
||
currentMembers, err := api.Database.GetGroupMembersByGroupID(ctx, database.GetGroupMembersByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need all these |
||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
|
@@ -180,7 +180,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) { | |
_, err := database.ExpectOne(api.Database.OrganizationMembers(ctx, database.OrganizationMembersParams{ | ||
OrganizationID: group.OrganizationID, | ||
UserID: uuid.MustParse(id), | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
})) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
|
@@ -296,7 +296,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) { | |
|
||
patchedMembers, err := api.Database.GetGroupMembersByGroupID(ctx, database.GetGroupMembersByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
|
@@ -307,7 +307,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) { | |
|
||
memberCount, err := api.Database.GetGroupMembersCountByGroupID(ctx, database.GetGroupMembersCountByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
|
@@ -353,7 +353,7 @@ func (api *API) deleteGroup(rw http.ResponseWriter, r *http.Request) { | |
|
||
groupMembers, getMembersErr := api.Database.GetGroupMembersByGroupID(ctx, database.GetGroupMembersByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if getMembersErr != nil { | ||
httpapi.InternalServerError(rw, getMembersErr) | ||
|
@@ -407,7 +407,7 @@ func (api *API) group(rw http.ResponseWriter, r *http.Request) { | |
|
||
users, err := api.Database.GetGroupMembersByGroupID(ctx, database.GetGroupMembersByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil && !errors.Is(err, sql.ErrNoRows) { | ||
httpapi.InternalServerError(rw, err) | ||
|
@@ -416,7 +416,7 @@ func (api *API) group(rw http.ResponseWriter, r *http.Request) { | |
|
||
memberCount, err := api.Database.GetGroupMembersCountByGroupID(ctx, database.GetGroupMembersCountByGroupIDParams{ | ||
GroupID: group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
|
@@ -512,15 +512,15 @@ func (api *API) groups(rw http.ResponseWriter, r *http.Request) { | |
for _, group := range groups { | ||
members, err := api.Database.GetGroupMembersByGroupID(ctx, database.GetGroupMembersByGroupIDParams{ | ||
GroupID: group.Group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
return | ||
} | ||
memberCount, err := api.Database.GetGroupMembersCountByGroupID(ctx, database.GetGroupMembersCountByGroupIDParams{ | ||
GroupID: group.Group.ID, | ||
IncludeSystem: false, | ||
IncludeSystem: true, | ||
}) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,37 +44,74 @@ func (s StoreMembershipReconciler) ReconcileAll(ctx context.Context, userID uuid | |
return xerrors.Errorf("determine prebuild organization membership: %w", err) | ||
} | ||
|
||
systemUserMemberships := make(map[uuid.UUID]struct{}, 0) | ||
orgMemberShips := make(map[uuid.UUID]struct{}, 0) | ||
defaultOrg, err := s.store.GetDefaultOrganization(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("get default organization: %w", err) | ||
} | ||
systemUserMemberships[defaultOrg.ID] = struct{}{} | ||
orgMemberShips[defaultOrg.ID] = struct{}{} | ||
for _, o := range organizationMemberships { | ||
systemUserMemberships[o.ID] = struct{}{} | ||
orgMemberShips[o.ID] = struct{}{} | ||
} | ||
|
||
var membershipInsertionErrors error | ||
for _, preset := range presets { | ||
_, alreadyMember := systemUserMemberships[preset.OrganizationID] | ||
if alreadyMember { | ||
continue | ||
_, alreadyOrgMember := orgMemberShips[preset.OrganizationID] | ||
if !alreadyOrgMember { | ||
// Add the organization to our list of memberships regardless of potential failure below | ||
// to avoid a retry that will probably be doomed anyway. | ||
orgMemberShips[preset.OrganizationID] = struct{}{} | ||
|
||
// Insert the missing membership | ||
_, err = s.store.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{ | ||
OrganizationID: preset.OrganizationID, | ||
UserID: userID, | ||
CreatedAt: s.clock.Now(), | ||
UpdatedAt: s.clock.Now(), | ||
Roles: []string{}, | ||
}) | ||
if err != nil { | ||
membershipInsertionErrors = errors.Join(membershipInsertionErrors, xerrors.Errorf("insert membership for prebuilt workspaces: %w", err)) | ||
continue | ||
} | ||
} | ||
// Add the organization to our list of memberships regardless of potential failure below | ||
// to avoid a retry that will probably be doomed anyway. | ||
systemUserMemberships[preset.OrganizationID] = struct{}{} | ||
|
||
// Insert the missing membership | ||
_, err = s.store.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{ | ||
// Create a "prebuilds" group in the organization and add the system user to it | ||
// This group will have a quota of 0 by default, which users can adjust based on their needs | ||
prebuildsGroup, err := s.store.InsertGroup(ctx, database.InsertGroupParams{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this be done inside the |
||
ID: uuid.New(), | ||
Name: "prebuilds", | ||
DisplayName: "Prebuilds", | ||
OrganizationID: preset.OrganizationID, | ||
UserID: userID, | ||
CreatedAt: s.clock.Now(), | ||
UpdatedAt: s.clock.Now(), | ||
Roles: []string{}, | ||
AvatarURL: "", | ||
QuotaAllowance: 0, // Default quota of 0, users should set this based on their needs | ||
}) | ||
if err != nil { | ||
// If the group already exists, try to get it | ||
if !database.IsUniqueViolation(err) { | ||
membershipInsertionErrors = errors.Join(membershipInsertionErrors, xerrors.Errorf("create prebuilds group: %w", err)) | ||
continue | ||
} | ||
prebuildsGroup, err = s.store.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{ | ||
OrganizationID: preset.OrganizationID, | ||
Name: "prebuilds", | ||
}) | ||
if err != nil { | ||
membershipInsertionErrors = errors.Join(membershipInsertionErrors, xerrors.Errorf("get existing prebuilds group: %w", err)) | ||
continue | ||
} | ||
} | ||
|
||
// Add the system user to the prebuilds group | ||
err = s.store.InsertGroupMember(ctx, database.InsertGroupMemberParams{ | ||
GroupID: prebuildsGroup.ID, | ||
UserID: userID, | ||
}) | ||
if err != nil { | ||
membershipInsertionErrors = errors.Join(membershipInsertionErrors, xerrors.Errorf("insert membership for prebuilt workspaces: %w", err)) | ||
continue | ||
// Ignore unique violation errors as the user might already be in the group | ||
if !database.IsUniqueViolation(err) { | ||
membershipInsertionErrors = errors.Join(membershipInsertionErrors, xerrors.Errorf("add system user to prebuilds group: %w", err)) | ||
} | ||
} | ||
} | ||
return membershipInsertionErrors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit for readability: