Skip to content

Commit ed93d09

Browse files
committed
return 404 if external_agent is not associated with agent
1 parent fa715a9 commit ed93d09

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

enterprise/coderd/workspaceagents.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ func (api *API) workspaceExternalAgentCredentials(rw http.ResponseWriter, r *htt
5252
return
5353
}
5454

55+
resources, err := api.Database.GetWorkspaceResourcesByJobID(ctx, build.JobID)
56+
if err != nil {
57+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
58+
Message: "Failed to get workspace resources.",
59+
Detail: err.Error(),
60+
})
61+
return
62+
}
63+
64+
found := false
65+
for _, resource := range resources {
66+
if resource.Type == "coder_external_agent" && resource.Name == agentName {
67+
found = true
68+
break
69+
}
70+
}
71+
if !found {
72+
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
73+
Message: fmt.Sprintf("Agent '%s' does not have an external agent associated with it.", agentName),
74+
})
75+
return
76+
}
77+
5578
agents, err := api.Database.GetWorkspaceAgentsByWorkspaceAndBuildNumber(ctx, database.GetWorkspaceAgentsByWorkspaceAndBuildNumberParams{
5679
WorkspaceID: workspace.ID,
5780
BuildNumber: build.BuildNumber,

enterprise/coderd/workspaceagents_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ func TestWorkspaceExternalAgentCredentials(t *testing.T) {
370370
Bool: true,
371371
Valid: true,
372372
},
373+
}).Resource(&proto.Resource{
374+
Name: "test-agent",
375+
Type: "coder_external_agent",
373376
}).WithAgent(func(a []*proto.Agent) []*proto.Agent {
374377
a[0].Name = "test-agent"
375378
a[0].OperatingSystem = "linux"
@@ -393,6 +396,9 @@ func TestWorkspaceExternalAgentCredentials(t *testing.T) {
393396
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
394397
OrganizationID: user.OrganizationID,
395398
OwnerID: user.UserID,
399+
}).Resource(&proto.Resource{
400+
Name: "test-agent",
401+
Type: "coder_external_agent",
396402
}).Seed(database.WorkspaceBuild{
397403
HasExternalAgent: sql.NullBool{
398404
Bool: true,
@@ -426,6 +432,9 @@ func TestWorkspaceExternalAgentCredentials(t *testing.T) {
426432
Bool: true,
427433
Valid: true,
428434
},
435+
}).Resource(&proto.Resource{
436+
Name: "test-agent",
437+
Type: "coder_external_agent",
429438
}).WithAgent(func(a []*proto.Agent) []*proto.Agent {
430439
a[0].Name = "test-agent"
431440
a[0].Auth = &proto.Agent_InstanceId{
@@ -456,4 +465,34 @@ func TestWorkspaceExternalAgentCredentials(t *testing.T) {
456465
require.ErrorAs(t, err, &apiErr)
457466
require.Equal(t, "Workspace does not have an external agent.", apiErr.Message)
458467
})
468+
469+
t.Run("No external agent associated with agent - should return 404", func(t *testing.T) {
470+
t.Parallel()
471+
ctx := testutil.Context(t, testutil.WaitShort)
472+
473+
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
474+
OrganizationID: user.OrganizationID,
475+
OwnerID: user.UserID,
476+
}).Seed(database.WorkspaceBuild{
477+
HasExternalAgent: sql.NullBool{
478+
Bool: true,
479+
Valid: true,
480+
},
481+
}).Resource(&proto.Resource{
482+
Name: "test-external-agent",
483+
Type: "coder_external_agent",
484+
}).WithAgent(func(a []*proto.Agent) []*proto.Agent {
485+
a[0].Name = "test-agent-no-external-agent"
486+
a[0].Auth = &proto.Agent_Token{
487+
Token: uuid.NewString(),
488+
}
489+
return a
490+
}).Do()
491+
492+
_, err := client.WorkspaceExternalAgentCredentials(ctx, r.Workspace.ID, "test-agent-no-external-agent")
493+
require.Error(t, err)
494+
var apiErr *codersdk.Error
495+
require.ErrorAs(t, err, &apiErr)
496+
require.Equal(t, "Agent 'test-agent-no-external-agent' does not have an external agent associated with it.", apiErr.Message)
497+
})
459498
}

0 commit comments

Comments
 (0)