Files
uptimesebi/tools/release/sbom.go
T
Sebastian UnterschützandClaude Opus 5.5 b1ca8eb19f
ci / image (pull_request) Successful in 4m1s
ci / build (pull_request) Successful in 8m59s
ci / lint (pull_request) Successful in 10m34s
ci / test (pull_request) Successful in 12m29s
feat(release): sign checksums and publish an SBOM
Releases carry SHA256SUMS signed in the minisign format (prehashed
Ed25519, the release tag as trusted comment) and an SPDX SBOM of the Go
modules and the npm packages the frontend build bundles. internal/signing
signs and verifies; the release keys are committed in release.pub and
compiled in, so the self-updating agents can check their downloads.
Without the RELEASE_SIGNING_KEY secret a release is published unsigned
with a warning.

Closes #34

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 18:20:05 +01:00

184 lines
6.0 KiB
Go

package main
import (
"crypto/rand"
"debug/buildinfo"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"regexp"
"sort"
"strings"
"time"
)
// SPDX 2.3, the subset needed to list the components of a release.
type spdxDoc struct {
SPDXVersion string `json:"spdxVersion"`
DataLicense string `json:"dataLicense"`
SPDXID string `json:"SPDXID"`
Name string `json:"name"`
DocumentNamespace string `json:"documentNamespace"`
CreationInfo spdxCreation `json:"creationInfo"`
Packages []spdxPackage `json:"packages"`
Relationships []spdxRelationship `json:"relationships"`
}
type spdxCreation struct {
Created string `json:"created"`
Creators []string `json:"creators"`
}
type spdxPackage struct {
Name string `json:"name"`
SPDXID string `json:"SPDXID"`
VersionInfo string `json:"versionInfo"`
DownloadLocation string `json:"downloadLocation"`
FilesAnalyzed bool `json:"filesAnalyzed"`
LicenseConcluded string `json:"licenseConcluded,omitempty"`
LicenseDeclared string `json:"licenseDeclared,omitempty"`
CopyrightText string `json:"copyrightText,omitempty"`
ExternalRefs []spdxExtRef `json:"externalRefs,omitempty"`
Checksums []spdxSummary `json:"checksums,omitempty"`
}
type spdxExtRef struct {
Category string `json:"referenceCategory"`
Type string `json:"referenceType"`
Locator string `json:"referenceLocator"`
}
type spdxSummary struct {
Algorithm string `json:"algorithm"`
Value string `json:"checksumValue"`
}
type spdxRelationship struct {
Element string `json:"spdxElementId"`
Type string `json:"relationshipType"`
Related string `json:"relatedSpdxElement"`
}
// component is one third-party part of a release.
type component struct{ purl, name, version string }
var spdxIDUnsafe = regexp.MustCompile(`[^A-Za-z0-9.-]+`)
// sbom describes a release: the Go modules and toolchain compiled into the
// binaries and the npm packages whose files the frontend build copies.
func sbom(v version, binaries []string, packageJSON, packageLock []byte, now time.Time, repoURL string) ([]byte, error) {
seen := map[string]component{}
for _, path := range binaries {
info, err := buildinfo.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("release: %s: %w", path, err)
}
toolchain := strings.TrimPrefix(strings.SplitN(info.GoVersion, " ", 2)[0], "go")
toolchain, _, _ = strings.Cut(toolchain, "-")
seen["stdlib"] = component{"pkg:golang/stdlib@v" + toolchain, "stdlib", "v" + toolchain}
for _, d := range info.Deps {
if d.Replace != nil {
d = d.Replace
}
seen[d.Path] = component{"pkg:golang/" + d.Path + "@" + d.Version, d.Path, d.Version}
}
}
npm, err := bundledNPM(packageJSON, packageLock)
if err != nil {
return nil, err
}
for _, c := range npm {
seen["npm:"+c.name] = c
}
comps := make([]component, 0, len(seen))
for _, c := range seen {
comps = append(comps, c)
}
sort.Slice(comps, func(i, j int) bool { return comps[i].purl < comps[j].purl })
ns := make([]byte, 8)
_, _ = rand.Read(ns)
root := spdxPackage{
Name: "uptimesebi", SPDXID: "SPDXRef-uptimesebi", VersionInfo: v.String(),
DownloadLocation: "NOASSERTION", LicenseConcluded: "AGPL-3.0-only", LicenseDeclared: "AGPL-3.0-only",
CopyrightText: "NOASSERTION",
}
if repoURL != "" {
root.DownloadLocation = "git+" + repoURL + ".git@" + v.String()
}
doc := spdxDoc{
SPDXVersion: "SPDX-2.3", DataLicense: "CC0-1.0", SPDXID: "SPDXRef-DOCUMENT",
Name: "uptimesebi-" + v.String(),
DocumentNamespace: "https://uptimesebi.invalid/spdx/" + v.String() + "-" + hex.EncodeToString(ns),
CreationInfo: spdxCreation{Created: now.UTC().Format(time.RFC3339), Creators: []string{"Tool: uptimesebi-release"}},
Packages: []spdxPackage{root},
Relationships: []spdxRelationship{{"SPDXRef-DOCUMENT", "DESCRIBES", root.SPDXID}},
}
for _, c := range comps {
id := "SPDXRef-" + spdxIDUnsafe.ReplaceAllString(strings.TrimPrefix(c.purl, "pkg:"), "-")
doc.Packages = append(doc.Packages, spdxPackage{
Name: c.name, SPDXID: id, VersionInfo: c.version, DownloadLocation: "NOASSERTION",
ExternalRefs: []spdxExtRef{{"PACKAGE-MANAGER", "purl", c.purl}},
})
doc.Relationships = append(doc.Relationships, spdxRelationship{root.SPDXID, "CONTAINS", id})
}
return json.MarshalIndent(doc, "", " ")
}
var nodeModule = regexp.MustCompile(`node_modules/((?:@[^/\s]+/)?[^/\s]+)/`)
// bundledNPM finds the npm packages the build scripts copy files from, with
// their locked versions. Build tools such as Tailwind are not shipped and
// stay out.
func bundledNPM(packageJSON, packageLock []byte) ([]component, error) {
var pkg struct {
Scripts map[string]string `json:"scripts"`
}
var lock struct {
Packages map[string]struct {
Version string `json:"version"`
} `json:"packages"`
}
if err := json.Unmarshal(packageJSON, &pkg); err != nil {
return nil, fmt.Errorf("release: package.json: %w", err)
}
if err := json.Unmarshal(packageLock, &lock); err != nil {
return nil, fmt.Errorf("release: package-lock.json: %w", err)
}
names := map[string]bool{}
for _, s := range pkg.Scripts {
for _, m := range nodeModule.FindAllStringSubmatch(s, -1) {
names[m[1]] = true
}
}
var out []component
for name := range names {
p, ok := lock.Packages["node_modules/"+name]
if !ok {
return nil, fmt.Errorf("release: %s is not in package-lock.json", name)
}
// purl encodes the @ of a scope.
purl := "pkg:npm/" + strings.Replace(name, "@", "%40", 1) + "@" + p.Version
out = append(out, component{purl, name, p.Version})
}
return out, nil
}
func writeSBOM(v version, binaries []string) error {
pj, err := os.ReadFile("package.json")
if err != nil {
return err
}
pl, err := os.ReadFile("package-lock.json")
if err != nil {
return err
}
out, err := sbom(v, binaries, pj, pl, time.Now(), repoURL())
if err != nil {
return err
}
_, err = os.Stdout.Write(append(out, '\n'))
return err
}