Files
archie/internal/releaseupdate/prepare.go
T
sam 9d8f364af1
Quality / check (push) Successful in 5m53s
deploy / gate (push) Canceled after 0s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
feat(releaseupdate): signed OCI distribution sources for releases and the catalogue (#1715)
2026-10-31 18:18:43 +11:00

337 lines
11 KiB
Go

package releaseupdate
import (
"archive/zip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/domain/distribution"
)
var (
releaseVersion = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$`)
releaseUnit = regexp.MustCompile(`^archie[a-z0-9-]*$`)
pinnedImage = regexp.MustCompile(`^[a-zA-Z0-9./:_-]+@sha256:[a-f0-9]{64}$`)
)
// PrepareRequest names the release to stage and who hears the outcome.
type PrepareRequest struct {
Version string
// Source is the distribution source the release is fetched and verified
// from.
Source distribution.Source
Meta InstallMeta
// CheckTopology refuses a host missing a unit or config section the
// release requires. managed reports whether units are observable.
CheckTopology func(ctx context.Context, required Topology, managed bool) error
}
// Manifest is the release.json a release zip carries, with the agent image
// taken from the signed statement rather than from the zip.
type Manifest struct {
Required Topology `json:"required_topology"`
AgentImage string `json:"agent_image"`
}
// Prepare verifies and stages a release without changing anything that runs:
// the statement must verify against the source's key and the zip against the
// statement, every binary must report the release,
// the agent image is pulled by digest and the database is backed up before a
// journal is published. The watchdog (Run) takes it from there.
func (t *Transaction) Prepare(ctx context.Context, req PrepareRequest) (Result, error) {
if !releaseVersion.MatchString(req.Version) {
return Result{}, fmt.Errorf("refusing non-release version %q", req.Version)
}
if err := t.loadEnvCredentials(); err != nil {
return Result{}, err
}
unlock, held, err := lockState(ctx, t.StateDir, false)
if err != nil {
return Result{}, err
}
if !held {
return Result{}, ErrInstallInProgress
}
defer unlock()
if _, err := os.Stat(JournalPath(t.StateDir)); err == nil {
return Result{}, fmt.Errorf("an unfinished update is pending recovery at %s", JournalPath(t.StateDir))
}
work, err := os.MkdirTemp(t.StateDir, ".release-*")
if err != nil {
return Result{}, err
}
defer func() { _ = os.RemoveAll(work) }()
release, manifest, err := t.fetch(ctx, req, work)
if err != nil {
return Result{}, err
}
managed := t.Units.Managed(ctx)
if req.CheckTopology != nil {
if err := req.CheckTopology(ctx, manifest.Required, managed); err != nil {
return Result{}, err
}
}
j, err := t.snapshotHost(ctx, req, manifest, managed)
if err != nil {
return Result{}, err
}
if err := t.stage(req, release, j); err != nil {
for _, app := range append(slices.Clone(j.Units), j.Binaries...) {
_ = os.Remove(t.staged(app))
}
return Result{}, err
}
installed := map[string]string{ComponentDaemon: req.Version, ComponentAgent: req.Version}
return Result{
Previous: j.Previous,
Installed: installed,
Components: []ComponentResult{
{ID: ComponentDaemon, Label: "Gateway", Status: "updated"},
{ID: ComponentAgent, Label: "Runtime", Status: "updated"},
},
RestartRequested: managed,
}, nil
}
// fetch downloads, verifies and unpacks the release, and checks every binary
// reports the version being installed.
func (t *Transaction) fetch(ctx context.Context, req PrepareRequest, work string) (string, Manifest, error) {
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
return "", Manifest{}, fmt.Errorf("no published release for %s/%s", runtime.GOOS, runtime.GOARCH)
}
verified, err := Fetch(ctx, req.Source, Tag("exact-pin", req.Version))
if err != nil {
return "", Manifest{}, err
}
zipName := verified.Statement.Zip.Name
name := strings.TrimSuffix(zipName, ".zip")
t.say("downloading %s", zipName)
archive := filepath.Join(work, zipName)
if err := writeBlob(ctx, verified, archive); err != nil {
return "", Manifest{}, err
}
if err := unpack(archive, name, work); err != nil {
return "", Manifest{}, err
}
release := filepath.Join(work, name)
manifest, err := readManifest(filepath.Join(release, "release.json"))
if err != nil {
return "", Manifest{}, err
}
manifest.AgentImage = verified.Statement.AgentImage
for _, binary := range manifest.Required.apps() {
reported, err := reportedVersion(ctx, filepath.Join(release, binary), binary)
if err != nil {
return "", Manifest{}, err
}
if !sameVersion(reported, req.Version) {
return "", Manifest{}, fmt.Errorf("candidate %s reports %q, not %s", binary, reported, req.Version)
}
}
return release, manifest, nil
}
// snapshotHost pulls the image and records what rollback needs: the current
// versions, the current image policy, the schema version and a backup.
func (t *Transaction) snapshotHost(ctx context.Context, req PrepareRequest, manifest Manifest, managed bool) (journal, error) {
t.say("pulling %s", manifest.AgentImage)
if err := t.Images.Pull(ctx, manifest.AgentImage); err != nil {
return journal{}, fmt.Errorf("pull agent image: %w", err)
}
image, err := t.Policy.Image(ctx)
if err != nil {
return journal{}, fmt.Errorf("read image policy: %w", err)
}
schema, err := t.Database.SchemaVersion(ctx)
if err != nil {
return journal{}, fmt.Errorf("read schema version: %w", err)
}
t.say("backing up the database to %s", t.snapshot())
if err := t.Database.Backup(ctx, t.snapshot()); err != nil {
return journal{}, fmt.Errorf("back up the database: %w", err)
}
return journal{
Phase: phasePrepared,
Version: req.Version,
Previous: map[string]string{
ComponentDaemon: ReleaseVersion(buildinfo.Version),
ComponentAgent: installedAgent(t.BinDir),
},
Units: manifest.Required.Units,
Binaries: manifest.Required.Binaries,
Managed: managed,
Image: imageChange{Previous: image, Candidate: manifest.AgentImage},
SchemaBefore: schema,
Meta: req.Meta,
}, nil
}
// stage freezes the watchdog and every file rollback restores, places the
// candidates beside the live binaries, and publishes the journal last.
func (t *Transaction) stage(req PrepareRequest, release string, j journal) error {
preparing := filepath.Join(t.StateDir, "preparing")
if err := os.RemoveAll(preparing); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(preparing, "previous"), 0o700); err != nil {
return err
}
// The release's own archied runs the watchdog, so it speaks the protocol
// of the services it switches to.
if err := installFile(filepath.Join(release, WatchdogBinary), filepath.Join(preparing, WatchdogBinary), 0o700); err != nil {
return fmt.Errorf("freeze the watchdog: %w", err)
}
for _, unit := range append(slices.Clone(j.Units), j.Binaries...) {
live := filepath.Join(t.BinDir, unit)
if _, err := os.Stat(live); err == nil {
if err := installFile(live, filepath.Join(preparing, "previous", unit), 0o755); err != nil {
return fmt.Errorf("save %s: %w", unit, err)
}
}
if err := installFile(filepath.Join(release, unit), t.staged(unit), 0o755); err != nil {
return fmt.Errorf("stage %s: %w", unit, err)
}
}
version := filepath.Join(t.BinDir, agentVersionFile)
if _, err := os.Stat(version); err == nil {
if err := installFile(version, filepath.Join(preparing, agentVersionFile), 0o644); err != nil {
return err
}
}
if err := syncDir(t.BinDir); err != nil {
return err
}
if err := writeJournal(preparing, j); err != nil {
return err
}
if err := syncDir(filepath.Join(preparing, "previous")); err != nil {
return err
}
if err := os.Rename(preparing, JournalPath(t.StateDir)); err != nil {
return err
}
return syncDir(t.StateDir)
}
// writeBlob writes the release's zip to dst, checked against the digest the
// signed statement names.
func writeBlob(ctx context.Context, release Release, dst string) error {
file, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
err = release.Artifact.Blob(ctx, release.Statement.Zip.Digest, file, 1<<30)
if closeErr := file.Close(); err == nil {
err = closeErr
}
if err != nil {
return fmt.Errorf("download %s: %w", release.Statement.Zip.Name, err)
}
return nil
}
// unpack extracts the zip's single distribution directory, refusing any
// entry outside it and anything but regular files and directories.
func unpack(archive, name, dst string) error {
reader, err := zip.OpenReader(archive)
if err != nil {
return err
}
defer func() { _ = reader.Close() }()
for _, file := range reader.File {
clean := path.Clean(file.Name)
if !strings.HasPrefix(file.Name, name+"/") || strings.Contains(file.Name, `\`) ||
clean != strings.TrimSuffix(file.Name, "/") || !strings.HasPrefix(clean, name) {
return fmt.Errorf("unsafe archive path %q", file.Name)
}
mode := file.Mode()
target := filepath.Join(dst, filepath.FromSlash(clean))
switch {
case mode.IsDir():
if err := os.MkdirAll(target, 0o700); err != nil {
return err
}
case mode.IsRegular():
if err := extract(file, target); err != nil {
return err
}
default:
return fmt.Errorf("archive entry %q is not a regular file", file.Name)
}
}
return nil
}
func extract(file *zip.File, target string) error {
if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer func() { _ = src.Close() }()
dst, err := os.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o700)
if err != nil {
return err
}
if _, err := io.Copy(dst, io.LimitReader(src, 1<<30)); err != nil {
_ = dst.Close()
return err
}
return dst.Close()
}
func readManifest(path string) (Manifest, error) {
data, err := os.ReadFile(path)
if err != nil {
return Manifest{}, err
}
var manifest Manifest
if err := json.Unmarshal(data, &manifest); err != nil {
return Manifest{}, fmt.Errorf("decode release.json: %w", err)
}
if len(manifest.Required.Units) == 0 {
return Manifest{}, errors.New("release has no required_topology")
}
for _, unit := range manifest.Required.apps() {
if !releaseUnit.MatchString(unit) {
return Manifest{}, fmt.Errorf("invalid release unit %q", unit)
}
}
return manifest, nil
}
// reportedVersion runs binary -version and returns the version on the line
// naming unit.
func reportedVersion(ctx context.Context, binary, unit string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
output, err := exec.CommandContext(ctx, binary, "-version").Output()
if err != nil {
return "", fmt.Errorf("%s -version: %w", unit, err)
}
for line := range strings.SplitSeq(string(output), "\n") {
if fields := strings.Fields(line); len(fields) == 2 && fields[0] == unit {
return fields[1], nil
}
}
return "", fmt.Errorf("%s -version names no version", unit)
}