mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm-cosign's run (Cosigner.Run), its command (cmd/devvm-cosign, Main) and the maintainer's commands, devvm-cosign retry, ack and failed. CI's token can only read the repo, so CI hands each build over as a workflow artifact, and only the co-signer, and the maintainer by hand, can make or publish a release. For each release CI handed over that it hasn't published, oldest first, from the newest successful run of the pinned release workflow for each tag, of this repo's own: - its version is newer than the newest it signed, or one it signed, with the same SHA256SUMS; an older one is an alert, as a replay, until the maintainer acks it, unless it was published before the state began; - CI's artifact is staged (stage/, which no build can reach) and checked: SHA256SUMS, CI's signature with CI's key and devvm-release <version>, and exactly the files it lists, each with its SHA-256; its files are deleted before the build and downloaded again after a match, so none of CI's bytes is on disk while a container could be live; - a run builds once, so the podman service, which lives for the run, lives for one build, and anything that escaped a container is gone before the next build's host git and release.sh run; the next release or base waits for the next run; - the build's finished files go in a folder of the state's src/, which release.sh copies to and the co-signer reads, where no container can swap a file or the folder; - the tag's commit has to be the commit CI's run built, and the co-signer's own build has to match every file and SHA256SUMS; it renders the tap's formulas itself from the tag's template and the signed SHA256SUMS (formula.go, as release.sh's formula function does), and the build's own formulas have to be the same bytes, so neither builder's container decides what Homebrew installs. The formulas and the notes, the tag's CHANGELOG.md entry, are kept in kept/<v>/ with their SHA-256s in the state, and checked again before the tap; - it signs, makes the draft itself, titled devvm <version> with the tag's notes, and uploads SHA256SUMS, both signatures and CI's files, each hashed as it's sent, keeping an asset only when its size and GitHub's digest are right and deleting any other; - while the bridge lasts it waits for the old key's signature on the draft, failing each run so the maintainer hears of it, and downloading nothing again while it waits; - it publishes only when the draft is exactly what it made, its tag, title and notes (but for line endings and the space at either end), and its assets by ID, size and digest, and the tag still names the tag object its rebuild checked; then checks the published release again, and finishes the tap. Alerts and refusals: - init records the highest release ID GitHub showed and the drafts there were then; IDs only go up, so any release published since that isn't a draft the co-signer made is an alert, whatever its tag or version, by <tag>#<ID>, so another release of an acked tag is alerted on again; each run also reads GitHub's latest release, wherever the list puts it. - A published release that differs from what it checked, its own draft published without it seeing the answer that then doesn't check out, and a tap formula newer than any release it signed, by tap:<formula>@<version>, are alerts; the tap is updated only for a release it published and checked after. - A rebuild that doesn't match is a refusal, which isn't downloaded or rebuilt again, even for CI's run again; devvm-cosign retry <tag> lets the next run rebuild it once more. Nothing retries on its own. - Every run fails (exit 1) while an alert or refusal waits, until devvm-cosign ack or retry. A run records in last-failure.txt that it started, then what failed, or removes it when it succeeds; devvm-cosign failed, which the units' OnFailure= runs for any failure, a killed or timed-out run's or a config that won't load included, runs notify, the maintainer's command, with it on its stdin, a fixed environment and a minute of its own, whatever ended the run's context. Bases go the same way, from the newest successful run of the base workflow on main, made at the index's commit and never GitHub's latest. Main makes the process non-dumpable (prctl PR_SET_DUMPABLE 0) before anything else, so no process of its user can read its memory, where the keys and token are, and reads the keys from systemd's credentials when CREDENTIALS_DIRECTORY is set. A run empties the build folder's work/ and the state's stage/ and src/ in place, never making them again. Tests, each fix reverted once to see it fail: TestPublishedBySomeoneElse (a release published by someone else, whatever its tag or version, the same tag again after an ack, a draft from before init published since, and GitHub's latest past where the list stops; one from before the state left alone), TestOneBuildPerRun (the build's files in the state, none of CI's on disk during the build, and the next release left for the next run), TestFormulas (a build's formula of its own; a kept formula changed on disk), TestRenderFormula, TestPrereleasePublished (a tap formula newer than any release signed), TestDraftChangedBeforePublish (a swapped file, a retag, other notes), TestTagMovedBeforePublish, TestAlertAfterPublish and TestLostPublishChecked, TestMismatchSignsNothing (a failure every run, and retry), TestReplayedOlderVersion (an alert, then acked), TestNotify, TestLegacyBridge (a failure every run, with no download), TestRunPagesReleases, TestInitPaged (init past ten pages of bases, which fails with a page check that stops at its first unfinished entry), TestRunsChecked, TestArtifactChecked, TestUploadChangedOnDisk, TestBaseIndexChecked (a re-signed base on a 61-day snapshot), TestBasePublishChecked, TestBasePublishedElsewhere (an expired base still counts in checkBehind), TestWorkEmptiedInPlace, TestCommandLine (retry and ack read no key file), and on Linux TestUndumpable. A build that fails, rather than differs, is built again at the next run, but the third failure in a row refuses it, with a report holding the last failure (buildFailed), so a build that keeps failing no longer takes every run's one build and holds up each newer release and base; retry builds it again, and a build stopped with its run isn't counted. Refusing rather than rotating past it keeps releases oldest first, as versions only go up, and keeps the failure in front of the maintainer through held and notify. buildRelease and buildBase are the build and its comparison, apart from what's recorded. And GitHub's latest release has to be the one init saw or one the co-signer published, not one of its own older than another it published: anything else is an alert, latest:<tag>#<ID> (staleLatest), as the install script's default downloads it; so is the one init saw, once it's older than one the co-signer published. The co-signer makes a release GitHub's latest only when it's newer than every release it published, so publishing an older release after a newer one, as when the old key signs the newer draft first, never moves the latest back itself. And a release's run counts only when a tag's push started it (PushEvent, which the structure test's nameFree lets name "push"). Tests, each failing before its code: TestFailingBuildRefused, TestBaseRefused's base that keeps failing, TestLatestChecked, TestOlderReleaseNotLatest, TestRunsChecked's run by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
551 lines
20 KiB
Go
551 lines
20 KiB
Go
package cosign
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/minisign"
|
|
)
|
|
|
|
// formulas are the Homebrew formulas a release gets in the tap: devvm for
|
|
// a release, devvm-rc for every one.
|
|
func formulas(v version) []string {
|
|
if v.prerelease() {
|
|
return []string{"devvm-rc.rb"}
|
|
}
|
|
return []string{"devvm.rb", "devvm-rc.rb"}
|
|
}
|
|
|
|
// A candidate is a release CI handed over: its tag and version, and the
|
|
// newest successful run of the release workflow for that tag.
|
|
type candidate struct {
|
|
tag string
|
|
v version
|
|
run Run
|
|
}
|
|
|
|
// releaseRuns are the releases CI handed over that the co-signer hasn't
|
|
// published, oldest version first: from the successful runs of the
|
|
// pinned release workflow, pushed for a tag of this repo, each tag's
|
|
// newest run.
|
|
func (c *Cosigner) releaseRuns(ctx context.Context, s State, rs []Release) ([]candidate, error) {
|
|
last, err := parseVersion(s.Release.Last)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
runs, err := c.GitHub.Runs(ctx, c.Repo, c.Release.Workflow, func(r Run) bool {
|
|
v, _, kind := tagKind(r.HeadBranch)
|
|
return kind != minisign.Release || v.compare(last) <= 0 && (s.Release.Signed[v.raw] == "" || s.Release.Published[v.raw])
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing CI's runs of %s: %w", c.Release.Workflow, err)
|
|
}
|
|
published := map[string]bool{}
|
|
for _, r := range rs {
|
|
published[r.TagName] = published[r.TagName] || !r.Draft
|
|
}
|
|
seen := map[string]bool{}
|
|
var out []candidate
|
|
for _, r := range runs { // newest first: a tag's first is its newest
|
|
v, _, kind := tagKind(r.HeadBranch)
|
|
// Only a tag's push starts a release's run: one started otherwise, on
|
|
// a branch of a tag's name, isn't one.
|
|
if kind != minisign.Release || seen[r.HeadBranch] || r.Event != PushEvent || !fromWorkflow(r, c.Repo, c.Release.Workflow) {
|
|
continue
|
|
}
|
|
seen[r.HeadBranch] = true
|
|
if !published[r.HeadBranch] && !s.Release.Published[v.raw] {
|
|
out = append(out, candidate{tag: r.HeadBranch, v: v, run: r})
|
|
}
|
|
}
|
|
slices.SortStableFunc(out, func(a, b candidate) int { return a.v.compare(b.v) })
|
|
return out, nil
|
|
}
|
|
|
|
// PushEvent is the GitHub Actions event a tag's push starts a run with,
|
|
// the only one a release's run is: GitHub's name for the event, not git
|
|
// push, which devvm never runs.
|
|
const PushEvent = "push"
|
|
|
|
// fromWorkflow is whether r is a successful run of the workflow file of
|
|
// repo's own, not a fork's.
|
|
func fromWorkflow(r Run, repo, workflow string) bool {
|
|
return r.Path == ".github/workflows/"+workflow && r.Conclusion == "success" && r.HeadRepository.FullName == repo
|
|
}
|
|
|
|
// release signs, then publishes, the release CI handed over in cd:
|
|
// 1. its version is newer than any it signed, or one it signed already
|
|
// (a run that stopped), with the same SHA256SUMS;
|
|
// 2. CI's artifact holds SHA256SUMS, CI's signature of it, which checks
|
|
// out with CI's key, and the files it lists, with its SHA-256s, and
|
|
// nothing else (stage);
|
|
// 3. the co-signer's own build of its tag gives the same SHA256SUMS and
|
|
// files, and the formulas it renders from SHA256SUMS, or it signs
|
|
// nothing and writes a report (rebuild);
|
|
// 4. it signs SHA256SUMS, makes the release's draft, uploads every file,
|
|
// and publishes it (publishRelease).
|
|
func (c *Cosigner) release(ctx context.Context, s *State, cd candidate, rs []Release) error {
|
|
l, v := &s.Release.Ledger, cd.v.raw
|
|
if l.Alerts[v].active() {
|
|
return nil // held reports it
|
|
}
|
|
signed := l.Signed[v]
|
|
last, err := parseVersion(s.Release.Last)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if signed == "" && cd.v.compare(last) <= 0 {
|
|
if _, found, err := c.GitHub.ReleaseByTag(ctx, c.Repo, cd.tag); err != nil || found {
|
|
return err // published before the co-signer's state began
|
|
}
|
|
// An alert, so it's said every run until a maintainer, who knows
|
|
// why CI ran for it, acks it; acked, it's left alone.
|
|
if _, raised := l.Alerts[v]; !raised {
|
|
l.alert(v, "refused: CI handed it over, but it isn't newer than "+s.Release.Last+", the newest release the co-signer signed, so it's an older release replayed; find out why CI ran for it")
|
|
return c.save(s)
|
|
}
|
|
return nil
|
|
}
|
|
if _, refused := l.refusedRun(cd.run.ID); refused {
|
|
return nil // held reports it, with no download
|
|
}
|
|
draft, err := c.ownDraft(ctx, l, v, cd.tag, rs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Waiting for the old key's signature costs no download, and fails the
|
|
// run, so the maintainer hears that the draft waits for them.
|
|
if _, ok := draft.asset(legacySig); signed != "" && draft.ID != 0 && c.Release.Legacy != nil && !ok {
|
|
return errLegacy
|
|
}
|
|
if signed == "" && c.built {
|
|
return errNextRun // with no download
|
|
}
|
|
parse := func(data []byte) (Manifest, error) { return ParseSums(v, data) }
|
|
h, err := c.stage(ctx, &c.Release, cd.run, sumsName, parse)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sum := h.m.sum()
|
|
switch {
|
|
case signed != "" && signed != sum:
|
|
return fmt.Errorf("refused: its SHA256SUMS (%s) isn't the one the co-signer signed for %s (%s)", sum, v, signed)
|
|
case l.Refused[sum].active():
|
|
r := l.Refused[sum]
|
|
r.Run = cd.run.ID
|
|
l.Refused[sum] = r
|
|
return c.save(s) // held reports it
|
|
}
|
|
if signed == "" {
|
|
if ok, err := c.rebuild(ctx, s, cd, h); err != nil || !ok {
|
|
return err
|
|
}
|
|
// The build took CI's files off the disk: staged again, and the
|
|
// same manifest, for the upload.
|
|
again, err := c.stage(ctx, &c.Release, cd.run, sumsName, parse)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !bytes.Equal(again.m.Data, h.m.Data) {
|
|
return fmt.Errorf("refused: CI's artifact's SHA256SUMS changed while the co-signer built it")
|
|
}
|
|
h = again
|
|
}
|
|
return c.publishRelease(ctx, s, cd, draft, h)
|
|
}
|
|
|
|
// ownDraft is the draft release of tag that the co-signer made for key,
|
|
// or an empty Release when it has none to use: none made, or one deleted
|
|
// since. A draft of tag that it didn't make is refused, as it makes the
|
|
// one it publishes.
|
|
func (c *Cosigner) ownDraft(ctx context.Context, l *Ledger, key, tag string, rs []Release) (Release, error) {
|
|
id := l.draft(key)
|
|
var mine Release
|
|
for _, r := range rs {
|
|
switch {
|
|
case !r.Draft || r.TagName != tag:
|
|
case r.ID == id:
|
|
mine = r
|
|
default:
|
|
return Release{}, fmt.Errorf("a draft release of %s (ID %d) that the co-signer didn't make: delete it by hand, as the co-signer makes the release it publishes", tag, r.ID)
|
|
}
|
|
}
|
|
if mine.ID != 0 || id == 0 {
|
|
return mine, nil
|
|
}
|
|
// Not in the list read: read it by its ID.
|
|
r, err := c.GitHub.Release(ctx, c.Repo, id)
|
|
switch {
|
|
case isStatus(err, 404):
|
|
return Release{}, nil
|
|
case err != nil:
|
|
return Release{}, err
|
|
case !r.Draft:
|
|
return Release{}, fmt.Errorf("%w: its release %d is published, which the next run finishes", errWaiting, id)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// rebuild builds cd's tag with the co-signer's own devvm and compares it
|
|
// with CI's, and its formulas with those it renders from CI's SHA256SUMS.
|
|
// On a match it keeps the formulas and notes and records the manifest
|
|
// signed, and returns true; on a mismatch it writes a report and records
|
|
// the manifest refused, which held reports, and returns false.
|
|
func (c *Cosigner) rebuild(ctx context.Context, s *State, cd candidate, h handover) (bool, error) {
|
|
if err := oneBuild(&c.built, h); err != nil {
|
|
return false, err
|
|
}
|
|
c.logf("%s: CI's signature and files check out; rebuilding it", cd.tag)
|
|
b, err := buildRelease(ctx, c.Builder, c.Dir, c.Repo, cd, h)
|
|
if err != nil {
|
|
return false, c.buildFailed(ctx, s, &s.Release.Ledger, cd.tag, cd.v.raw, h, err)
|
|
}
|
|
sum := h.m.sum()
|
|
if len(b.diffs) > 0 {
|
|
path, err := report(c.Dir, c.now(), cd.tag, h.m, b.diffs)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
s.Release.refuse(sum, cd.v.raw, cd.run.ID)
|
|
c.logf("%s: %v: %d files differ, in %s", cd.tag, errMismatch, len(b.diffs), path)
|
|
return false, c.save(s)
|
|
}
|
|
dir := filepath.Join(c.Dir, "kept", cd.v.raw)
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return false, err
|
|
}
|
|
for _, name := range sortedKeys(b.keep) {
|
|
if err := writeWhole(filepath.Join(dir, name), b.keep[name], 0o600); err != nil {
|
|
return false, err
|
|
}
|
|
s.Release.Kept[cd.v.raw+"/"+name] = sha256Hex(b.keep[name])
|
|
}
|
|
s.Release.Signed[cd.v.raw] = sum
|
|
s.Release.Tags[cd.v.raw] = b.object
|
|
s.Release.Runs[strconv.FormatInt(cd.run.ID, 10)] = cd.v.raw
|
|
if cd.v.compare(mustVersion(s.Release.Last)) > 0 {
|
|
s.Release.Last = cd.v.raw
|
|
}
|
|
return true, c.save(s)
|
|
}
|
|
|
|
// A releaseBuild is the co-signer's own build of a release, compared with
|
|
// CI's: the tag object it checked, the files it keeps for publishing (the
|
|
// notes and the formulas it rendered), and each file that differs.
|
|
type releaseBuild struct {
|
|
object string
|
|
keep map[string][]byte
|
|
diffs []diff
|
|
}
|
|
|
|
// buildRelease builds cd's tag with builder, its files in a folder of the
|
|
// state folder dir's, and compares it with h, CI's: every file, and the
|
|
// formulas it renders for repo from CI's SHA256SUMS with the build's. An
|
|
// error is a build that failed, rather than differed.
|
|
func buildRelease(ctx context.Context, builder ReleaseBuilder, dir, repo string, cd candidate, h handover) (releaseBuild, error) {
|
|
work, err := newOut(dir, cd.tag)
|
|
if err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
defer os.RemoveAll(work)
|
|
out := filepath.Join(work, "out")
|
|
built, err := builder.Build(ctx, cd.tag, out)
|
|
if err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
if built.Tag.Commit != cd.run.HeadSHA {
|
|
return releaseBuild{}, fmt.Errorf("CI's run %d built %s, but %s is %s", cd.run.ID, cd.run.HeadSHA, cd.tag, built.Tag.Commit)
|
|
}
|
|
notes, err := releaseNotes(built.Changelog, cd.v.raw)
|
|
if err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
b := releaseBuild{object: built.Object, keep: map[string][]byte{notesFile: []byte(notes)}}
|
|
if b.diffs, err = compareBuild(h.m, out); err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
for _, f := range formulas(cd.v) {
|
|
want, err := renderFormula(built.Formula, strings.TrimSuffix(f, ".rb"), repo, cd.v, h.m)
|
|
if err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
got, err := readBuilt(filepath.Join(out, f))
|
|
if err != nil {
|
|
return releaseBuild{}, err
|
|
}
|
|
if !bytes.Equal(got, want) {
|
|
ours := ""
|
|
if got != nil {
|
|
ours = sha256Hex(got)
|
|
}
|
|
// What's compared here is the co-signer's render, from the
|
|
// signed SHA256SUMS, with its own build's.
|
|
b.diffs = append(b.diffs, diff{f + " (rendered from SHA256SUMS, then the rebuild's)", sha256Hex(want), ours})
|
|
}
|
|
b.keep[f] = want
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// errLegacy is a signed draft waiting for the old release key's
|
|
// signature, which the maintainer makes by hand while releases bridge
|
|
// from that key: a failure, so every run tells them until they do.
|
|
var errLegacy = errors.New("signed, and waiting for you: sign its SHA256SUMS with the old release key (trusted comment devvm <version>) and upload SHA256SUMS.minisig to its draft")
|
|
|
|
// notesFile is the release's notes the co-signer keeps, from the tag's
|
|
// CHANGELOG.md.
|
|
const notesFile = "notes.md"
|
|
|
|
// mustVersion is v parsed, which State.check made sure it is.
|
|
func mustVersion(v string) version {
|
|
out, _ := parseVersion(v)
|
|
return out
|
|
}
|
|
|
|
// keptFile is the file name the co-signer kept for release v in its
|
|
// state folder dir, read again and checked against the SHA-256 the state
|
|
// recorded, as it sits on disk between runs.
|
|
func keptFile(dir string, s *State, v, name string) ([]byte, error) {
|
|
want, ok := s.Release.Kept[v+"/"+name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("the co-signer kept no %s for %s: copy kept/%s from the machine that signed it", name, v, v)
|
|
}
|
|
f, err := os.Open(filepath.Join(dir, "kept", v, name)) //nolint:gosec // the co-signer's own state folder
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
data, err := io.ReadAll(io.LimitReader(f, maxManifest+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if sha256Hex(data) != want {
|
|
return nil, fmt.Errorf("kept/%s/%s isn't the file the co-signer kept: check the machine", v, name)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// releaseTitle is a release's title on GitHub, as scripts/publish.sh
|
|
// names it.
|
|
func releaseTitle(v string) string { return "devvm " + v }
|
|
|
|
// publishRelease signs h's SHA256SUMS, makes cd's draft release if the
|
|
// co-signer has none, uploads every file to it, and publishes it once two
|
|
// of three keys signed it, and while the bridge lasts the old key too, and
|
|
// the draft is exactly what it uploaded; then it checks the release again
|
|
// and finishes the tap.
|
|
func (c *Cosigner) publishRelease(ctx context.Context, s *State, cd candidate, draft Release, h handover) error {
|
|
l, v := &s.Release.Ledger, cd.v.raw
|
|
notes, err := keptFile(c.Dir, s, v, notesFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sig, err := c.Release.Secret.Sign(h.m.Data, minisign.Release.Comment(v))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := c.Release.Keys.Check(h.m.Data, map[minisign.Role][]byte{minisign.CI: h.ciSig, minisign.Cosign: sig}, v); err != nil {
|
|
return fmt.Errorf("not published: %w", err)
|
|
}
|
|
if draft.ID == 0 {
|
|
if draft, err = c.GitHub.CreateRelease(ctx, c.Repo, cd.tag, "", releaseTitle(v), string(notes), cd.v.prerelease()); err != nil {
|
|
return fmt.Errorf("making its draft release: %w", err)
|
|
}
|
|
l.Drafts[v] = append(l.Drafts[v], draft.ID)
|
|
if err := c.save(s); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
keep := ""
|
|
if c.Release.Legacy != nil {
|
|
keep = legacySig
|
|
}
|
|
ids, err := c.upload(ctx, draft, h.assets(sig), keep)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.logf("%s: signed, and its draft release holds its files", cd.tag)
|
|
now, err := c.GitHub.Release(ctx, c.Repo, draft.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c.Release.Legacy != nil {
|
|
old, a, err := c.download(ctx, now, legacySig, maxSig)
|
|
if errors.Is(err, errWaiting) {
|
|
return errLegacy
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
if err := c.Release.Legacy.Check(h.m.Data, old, minisign.LegacyComment(v)); err != nil {
|
|
return fmt.Errorf("not published: its %s: %w", legacySig, err)
|
|
}
|
|
ids[legacySig] = a
|
|
}
|
|
if err := exact(now, cd.tag, releaseTitle(v), string(notes), ids, ""); err != nil {
|
|
return fmt.Errorf("not published: %w", err)
|
|
}
|
|
// The tag must still be the one the rebuild checked: a tag deleted or
|
|
// pushed again while the draft waited would publish other code.
|
|
if obj, found, err := c.GitHub.TagRef(ctx, c.Repo, cd.tag); err != nil {
|
|
return err
|
|
} else if !found || obj != s.Release.Tags[v] {
|
|
return fmt.Errorf("not published: %s now names %q, not the tag object %s the co-signer checked; find out who moved it", cd.tag, obj, s.Release.Tags[v])
|
|
}
|
|
// GitHub's latest only when it's newer than every release published,
|
|
// so a release published after a newer one, as when the old key signs
|
|
// the newer one's draft first, never moves the latest back.
|
|
pub, err := c.GitHub.Publish(ctx, c.Repo, draft.ID, cd.v.prerelease(), !cd.v.prerelease() && !olderThanPublished(*s, cd.v))
|
|
if err != nil {
|
|
return fmt.Errorf("publishing: %w", err)
|
|
}
|
|
if err := exact(pub, cd.tag, releaseTitle(v), string(notes), ids, ""); err != nil {
|
|
l.alert(v, "published, but "+err.Error()+": compare its files with its SHA256SUMS by hand. devvm and the install script refuse a file that doesn't match it, and Homebrew one that doesn't match its formula, which the tap doesn't get; with immutable releases on, the fix is a newer release")
|
|
return c.save(s)
|
|
}
|
|
l.Published[v] = true
|
|
if err := c.save(s); err != nil {
|
|
return err
|
|
}
|
|
c.logf("%s: published", cd.tag)
|
|
return c.finish(ctx, s, cd.tag, cd.v)
|
|
}
|
|
|
|
// publishedRelease looks at r, a published release: one the co-signer
|
|
// published gets its tap finished; its own draft that it didn't see
|
|
// published (its publish's answer lost, or a maintainer published it) is
|
|
// checked as published first. One it didn't make is foreign's.
|
|
func (c *Cosigner) publishedRelease(ctx context.Context, s *State, r Release) error {
|
|
v, _, _ := tagKind(r.TagName)
|
|
l := &s.Release.Ledger
|
|
switch {
|
|
case s.Release.Done[v.raw] || l.Alerts[v.raw].active():
|
|
return nil
|
|
case l.Published[v.raw]:
|
|
return c.finish(ctx, s, r.TagName, v)
|
|
case l.Signed[v.raw] != "" && l.draft(v.raw) == r.ID:
|
|
if err := c.checkPublished(ctx, s, r, v); err != nil {
|
|
l.alert(v.raw, "the co-signer's draft was published before its last check, and "+err.Error()+": compare its files with its SHA256SUMS by hand, then ack it; the tap isn't updated")
|
|
return c.save(s)
|
|
}
|
|
l.Published[v.raw] = true
|
|
if err := c.save(s); err != nil {
|
|
return err
|
|
}
|
|
return c.finish(ctx, s, r.TagName, v)
|
|
}
|
|
return nil // not its own: foreign alerts on it, if it's new
|
|
}
|
|
|
|
// checkPublished checks r, the co-signer's draft, published: its
|
|
// SHA256SUMS is the one it signed, CI's signature and its own check out,
|
|
// and the old key's while the bridge lasts, every file has its SHA-256,
|
|
// downloaded, and its title and notes are the co-signer's.
|
|
func (c *Cosigner) checkPublished(ctx context.Context, s *State, r Release, v version) error {
|
|
data, _, err := c.download(ctx, r, sumsName, maxManifest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if sha256Hex(data) != s.Release.Signed[v.raw] {
|
|
return fmt.Errorf("its SHA256SUMS isn't the one the co-signer signed")
|
|
}
|
|
m, err := ParseSums(v.raw, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sigs := map[minisign.Role][]byte{}
|
|
for _, role := range []minisign.Role{minisign.CI, minisign.Cosign} {
|
|
if sigs[role], _, err = c.download(ctx, r, minisign.SigName(sumsName, role), maxSig); err != nil {
|
|
return err
|
|
}
|
|
if err := c.Release.Keys.CheckRole(role, m.Data, sigs[role], v.raw); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
names := []string{minisign.SigName(sumsName, minisign.CI), minisign.SigName(sumsName, minisign.Cosign)}
|
|
if c.Release.Legacy != nil {
|
|
old, _, err := c.download(ctx, r, legacySig, maxSig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := c.Release.Legacy.Check(m.Data, old, minisign.LegacyComment(v.raw)); err != nil {
|
|
return err
|
|
}
|
|
names = append(names, legacySig)
|
|
}
|
|
notes, err := keptFile(c.Dir, s, v.raw, notesFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !sameText(r.Name, releaseTitle(v.raw)) || !sameText(r.Body, string(notes)) {
|
|
return fmt.Errorf("its title or notes aren't the co-signer's")
|
|
}
|
|
return c.GitHub.checkFiles(ctx, c.Repo, r, m, names...)
|
|
}
|
|
|
|
// formulaVersion is the release the formula data installs, from its
|
|
// first download URL; "" for none.
|
|
func formulaVersion(data []byte) string {
|
|
m := regexp.MustCompile(`/releases/download/v([0-9a-z.-]+)/`).FindSubmatch(data)
|
|
if m == nil {
|
|
return ""
|
|
}
|
|
return string(m[1])
|
|
}
|
|
|
|
// finish updates the tap for release tag, v, which the co-signer
|
|
// published and checked after: the formulas it rendered from the signed
|
|
// SHA256SUMS and kept, each checked again, each only when the tap's is
|
|
// older, then records the release done.
|
|
func (c *Cosigner) finish(ctx context.Context, s *State, tag string, ver version) error {
|
|
v := ver.raw
|
|
message := "devvm " + v
|
|
if ver.prerelease() {
|
|
message = "devvm-rc " + v
|
|
}
|
|
for _, f := range formulas(ver) {
|
|
want, err := keptFile(c.Dir, s, v, f)
|
|
if err != nil {
|
|
return fmt.Errorf("the tap's %s: %w", f, err)
|
|
}
|
|
path := "Formula/" + f
|
|
have, sha, found, err := c.GitHub.Contents(ctx, c.Tap, path, "main")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found && bytes.Equal(have, want) {
|
|
continue
|
|
}
|
|
if was, err := parseVersion(formulaVersion(have)); found && err == nil && was.compare(ver) > 0 {
|
|
// Newer than any release the co-signer signed: nothing it
|
|
// published, so someone else wrote the tap, which would hold
|
|
// every update back. Its alert is its own, by the formula and
|
|
// the version it installs, whatever else was raised on v.
|
|
key := "tap:" + f + "@" + was.raw
|
|
if a, raised := s.Others[key]; was.compare(mustVersion(s.Release.Last)) > 0 && (!raised || a.active()) {
|
|
if !raised {
|
|
s.Others[key] = Alert{Reason: "the tap's " + path + " installs " + was.raw + ", newer than any release the co-signer signed, so someone else wrote it, and it holds " + v + " back: find out who and put the tap right", Raised: 1}
|
|
return c.save(s)
|
|
}
|
|
return nil // held reports it
|
|
}
|
|
c.logf("%s: the tap's %s stays at %s, which is newer", tag, path, was.raw)
|
|
continue
|
|
}
|
|
if err := c.GitHub.PutContents(ctx, c.Tap, path, "main", message, want, sha); err != nil {
|
|
return fmt.Errorf("the tap's %s: %w", path, err)
|
|
}
|
|
c.logf("%s: the tap's %s is now devvm %s", tag, path, v)
|
|
}
|
|
s.Release.Done[v] = true
|
|
return c.save(s)
|
|
}
|