Skip to content

Commit 8024c1d

Browse files
authored
fix: allow workspace owners to view timings (#15364)
Anyone with authz access to a workspace should be able to read timings information of its builds. To do this without `AsSystemContext` would do an extra 4 db calls.
1 parent 2cf7457 commit 8024c1d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

coderd/workspacebuilds.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ func (api *API) buildTimings(ctx context.Context, build database.WorkspaceBuild)
952952
return codersdk.WorkspaceBuildTimings{}, xerrors.Errorf("fetching provisioner job timings: %w", err)
953953
}
954954

955-
agentScriptTimings, err := api.Database.GetWorkspaceAgentScriptTimingsByBuildID(ctx, build.ID)
955+
//nolint:gocritic // Already checked if the build can be fetched.
956+
agentScriptTimings, err := api.Database.GetWorkspaceAgentScriptTimingsByBuildID(dbauthz.AsSystemRestricted(ctx), build.ID)
956957
if err != nil && !errors.Is(err, sql.ErrNoRows) {
957958
return codersdk.WorkspaceBuildTimings{}, xerrors.Errorf("fetching workspace agent script timings: %w", err)
958959
}
@@ -965,7 +966,8 @@ func (api *API) buildTimings(ctx context.Context, build database.WorkspaceBuild)
965966
for _, resource := range resources {
966967
resourceIDs = append(resourceIDs, resource.ID)
967968
}
968-
agents, err := api.Database.GetWorkspaceAgentsByResourceIDs(ctx, resourceIDs)
969+
//nolint:gocritic // Already checked if the build can be fetched.
970+
agents, err := api.Database.GetWorkspaceAgentsByResourceIDs(dbauthz.AsSystemRestricted(ctx), resourceIDs)
969971
if err != nil && !errors.Is(err, sql.ErrNoRows) {
970972
return codersdk.WorkspaceBuildTimings{}, xerrors.Errorf("fetching workspace agents: %w", err)
971973
}

coderd/workspacebuilds_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1188,17 +1188,19 @@ func TestWorkspaceBuildTimings(t *testing.T) {
11881188

11891189
// Setup the test environment with a template and version
11901190
db, pubsub := dbtestutil.NewDB(t)
1191-
client := coderdtest.New(t, &coderdtest.Options{
1191+
ownerClient := coderdtest.New(t, &coderdtest.Options{
11921192
Database: db,
11931193
Pubsub: pubsub,
11941194
})
1195-
owner := coderdtest.CreateFirstUser(t, client)
1195+
owner := coderdtest.CreateFirstUser(t, ownerClient)
1196+
client, user := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID)
1197+
11961198
file := dbgen.File(t, db, database.File{
11971199
CreatedBy: owner.UserID,
11981200
})
11991201
versionJob := dbgen.ProvisionerJob(t, db, pubsub, database.ProvisionerJob{
12001202
OrganizationID: owner.OrganizationID,
1201-
InitiatorID: owner.UserID,
1203+
InitiatorID: user.ID,
12021204
FileID: file.ID,
12031205
Tags: database.StringMap{
12041206
"custom": "true",
@@ -1219,7 +1221,7 @@ func TestWorkspaceBuildTimings(t *testing.T) {
12191221
// build number, each test will have its own workspace and build.
12201222
makeBuild := func() database.WorkspaceBuild {
12211223
ws := dbgen.Workspace(t, db, database.WorkspaceTable{
1222-
OwnerID: owner.UserID,
1224+
OwnerID: user.ID,
12231225
OrganizationID: owner.OrganizationID,
12241226
TemplateID: template.ID,
12251227
})

0 commit comments

Comments
 (0)