Skip to content

Commit c6dc504

Browse files
authored
Fix: Incorporate platform architecture (#1029)
🐛 Right now `--sbom-dir` with a multi-arch build just writes the same file over and over. This loosely follows the lead of apko which uses the form `sbom-{arch}.{form}.json`, but we are going with: `{app}-{platform}.{form}.json`. It is notable that `{platform}` is a superset of `{arch}` and we sanitize the string encoding replacing the `/` and `:` characters with `-`. /kind bug
1 parent c70c4c1 commit c6dc504

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.github/workflows/e2e.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ jobs:
4040
4141
# Check that using sbom-dir works.
4242
KO_DOCKER_REPO="" go run ./ build -t test --push=false --sbom-dir ./sbom-data ./test
43-
jq . ./sbom-data/test.spdx.json
43+
jq . ./sbom-data/test-linux-amd64.spdx.json
44+
45+
# Check that using sbom-dir works for multi-arch
46+
KO_DOCKER_REPO="" go run ./ build --platform=linux/amd64,linux/arm64 -t test --push=false --sbom-dir ./sbom-data2 ./test
47+
jq . ./sbom-data2/test-index.spdx.json
48+
jq . ./sbom-data2/test-linux-amd64.spdx.json
49+
jq . ./sbom-data2/test-linux-arm64.spdx.json
4450
4551
export PLATFORM=${GOOS}/${GOARCH}
4652

pkg/build/gobuild.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func spdx(version string) sbomber {
356356
return func(ctx context.Context, file string, appPath string, appFileName string, se oci.SignedEntity, dir string) ([]byte, types.MediaType, error) {
357357
switch obj := se.(type) {
358358
case oci.SignedImage:
359-
b, _, err := goversionm(ctx, file, appPath, appFileName, obj, "")
359+
b, _, err := goversionm(ctx, file, appPath, "", obj, "")
360360
if err != nil {
361361
return nil, "", err
362362
}
@@ -933,7 +933,9 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
933933
si := signed.Image(image)
934934

935935
if g.sbom != nil {
936-
sbom, mt, err := g.sbom(ctx, file, appPath, appFileName, si, g.sbomDir)
936+
// Construct a path-safe encoding of platform.
937+
pf := strings.ReplaceAll(strings.ReplaceAll(platform.String(), "/", "-"), ":", "-")
938+
sbom, mt, err := g.sbom(ctx, file, appPath, fmt.Sprintf("%s-%s", appFileName, pf), si, g.sbomDir)
937939
if err != nil {
938940
return nil, err
939941
}
@@ -1138,7 +1140,9 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
11381140
adds...)
11391141

11401142
if g.sbom != nil {
1141-
sbom, mt, err := g.sbom(ctx, "", "", "", idx, g.sbomDir)
1143+
ref := newRef(ref)
1144+
appFileName := appFilename(ref.Path())
1145+
sbom, mt, err := g.sbom(ctx, "", "", fmt.Sprintf("%s-index", appFileName), idx, g.sbomDir)
11421146
if err != nil {
11431147
return nil, err
11441148
}

0 commit comments

Comments
 (0)