gunk-dev/the-valley

doc.go

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