Skip to content

Commit abddcbe

Browse files
author
Callum Styan
committed
perf(coderd/dbauthz): bypass authorization cascade for system-restricted contexts
GetWorkspaceResourceWithJobByID now checks if the context is system-restricted and, if so, directly calls the raw database query without going through the authorization cascade. This completely eliminates the GetWorkspaceBuildByJobID call for system-restricted contexts (like handleAuthInstanceID) by skipping the GetProvisionerJobByID authorization chain. Before: GetWorkspaceResourceWithJobByID → GetWorkspaceResourceByID → GetProvisionerJobByID → GetWorkspaceBuildByJobID (1 call) After: GetWorkspaceResourceWithJobByID → direct db query (0 calls) The function returns an error if called without system-restricted context to ensure it's only used in appropriate scenarios.
1 parent 5aadcb7 commit abddcbe

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3925,24 +3925,19 @@ func (q *querier) GetWorkspaceResourceByID(ctx context.Context, id uuid.UUID) (d
39253925
}
39263926

39273927
// GetWorkspaceResourceWithJobByID is an optimized version that fetches both
3928-
// the resource and job information in a single query. This reduces the number
3929-
// of database calls compared to GetWorkspaceResourceByID + GetProvisionerJobByID.
3930-
// The authorization check cascades through the job to the workspace build.
3928+
// the resource and job information in a single query. This is specifically
3929+
// designed for system-restricted contexts (like agent authentication) where
3930+
// we can bypass the authorization cascade that would normally call
3931+
// GetWorkspaceBuildByJobID.
39313932
func (q *querier) GetWorkspaceResourceWithJobByID(ctx context.Context, id uuid.UUID) (database.GetWorkspaceResourceWithJobByIDRow, error) {
3932-
// First, get just the resource to extract the job_id for authorization.
3933-
resource, err := q.db.GetWorkspaceResourceByID(ctx, id)
3934-
if err != nil {
3935-
return database.GetWorkspaceResourceWithJobByIDRow{}, err
3936-
}
3937-
3938-
// Authorize the provisioner job before fetching sensitive data (this will
3939-
// cascade to GetWorkspaceBuildByJobID).
3940-
_, err = q.GetProvisionerJobByID(ctx, resource.JobID)
3941-
if err != nil {
3942-
return database.GetWorkspaceResourceWithJobByIDRow{}, err
3933+
// Check if this is a system-restricted context.
3934+
actor, ok := ActorFromContext(ctx)
3935+
if !ok || actor.Type != rbac.SubjectTypeSystemRestricted {
3936+
return database.GetWorkspaceResourceWithJobByIDRow{}, xerrors.New("GetWorkspaceResourceWithJobByID requires system-restricted context")
39433937
}
39443938

3945-
// Now fetch the full data including job details.
3939+
// With system-restricted context, we can safely bypass the authorization
3940+
// cascade and call the database directly.
39463941
return q.db.GetWorkspaceResourceWithJobByID(ctx, id)
39473942
}
39483943

0 commit comments

Comments
 (0)