@@ -2,6 +2,8 @@ package authzquery
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
6
+ "errors"
5
7
6
8
"golang.org/x/xerrors"
7
9
@@ -80,11 +82,18 @@ func (q *AuthzQuerier) GetWorkspaceAgentsByResourceIDs(ctx context.Context, ids
80
82
}
81
83
82
84
for _ , a := range agents {
83
- // Check if we can fetch the agent.
85
+ // Check if we can fetch the workspace by the agent ID .
84
86
_ , 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
87
94
}
95
+ // Otherwise, we cannot read the workspace, so we cannot read the agent.
96
+ return nil , err
88
97
}
89
98
return agents , nil
90
99
}
@@ -256,31 +265,21 @@ func (q *AuthzQuerier) GetWorkspaceResourceMetadataByResourceIDs(ctx context.Con
256
265
}
257
266
258
267
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 )
260
269
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
- }
278
270
return nil , err
279
271
}
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
+ }
280
281
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 {
284
283
return nil , err
285
284
}
286
285
return q .database .GetWorkspaceResourcesByJobID (ctx , jobID )
0 commit comments