(ctx context.Context, arg database.UpdateProvisionerJobWithCancelByIDParams)
| 7103 | } |
| 7104 | |
| 7105 | func (q *querier) UpdateProvisionerJobWithCancelByID(ctx context.Context, arg database.UpdateProvisionerJobWithCancelByIDParams) error { |
| 7106 | // TODO: Remove this once we have a proper rbac check for provisioner jobs. |
| 7107 | // Details in https://github.com/coder/coder/issues/16160 |
| 7108 | |
| 7109 | job, err := q.db.GetProvisionerJobByID(ctx, arg.ID) |
| 7110 | if err != nil { |
| 7111 | return err |
| 7112 | } |
| 7113 | |
| 7114 | switch job.Type { |
| 7115 | case database.ProvisionerJobTypeWorkspaceBuild: |
| 7116 | build, err := q.db.GetWorkspaceBuildByJobID(ctx, arg.ID) |
| 7117 | if err != nil { |
| 7118 | return err |
| 7119 | } |
| 7120 | workspace, err := q.db.GetWorkspaceByID(ctx, build.WorkspaceID) |
| 7121 | if err != nil { |
| 7122 | return err |
| 7123 | } |
| 7124 | |
| 7125 | template, err := q.db.GetTemplateByID(ctx, workspace.TemplateID) |
| 7126 | if err != nil { |
| 7127 | return err |
| 7128 | } |
| 7129 | |
| 7130 | // Template can specify if cancels are allowed. |
| 7131 | // Would be nice to have a way in the rbac rego to do this. |
| 7132 | if !template.AllowUserCancelWorkspaceJobs { |
| 7133 | // Only owners can cancel workspace builds |
| 7134 | actor, ok := ActorFromContext(ctx) |
| 7135 | if !ok { |
| 7136 | return ErrNoActor |
| 7137 | } |
| 7138 | if !slice.Contains(actor.Roles.Names(), rbac.RoleOwner()) { |
| 7139 | return xerrors.Errorf("only owners can cancel workspace builds") |
| 7140 | } |
| 7141 | } |
| 7142 | |
| 7143 | err = q.authorizeContext(ctx, policy.ActionUpdate, workspace) |
| 7144 | if err != nil { |
| 7145 | return err |
| 7146 | } |
| 7147 | case database.ProvisionerJobTypeTemplateVersionDryRun, database.ProvisionerJobTypeTemplateVersionImport: |
| 7148 | // Authorized call to get template version. |
| 7149 | templateVersion, err := authorizedTemplateVersionFromJob(ctx, q, job) |
| 7150 | if err != nil { |
| 7151 | return err |
| 7152 | } |
| 7153 | |
| 7154 | if templateVersion.TemplateID.Valid { |
| 7155 | template, err := q.db.GetTemplateByID(ctx, templateVersion.TemplateID.UUID) |
| 7156 | if err != nil { |
| 7157 | return err |
| 7158 | } |
| 7159 | err = q.authorizeContext(ctx, policy.ActionUpdate, templateVersion.RBACObject(template)) |
| 7160 | if err != nil { |
| 7161 | return err |
| 7162 | } |
nothing calls this directly
no test coverage detected