Skip to content

Commit

Permalink
Update SBOM file format and extract different payload (#1123)
Browse files Browse the repository at this point in the history
* Update SBOM file format and extract different payload

* Update changelog

* Strip any leading v from version string
  • Loading branch information
willdollman authored Nov 11, 2024
1 parent 3eb07f4 commit b9d5d30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ All notable changes to `src-cli` are documented in this file.

## Unreleased

## 5.9.1

- Update SBOM output file extension from `.json` to `.cdx.json` [#1123](https://github.com/sourcegraph/src-cli/pull/1123)
- Improve SBOM compatibility with scanning tools [#1123](https://github.com/sourcegraph/src-cli/pull/1123)

## 5.9.0

## 5.8.2
Expand Down
25 changes: 16 additions & 9 deletions cmd/src/sbom_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"unicode"
Expand Down Expand Up @@ -71,12 +70,12 @@ Examples:
if versionFlag == nil || *versionFlag == "" {
return cmderrors.Usage("version is required")
}
c.version = *versionFlag
c.version = sanitizeVersion(*versionFlag)

if outputDirFlag == nil || *outputDirFlag == "" {
return cmderrors.Usage("output directory is required")
}
c.outputDir = getOutputDir(*outputDirFlag, *versionFlag)
c.outputDir = getOutputDir(*outputDirFlag, c.version)

if internalReleaseFlag == nil || !*internalReleaseFlag {
c.internalRelease = false
Expand Down Expand Up @@ -283,7 +282,19 @@ func extractSBOM(attestationBytes []byte) (string, error) {
return "", fmt.Errorf("failed to decode payload: %w", err)
}

return string(decodedPayload), nil
// Unmarshal the decoded payload to extract predicate
var payload map[string]json.RawMessage
if err := json.Unmarshal(decodedPayload, &payload); err != nil {
return "", fmt.Errorf("failed to unmarshal decoded payload: %w", err)
}

// Extract just the predicate field
predicate, ok := payload["predicate"]
if !ok {
return "", fmt.Errorf("no predicate field found in payload")
}

return string(predicate), nil
}

func (c sbomConfig) storeSBOM(sbom string, image string) error {
Expand All @@ -296,7 +307,7 @@ func (c sbomConfig) storeSBOM(sbom string, image string) error {
}, image)

// Create the output file path
outputFile := filepath.Join(c.outputDir, safeImageName+".json")
outputFile := filepath.Join(c.outputDir, safeImageName+".cdx.json")

// Ensure the output directory exists
if err := os.MkdirAll(c.outputDir, 0755); err != nil {
Expand All @@ -311,10 +322,6 @@ func (c sbomConfig) storeSBOM(sbom string, image string) error {
return nil
}

func getOutputDir(parentDir, version string) string {
return path.Join(parentDir, "sourcegraph-"+version)
}

// getImageReleaseListURL returns the URL for the list of images in a release, based on the version and whether it's an internal release.
func (c *sbomConfig) getImageReleaseListURL() string {
if c.internalRelease {
Expand Down
10 changes: 10 additions & 0 deletions cmd/src/sbom_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"os/exec"
"path"
"strings"
"time"
)
Expand Down Expand Up @@ -190,3 +191,12 @@ func spinner(name string, stop chan bool) {
}
}
}

func getOutputDir(parentDir, version string) string {
return path.Join(parentDir, "sourcegraph-"+version)
}

// sanitizeVersion removes any leading "v" from the version string
func sanitizeVersion(version string) string {
return strings.TrimPrefix(version, "v")
}

0 comments on commit b9d5d30

Please sign in to comment.