-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: allow TemplateAdmin to delete prebuilds via auth layer #18333
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
Conversation
e05480d to
db80b4d
Compare
db80b4d to
2ba15c5
Compare
…resolve import cycle
| // PrebuiltWorkspaces are a subset of Workspaces. | ||
| // Explicitly setting PrebuiltWorkspace permissions for clarity. | ||
| // Note: even without PrebuiltWorkspace permissions, access is still granted via Workspace permissions. | ||
| ResourcePrebuiltWorkspace.Type: {policy.ActionUpdate, policy.ActionDelete}, |
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.
👍 Always +1 for clarity.
| OrganizationID: orgID, | ||
| UserID: user.ID, | ||
| IncludeSystem: false, | ||
| IncludeSystem: true, |
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.
review: this is required in order to be able to delete a prebuilt workspace from the CLI, as it needs to fetch it first.
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.
Added to the PR description as well:
- Update middleware ExtractOrganizationMember to include system user members.
johnstcn
left a comment
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.
Nice work! I don't have any blocking comments.
Emyrk
left a comment
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.
Requiring workspace.read simplifies the problem. 👍
| if (action == policy.ActionUpdate || action == policy.ActionDelete) && workspace.IsPrebuild() { | ||
| // Try prebuilt-specific authorization first | ||
| if prebuiltErr = q.authorizeContext(ctx, action, workspace.AsPrebuild()); prebuiltErr == nil { | ||
| return nil | ||
| } | ||
| } | ||
| // Fallback to normal workspace authorization check | ||
| if err := q.authorizeContext(ctx, action, workspace); err != nil { | ||
| return xerrors.Errorf("authorize context: %w", errors.Join(prebuiltErr, err)) | ||
| } |
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.
I think the normal workspace check is more likely to succeed then the prebuild. So it might be a slight optimization to flip these checks.
But that will probably break some of the unit tests? No need to change this, just a thought.
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.
Good point, I had considered that optimization as well. I believe it might require some test updates, so I'd prefer to merge this PR and address the optimization in a follow-up PR.
| })) | ||
| } | ||
|
|
||
| func (s *MethodTestSuite) TestAuthorizePrebuiltWorkspace() { |
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.
👍
Description
This PR adds support for deleting prebuilt workspaces via the authorization layer. It introduces special-case handling to ensure that
prebuilt_workspacepermissions are evaluated when attempting to delete a prebuilt workspace, falling back to the standardworkspaceresource as needed.Prebuilt workspaces are a subset of workspaces, identified by having
owner_idset toPREBUILD_SYSTEM_USER.This means:
prebuilt_workspace.deletepermission is allowed to delete only prebuilt workspaces.workspace.deletepermission can delete both normal and prebuilt workspaces.prebuilt_workspaceresource.To delete a workspace, users must have the following permissions:
workspace.read: to read the current workspace stateupdate: to modify workspace metadata and related resources during deletion (e.g., updating thedeletedfield in the database)delete: to perform the actual deletion of the workspaceChanges
authorizeWorkspace()helper to handle prebuilt workspace authorization logic.prebuilt_workspaceandworkspacepermissions are checked.SystemUserIDconstant from theprebuildspackage to thedatabasepackagePrebuildsSystemUserIDto resolve an import cycle (commit f24e4ab).ExtractOrganizationMemberto include system user members.