-
Notifications
You must be signed in to change notification settings - Fork 943
feat: Allow user to cancel workspace jobs #5115
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
Changes from 1 commit
4dcbf5c
400413e
7f8dcaa
3f76534
64b8bf3
73431c9
ba5c06b
9be6b1f
8579883
3fe5bef
9a303b5
ba443eb
7d36b2d
472fee8
056894a
4ec5aab
c215f5a
69c544f
4e5ee76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,11 @@ import ( | |
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
var ( | ||
errTemplateNotExists = xerrors.New("No template exists for this workspace") | ||
errUserNotExists = xerrors.New("User does not exist") | ||
) | ||
|
||
func (api *API) workspaceBuild(rw http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
workspaceBuild := httpmw.WorkspaceBuildParam(r) | ||
|
@@ -599,6 +604,21 @@ func (api *API) patchCancelWorkspaceBuild(rw http.ResponseWriter, r *http.Reques | |
return | ||
} | ||
|
||
valid, err := api.verifyUserCanCancelWorkspaceBuilds(ctx, httpmw.APIKey(r).UserID, workspace.TemplateID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
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. Suggestion: Both errors returned by verify seem like known states and caused by a user doing a nonsense request (vs. server making a mistake), maybe unauthorized would be a good response? 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. Unauthorized might be misleading while debugging potential customer issues. In theory, we don't expect an invalid user or an invalid template, and it doesn't look possible that the user passes it, hence an internal error. 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. Good points. After a second look I agree, didn't look closely enough at the inputs at first. 👍🏻 |
||
Message: "Internal error verifying permission to cancel workspace build.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
if !valid { | ||
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ | ||
Message: "User is not allowed cancel workspace builds. Owner role is required.", | ||
}) | ||
return | ||
} | ||
|
||
job, err := api.Database.GetProvisionerJobByID(ctx, workspaceBuild.JobID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
|
@@ -646,6 +666,23 @@ func (api *API) patchCancelWorkspaceBuild(rw http.ResponseWriter, r *http.Reques | |
}) | ||
} | ||
|
||
func (api *API) verifyUserCanCancelWorkspaceBuilds(ctx context.Context, userID uuid.UUID, templateID uuid.UUID) (bool, error) { | ||
template, err := api.Database.GetTemplateByID(ctx, templateID) | ||
if err != nil { | ||
return false, errTemplateNotExists | ||
} | ||
|
||
if template.AllowUserCancelWorkspaceJobs { | ||
return true, nil // all users can cancel workspace builds | ||
} | ||
|
||
user, err := api.Database.GetUserByID(ctx, userID) | ||
if err != nil { | ||
return false, errUserNotExists | ||
} | ||
return slices.Contains(user.RBACRoles, rbac.RoleOwner()), nil // only user with "owner" role can cancel workspace builds | ||
} | ||
|
||
func (api *API) workspaceBuildResources(rw http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
workspaceBuild := httpmw.WorkspaceBuildParam(r) | ||
|
Uh oh!
There was an error while loading. Please reload this page.