gunk-dev/the-valley

protocol.go

sourceed42f27353e2main's published tip history
8.3 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 HighWater int64 `json:"high_water,omitempty"`
116 // Needs are the bodies still needed, given with incomplete.
117 Needs []Need `json:"needs,omitempty"`
118}
119
120// Code is a refusal's stable name. A publisher acts on the code; the
121// message is for people.
122type Code string
123
124const (
125 // CodeUnauthenticated: a request that does not carry the receiver's
126 // token.
127 CodeUnauthenticated Code = "unauthenticated"
128 // CodeTooLarge: a manifest over MaxManifest bytes, a body declared
129 // larger than the receiver's cap, or bodies whose declared sizes
130 // together pass the receiver's cap on one upload.
131 CodeTooLarge Code = "too-large"
132 // CodeEncoding: a body sent with a Content-Encoding.
133 CodeEncoding Code = "content-encoding"
134 // CodeUnreadable: a request body that could not be read.
135 CodeUnreadable Code = "unreadable"
136 // CodeBadSignature: a manifest that does not open under the pinned key.
137 CodeBadSignature Code = "bad-signature"
138 // CodeBadManifest: a manifest or authorization whose text is not a
139 // valid one, or that is the other kind of document.
140 CodeBadManifest Code = "bad-manifest"
141 // CodeWrongInstance and CodeWrongDestination: a manifest signed for
142 // another instance, or for another receiver.
143 CodeWrongInstance Code = "wrong-instance"
144 CodeWrongDestination Code = "wrong-destination"
145 // CodeReplayed: a manifest whose sequence is not greater than the
146 // greatest accepted. It is a replay of an accepted manifest, or older
147 // than one. The publisher waits for its clock to pass HighWater. For
148 // an authorization, its sequence is not greater than the stored one's,
149 // and the publisher signs the current policy again above HighWater.
150 CodeReplayed Code = "replayed"
151 // CodeOutOfWindow: a manifest signed more than MaxAge ago, or more than
152 // MaxAhead ahead of the receiver's clock.
153 CodeOutOfWindow Code = "out-of-window"
154 // CodeSuperseded: a manifest older than the one staged, or a body or
155 // commit for an upload a newer manifest replaced.
156 CodeSuperseded Code = "superseded"
157 // CodeNoManifest: a body or commit for a sequence nothing is staged
158 // for.
159 CodeNoManifest Code = "no-manifest"
160 // CodeNotDeclared: a body whose hash the staged manifest does not
161 // declare.
162 CodeNotDeclared Code = "not-declared"
163 // CodeSizeMismatch and CodeHashMismatch: a body whose size or SHA-256
164 // is not what the manifest declares.
165 CodeSizeMismatch Code = "size-mismatch"
166 CodeHashMismatch Code = "hash-mismatch"
167 // CodeBadExport: an export.json that is not a valid export, or that
168 // disagrees with the manifest.
169 CodeBadExport Code = "bad-export"
170 // CodeBadBundle: a bundle the acceptance procedure refuses.
171 CodeBadBundle Code = "bad-bundle"
172 // CodeIncomplete: a commit before every needed body has verified.
173 CodeIncomplete Code = "incomplete"
174 // CodeUnsupportedVersion: a request under LegacyPrefix, from a
175 // publisher older than the receiver's protocol.
176 CodeUnsupportedVersion Code = "unsupported-version"
177)
178
179// Status is the HTTP status a refusal is replied with.
180func (c Code) Status() int {
181 switch c {
182 case CodeUnauthenticated:
183 return http.StatusUnauthorized
184 case CodeTooLarge:
185 return http.StatusRequestEntityTooLarge
186 case CodeEncoding:
187 return http.StatusUnsupportedMediaType
188 case CodeBadSignature, CodeWrongInstance, CodeWrongDestination, CodeOutOfWindow:
189 return http.StatusForbidden
190 case CodeReplayed, CodeSuperseded, CodeIncomplete:
191 return http.StatusConflict
192 case CodeNoManifest, CodeNotDeclared:
193 return http.StatusNotFound
194 case CodeHashMismatch, CodeBadExport, CodeBadBundle:
195 return http.StatusUnprocessableEntity
196 case CodeUnsupportedVersion:
197 return http.StatusGone
198 }
199 return http.StatusBadRequest
200}
201
202// Error is a refusal: its code, and what was wrong.
203type Error struct {
204 Code Code
205 Message string
206 // HighWater is set with CodeReplayed.
207 HighWater int64
208 // Needs is set with CodeIncomplete.
209 Needs []Need
210}
211
212func (e *Error) Error() string { return string(e.Code) + ": " + e.Message }
213
214// Reply is the refusal as the receiver replies with it.
215func (e *Error) Reply() ErrorReply {
216 return ErrorReply{Schema: ErrorSchema, Code: e.Code, Message: e.Message, HighWater: e.HighWater, Needs: e.Needs}
217}
218
219// CodeOf is the code of the refusal err is or wraps, or "" for none.
220func CodeOf(err error) Code {
221 var e *Error
222 if errors.As(err, &e) {
223 return e.Code
224 }
225 return ""
226}
227
228func refuse(code Code, format string, args ...any) *Error {
229 return &Error{Code: code, Message: fmt.Sprintf(format, args...)}
230}