gunk-dev/the-valley

protocol.go

source535d69c9a69emain's published tip history
8.7 KiB
1package export
2
3import (
4 "errors"
5 "fmt"
6 "net/http"
7 "strconv"
8)
9
10// The receiver's paths, under its base URL. Every path is under
11// PathPrefix. A receiver answers a path under LegacyPrefix, where the
12// first version of the upload went, with an unsupported-version refusal.
13const (
14 PathPrefix = "/web-export/v2/"
15 LegacyPrefix = "/web-export/v1/"
16
17 // AuthorizationPath takes the POST of a signed authorization.
18 AuthorizationPath = PathPrefix + "authorization"
19 // ManifestPath takes step 1's POST of a signed manifest.
20 ManifestPath = PathPrefix + "manifest"
21)
22
23// BodyPath takes step 2's PUT of one body of the upload with sequence.
24func BodyPath(sequence int64, sha256 string) string {
25 return PathPrefix + strconv.FormatInt(sequence, 10) + "/" + sha256
26}
27
28// CommitPath takes step 3's POST that commits the upload with sequence.
29func CommitPath(sequence int64) string {
30 return PathPrefix + strconv.FormatInt(sequence, 10) + "/commit"
31}
32
33// The schemas of the receiver's replies.
34const (
35 AuthorizedSchema = "valley-web-authorized/v1"
36 InventorySchema = "valley-web-inventory/v2"
37 CommittedSchema = "valley-web-committed/v2"
38 ErrorSchema = "valley-web-error/v1"
39)
40
41// AuthorizationReply is the reply to an authorization the receiver admitted and
42// stored durably: its sequence, which every request a server answers from
43// then on is held to. It also says what content the receiver serves, so a
44// publisher whose export is unchanged knows whether to upload it.
45type AuthorizationReply struct {
46 Schema string `json:"schema"`
47 Sequence int64 `json:"sequence"`
48 // Active is the export the receiver serves, or nil for none.
49 Active *Active `json:"active"`
50 // Missing are the projects the active export holds and the stored
51 // authorization names, whose repositories the receiver does not
52 // hold: it deleted them when an earlier authorization withdrew the
53 // project. A content upload sends their bundles again.
54 Missing []string `json:"missing"`
55}
56
57// Serves reports whether the reply says the receiver serves the export
58// with digest whole: it is the active export, and no repository of it
59// that the authorization names is missing.
60func (r *AuthorizationReply) Serves(digest string) bool {
61 return r != nil && r.Active != nil && r.Active.Digest == digest && len(r.Missing) == 0
62}
63
64// Inventory is the reply to a manifest the receiver admitted: what it
65// already holds, and the bodies it still needs before the upload can be
66// committed.
67type Inventory struct {
68 Schema string `json:"schema"`
69 Sequence int64 `json:"sequence"`
70 // Active is the export the receiver serves, or nil for none.
71 Active *Active `json:"active"`
72 // Holds are, per project, the source tip the receiver holds a verified
73 // repository at.
74 Holds []Held `json:"holds"`
75 // Needs are the bodies the publisher must PUT, export.json first. A
76 // bundle is not needed when the receiver holds its project at the
77 // declared source tip, and no body is needed twice.
78 Needs []Need `json:"needs"`
79}
80
81// Active is the export a receiver serves: its sequence and its content
82// digest. A publisher whose export has this digest, and for which the
83// receiver needs no bundle, need not commit.
84type Active struct {
85 Sequence int64 `json:"sequence"`
86 Digest string `json:"digest"`
87}
88
89// Held is a project the receiver holds a verified repository of, at its
90// source tip.
91type Held struct {
92 Project string `json:"project"`
93 SourceTip string `json:"source_tip"`
94}
95
96// Need is one body the receiver needs, as the manifest declares it.
97type Need struct {
98 Name string `json:"name"`
99 SHA256 string `json:"sha256"`
100 Size int64 `json:"size"`
101}
102
103// Committed is the reply to a commit: the sequence now active.
104type Committed struct {
105 Schema string `json:"schema"`
106 Sequence int64 `json:"sequence"`
107}
108
109// ErrorReply is the reply to a request the receiver refused.
110type ErrorReply struct {
111 Schema string `json:"schema"`
112 Code Code `json:"error"`
113 Message string `json:"message"`
114 // HighWater is the greatest sequence accepted, given with replayed
115 // and stale-basis.
116 HighWater int64 `json:"high_water,omitempty"`
117 // Needs are the bodies still needed, given with incomplete.
118 Needs []Need `json:"needs,omitempty"`
119}
120
121// Code is a refusal's stable name. A publisher acts on the code; the
122// message is for people.
123type Code string
124
125const (
126 // CodeUnauthenticated: a request that does not carry the receiver's
127 // token.
128 CodeUnauthenticated Code = "unauthenticated"
129 // CodeTooLarge: a manifest over MaxManifest bytes, a body declared
130 // larger than the receiver's cap, or bodies whose declared sizes
131 // together pass the receiver's cap on one upload.
132 CodeTooLarge Code = "too-large"
133 // CodeEncoding: a body sent with a Content-Encoding.
134 CodeEncoding Code = "content-encoding"
135 // CodeUnreadable: a request body that could not be read.
136 CodeUnreadable Code = "unreadable"
137 // CodeBadSignature: a manifest that does not open under the pinned key.
138 CodeBadSignature Code = "bad-signature"
139 // CodeBadManifest: a manifest or authorization whose text is not a
140 // valid one, or that is the other kind of document.
141 CodeBadManifest Code = "bad-manifest"
142 // CodeWrongInstance and CodeWrongDestination: a manifest signed for
143 // another instance, or for another receiver.
144 CodeWrongInstance Code = "wrong-instance"
145 CodeWrongDestination Code = "wrong-destination"
146 // CodeReplayed: a manifest whose sequence is not greater than the
147 // greatest accepted. It is a replay of an accepted manifest, or older
148 // than one. The publisher waits for its clock to pass HighWater. For
149 // an authorization, its sequence is not greater than the stored one's,
150 // and the publisher signs the current policy again above HighWater.
151 CodeReplayed Code = "replayed"
152 // CodeOutOfWindow: a manifest signed more than MaxAge ago, or more than
153 // MaxAhead ahead of the receiver's clock.
154 CodeOutOfWindow Code = "out-of-window"
155 // CodeSuperseded: a manifest older than the one staged, or a body or
156 // commit for an upload a newer manifest replaced.
157 CodeSuperseded Code = "superseded"
158 // CodeNoManifest: a body or commit for a sequence nothing is staged
159 // for.
160 CodeNoManifest Code = "no-manifest"
161 // CodeNotDeclared: a body whose hash the staged manifest does not
162 // declare.
163 CodeNotDeclared Code = "not-declared"
164 // CodeSizeMismatch and CodeHashMismatch: a body whose size or SHA-256
165 // is not what the manifest declares.
166 CodeSizeMismatch Code = "size-mismatch"
167 CodeHashMismatch Code = "hash-mismatch"
168 // CodeBadExport: an export.json that is not a valid export, or that
169 // disagrees with the manifest.
170 CodeBadExport Code = "bad-export"
171 // CodeBadBundle: a bundle the acceptance procedure refuses.
172 CodeBadBundle Code = "bad-bundle"
173 // CodeIncomplete: a commit before every needed body has verified.
174 CodeIncomplete Code = "incomplete"
175 // CodeStaleBasis: an authorization whose basis is not the stored
176 // authorization's sequence. It keeps access the publisher could not
177 // check again, from a record of the receiver's state that is out of
178 // date. HighWater is the stored sequence, and the publisher checks the
179 // policy again and keeps nothing it could not check.
180 CodeStaleBasis Code = "stale-basis"
181 // CodeUnsupportedVersion: a request under LegacyPrefix, from a
182 // publisher older than the receiver's protocol.
183 CodeUnsupportedVersion Code = "unsupported-version"
184)
185
186// Status is the HTTP status a refusal is replied with.
187func (c Code) Status() int {
188 switch c {
189 case CodeUnauthenticated:
190 return http.StatusUnauthorized
191 case CodeTooLarge:
192 return http.StatusRequestEntityTooLarge
193 case CodeEncoding:
194 return http.StatusUnsupportedMediaType
195 case CodeBadSignature, CodeWrongInstance, CodeWrongDestination, CodeOutOfWindow:
196 return http.StatusForbidden
197 case CodeReplayed, CodeStaleBasis, CodeSuperseded, CodeIncomplete:
198 return http.StatusConflict
199 case CodeNoManifest, CodeNotDeclared:
200 return http.StatusNotFound
201 case CodeHashMismatch, CodeBadExport, CodeBadBundle:
202 return http.StatusUnprocessableEntity
203 case CodeUnsupportedVersion:
204 return http.StatusGone
205 }
206 return http.StatusBadRequest
207}
208
209// Error is a refusal: its code, and what was wrong.
210type Error struct {
211 Code Code
212 Message string
213 // HighWater is set with CodeReplayed and CodeStaleBasis.
214 HighWater int64
215 // Needs is set with CodeIncomplete.
216 Needs []Need
217}
218
219func (e *Error) Error() string { return string(e.Code) + ": " + e.Message }
220
221// Reply is the refusal as the receiver replies with it.
222func (e *Error) Reply() ErrorReply {
223 return ErrorReply{Schema: ErrorSchema, Code: e.Code, Message: e.Message, HighWater: e.HighWater, Needs: e.Needs}
224}
225
226// CodeOf is the code of the refusal err is or wraps, or "" for none.
227func CodeOf(err error) Code {
228 var e *Error
229 if errors.As(err, &e) {
230 return e.Code
231 }
232 return ""
233}
234
235func refuse(code Code, format string, args ...any) *Error {
236 return &Error{Code: code, Message: fmt.Sprintf(format, args...)}
237}