Skip to content

Commit b9d5d30

Browse files
authored
Update SBOM file format and extract different payload (#1123)
* Update SBOM file format and extract different payload * Update changelog * Strip any leading v from version string
1 parent 3eb07f4 commit b9d5d30

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ All notable changes to `src-cli` are documented in this file.
1111

1212
## Unreleased
1313

14+
## 5.9.1
15+
16+
- Update SBOM output file extension from `.json` to `.cdx.json` [#1123](https://github.com/sourcegraph/src-cli/pull/1123)
17+
- Improve SBOM compatibility with scanning tools [#1123](https://github.com/sourcegraph/src-cli/pull/1123)
18+
1419
## 5.9.0
1520

1621
## 5.8.2

cmd/src/sbom_fetch.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111
"os"
1212
"os/exec"
13-
"path"
1413
"path/filepath"
1514
"strings"
1615
"unicode"
@@ -71,12 +70,12 @@ Examples:
7170
if versionFlag == nil || *versionFlag == "" {
7271
return cmderrors.Usage("version is required")
7372
}
74-
c.version = *versionFlag
73+
c.version = sanitizeVersion(*versionFlag)
7574

7675
if outputDirFlag == nil || *outputDirFlag == "" {
7776
return cmderrors.Usage("output directory is required")
7877
}
79-
c.outputDir = getOutputDir(*outputDirFlag, *versionFlag)
78+
c.outputDir = getOutputDir(*outputDirFlag, c.version)
8079

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

286-
return string(decodedPayload), nil
285+
// Unmarshal the decoded payload to extract predicate
286+
var payload map[string]json.RawMessage
287+
if err := json.Unmarshal(decodedPayload, &payload); err != nil {
288+
return "", fmt.Errorf("failed to unmarshal decoded payload: %w", err)
289+
}
290+
291+
// Extract just the predicate field
292+
predicate, ok := payload["predicate"]
293+
if !ok {
294+
return "", fmt.Errorf("no predicate field found in payload")
295+
}
296+
297+
return string(predicate), nil
287298
}
288299

289300
func (c sbomConfig) storeSBOM(sbom string, image string) error {
@@ -296,7 +307,7 @@ func (c sbomConfig) storeSBOM(sbom string, image string) error {
296307
}, image)
297308

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

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

314-
func getOutputDir(parentDir, version string) string {
315-
return path.Join(parentDir, "sourcegraph-"+version)
316-
}
317-
318325
// getImageReleaseListURL returns the URL for the list of images in a release, based on the version and whether it's an internal release.
319326
func (c *sbomConfig) getImageReleaseListURL() string {
320327
if c.internalRelease {

cmd/src/sbom_utils.go

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net/http"
88
"os/exec"
9+
"path"
910
"strings"
1011
"time"
1112
)
@@ -190,3 +191,12 @@ func spinner(name string, stop chan bool) {
190191
}
191192
}
192193
}
194+
195+
func getOutputDir(parentDir, version string) string {
196+
return path.Join(parentDir, "sourcegraph-"+version)
197+
}
198+
199+
// sanitizeVersion removes any leading "v" from the version string
200+
func sanitizeVersion(version string) string {
201+
return strings.TrimPrefix(version, "v")
202+
}

0 commit comments

Comments
 (0)