From 2c2e98cc3974b21929e1ab9b01c971a757d36674 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 4 Oct 2023 15:50:51 +0100 Subject: [PATCH] fix(coderd): fetch workspace agent scripts and log sources using system auth ctx (#10043) * add failing unit test * fetch log sources and agent scripts using system auth ctx --- coderd/workspaceagents.go | 6 ++++-- coderd/workspaceagents_test.go | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/coderd/workspaceagents.go b/coderd/workspaceagents.go index a3b624dc37..6c8eed5734 100644 --- a/coderd/workspaceagents.go +++ b/coderd/workspaceagents.go @@ -68,11 +68,13 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) { return err }) eg.Go(func() (err error) { - scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(ctx, []uuid.UUID{workspaceAgent.ID}) + //nolint:gocritic // TODO: can we make this not require system restricted? + scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(dbauthz.AsSystemRestricted(ctx), []uuid.UUID{workspaceAgent.ID}) return err }) eg.Go(func() (err error) { - logSources, err = api.Database.GetWorkspaceAgentLogSourcesByAgentIDs(ctx, []uuid.UUID{workspaceAgent.ID}) + //nolint:gocritic // TODO: can we make this not require system restricted? + logSources, err = api.Database.GetWorkspaceAgentLogSourcesByAgentIDs(dbauthz.AsSystemRestricted(ctx), []uuid.UUID{workspaceAgent.ID}) return err }) err := eg.Wait() diff --git a/coderd/workspaceagents_test.go b/coderd/workspaceagents_test.go index 014a2b941d..687bfe4275 100644 --- a/coderd/workspaceagents_test.go +++ b/coderd/workspaceagents_test.go @@ -65,16 +65,18 @@ func TestWorkspaceAgent(t *testing.T) { }) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) - workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID) - coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) + + anotherClient, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID) + workspace := coderdtest.CreateWorkspace(t, anotherClient, user.OrganizationID, template.ID) + coderdtest.AwaitWorkspaceBuildJobCompleted(t, anotherClient, workspace.LatestBuild.ID) ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) defer cancel() - workspace, err := client.Workspace(ctx, workspace.ID) + workspace, err := anotherClient.Workspace(ctx, workspace.ID) require.NoError(t, err) require.Equal(t, tmpDir, workspace.LatestBuild.Resources[0].Agents[0].Directory) - _, err = client.WorkspaceAgent(ctx, workspace.LatestBuild.Resources[0].Agents[0].ID) + _, err = anotherClient.WorkspaceAgent(ctx, workspace.LatestBuild.Resources[0].Agents[0].ID) require.NoError(t, err) require.True(t, workspace.LatestBuild.Resources[0].Agents[0].Health.Healthy) })