-
Notifications
You must be signed in to change notification settings - Fork 914
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
base: main
Are you sure you want to change the base?
Conversation
e05480d
to
db80b4d
Compare
db80b4d
to
2ba15c5
Compare
runningWorkspaces, err := db.GetRunningPrebuiltWorkspaces(ctx) | ||
require.NoError(t, err) | ||
|
||
prebuiltWorkspace, err := db.GetWorkspaceByID(ctx, runningWorkspaces[0].ID) | ||
require.NoError(t, 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.
You should also require there to be at least one running workspace. This may need to be done in a testutil.Eventually(ctx)
to avoid potential flakiness.
…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.
// IsPrebuild returns true if the workspace is a prebuild workspace. | ||
// A workspace is considered a prebuild if its owner is the prebuild system user. | ||
func (w Workspace) IsPrebuild() bool { | ||
return w.OwnerID == PrebuildsSystemUserID | ||
} | ||
|
||
// AsPrebuild returns the RBAC object corresponding to the workspace type. | ||
// If the workspace is a prebuild, it returns a prebuilt_workspace RBAC object. | ||
// Otherwise, it returns a normal workspace RBAC object. | ||
func (w Workspace) AsPrebuild() rbac.Object { | ||
if w.IsPrebuild() { | ||
return rbac.ResourcePrebuiltWorkspace.WithID(w.ID). | ||
InOrg(w.OrganizationID). | ||
WithOwner(w.OwnerID.String()) | ||
} | ||
return w.RBACObject() | ||
} | ||
|
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.
Helps to have it on the table type too.
// IsPrebuild returns true if the workspace is a prebuild workspace. | |
// A workspace is considered a prebuild if its owner is the prebuild system user. | |
func (w Workspace) IsPrebuild() bool { | |
return w.OwnerID == PrebuildsSystemUserID | |
} | |
// AsPrebuild returns the RBAC object corresponding to the workspace type. | |
// If the workspace is a prebuild, it returns a prebuilt_workspace RBAC object. | |
// Otherwise, it returns a normal workspace RBAC object. | |
func (w Workspace) AsPrebuild() rbac.Object { | |
if w.IsPrebuild() { | |
return rbac.ResourcePrebuiltWorkspace.WithID(w.ID). | |
InOrg(w.OrganizationID). | |
WithOwner(w.OwnerID.String()) | |
} | |
return w.RBACObject() | |
} | |
// IsPrebuild returns true if the workspace is a prebuild workspace. | |
// A workspace is considered a prebuild if its owner is the prebuild system user. | |
func (w Workspace) IsPrebuild() bool { | |
return w.OwnerID == PrebuildsSystemUserID | |
} | |
func (w WorkspaceTable) IsPrebuild() bool { | |
return w.OwnerID == PrebuildsSystemUserID | |
} | |
// AsPrebuild returns the RBAC object corresponding to the workspace type. | |
// If the workspace is a prebuild, it returns a prebuilt_workspace RBAC object. | |
// Otherwise, it returns a normal workspace RBAC object. | |
func (w Workspace) AsPrebuild() rbac.Object { | |
return w.WorkspaceTable().AsPrebuild() | |
} | |
func (w WorkspaceTable) AsPrebuild() rbac.Object { | |
if w.IsPrebuild() { | |
return rbac.ResourcePrebuiltWorkspace.WithID(w.ID). | |
InOrg(w.OrganizationID). | |
WithOwner(w.OwnerID.String()) | |
} | |
return w.RBACObject() | |
} |
// authorization. | ||
// Note: Delete operations of workspaces requires both update and delete | ||
// permissions. | ||
func (q *querier) authorizePrebuiltWorkspace(ctx context.Context, action policy.Action, workspace database.Workspace) error { |
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.
You can add this dbauthz_test
to assert the delete
on the prebuild is sufficient to run this workspace delete build
s.Run("PrebuildDelete/InsertWorkspaceBuild", s.Subtest(func(db database.Store, check *expects) {
u := dbgen.User(s.T(), db, database.User{})
o := dbgen.Organization(s.T(), db, database.Organization{})
tpl := dbgen.Template(s.T(), db, database.Template{
OrganizationID: o.ID,
CreatedBy: u.ID,
})
w := dbgen.Workspace(s.T(), db, database.WorkspaceTable{
TemplateID: tpl.ID,
OrganizationID: o.ID,
OwnerID: database.PrebuildsSystemUserID,
})
pj := dbgen.ProvisionerJob(s.T(), db, nil, database.ProvisionerJob{
OrganizationID: o.ID,
})
tv := dbgen.TemplateVersion(s.T(), db, database.TemplateVersion{
TemplateID: uuid.NullUUID{UUID: tpl.ID, Valid: true},
OrganizationID: o.ID,
CreatedBy: u.ID,
})
check.Args(database.InsertWorkspaceBuildParams{
WorkspaceID: w.ID,
Transition: database.WorkspaceTransitionDelete,
Reason: database.BuildReasonInitiator,
TemplateVersionID: tv.ID,
JobID: pj.ID,
}).Asserts(w.AsPrebuild(), policy.ActionDelete)
}))
Description
This PR adds support for deleting prebuilt workspaces via the authorization layer. It introduces special-case handling to ensure that
prebuilt_workspace
permissions are evaluated when attempting to delete a prebuilt workspace, falling back to the standardworkspace
resource as needed.Prebuilt workspaces are a subset of workspaces, identified by having
owner_id
set toPREBUILD_SYSTEM_USER
.This means:
prebuilt_workspace.delete
permission is allowed to delete only prebuilt workspaces.workspace.delete
permission can delete both normal and prebuilt workspaces.prebuilt_workspace
resource.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 thedeleted
field in the database)delete
: to perform the actual deletion of the workspaceChanges
authorizeWorkspace()
helper to handle prebuilt workspace authorization logic.prebuilt_workspace
andworkspace
permissions are checked.SystemUserID
constant from theprebuilds
package to thedatabase
packagePrebuildsSystemUserID
to resolve an import cycle (commit f24e4ab).