gunk-dev/the-valley

doc.go

source9f64ac954238main's published tip history
6.4 KiB
1// Package export is the contract between the web UI's publisher, which
2// runs on a valley host, and its receiver and server, which run on a web
3// server the host pushes to and never hears back from. Both sides build
4// against this package, so neither reinvents its rules.
5//
6// # The export
7//
8// An export is export.json and one git bundle of main per project. It
9// holds the projects the publisher exports, and only what a reader of one
10// may see. Build projects the valley's own JSON onto it: `valley plan
11// --all --json`, `valley pending --all --json`, and `valley knot show
12// --project P <id> --json` for each knot of each exported project, with
13// each project's publication identity, its main, and a display mapping
14// for principal names. Every knot and comment of an exported project is
15// exported. The export says nothing of who may read a project.
16//
17// # The authorization
18//
19// An Authorization is a signed note, apart from the export, that names
20// each project a reader may read, by name and publication identity, and
21// its readers. Its sequence is the publisher's own counter, and the
22// receiver admits one with VerifyAuthorization by its sequence alone,
23// never its age. The receiver stores it with Store.Authorize, without the
24// activation lock, and a server serves a project only while the stored
25// authorization names it. No content manifest can grant or restore
26// access.
27//
28// Digest is the export's content digest. It leaves out every
29// display-time field, so the publisher uploads only when the valley
30// changed, or when the receiver's inventory says it lacks something.
31//
32// # The manifest
33//
34// A Manifest is a signed note that names the instance that signed it,
35// the destination it is for, its sequence (the signing time in Unix
36// nanoseconds), export.json's hash and size, and each project's
37// identity, graph tip, source tip and bundle. VerifyManifest admits one
38// in a fixed order of steps, and CheckExport holds export.json to it.
39//
40// # The upload
41//
42// The publisher uploads an export to the receiver in three steps, each
43// one HTTP request. Every reply is JSON.
44//
45// Every request carries the receiver's token, which ReadToken reads, in
46// its Authorization header (Authorization). The token authenticates the
47// transport; the manifest's signature still authenticates the export. The
48// receiver refuses a request without the token as unauthenticated, with
49// 401, before it reads the body or anything else of the request.
50//
51// Before the upload, the publisher sends its authorization: POST
52// AuthorizationPath with the signed note as the body. The receiver admits
53// it with VerifyAuthorization, stores it, and replies 200 with an
54// AuthorizationReply, which says what export it serves.
55//
56// 1. POST ManifestPath with the signed manifest as the body. The
57// receiver admits it with VerifyManifest, holds the bodies it declares
58// to the receiver's caps, and stages it with Store.Stage. It replies
59// 200 with an Inventory: the export it
60// serves, with its sequence and content digest; per project, the
61// source tip it holds a verified repository at; and the bodies it
62// still needs. Posting the same manifest again is harmless and gets
63// the same inventory, less what has been sent since. A publisher may
64// stop here when the inventory shows the receiver serves its digest
65// and needs no bundle; the next manifest it posts discards the
66// staged upload.
67// 2. PUT BodyPath(sequence, sha256) for each needed body, export.json
68// and the bundles, with the body's bytes as they are: no
69// Content-Encoding, and a Content-Length, when given, equal to the
70// declared size. The receiver streams it into staging with
71// StageBody, and replies 204 once it verified. A body already staged
72// is accepted again without being read.
73// 3. POST CommitPath(sequence) with an empty body. The receiver
74// activates the upload with Store.Activate only when every body has
75// verified, its bundles pass AcceptBundle, and export.json agrees
76// with the manifest. It replies 200 with Committed.
77//
78// A refusal is replied with Code.Status and an ErrorReply, whose error is
79// a Code and stable. What each step refuses:
80//
81// - every step: unauthenticated, before anything else;
82// - step 1: too-large, bad-signature, bad-manifest, wrong-instance,
83// wrong-destination, replayed, out-of-window, in VerifyManifest's
84// order; then too-large for a body over the receiver's cap, or bodies
85// over its cap on one upload together; superseded when a newer
86// manifest is staged;
87// - step 2: no-manifest when nothing is staged for the sequence,
88// superseded when a newer manifest replaced it, not-declared for a
89// hash the manifest does not declare, too-large, content-encoding,
90// size-mismatch, hash-mismatch;
91// - step 3: no-manifest, superseded, replayed, incomplete with the
92// bodies still needed, bad-export, bad-bundle.
93//
94// A replayed manifest, or one older than the receiver's high-water mark,
95// is refused as replayed, with the high-water mark. The publisher's
96// clock is behind the receiver's record, and it waits for its clock to
97// pass the mark rather than publish out of order. A refused upload
98// leaves the active export as it was.
99//
100// A commit has succeeded once the receiver has written its sequence to
101// the high-water mark and swapped the active export to it, and the reply
102// is then 200 with Committed. A later failure does not change the reply:
103// deleting old exports, stale uploads and the trash, or syncing the swap
104// to disk. Store passes it to the report function given to OpenStore,
105// for the receiver to log, and the next sweep or OpenStore repairs it. A
106// commit that fails before then leaves the active export as it was. If
107// it failed after the high-water mark was written, its sequence is
108// accepted, and the commit retried is refused as replayed. The
109// publisher's next manifest, signed later, replaces the upload.
110//
111// # The receiver's state
112//
113// Store is the receiver's store: a descriptor file, and beside it the
114// payload directory v2 with the authorization, the generations, the
115// current symlink, the high-water mark and the staging area. Open
116// resolves the current generation once per request and holds it until
117// released, Activate makes a staged upload the current generation, and
118// Prune deletes the repositories a withdrawal left. The receiver opens the
119// store with OpenStore, and the server with OpenReadOnly, which needs only
120// read access to it.
121package export