From 347c57306c1b9917e4fe2435a04f70f0b7436e11 Mon Sep 17 00:00:00 2001 From: Jenna Goldstrich Date: Mon, 6 Jan 2025 18:02:08 -0800 Subject: [PATCH] hcp: use enum for HCP SBOM upload Since the protos for uploading an SBOM for a build have been changed to use an enumeration instead of a plain string with the latest revisions to the HCP Packer SBOM support feature, we update how we reference those values for the SBOM format to use that enum instead. --- internal/hcp/registry/types.bucket.go | 2 +- packer/build.go | 3 ++- packer/provisioner.go | 3 ++- provisioner/hcp-sbom/provisioner.go | 5 +++-- provisioner/hcp-sbom/validate.go | 7 ++++--- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/hcp/registry/types.bucket.go b/internal/hcp/registry/types.bucket.go index b9be5fd81..184a46e2a 100644 --- a/internal/hcp/registry/types.bucket.go +++ b/internal/hcp/registry/types.bucket.go @@ -246,7 +246,7 @@ func (bucket *Bucket) uploadSbom(ctx context.Context, buildName string, sbom pac Body: &hcpPackerModels.HashicorpCloudPacker20230101UploadSbomBody{ CompressedSbom: sbom.CompressedData, Name: sbom.Name, - Format: sbom.Format, + Format: &sbom.Format, }, }, nil, diff --git a/packer/build.go b/packer/build.go index d23637f67..4a311461e 100644 --- a/packer/build.go +++ b/packer/build.go @@ -9,6 +9,7 @@ import ( "log" "sync" + hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models" "github.com/hashicorp/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata" @@ -56,7 +57,7 @@ type CoreBuild struct { type SBOM struct { Name string - Format string + Format hcpPackerModels.HashicorpCloudPacker20230101SbomFormat CompressedData []byte } diff --git a/packer/provisioner.go b/packer/provisioner.go index 24e20b3a2..4be4f99dd 100644 --- a/packer/provisioner.go +++ b/packer/provisioner.go @@ -12,6 +12,7 @@ import ( hcpSbomProvisioner "github.com/hashicorp/packer/provisioner/hcp-sbom" + hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models" "github.com/klauspost/compress/zstd" "time" @@ -249,7 +250,7 @@ func (p *DebuggedProvisioner) Provision(ctx context.Context, ui packersdk.Ui, co type SBOMInternalProvisioner struct { Provisioner packersdk.Provisioner CompressedData []byte - SBOMFormat string + SBOMFormat hcpPackerModels.HashicorpCloudPacker20230101SbomFormat SBOMName string } diff --git a/provisioner/hcp-sbom/provisioner.go b/provisioner/hcp-sbom/provisioner.go index cbc515c13..cf03e5670 100644 --- a/provisioner/hcp-sbom/provisioner.go +++ b/provisioner/hcp-sbom/provisioner.go @@ -20,6 +20,7 @@ import ( "path/filepath" "github.com/hashicorp/hcl/v2/hcldec" + hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models" "github.com/hashicorp/packer-plugin-sdk/common" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/template/config" @@ -116,8 +117,8 @@ type PackerSBOM struct { RawSBOM []byte `json:"raw_sbom"` // Format is the format detected by the provisioner // - // Supported values: `spdx` or `cyclonedx` - Format string `json:"format"` + // Supported values: `SPDX` or `CYCLONEDX` + Format hcpPackerModels.HashicorpCloudPacker20230101SbomFormat `json:"format"` // Name is the name of the SBOM to be set on HCP Packer // // If unset, HCP Packer will generate one diff --git a/provisioner/hcp-sbom/validate.go b/provisioner/hcp-sbom/validate.go index 4f17a4ac0..7343dcb9b 100644 --- a/provisioner/hcp-sbom/validate.go +++ b/provisioner/hcp-sbom/validate.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/CycloneDX/cyclonedx-go" + hcpPackerModels "github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2023-01-01/models" spdxjson "github.com/spdx/tools-golang/json" ) @@ -61,11 +62,11 @@ func validateSPDX(content []byte) error { } // validateSBOM validates the SBOM file and returns the format of the SBOM. -func validateSBOM(content []byte) (string, error) { +func validateSBOM(content []byte) (hcpPackerModels.HashicorpCloudPacker20230101SbomFormat, error) { // Try validating as SPDX spdxErr := validateSPDX(content) if spdxErr == nil { - return "spdx", nil + return hcpPackerModels.HashicorpCloudPacker20230101SbomFormatSPDX, nil } if vErr, ok := spdxErr.(*ValidationError); ok { @@ -74,7 +75,7 @@ func validateSBOM(content []byte) (string, error) { cycloneDxErr := validateCycloneDX(content) if cycloneDxErr == nil { - return "cyclonedx", nil + return hcpPackerModels.HashicorpCloudPacker20230101SbomFormatCYCLONEDX, nil } if vErr, ok := cycloneDxErr.(*ValidationError); ok {