Skip to content

Commit 6bb2e1c

Browse files
committed
authzquery: fixes in workspaces.go
- GetWorkspaceAgentsByResourceIDs: handle workspace agents created by TemplateVersionImport jobs - GetWorkspaceResourcesByJobID: handle all provisioner job types and simplify RBAC logic
1 parent 0ce75c6 commit 6bb2e1c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

coderd/authzquery/workspace.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package authzquery
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57

68
"golang.org/x/xerrors"
79

@@ -80,11 +82,18 @@ func (q *AuthzQuerier) GetWorkspaceAgentsByResourceIDs(ctx context.Context, ids
8082
}
8183

8284
for _, a := range agents {
83-
// Check if we can fetch the agent.
85+
// Check if we can fetch the workspace by the agent ID.
8486
_, err := q.GetWorkspaceByAgentID(ctx, a.ID)
85-
if err != nil {
86-
return nil, err
87+
if err == nil {
88+
continue
89+
}
90+
if errors.Is(err, sql.ErrNoRows) {
91+
// The agent is not tied to a workspace, likely from an orphaned template version.
92+
// Just return it.
93+
continue
8794
}
95+
// Otherwise, we cannot read the workspace, so we cannot read the agent.
96+
return nil, err
8897
}
8998
return agents, nil
9099
}
@@ -256,31 +265,21 @@ func (q *AuthzQuerier) GetWorkspaceResourceMetadataByResourceIDs(ctx context.Con
256265
}
257266

258267
func (q *AuthzQuerier) GetWorkspaceResourcesByJobID(ctx context.Context, jobID uuid.UUID) ([]database.WorkspaceResource, error) {
259-
build, err := q.database.GetWorkspaceBuildByJobID(ctx, jobID)
268+
job, err := q.database.GetProvisionerJobByID(ctx, jobID)
260269
if err != nil {
261-
job, err := q.database.GetProvisionerJobByID(ctx, jobID)
262-
if err == nil && job.Type == database.ProvisionerJobTypeTemplateVersionDryRun {
263-
// TODO: We should really remove this branch path. It is kinda jank.
264-
// This is really annoying, but if a job is a dry run, there is no workspace
265-
// for this job. So we need to make up an rbac object for the workspace.
266-
tv, err := authorizedTemplateVersionFromJob(ctx, q, job)
267-
if err != nil {
268-
return nil, err
269-
}
270-
271-
err = q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceWorkspace.InOrg(tv.OrganizationID).WithOwner(job.InitiatorID.String()))
272-
if err != nil {
273-
return nil, err
274-
}
275-
276-
return q.database.GetWorkspaceResourcesByJobID(ctx, jobID)
277-
}
278270
return nil, err
279271
}
272+
var obj rbac.Objecter
273+
switch job.Type {
274+
case database.ProvisionerJobTypeTemplateVersionDryRun, database.ProvisionerJobTypeTemplateVersionImport:
275+
obj = rbac.ResourceTemplate.InOrg(job.OrganizationID).WithOwner(job.InitiatorID.String())
276+
case database.ProvisionerJobTypeWorkspaceBuild:
277+
obj = rbac.ResourceWorkspace.InOrg(job.OrganizationID).WithOwner(job.InitiatorID.String())
278+
default:
279+
return nil, xerrors.Errorf("unknown job type: %s", job.Type)
280+
}
280281

281-
// If the workspace can be read, then the resource can be read.
282-
_, err = authorizedFetch(q.authorizer, q.database.GetWorkspaceByID)(ctx, build.WorkspaceID)
283-
if err != nil {
282+
if err := q.authorizeContext(ctx, rbac.ActionRead, obj); err != nil {
284283
return nil, err
285284
}
286285
return q.database.GetWorkspaceResourcesByJobID(ctx, jobID)

0 commit comments

Comments
 (0)