Skip to content

Commit 7b5ffb0

Browse files
leodidoona-agent
andcommitted
refactor: consolidate SBOM extension constants in cache package
Move SBOM file extension constants to pkg/leeway/cache/types.go to avoid duplication across sbom.go, s3.go, and signing/upload.go. Add SBOMSidecarExtensions() function that returns all SBOM sidecar file extensions, ensuring consistency across the codebase. Co-authored-by: Ona <[email protected]>
1 parent 7e41168 commit 7b5ffb0

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

pkg/leeway/cache/remote/s3.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,23 +1446,11 @@ func (s *S3Cache) uploadProvenanceBundle(ctx context.Context, packageName, artif
14461446
}).Debug("Successfully uploaded provenance bundle to remote cache")
14471447
}
14481448

1449-
// SBOM file extensions - must match pkg/leeway/sbom.go constants
1450-
const (
1451-
sbomBaseFilename = "sbom"
1452-
sbomCycloneDXFileExtension = ".cdx.json"
1453-
sbomSPDXFileExtension = ".spdx.json"
1454-
sbomSyftFileExtension = ".json"
1455-
)
1456-
14571449
// uploadSBOMFiles uploads SBOM files to S3 with retry logic.
14581450
// This is a non-blocking operation - failures are logged but don't fail the build.
14591451
// SBOM files are stored alongside artifacts as <artifact>.sbom.<ext>
14601452
func (s *S3Cache) uploadSBOMFiles(ctx context.Context, packageName, artifactKey, localPath string) {
1461-
sbomExtensions := []string{
1462-
"." + sbomBaseFilename + sbomCycloneDXFileExtension,
1463-
"." + sbomBaseFilename + sbomSPDXFileExtension,
1464-
"." + sbomBaseFilename + sbomSyftFileExtension,
1465-
}
1453+
sbomExtensions := cache.SBOMSidecarExtensions()
14661454

14671455
for _, ext := range sbomExtensions {
14681456
sbomPath := localPath + ext
@@ -1510,11 +1498,7 @@ func (s *S3Cache) uploadSBOMFiles(ctx context.Context, packageName, artifactKey,
15101498
// This is a best-effort operation - missing SBOMs are expected for older artifacts.
15111499
// SBOM files are stored alongside artifacts as <artifact>.sbom.<ext>
15121500
func (s *S3Cache) downloadSBOMFiles(ctx context.Context, packageName, artifactKey, localPath string) {
1513-
sbomExtensions := []string{
1514-
"." + sbomBaseFilename + sbomCycloneDXFileExtension,
1515-
"." + sbomBaseFilename + sbomSPDXFileExtension,
1516-
"." + sbomBaseFilename + sbomSyftFileExtension,
1517-
}
1501+
sbomExtensions := cache.SBOMSidecarExtensions()
15181502

15191503
for _, ext := range sbomExtensions {
15201504
sbomPath := localPath + ext

pkg/leeway/cache/types.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Package cache provides local and remote caching capabilities for build artifacts.
22
//
3+
// SBOM Sidecar Files:
4+
// SBOM (Software Bill of Materials) files are stored alongside artifacts as sidecar files.
5+
// The naming convention is: <artifact>.<extension> where extension is one of:
6+
// - .sbom.cdx.json (CycloneDX format)
7+
// - .sbom.spdx.json (SPDX format)
8+
// - .sbom.json (Syft native format)
9+
//
310
// SLSA Verification Behavior:
411
// The cache system supports SLSA (Supply-chain Levels for Software Artifacts) verification
512
// for enhanced security. The behavior is controlled by the SLSAConfig.RequireAttestation field:
@@ -27,6 +34,31 @@ import (
2734
"context"
2835
)
2936

37+
// SBOM file format constants
38+
const (
39+
// SBOMBaseFilename is the base filename for SBOM files (e.g., "sbom" in "artifact.sbom.cdx.json")
40+
SBOMBaseFilename = "sbom"
41+
42+
// SBOMCycloneDXFileExtension is the extension of the CycloneDX SBOM file
43+
SBOMCycloneDXFileExtension = ".cdx.json"
44+
45+
// SBOMSPDXFileExtension is the extension of the SPDX SBOM file
46+
SBOMSPDXFileExtension = ".spdx.json"
47+
48+
// SBOMSyftFileExtension is the extension of the Syft SBOM file
49+
SBOMSyftFileExtension = ".json"
50+
)
51+
52+
// SBOMSidecarExtensions returns all SBOM sidecar file extensions.
53+
// These are the extensions used for SBOM files stored alongside artifacts.
54+
func SBOMSidecarExtensions() []string {
55+
return []string{
56+
"." + SBOMBaseFilename + SBOMCycloneDXFileExtension, // .sbom.cdx.json
57+
"." + SBOMBaseFilename + SBOMSPDXFileExtension, // .sbom.spdx.json
58+
"." + SBOMBaseFilename + SBOMSyftFileExtension, // .sbom.json
59+
}
60+
}
61+
3062
// Package represents a build package that can be cached
3163
type Package interface {
3264
// Version returns a unique identifier for the package

pkg/leeway/sbom.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/anchore/syft/syft/format/syftjson"
2626
"github.com/anchore/syft/syft/sbom"
2727
"github.com/anchore/syft/syft/source"
28+
"github.com/gitpod-io/leeway/pkg/leeway/cache"
2829
"github.com/google/uuid"
2930
log "github.com/sirupsen/logrus"
3031
"golang.org/x/xerrors"
@@ -39,18 +40,6 @@ const (
3940

4041
// EnvvarVulnReportsDir names the environment variable we take the vulnerability reports directory location from
4142
EnvvarVulnReportsDir = "LEEWAY_VULN_REPORTS_DIR"
42-
43-
// SBOM file format constants
44-
sbomBaseFilename = "sbom"
45-
46-
// sbomCycloneDXFileExtension is the extension of the CycloneDX SBOM file we store in the archived build artifacts
47-
sbomCycloneDXFileExtension = ".cdx.json"
48-
49-
// sbomSPDXFileExtension is the extension of the SPDX SBOM file we store in the archived build artifacts
50-
sbomSPDXFileExtension = ".spdx.json"
51-
52-
// sbomSyftFileExtension is the extension of the Syft SBOM file we store in the archived build artifacts
53-
sbomSyftFileExtension = ".json"
5443
)
5544

5645
// WorkspaceSBOM configures SBOM generation for a workspace
@@ -362,14 +351,14 @@ func writeSBOMToCache(buildctx *buildContext, p *Package, builddir string) (err
362351
}
363352

364353
// Normalize CycloneDX
365-
cycloneDXPath := artifactPath + "." + sbomBaseFilename + sbomCycloneDXFileExtension
354+
cycloneDXPath := artifactPath + "." + cache.SBOMBaseFilename + cache.SBOMCycloneDXFileExtension
366355
if err := normalizeCycloneDX(cycloneDXPath, timestamp); err != nil {
367356
buildctx.Reporter.PackageBuildLog(p, true,
368357
[]byte(fmt.Sprintf("Warning: failed to normalize CycloneDX SBOM: %v\n", err)))
369358
}
370359

371360
// Normalize SPDX
372-
spdxPath := artifactPath + "." + sbomBaseFilename + sbomSPDXFileExtension
361+
spdxPath := artifactPath + "." + cache.SBOMBaseFilename + cache.SBOMSPDXFileExtension
373362
if err := normalizeSPDX(spdxPath, timestamp); err != nil {
374363
buildctx.Reporter.PackageBuildLog(p, true,
375364
[]byte(fmt.Sprintf("Warning: failed to normalize SPDX SBOM: %v\n", err)))
@@ -392,21 +381,21 @@ func getSBOMEncoder(format string) (encoder sbom.FormatEncoder, filename string,
392381
if err != nil {
393382
return nil, "", xerrors.Errorf("failed to create CycloneDX encoder: %w", err)
394383
}
395-
fileExtension = sbomCycloneDXFileExtension
384+
fileExtension = cache.SBOMCycloneDXFileExtension
396385
case "spdx":
397386
encoder, err = spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig())
398387
if err != nil {
399388
return nil, "", xerrors.Errorf("failed to create SPDX encoder: %w", err)
400389
}
401-
fileExtension = sbomSPDXFileExtension
390+
fileExtension = cache.SBOMSPDXFileExtension
402391
case "syft":
403392
encoder = syftjson.NewFormatEncoder()
404-
fileExtension = sbomSyftFileExtension
393+
fileExtension = cache.SBOMSyftFileExtension
405394
default:
406395
return nil, "", xerrors.Errorf("unsupported SBOM format: %s", format)
407396
}
408397

409-
return encoder, sbomBaseFilename + fileExtension, nil
398+
return encoder, cache.SBOMBaseFilename + fileExtension, nil
410399
}
411400

412401
// writeFileHandler returns a handler function for AccessSBOMInCachedArchive that writes to a file.
@@ -442,11 +431,11 @@ func ValidateSBOMFormat(format string) (bool, []string) {
442431
func GetSBOMFileExtension(format string) string {
443432
switch format {
444433
case "cyclonedx":
445-
return sbomCycloneDXFileExtension
434+
return cache.SBOMCycloneDXFileExtension
446435
case "spdx":
447-
return sbomSPDXFileExtension
436+
return cache.SBOMSPDXFileExtension
448437
case "syft":
449-
return sbomSyftFileExtension
438+
return cache.SBOMSyftFileExtension
450439
default:
451440
return ".json"
452441
}
@@ -474,7 +463,7 @@ func AccessSBOMInCachedArchive(fn string, format string, handler func(sbomFile i
474463
}
475464

476465
// Try reading from separate SBOM file first (new format)
477-
sbomExt := "." + sbomBaseFilename + GetSBOMFileExtension(format)
466+
sbomExt := "." + cache.SBOMBaseFilename + GetSBOMFileExtension(format)
478467
sbomPath := fn + sbomExt
479468

480469
if _, statErr := os.Stat(sbomPath); statErr == nil {
@@ -497,7 +486,7 @@ func AccessSBOMInCachedArchive(fn string, format string, handler func(sbomFile i
497486

498487
// accessSBOMInTarArchive extracts an SBOM file from inside a tar.gz archive (legacy format).
499488
func accessSBOMInTarArchive(fn string, format string, handler func(sbomFile io.Reader) error) error {
500-
sbomFilename := sbomBaseFilename + GetSBOMFileExtension(format)
489+
sbomFilename := cache.SBOMBaseFilename + GetSBOMFileExtension(format)
501490

502491
f, err := os.Open(fn)
503492
if err != nil {

pkg/leeway/signing/upload.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ func (u *ArtifactUploader) UploadArtifactWithAttestation(ctx context.Context, ar
176176
// uploadSBOMFiles uploads SBOM sidecar files alongside the artifact.
177177
// This is a non-blocking operation - failures are logged but don't fail the upload.
178178
func (u *ArtifactUploader) uploadSBOMFiles(ctx context.Context, artifactPath, artifactKey string) {
179-
// SBOM file extensions - must match pkg/leeway/sbom.go constants
180-
sbomExtensions := []string{
181-
".sbom.cdx.json", // CycloneDX format
182-
".sbom.spdx.json", // SPDX format
183-
".sbom.json", // Syft native format
184-
}
179+
sbomExtensions := cache.SBOMSidecarExtensions()
185180

186181
for _, ext := range sbomExtensions {
187182
sbomPath := artifactPath + ext

0 commit comments

Comments
 (0)