Documentation
¶
Overview ¶
Package update implements the verification side of the Sweef auto-updater: a signed release manifest (ed25519), resumable checksum-verified downloads, and a staged binary replacement with rollback. It is deliberately pure Go, has no dependency on the rest of Sweef, and is not wired into the CLI yet. The design is described in docs/updates.md.
Index ¶
- Constants
- Variables
- func Download(ctx context.Context, c *http.Client, a Artifact, dest string) error
- func ExecutablePath() (string, error)
- func Fetch(ctx context.Context, c *http.Client, url string) ([]byte, error)
- func IsNewer(candidate, current string) bool
- func ParsePublicKey(b64 string) (ed25519.PublicKey, error)
- func Recover(target string) error
- func Sign(priv ed25519.PrivateKey, data []byte) string
- func Verify(data []byte, sigB64 string, keys []ed25519.PublicKey) error
- type Artifact
- type Manifest
- type Replacement
- type Version
Constants ¶
const MaxManifestBytes = 1 << 20
MaxManifestBytes bounds manifest and signature downloads.
const SchemaVersion = 1
SchemaVersion is the only manifest schema this code understands.
Variables ¶
var ( ErrBadSignature = errors.New("update: manifest signature verification failed") ErrExpired = errors.New("update: manifest has expired") ErrNoArtifact = errors.New("update: no artifact for this platform") ErrNotNewer = errors.New("update: manifest version is not newer than the running version") ErrChecksum = errors.New("update: sha256 mismatch") )
Errors returned by this package.
Functions ¶
func Download ¶
Download fetches a.URL into dest, resuming a previous partial download (dest+".part") with an HTTP Range request when possible. The finished file is size- and SHA-256-verified against the (already signature-verified) artifact before being renamed to dest. On checksum failure the partial file is deleted so the next attempt starts clean.
func ExecutablePath ¶
ExecutablePath returns the resolved path of the running binary.
func IsNewer ¶
IsNewer reports whether candidate outranks current. Unparseable versions are never considered newer (fail closed).
func ParsePublicKey ¶
ParsePublicKey decodes a base64 (std) ed25519 public key.
func Recover ¶
Recover repairs an interrupted update at startup: if target is missing but target+".old" exists the swap was cut off between steps 2 and 3 (power loss), so restore it. Leftover .new/.bad files are removed.
Types ¶
type Artifact ¶
type Artifact struct {
OS string `json:"os"` // GOOS: linux, darwin, windows
Arch string `json:"arch"` // GOARCH: amd64, arm64, or "universal" on darwin
Kind string `json:"kind"` // "binary" (raw executable, used by the updater), "archive", "installer"
URL string `json:"url"`
SHA256 string `json:"sha256"` // lowercase hex
Size int64 `json:"size"`
}
Artifact is one downloadable file listed in a manifest.
type Manifest ¶
type Manifest struct {
Schema int `json:"schema"`
Channel string `json:"channel"` // e.g. "alpha"
Version string `json:"version"` // e.g. "0.1.0-alpha.7"
Published time.Time `json:"published"`
Expires time.Time `json:"expires"` // freeze-attack protection
NotesURL string `json:"notes_url,omitempty"`
Artifacts []Artifact `json:"artifacts"`
}
Manifest is the document published as latest.json. It is signed as exact bytes: the detached signature (latest.json.sig, base64) covers the file content verbatim, so there is no canonicalisation to get wrong.
func Check ¶
func Check(ctx context.Context, c *http.Client, manifestURL string, keys []ed25519.PublicKey, currentVersion string, now func() time.Time) (*Manifest, error)
Check downloads manifestURL and manifestURL+".sig", verifies the signature, and returns the manifest only when it is newer than currentVersion. It returns ErrNotNewer when already up to date.
func Parse ¶
Parse decodes and validates a manifest. It does NOT check the signature; callers must call Verify on the same bytes first (see VerifyAndParse).
func VerifyAndParse ¶
func VerifyAndParse(data []byte, sigB64 string, keys []ed25519.PublicKey, now time.Time) (*Manifest, error)
VerifyAndParse verifies the signature, then parses, then checks expiry.
type Replacement ¶
type Replacement struct {
// contains filtered or unexported fields
}
Replacement is an installed-but-not-yet-committed binary swap.
func Replace ¶
func Replace(target, newFile string) (*Replacement, error)
Replace atomically swaps newFile in as target, keeping the previous binary as target+".old" until Commit. Steps:
- copy newFile next to target (same filesystem) as target+".new", mode copied from target
- rename target -> target+".old" (works for a running executable on Linux, macOS and Windows)
- rename target+".new" -> target
If step 3 fails the old binary is put back. After the new version has started successfully and passed its self-check, call Commit; if it did not, call Rollback.
func (*Replacement) Commit ¶
func (r *Replacement) Commit() error
Commit discards the backup. On Windows a running .old file may be locked; that is ignored and it is cleaned up by Recover on the next start.
func (*Replacement) Rollback ¶
func (r *Replacement) Rollback() error
Rollback restores the previous binary. Safe to call after a failed health check.