Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cmd/spin-machine-dev/verbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/spf13/cobra"
"github.com/spin-stack/go-tools/versions"

"github.com/spin-stack/machine/internal/build"
"github.com/spin-stack/machine/internal/release"
Expand Down Expand Up @@ -510,7 +511,19 @@ func packed(t *testing.T, dir string) string {
"kernel/patches/0001-k.patch", "image/rootfs.qcow2", "licenses.txt"} {
put(t, filepath.Join(out, f), f, 0o644)
}
put(t, filepath.Join(out, "packages.txt"), "bash 5.2-1 amd64\n", 0o644)
// And Docker's packages as dpkg records those versions.yaml pins: release pack refuses an image without them.
list := "bash 5.2-1 amd64\n"
v, err := versions.Load(versions.File) // checkout's copy, in the working directory
if err != nil {
t.Fatal(err)
}
for _, e := range v.Entries {
if file := filepath.Base(e.URL()); strings.HasSuffix(file, ".deb") {
f := strings.Split(strings.TrimSuffix(file, ".deb"), "_")
list += f[0] + " " + f[1] + " " + f[2] + "\n"
}
}
put(t, filepath.Join(out, "packages.txt"), list, 0o644)
apk := func(only string) string {
var b strings.Builder
for _, p := range strings.Split(only, ",") {
Expand Down
43 changes: 43 additions & 0 deletions internal/release/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ func BOM(o Options, share string) (*cdx.BOM, error) {
if err != nil {
return nil, err
}
if err := fromTheirRepositories(o.Pins, image); err != nil {
return nil, err
}
parts = append(parts, sbom.New(subject("base-image", o.Version), image, nil, o.Epoch))
// QEMU, qboot, the kernel, systemd and OpenSSH are built with this repository's patches: each is in its
// component's pedigree, so the SBOM does not say upstream's source is what was compiled.
Expand Down Expand Up @@ -395,6 +398,46 @@ func BOM(o Options, share string) (*cdx.BOM, error) {
return b, nil
}

// debRepositories are the apt repositories versions.yaml pins a .deb from, each with the vendor a
// package URL names for it.
var debRepositories = map[string]string{"https://download.docker.com/linux/ubuntu": "docker"}

// fromTheirRepositories names each package versions.yaml pins as a .deb by the repository it was
// downloaded from rather than as Ubuntu's: Docker's six (#609) as pkg:deb/docker, with that
// repository's URL and the .deb's SHA-256. Named pkg:deb/ubuntu, a scanner looks for them in
// Ubuntu's advisories, where they are not, and misses Docker's (#621). A pinned .deb the image
// did not record, or recorded at another version, is an image that did not install what
// versions.yaml says it did.
func fromTheirRepositories(v *versions.Versions, image []cdx.Component) error {
for _, e := range v.Entries {
file := filepath.Base(e.URL())
if e.Kind != versions.Download || !strings.HasSuffix(file, ".deb") {
continue
}
repository, _, _ := strings.Cut(e.URL(), "/dists/")
vendor, ok := debRepositories[repository]
if !ok {
return fmt.Errorf("%s: %s is from no apt repository the SBOM knows a vendor for", e.Name, e.URL())
}
pkg, _, _ := strings.Cut(file, "_")
i := slices.IndexFunc(image, func(c cdx.Component) bool { return c.Name == pkg })
if i < 0 {
return fmt.Errorf("%s: versions.yaml pins %s, and the image's package list does not have it", e.Name, pkg)
}
c := &image[i]
// dpkg's version is the pinned one between an epoch and Docker's -1~ubuntu suffix.
rest, ok := strings.CutPrefix(c.Version[strings.Index(c.Version, ":")+1:], e.Version)
if !ok || !(rest == "" || rest[0] == '-' || rest[0] == '~') {
return fmt.Errorf("%s: versions.yaml pins %s %s, and the image has %s", e.Name, pkg, e.Version, c.Version)
}
_, arch, _ := strings.Cut(c.PackageURL, "?arch=")
c.PackageURL = "pkg:deb/" + vendor + "/" + pkg + "@" + c.Version + "?arch=" + arch + "&repository_url=" + repository
c.Hashes = &[]cdx.Hash{{Algorithm: cdx.HashAlgoSHA256, Value: e.Pin}}
c.ExternalReferences = &[]cdx.ExternalReference{{Type: cdx.ERTypeDistribution, URL: e.URL()}}
}
return nil
}

// subject is a BOM's subject as go-tools' sbom command names one.
func subject(name, version string) cdx.Component {
return cdx.Component{Type: cdx.ComponentTypeApplication, Name: name, Version: version,
Expand Down
57 changes: 53 additions & 4 deletions internal/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -67,7 +68,17 @@ func tree(t *testing.T) (root, out string) {
"kernel/patches/0001-k.patch", "image/rootfs.qcow2", "licenses.txt"} {
put(t, filepath.Join(out, f), f, 0o644)
}
put(t, filepath.Join(out, "packages.txt"), "bash 5.2-1 amd64\nlibc6 2.39-1 amd64\n", 0o644)
// Docker's packages, as dpkg records them: an epoch and Docker's suffix around the pinned version.
// First, so that the first line of the list is one of them.
var list string
for _, e := range pins(t).Entries {
if file := filepath.Base(e.URL()); strings.HasSuffix(file, ".deb") {
// The .deb's name is <package>_<version>_<arch>.deb.
f := strings.Split(strings.TrimSuffix(file, ".deb"), "_")
list += f[0] + " 5:" + f[1] + " " + f[2] + "\n"
}
}
put(t, filepath.Join(out, "packages.txt"), list+"bash 5.2-1 amd64\nlibc6 2.39-1 amd64\n", 0o644)
put(t, filepath.Join(out, "sbom/qemu.apk"), apkDB("glib-static,pcre2-static,libffi-dev,pixman-static,zlib-static,libseccomp-static,libcap-ng-static,liburing-dev,libaio-dev,gettext-static,musl-dev,gcc"), 0o644)
put(t, filepath.Join(out, "sbom/e2fsprogs.apk"), apkDB("libarchive-static,acl-static,zlib-static,xz-static,zstd-static,bzip2-static,lz4-static,expat-static,openssl-libs-static,musl-dev,gcc"), 0o644)
return root, out
Expand Down Expand Up @@ -180,12 +191,20 @@ func TestAReleaseShipsEveryPartAndSaysWhatItIs(t *testing.T) {
if err != nil || inner.Metadata.Component.Name != "spin-machine" {
t.Fatalf("the machine's SBOM: %v", err)
}
text, _ := io.ReadAll(mustOpen(t, filepath.Join(out, "release", name, share, "sbom", "machine.cdx.json")))
for _, c := range []string{"pkg:apk/alpine/glib-static@1.0-r0", "pkg:deb/ubuntu/bash@5.2-1", `"name": "qboot.bin"`, `"name": "rootfs.qcow2"`, "0001-k.patch", "mtrr.patch"} {
if !strings.Contains(string(text), c) {
raw, _ := io.ReadAll(mustOpen(t, filepath.Join(out, "release", name, share, "sbom", "machine.cdx.json")))
// encoding/json writes a purl's & as \u0026.
text := strings.ReplaceAll(string(raw), `\u0026`, "&")
docker, _ := pins(t).Get("docker-ce")
for _, c := range []string{"pkg:apk/alpine/glib-static@1.0-r0", "pkg:deb/ubuntu/bash@5.2-1", `"name": "qboot.bin"`, `"name": "rootfs.qcow2"`, "0001-k.patch", "mtrr.patch",
"pkg:deb/docker/docker-ce@5:" + docker.Version + "-1~ubuntu.26.04~resolute?arch=amd64&repository_url=https://download.docker.com/linux/ubuntu", docker.Pin, docker.URL(),
"pkg:deb/docker/containerd.io@"} {
if !strings.Contains(text, c) {
t.Errorf("the machine's SBOM does not name %s", c)
}
}
if strings.Contains(text, "pkg:deb/ubuntu/docker-ce") {
t.Error("the machine's SBOM names Docker's docker-ce as Ubuntu's")
}
}

func maps(a, b map[string]string) bool {
Expand Down Expand Up @@ -242,8 +261,38 @@ func TestAReleaseMissingAnythingIsRefused(t *testing.T) {
}
})
}
// A Docker package the image did not record, or recorded at a version versions.yaml does not
// pin, is an image that did not install what the release would say it did.
for name, edit := range map[string]func(string) string{
"without docker-ce": func(list string) string {
return regexp.MustCompile(`(?m)^docker-ce .*\n`).ReplaceAllString(list, "")
},
"another docker-ce": func(list string) string {
return regexp.MustCompile(`(?m)^docker-ce .*`).ReplaceAllString(list, "docker-ce 5:1.0.0-1~ubuntu.26.04~resolute amd64")
},
"a docker-ce whose version only starts with the pin's": func(list string) string {
return regexp.MustCompile(`(?m)^docker-ce 5:([^-]*)-`).ReplaceAllString(list, "docker-ce 5:${1}1-")
},
} {
root, out := tree(t)
put(t, filepath.Join(out, "packages.txt"), edit(read(t, filepath.Join(out, "packages.txt"))), 0o644)
if _, err := Pack(context.Background(), options(t, root, out)); err == nil || !strings.Contains(err.Error(), "docker-ce") {
t.Errorf("an image %s: Pack = %v", name, err)
}
}
// A .deb pinned from a repository the SBOM has no vendor for would be named as Ubuntu's again.
root, out := tree(t)
o := options(t, root, out)
for i, e := range o.Pins.Entries {
if e.Name == "docker-ce" {
o.Pins.Entries[i].Source = strings.Replace(e.Source, "download.docker.com", "example.org", 1)
}
}
if _, err := Pack(context.Background(), o); err == nil || !strings.Contains(err.Error(), "example.org") {
t.Errorf("a .deb from an unknown repository: Pack = %v", err)
}
root, out = tree(t)
o = options(t, root, out)
o.Pins = &versions.Versions{}
if _, err := Pack(context.Background(), o); err == nil {
t.Error("a release with no pins packed")
Expand Down