From 3e2e2ac49e1487ff3de2a845569d188d34e68ea8 Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Thu, 22 Dec 2022 15:20:35 -0800 Subject: [PATCH] fix: enforce unique agent names per workspace (#5497) --- coderd/provisionerdserver/provisionerdserver.go | 10 +++++++++- provisioner/terraform/resources.go | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index 88881c91b6..4425e498f3 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -854,8 +854,16 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid. } snapshot.WorkspaceResources = append(snapshot.WorkspaceResources, telemetry.ConvertWorkspaceResource(resource)) - appSlugs := make(map[string]struct{}) + var ( + agentNames = make(map[string]struct{}) + appSlugs = make(map[string]struct{}) + ) for _, prAgent := range protoResource.Agents { + if _, ok := agentNames[prAgent.Name]; ok { + return xerrors.Errorf("duplicate agent name %q", prAgent.Name) + } + agentNames[prAgent.Name] = struct{}{} + var instanceID sql.NullString if prAgent.GetInstanceId() != "" { instanceID = sql.NullString{ diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index 7ffdf14806..474b2a7985 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -101,6 +101,7 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res findTerraformResources(module) // Find all agents! + agentNames := map[string]struct{}{} for _, tfResource := range tfResourceByLabel { if tfResource.Type != "coder_agent" { continue @@ -110,6 +111,12 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res if err != nil { return nil, xerrors.Errorf("decode agent attributes: %w", err) } + + if _, ok := agentNames[tfResource.Name]; ok { + return nil, xerrors.Errorf("duplicate agent name: %s", tfResource.Name) + } + agentNames[tfResource.Name] = struct{}{} + agent := &proto.Agent{ Name: tfResource.Name, Id: attrs.ID,