Files
w3cjandClaude Opus 5.5 f3052e5a8e feat(upgrade): releases checked by two of three release keys, bridged from the old key
devvm upgrade and install.sh accept a release's SHA256SUMS when any two
of three release keys signed it (CI's, the co-signer's and an offline
recovery key's), each in SHA256SUMS.<role>.minisig, checked only against
its own role's key, with the trusted comment devvm-release <version>
compared exactly; one signer counts once. While releases bridge from it,
the old release key's single SHA256SUMS.minisig (devvm <version>) still
passes alone, so 0.1 and 0.2 keep upgrading and --to/--version can
install them; install.sh now checks that key's trusted comment too.

The keys themselves aren't made yet, so packaging/keys holds none and
install.sh's three key lines are empty: a devvm built so checks each
release with the old release key alone, and a release that only the
release keys signed is refused. Releases go out as before, signed with
the old key, until the keys are made and added, all three at once.

- internal/minisign: the one owner of signature checks (moved from
  internal/upgrade): ParsePublicKey, PublicKey.Check (comment always
  compared), Role (CI, Cosign, Recovery), Kind (Release, Base),
  Kind.Comment, SigName, KeyFile, Keys/NewKeys/Keys.Check (two of three,
  distinct keys and key IDs). minisigntest/ signs for tests. Stdlib and
  blake2b only (depguard), a leaf; FuzzCheck, FuzzKeysCheck,
  FuzzParsePublicKey.
- The structure test's signatures rule: outside internal/minisign no
  code calls ed25519/rsa/ecdsa's Verify functions or imports blake2b,
  but signatureFree's files (ubuntu/pgp.go, proxy/nixcache.go).
- internal/upgrade: Source.Keys, checkSums (signatures.go); the bridge
  in legacy.go (checkLegacy, legacyKey, Source.Key); FuzzSumOf.
- packaging/keys/release-{ci,cosign,recovery}.pub, none yet;
  packaging.Key ("" without one). TestReleaseKeys (checkReleaseKeys:
  none, or three distinct minisign keys, none the old key),
  TestCheckReleaseKeys (fixture keys from minisigntest),
  TestInstallScriptKeys (checkInstallScriptKeys: one line per role,
  empty without keys), TestReleaseRefusesPlaceholders (no key in keys/
  marked PLACEHOLDER).
- scripts/release.sh (a tag, --check, --tag; never a snapshot) needs
  none or all three of packaging/keys' release keys. With none,
  install.sh's release_<role>_key lines are empty or missing; with all
  three, each is a minisign key, distinct, not the old release key, not
  marked PLACEHOLDER (placeholder), and equal to its install.sh line.
- install.sh checks with minisign -VHQm: prehashed signatures only.
- The structure guard also flags dot imports of the verifier packages
  and calls of x509's CheckSignature* method names.
- install.sh: release_<role>_key lines, download_signatures (a role's
  signature only when the script has its key), check_signature,
  signed_by_two, check_legacy; plainer warning without minisign, with
  the commands to check by hand.
- Tests: signatures_test.go (TestFetchWithoutReleaseKeys among them),
  upgrade.txtar (-legacy, -keys, and the keys built in, none),
  install.txtar (stub minisign with -Q; NO_KEYS), install_minisign.txtar
  (real minisign), release.txtar (none, nolines, nokeyline, old, marked
  and the rest); DEVVM_RELEASE_KEYS override.
- Docs: internals/install.md, using/upgrading.md, maintaining.md's
  Release keys, structure, tests, testing, SECURITY.md, CHANGELOG,
  releases/0.2.0.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 21:36:37 -07:00

164 lines
5.2 KiB
Go

package cli
import (
"cmp"
"context"
"crypto/x509"
"errors"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/internal/minisign"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/upgrade"
"github.com/devvmhq/devvm/packaging"
)
// upgradeOpts are devvm upgrade's flags.
type upgradeOpts struct {
upgrade.Options
check, asJSON bool
}
// newUpgradeCmd is devvm upgrade.
func newUpgradeCmd(a *app) *cobra.Command {
var o upgradeOpts
cmd := &cobra.Command{
Use: "upgrade",
Annotations: about{docs: "using/upgrading#upgrading", group: groupMachine}.annotations(),
Short: "Install devvm's newest release",
Long: "Install devvm's newest release the way this devvm was installed, after asking. " +
"The install script's devvm gets the release once its signature and checksum check out, and Homebrew's runs brew upgrade devvm, " +
"or brew upgrade devvm-rc for the formula that follows prereleases. " +
"--check only says whether a newer release is out.",
Example: examples(
"devvm upgrade", "install the newest release, asking first",
"devvm upgrade --check", "say whether a newer release is out",
"devvm upgrade --force", "install it without asking, as a script must",
"devvm upgrade --to 0.1.0", "install that release, even an older one",
"devvm upgrade --to 0.2.0-rc.1", "install a prerelease, which only --to installs",
),
Args: noArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, _ []string) error {
up, err := a.upgrader()
if err != nil {
return failed(err, msg.UpgradeFailedReason)
}
if o.check || o.asJSON {
return failed(showUpgrade(cmd.Context(), a, up, o.asJSON), msg.UpgradeFailedReason)
}
o.Version = strings.TrimPrefix(o.Version, "v")
return failed(up.Upgrade(cmd.Context(), o.Options), msg.UpgradeFailedReason)
},
}
f := cmd.Flags()
f.BoolVar(&o.check, "check", false, "say whether a newer release is out, and install nothing")
f.StringVar(&o.Version, "to", "", "install the release `VERSION` instead of the newest, even an older one or a prerelease")
addForceFlag(f, &o.Force)
jsonFlag(cmd, &o.asJSON)
exclusive(cmd, "check", "force")
exclusive(cmd, "json", "force")
exclusive(cmd, "check", "to")
exclusive(cmd, "json", "to")
return cmd
}
// upgrader is devvm upgrade's: this devvm, as it was installed, and the
// release source, GitHub's releases of devvm checked with the release
// keys built into devvm and the old release key, or a test build's
// stand-ins.
func (a *app) upgrader() (upgrade.Upgrader, error) {
o := a.testOverrides().Release
src := upgrade.NewSource(o.URL, releaseKeys(o.Keys), cmp.Or(o.Key, packaging.MinisignKey()))
if o.CA != "" {
pem, err := os.ReadFile(o.CA)
if err != nil {
return upgrade.Upgrader{}, err
}
roots := x509.NewCertPool()
if !roots.AppendCertsFromPEM(pem) {
return upgrade.Upgrader{}, errors.New(o.CA + " has no certificate")
}
src.Client.Trust(roots)
}
exe, err := os.Executable()
if err != nil {
return upgrade.Upgrader{}, err
}
return upgrade.Upgrader{
Reporter: a.u,
Source: src,
Install: upgrade.Detect(exe),
Current: cmp.Or(o.Version, devvmVersion()),
FromSource: version == "" && o.Version == "",
}, nil
}
// releaseKeys are the release keys that devvm upgrade checks releases
// with, by role: those built into devvm (packaging's keys/), or a test
// build's override, DEVVM_RELEASE_KEYS, as ROLE=KEY words. A role without
// a key has none, which minisign.NewKeys refuses unless every role has
// none.
func releaseKeys(override string) map[minisign.Role]string {
keys := map[minisign.Role]string{}
if override != "" {
for _, w := range strings.Fields(override) {
r, k, _ := strings.Cut(w, "=")
keys[minisign.Role(r)] = k
}
return keys
}
for _, r := range minisign.Roles() {
if k := packaging.Key(minisign.KeyFile(minisign.Release, r)); k != "" {
keys[r] = k
}
}
return keys
}
// upgradeJSON is devvm upgrade --json: this devvm's version, how and
// where it was installed, the newest release's version, and whether
// that's newer.
type upgradeJSON struct {
Current string `json:"current"`
Latest string `json:"latest"`
Newer bool `json:"newer"`
Install string `json:"install"`
Path string `json:"path"`
}
// showUpgrade says what devvm upgrade would install, as JSON with
// asJSON, and installs nothing.
func showUpgrade(ctx context.Context, a *app, up upgrade.Upgrader, asJSON bool) error {
u := a.u
done := u.Status(msg.UpgradeChecking())
av, err := up.Source.Check(ctx, up.Current, up.Install.Prereleases())
done()
if err != nil {
return err
}
if asJSON {
return printJSON(u, upgradeJSON{
Current: av.Current, Latest: av.Latest, Newer: av.Newer,
Install: string(up.Install.Kind), Path: up.Install.Exe,
})
}
switch {
case up.Install.Kind == upgrade.Other || !upgrade.Valid(up.Current):
u.Info(msg.UpgradeLatestIs(av.Latest))
u.Info(up.NotInstalled())
case av.Newer:
u.Info(upgrade.Announcement(av.Current, av.Latest))
case av.Latest == av.Current:
u.Info(msg.UpgradeAlreadyLatest(av.Current))
case upgrade.Prerelease(av.Current):
u.Info(msg.UpgradePrereleaseAhead(av.Current, av.Latest))
default:
u.Info(msg.UpgradeLatestOlder(av.Current, av.Latest))
}
return nil
}