Documentation
¶
Overview ¶
Package selfupdate keeps a running Flockdeck up to date from its releases, published at dl.flockdeck.ai (Site) and mirrored on GitHub.
The work is split into three steps that are deliberately kept apart, because the application can afford to do the first two at any time and can only ever do the third at a moment of the user's choosing:
- Latest asks what the latest release is, and Newer says whether it is one to move to; an untagged local build never has one.
- Stage downloads it, checks it against the published SHA-256, signed by the release key whether it comes from the site or from GitHub, and unpacks the binary into the state directory. Nothing about the installation has changed yet.
- Apply swaps the staged binary into place. This is the only step that touches the installed program, and it is never done under a running session: panes hold live agents, and replacing the binary beneath them to save a restart would cost far more work than it saved.
Staging is recorded on disk, so an update downloaded in one run is still there to be applied by the next, and a half-finished download is never mistaken for a finished one.
On Windows the program has a console twin beside it (chatName), the same program with its PE Subsystem set to console, which an API agent's pane runs. Stage and Apply carry it with the program when a release has one, and EnsureChatTwin keeps it exactly in step with the program at every start however the program was put there.
Index ¶
- Constants
- Variables
- func Apply(dir, exePath string) error
- func CheckPointer(data []byte) (string, error)
- func ConsoleTwin(program []byte) ([]byte, bool)
- func Dir(state string) (string, error)
- func Discard(dir string)
- func EncodePublicKey(k ed25519.PublicKey) string
- func EncodeSigningKey(k ed25519.PrivateKey) string
- func EnsureChatTwin(exePath string) error
- func Newer(candidate, current string) bool
- func ParseSigningKey(s string) (ed25519.PrivateKey, error)
- func Parseable(v string) bool
- func ReleaseKey() (ed25519.PublicKey, bool)
- func Sign(k ed25519.PrivateKey, data []byte) []byte
- func StandbyKey() (ed25519.PublicKey, bool)
- func Sweep(exePath string)
- func TrustKeysForTest(primary, standby ed25519.PublicKey) func()
- func TrustedKeys() []ed25519.PublicKey
- func Verify(key ed25519.PublicKey, data, sig []byte) error
- func VerifyAny(keys []ed25519.PublicKey, data, sig []byte) error
- type Asset
- type IndexedRelease
- type Manifest
- type ManifestFile
- type Pending
- type Pointer
- type Recalled
- type RecalledVersion
- type Release
- type ReleaseIndex
Constants ¶
const Repo = "Flockdeck/flockdeck"
Repo is the repository releases are read from, as owner/name.
const Site = "https://dl.flockdeck.ai"
Site is where releases are downloaded from first: a DigitalOcean Space behind DigitalOcean's CDN, laid out as
/latest.json {"version":"v1.2.3"}: which release is the latest
/<version>/manifest.json(.sig) that release's files and their SHA-256s, signed
/<version>/<archive> each release's files
/<version>/checksums.txt(.sig) and their SHA-256s, signed
/latest/<archive without the version> for the site's download buttons
/recalled.json(.sig) versions withdrawn after release, signed; see Recall
/releases.json(.sig) recent published releases, signed; see List
Everything under /<version>/ is written once, when the release is published, and never changes, so it is cached for a year. latest.json is the one file that moves, and it is neither signed nor purged from the CDN's edges when it does. All it can do is name a version, and the updater trusts nothing until that version's own manifest has passed the release key's signature and names the same version. So a stale latest.json, or a forged one, can only name another signed release: an older one, which Newer never moves to, or a pre-release, which latestFromSite refuses. Otherwise it can hold an update back for as long as it is cached, and it can never have anything unsigned installed, nor anything older than the copy running.
It can name a release that was withdrawn, though: one signed and published, then taken down because something was wrong with it. That release is still newer than a copy that never moved to it, and its signature still checks. So whoever can write the site can point latest.json back at it and have it installed, and so can whoever can publish on GitHub, where the updater goes whenever the site fails. latest.json alone still cannot stop that: only unpublishing the release everywhere Latest reads from does (see updateSteps, which already discards a staged download once it is no longer the newest published release). recalled.json is the other half, for a copy that already applied the bad release before it was pulled: a signed list of versions known to be bad, checked by Recall, which the ordinary "is something newer" check (Latest, Newer) never answers on its own.
GitHub carries every release as well, checksums.txt.sig with it, and is where the updater goes when the site cannot be reached, answers with something it cannot use, or fails a signature. What it downloads from there is still held to the release key's signature. GitHub is also the only place a copy from before the site knows about, which is why every release is still published there.
Asking here first is what keeps a shared office network from running out of GitHub's sixty unauthenticated API calls an hour: the site has no such limit.
Variables ¶
var ErrNoAsset = errors.New("this release has no build for this platform")
ErrNoAsset is returned when a release carries nothing built for this platform, which is what a partly-uploaded release looks like from here.
Functions ¶
func Apply ¶
Apply replaces the running program's file with the staged binary, and on Windows the console twin beside it (chatName) with the staged one.
The two go in together or not at all: a new program beside an old twin, or the other way round, would run a chat client of another version in its panes. So the twin goes in first and is taken back out if the program then cannot be put in, and the update stays staged for the next attempt.
func CheckPointer ¶ added in v0.2.10
CheckPointer reads latest.json and returns the version it names, refusing anything that does not name one.
func ConsoleTwin ¶ added in v0.2.3
ConsoleTwin is program with its PE Subsystem field set to console: the console twin of a GUI build, which is all that tells the two apart and all that decides whether Windows gives a pane's process a console. It is false for anything that is not a GUI-subsystem PE file.
func Discard ¶
func Discard(dir string)
Discard forgets a staged update and removes what it downloaded.
func EncodePublicKey ¶ added in v0.2.10
EncodePublicKey is a public key as `cmd/release -keygen` prints it.
func EncodeSigningKey ¶ added in v0.2.10
func EncodeSigningKey(k ed25519.PrivateKey) string
EncodeSigningKey is the content of the signing key's file -keygen writes.
func EnsureChatTwin ¶ added in v0.2.3
EnsureChatTwin keeps the console twin (chatName) beside the program at exePath exactly what the program makes of itself (ConsoleTwin), writing it when it is missing or differs.
A release carries the twin, but the first update to one is put in place by the release before it, whose updater knows only the program, and an installation made by an install script of before, or by `go install`, has none either: without one an API agent's pane is blank. And a program replaced by any route that leaves the twin as it was — a copy by hand, an updater of before — would have its panes run a chat client of another version, whose hook events, prompts and transcripts can have drifted.
Only a file of exactly that name in the program's own directory is ever written, never through a link, a reparse point or anything else that is not a regular file. The new twin is written beside it and renamed over it; a twin a chat pane is running from cannot be renamed over on Windows, and is left for the next start, the pane working on meanwhile. It does nothing off Windows, for the twin itself, or for a program that is not a GUI build, which runs in a pane as it is.
func Newer ¶
Newer reports whether candidate is a release worth moving to from current.
It answers no when either side cannot be read, which is the case that keeps an untagged local build in place: there is no sense in which `dev` is behind v1.4.0, because it may well be ahead of it.
func ParseSigningKey ¶ added in v0.2.10
func ParseSigningKey(s string) (ed25519.PrivateKey, error)
ParseSigningKey reads the signing key as FLOCKDECK_SIGNING_KEY holds it: the PKCS#8 PEM Terraform gives, or the base64 seed -keygen writes. The error never quotes the text, which is a secret.
func Parseable ¶
Parseable reports whether a version string is one that can be ordered against a release at all. An untagged local build, stamped `dev`, is not, which is a different thing from being up to date and worth saying so.
func ReleaseKey ¶ added in v0.2.10
ReleaseKey is the primary public key compiled into this build, and false while it is still the placeholder.
func Sign ¶ added in v0.2.10
func Sign(k ed25519.PrivateKey, data []byte) []byte
Sign is the content of the .sig file for data.
func StandbyKey ¶ added in v0.3.5
StandbyKey is the standby public key compiled into this build, and false while it is still the placeholder: before the user has generated the standby keypair and its public half has been put in releaseKeyStandby.
func Sweep ¶
func Sweep(exePath string)
Sweep removes the files previous Applies moved aside, the console twin's as well as the program's, and what an interrupted EnsureChatTwin left. It is called at startup, by which time nothing holds the old program open — or, for one an instance still running is using, fails to and leaves it for next time.
func TrustKeysForTest ¶ added in v0.3.5
TrustKeysForTest makes this build trust exactly primary and standby (either may be nil) in place of releaseKey and releaseKeyStandby, until the returned func puts back what it trusted before. It exists for tests of other packages, such as cmd/release's, that need TrustedKeys and runSign's check against it to see keys the test itself generated, since the real compiled keys' private halves are held by nobody the tests can reach.
It panics outside a test binary: exported only so another package's tests can reach it, never so that anything shipped could swap out the keys a build trusts.
func TrustedKeys ¶ added in v0.3.5
TrustedKeys is every key this build's updater accepts a signature from: the primary, and the standby once releaseKeyStandby holds a real key instead of its placeholder. Every signature this package checks against "the release key" is checked against all of these, so that a release signed with the standby is accepted exactly as one signed with the primary is.
func Verify ¶ added in v0.2.10
Verify reports whether sig, the content of a .sig file, is key's signature of data.
func VerifyAny ¶ added in v0.3.5
VerifyAny reports whether sig, the content of a .sig file, is any of keys' signature of data: the primary release key's, or the standby's once it holds one. It refuses everything, as errNoKey, when keys is empty, which is every build's own trusted keys (TrustedKeys) while both are still placeholders.
Types ¶
type Asset ¶
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
Asset is one file attached to a release.
type IndexedRelease ¶ added in v0.3.29
type IndexedRelease struct {
Version string `json:"version"`
Date time.Time `json:"date"`
NotesURL string `json:"notes_url,omitempty"`
Notes string `json:"notes,omitempty"`
}
IndexedRelease is one release in releases.json: as much about it as a picker needs to let somebody choose a version to install, not the whole of its signed manifest -- which is read separately, by Fetch, only for the one version somebody actually chooses.
type Manifest ¶ added in v0.2.10
type Manifest struct {
Version string `json:"version"`
Date time.Time `json:"date"`
NotesURL string `json:"notes_url"`
Notes string `json:"notes,omitempty"`
Files []ManifestFile `json:"files"`
}
Manifest is one release as the site holds it, <version>/manifest.json: every file of it, with the SHA-256 and size of each, and its notes. It is signed, in manifest.json.sig, and never changes once published. cmd/release writes it and the updater reads it, through this one type.
func CheckManifest ¶ added in v0.2.10
CheckManifest reads a release's manifest.json once its signature, the content of manifest.json.sig, has been checked against keys, and refuses a manifest the updater could not act on. cmd/release reads back what it wrote through this, so a release is never published with a manifest the updater would refuse. A signature from any one of keys is accepted, so a release signed with the standby passes exactly as one signed with the primary does.
type ManifestFile ¶ added in v0.2.10
type ManifestFile struct {
Name string `json:"name"`
URL string `json:"url"`
SHA256 string `json:"sha256"`
Size int64 `json:"size"`
}
ManifestFile is one file of a release on the site.
type Pending ¶
type Pending struct {
Version string `json:"version"`
Binary string `json:"binary"`
Chat string `json:"chat,omitempty"` // the staged console twin (chatName), when the release has one
Notes string `json:"notes,omitempty"`
URL string `json:"url,omitempty"`
Staged time.Time `json:"staged"`
// Chosen says this release was staged by naming it, rather than reached
// as "the latest": the CLI's `update -version=X`, or the interface's
// version picker (StageChosen). A caller deciding whether what is staged
// is still safe to apply, or to leave alone rather than replace with
// whatever is newest, treats a chosen release as somebody's own decision
// and never second-guesses it against Newer the way an ordinary staged
// update is -- see cli_update.go's stagedUpdate and checkRound.
Chosen bool `json:"chosen,omitempty"`
}
Pending is an update that has been downloaded, checked and unpacked, and is waiting for a restart to be applied.
func Load ¶
Load returns the update waiting to be applied, if there is one.
A record whose binary has gone is not an update: the file can be cleared out from under us by anything that tidies temporary directories, and reporting an update that could not possibly be applied would put a badge in the interface that a restart would never clear.
func Stage ¶
Stage downloads the release, checks it and unpacks the binary under dir, with the console twin beside it when the release has one.
A release read from the site is downloaded from there, and from GitHub when that fails for any reason, the reason logged. Either way the archive has to match the SHA-256 the release's signed manifest gave for it.
func StageChosen ¶ added in v0.3.29
StageChosen is Stage for a release chosen by name rather than reached as "the latest" -- the interface's version picker, which cannot Apply what it stages straight away the way the CLI's own `update -version=X` does, since Apply is never done under a running session (see the package comment): this runs inside the very process whose panes hold the live agents. What it stages is marked Pending.Chosen, so applying it at the next restart, and deciding whether to leave it alone in the meantime, never second-guesses the choice against Newer (see stagedUpdate and checkRound in cli_update.go).
type Pointer ¶ added in v0.2.10
type Pointer struct {
Version string `json:"version"`
}
Pointer is latest.json, the one file on the site that changes: it names the latest release and says nothing else about it. It is not signed; Site says why it need not be. cmd/release writes it and the updater reads it, through this one type.
type Recalled ¶ added in v0.3.26
type Recalled struct {
Versions []RecalledVersion `json:"versions"`
}
Recalled is recalled.json as the site holds it.
func CheckRecalled ¶ added in v0.3.26
CheckRecalled reads recalled.json once its signature, the content of recalled.json.sig, has passed VerifyAny, the same way CheckManifest reads a manifest. Signing it matters for the same reason everything else here is signed: an unsigned "you are running a known-bad build" is itself a thing an attacker could forge to push someone toward installing something else.
type RecalledVersion ¶ added in v0.3.26
type RecalledVersion struct {
Version string `json:"version"`
Reason string `json:"reason"`
Upgrade string `json:"upgrade,omitempty"`
}
RecalledVersion is one release recalled.json names: what was wrong with it, and, when the person publishing it knows of one, which version to move to instead of whatever Latest currently returns.
func Recall ¶ added in v0.3.26
func Recall(ctx context.Context, running string) *RecalledVersion
Recall reports whether running has been recalled: published, then pulled because something was found wrong with it, after copies had already updated into it. Latest and Newer only ever look forward from where a build already is, so nothing else tells a copy that already applied a bad release that the version it is running has a known problem -- this is meant to be checked at the same points Latest already is (the CLI's `update` and the background watcher), not only when looking for something newer.
It is best-effort in exactly the way Latest's use in the background watcher already is, and returns nil both when running is not on the list and when the list could not be read at all: a machine offline, a site that briefly fails, or a build with no release key to check the signature against costs nothing but silence until the next check, never a false "you are on a bad build." recalled.json has no GitHub mirror to fall back to, so a signature that does not check -- the site serving something that was not released, worth knowing about -- is logged as a warning the same way a bad manifest or checksums.txt from the site already is, rather than returned as an error nobody who only wants the answer would check for.
type Release ¶
type Release struct {
Version string `json:"tag_name"`
Notes string `json:"body"`
URL string `json:"html_url"`
Draft bool `json:"draft"`
Pre bool `json:"prerelease"`
Assets []Asset `json:"assets"`
// Published is when the release went out, for a picker to show. GitHub's
// API gives it on every release List and Fetch's GitHub path read; one
// read from the site carries its manifest's Date instead.
Published time.Time `json:"published_at,omitzero"`
// contains filtered or unexported fields
}
Release is a published release, reduced to what an update needs.
func Fetch ¶ added in v0.3.26
Fetch returns the named release, structured the same way Latest is but naming a specific version rather than deferring to whichever the site or GitHub currently call "latest." It tries the site first, reading <version>/manifest.json(.sig) directly rather than going through latest.json -- the site's per-version files are written once and kept forever, so this works for any version still hosted, published or withdrawn alike -- and falls back to GitHub's /repos/<repo>/releases/tags/<version> the same way Latest falls back to /releases/latest.
This is what lets Stage/Apply install a version other than the newest published one: reinstalling the version currently running, or rolling back to one that worked. Fetch itself does not decide whether moving to that version is a good idea -- unlike Latest, it does not refuse a pre-release, since asking for one by name is deliberate -- so a caller that means to only ever move forward keeps gating what Fetch returns with Newer, exactly as Latest's callers already do.
func Latest ¶
Latest returns the most recent published release: from the site when it answers with a manifest signed by the release key, and otherwise from GitHub, as every release before the site did, with the reason logged.
Neither is sent anything but the request itself: no version, no identifier, nothing a server could tell one installation from another by.
func List ¶ added in v0.3.26
List returns up to limit of the most recently published releases, newest first, for a picker that lets someone choose a version to install rather than only ever move to the latest one. It reads releases.json from the site first, trusted only once its signature checks -- the same way every other read here does -- and falls back to GitHub's API (/repos/<repo>/releases, not /releases/latest), the reason logged, when that cannot be used: releases.json unpublished, unreachable, or refused.
Unlike Latest, which every running copy asks every updateInterval, this is read only when somebody opens the picker, so asking GitHub on the fallback path costs nothing towards the sixty-an-hour unauthenticated limit that the site-first strategy elsewhere exists to spare.
Drafts are never returned, and neither are pre-releases, matching how Latest already treats them: this is a list of what somebody could roll back or forward to among shipped releases, not a way to find a candidate build.
func (*Release) DownloadSize ¶ added in v0.2.3
DownloadSize is the size in bytes of the archive Stage would fetch for this platform, or 0 when the release has none or does not say.
type ReleaseIndex ¶ added in v0.3.29
type ReleaseIndex struct {
Versions []IndexedRelease `json:"versions"`
}
ReleaseIndex is releases.json as the site holds it: the most recent published releases, signed the same way checksums.txt and recalled.json are. It exists only so List can offer a picker something to choose from without asking GitHub for it; nothing is ever installed on the strength of an entry here alone; see List.
func CheckReleaseIndex ¶ added in v0.3.29
func CheckReleaseIndex(keys []ed25519.PublicKey, data, sig []byte) (*ReleaseIndex, error)
CheckReleaseIndex reads releases.json once its signature, the content of releases.json.sig, has passed VerifyAny -- the same way CheckRecalled reads recalled.json.