Merge remote-tracking branch 'origin/feat/release-signing' into feat/autoupdate
# Conflicts: # README.md
This commit is contained in:
commit
431048e244
10 files changed
+677
-6
No files matched your search
+30
-1
@@ -5,6 +5,11 @@
|
||||
// release notes [-json] <tag> print the release notes of a tag, with
|
||||
// -json as the body of a Gitea release
|
||||
// release check <tag> fail unless tag is a valid release tag
|
||||
// release sbom <tag> <bin>... print the SPDX SBOM of a release
|
||||
// release sign <file> <tag> write file.minisig with the key in
|
||||
// RELEASE_SIGNING_KEY
|
||||
// release verify <file> check file.minisig against the release keys
|
||||
// release keygen <secret-file> create a signing key, print its public key
|
||||
//
|
||||
// Stable releases follow semantic versioning from the commits since the
|
||||
// last stable release: breaking changes move the major version (the minor
|
||||
@@ -21,7 +26,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const usage = `usage: release next [stable|beta] | notes [-json] <tag> | check <tag>`
|
||||
const usage = `usage: release next [stable|beta] | notes [-json] <tag> | check <tag> | sbom <tag> <binary>... | sign <file> <tag> | verify <file> | keygen <secret-file>`
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:]); err != nil {
|
||||
@@ -97,6 +102,30 @@ func run(args []string) error {
|
||||
}
|
||||
_, err := parseVersion(args[1])
|
||||
return err
|
||||
case "sbom":
|
||||
if len(args) < 3 {
|
||||
return usageError{}
|
||||
}
|
||||
v, err := parseVersion(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeSBOM(v, args[2:])
|
||||
case "sign":
|
||||
if len(args) != 3 {
|
||||
return usageError{}
|
||||
}
|
||||
return signFile(args[1], args[2])
|
||||
case "verify":
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
return verifyFile(args[1])
|
||||
case "keygen":
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
return keygen(args[1])
|
||||
default:
|
||||
return usageError{}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/signing"
|
||||
)
|
||||
|
||||
func TestSBOM(t *testing.T) {
|
||||
self, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pj := []byte(`{"scripts":{"build":"tailwindcss -o x && cp node_modules/axios/dist/a.js node_modules/@fontsource/ibm-plex-sans/files/b.woff2 out/"}}`)
|
||||
pl := []byte(`{"packages":{"node_modules/axios":{"version":"1.21.1"},"node_modules/@fontsource/ibm-plex-sans":{"version":"5.3.0"},"node_modules/tailwindcss":{"version":"4.3.5"}}}`)
|
||||
v, _ := parseVersion("v1.2.3")
|
||||
raw, err := sbom(v, []string{self}, pj, pl, time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC), "https://git.example/o/r")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var doc spdxDoc
|
||||
if err := json.Unmarshal(raw, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
purls := map[string]bool{}
|
||||
ids := map[string]bool{}
|
||||
for _, p := range doc.Packages {
|
||||
if ids[p.SPDXID] {
|
||||
t.Errorf("duplicate SPDXID %s", p.SPDXID)
|
||||
}
|
||||
ids[p.SPDXID] = true
|
||||
for _, r := range p.ExternalRefs {
|
||||
purls[r.Locator] = true
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"pkg:npm/axios@1.21.1", "pkg:npm/%40fontsource/ibm-plex-sans@5.3.0", "pkg:golang/golang.org/x/crypto@", "pkg:golang/stdlib@v1."} {
|
||||
if !hasPrefix(purls, want) {
|
||||
t.Errorf("SBOM lacks %s", want)
|
||||
}
|
||||
}
|
||||
if hasPrefix(purls, "pkg:npm/tailwindcss") {
|
||||
t.Error("build tools are not shipped")
|
||||
}
|
||||
if doc.Packages[0].VersionInfo != "v1.2.3" || doc.Packages[0].LicenseDeclared != "AGPL-3.0-only" ||
|
||||
len(doc.Relationships) != len(doc.Packages) || doc.CreationInfo.Created != "2026-01-02T03:04:05Z" {
|
||||
t.Errorf("document = %+v", doc.Packages[0])
|
||||
}
|
||||
|
||||
if _, err := bundledNPM([]byte(`{"scripts":{"b":"cp node_modules/missing/x ."}}`), pl); err == nil {
|
||||
t.Error("a package missing from the lock file was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "SHA256SUMS")
|
||||
if err := os.WriteFile(path, []byte("abc x.tar.gz\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
k, _ := signing.GenerateKey()
|
||||
t.Setenv("RELEASE_SIGNING_KEY", k.String())
|
||||
if err := signFile(path, "v1.0.0"); err == nil || !strings.Contains(err.Error(), "release.pub") {
|
||||
t.Errorf("a key not in release.pub signed: %v", err)
|
||||
}
|
||||
t.Setenv("RELEASE_SIGNING_KEY", "")
|
||||
if err := signFile(path, "v1.0.0"); err == nil {
|
||||
t.Error("signed without a key")
|
||||
}
|
||||
if err := keygen(filepath.Join(dir, "k")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := keygen(filepath.Join(dir, "k")); err == nil {
|
||||
t.Error("keygen overwrote a key")
|
||||
}
|
||||
if st, _ := os.Stat(filepath.Join(dir, "k")); st.Mode().Perm() != 0o600 {
|
||||
t.Errorf("key file mode = %v", st.Mode())
|
||||
}
|
||||
}
|
||||
|
||||
func hasPrefix(set map[string]bool, prefix string) bool {
|
||||
for s := range set {
|
||||
if strings.HasPrefix(s, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/signing"
|
||||
)
|
||||
|
||||
// signFile signs path for release tag; the tag becomes the trusted comment,
|
||||
// so a signature cannot be passed off for another release.
|
||||
func signFile(path, tag string) error {
|
||||
if _, err := parseVersion(tag); err != nil {
|
||||
return err
|
||||
}
|
||||
raw := os.Getenv("RELEASE_SIGNING_KEY")
|
||||
if raw == "" {
|
||||
return errors.New("release: RELEASE_SIGNING_KEY is not set")
|
||||
}
|
||||
key, err := signing.ParsePrivateKey(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !trusted(key.Public()) {
|
||||
return fmt.Errorf("release: key %s is not in internal/signing/release.pub", key.Public().IDString())
|
||||
}
|
||||
msg, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sig, err := signing.Sign(key, msg, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path+".minisig", sig, 0o644)
|
||||
}
|
||||
|
||||
func trusted(k signing.PublicKey) bool {
|
||||
for _, r := range signing.ReleaseKeys() {
|
||||
if r.ID == k.ID && r.Key.Equal(k.Key) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func verifyFile(path string) error {
|
||||
msg, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sig, err := os.ReadFile(path + ".minisig")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := signing.Verify(signing.ReleaseKeys(), msg, sig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("%s: good signature for %s\n", path, tag)
|
||||
return nil
|
||||
}
|
||||
|
||||
// keygen writes a new signing key to secretPath, readable only by the
|
||||
// owner, and prints the public key for internal/signing/release.pub.
|
||||
func keygen(secretPath string) error {
|
||||
k, err := signing.GenerateKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(secretPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintln(f, k.String()); err != nil {
|
||||
_ = f.Close()
|
||||
return err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("untrusted comment: uptimesebi release key %s\n%s\n", k.Public().IDString(), k.Public())
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user