gunk-dev/the-valley

doc.go

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