From 9267d933bce4a8d229b4ebf53a259ae6c5a6c07e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 4 Oct 2022 17:24:24 -0400 Subject: [PATCH] command: report multiple errors in bad HCP config When HCP is detected to be enabled, but some configuration is missing, we returned immediately on the first error. This commit changes this behaviour by reporting every error at once, so users will know immediately if something is wrong when they invoke Packer with HCP support, and one or more environment variables is not defined as we'd expect them. --- command/registry.go | 38 +++++++++++++++++++++++++++----------- command/registry_test.go | 3 +++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/command/registry.go b/command/registry.go index 60ba0e1f0..78b89f514 100644 --- a/command/registry.go +++ b/command/registry.go @@ -80,7 +80,6 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics " block(s). If this " + buildLabel + " is not meant for the Packer registry please " + "clear any HCP_PACKER_* environment variables."), }) - return diags } var err error @@ -94,8 +93,6 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics Detail: fmt.Sprintf("%s", err), Severity: hcl.DiagError, }) - - return diags } pc.Bucket.LoadDefaultSettingsFromEnv() @@ -119,17 +116,28 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics } } + if !env.HasHCPCredentials() { + diags = append(diags, &hcl.Diagnostic{ + Summary: "HCP authentication information required", + Detail: fmt.Sprintf("The client authentication requires both %s and %s environment "+ + "variables to be set for authenticating with HCP.", + env.HCPClientID, + env.HCPClientSecret), + Severity: hcl.DiagError, + }) + } + if pc.Bucket.Slug == "" { - return append(diags, &hcl.Diagnostic{ + diags = append(diags, &hcl.Diagnostic{ Summary: "bucket name cannot be empty", Detail: "empty bucket name, please set it with the HCP_PACKER_BUCKET_NAME environment variable, or in a `hcp_packer_registry` block", Severity: hcl.DiagError, }) } - vals, diags := pc.Datasources.Values() - if diags != nil { - return diags + vals, dsDiags := pc.Datasources.Values() + if dsDiags != nil { + diags = append(diags, dsDiags...) } imageDS, imageOK := vals[hcpImageDatasourceType] @@ -137,7 +145,7 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics // If we don't have any image or iteration defined, we can return directly if !imageOK && !iterOK { - return nil + return diags } iterations := map[string]iterds.DatasourceOutput{} @@ -280,6 +288,14 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics { var diags hcl.Diagnostics + if !env.HasHCPCredentials() { + diags = append(diags, &hcl.Diagnostic{ + Summary: "missing authentication information", + Detail: fmt.Sprintf("the client authentication requires both %s and %s environment variables to be set", env.HCPClientID, env.HCPClientSecret), + Severity: hcl.DiagError, + }) + } + var err error core := cfg.Core @@ -288,7 +304,7 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics { TemplateBaseDir: filepath.Dir(core.Template.Path), }) if err != nil { - return append(diags, &hcl.Diagnostic{ + diags = append(diags, &hcl.Diagnostic{ Summary: "bucket creation failure", Detail: fmt.Sprintf("failed to create Bucket: %s", err), Severity: hcl.DiagError, @@ -297,7 +313,7 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics { core.Bucket.LoadDefaultSettingsFromEnv() if core.Bucket.Slug == "" { - return append(diags, &hcl.Diagnostic{ + diags = append(diags, &hcl.Diagnostic{ Summary: "bucket name cannot be empty", Detail: "empty bucket name, please set it with the HCP_PACKER_BUCKET_NAME environment variable", Severity: hcl.DiagError, @@ -309,5 +325,5 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics { core.Bucket.RegisterBuildForComponent(b.Name) } - return nil + return diags } diff --git a/command/registry_test.go b/command/registry_test.go index 39c930cb6..42348725a 100644 --- a/command/registry_test.go +++ b/command/registry_test.go @@ -400,6 +400,9 @@ func TestRegistrySetup(t *testing.T) { }, } + t.Setenv("HCP_CLIENT_ID", "test") + t.Setenv("HCP_CLIENT_SECRET", "test") + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { runRegistryTest(t, tt)