| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | ) |
| 9 | |
| 10 | // The upload's paths, under the receiver's base URL. |
| 11 | const ( |
| 12 | // ManifestPath takes step 1's POST of a signed manifest. |
| 13 | ManifestPath = "/web-export/v1/manifest" |
| 14 | ) |
| 15 | |
| 16 | // BodyPath takes step 2's PUT of one body of the upload with sequence. |
| 17 | func BodyPath(sequence int64, sha256 string) string { |
| 18 | return "/web-export/v1/" + strconv.FormatInt(sequence, 10) + "/" + sha256 |
| 19 | } |
| 20 | |
| 21 | // CommitPath takes step 3's POST that commits the upload with sequence. |
| 22 | func CommitPath(sequence int64) string { |
| 23 | return "/web-export/v1/" + strconv.FormatInt(sequence, 10) + "/commit" |
| 24 | } |
| 25 | |
| 26 | // The schemas of the receiver's replies. |
| 27 | const ( |
| 28 | InventorySchema = "valley-web-inventory/v1" |
| 29 | CommittedSchema = "valley-web-committed/v1" |
| 30 | ErrorSchema = "valley-web-error/v1" |
| 31 | ) |
| 32 | |
| 33 | // Inventory is the reply to a manifest the receiver admitted: what it |
| 34 | // already holds, and the bodies it still needs before the upload can be |
| 35 | // committed. |
| 36 | type Inventory struct { |
| 37 | Schema string `json:"schema"` |
| 38 | Sequence int64 `json:"sequence"` |
| 39 | // Active is the export the receiver serves, or nil for none. |
| 40 | Active *Active `json:"active"` |
| 41 | // Holds are, per project, the source tip the receiver holds a verified |
| 42 | // repository at. |
| 43 | Holds []Held `json:"holds"` |
| 44 | // Needs are the bodies the publisher must PUT, export.json first. A |
| 45 | // bundle is not needed when the receiver holds its project at the |
| 46 | // declared source tip, and no body is needed twice. |
| 47 | Needs []Need `json:"needs"` |
| 48 | } |
| 49 | |
| 50 | // Active is the export a receiver serves: its sequence and its content |
| 51 | // digest. A publisher whose export has this digest, and for which the |
| 52 | // receiver needs no bundle, need not commit. |
| 53 | type Active struct { |
| 54 | Sequence int64 `json:"sequence"` |
| 55 | Digest string `json:"digest"` |
| 56 | } |
| 57 | |
| 58 | // Held is a project the receiver holds a verified repository of, at its |
| 59 | // source tip. |
| 60 | type Held struct { |
| 61 | Project string `json:"project"` |
| 62 | SourceTip string `json:"source_tip"` |
| 63 | } |
| 64 | |
| 65 | // Need is one body the receiver needs, as the manifest declares it. |
| 66 | type Need struct { |
| 67 | Name string `json:"name"` |
| 68 | SHA256 string `json:"sha256"` |
| 69 | Size int64 `json:"size"` |
| 70 | } |
| 71 | |
| 72 | // Committed is the reply to a commit: the sequence now active. |
| 73 | type Committed struct { |
| 74 | Schema string `json:"schema"` |
| 75 | Sequence int64 `json:"sequence"` |
| 76 | } |
| 77 | |
| 78 | // ErrorReply is the reply to a request the receiver refused. |
| 79 | type ErrorReply struct { |
| 80 | Schema string `json:"schema"` |
| 81 | Code Code `json:"error"` |
| 82 | Message string `json:"message"` |
| 83 | // HighWater is the greatest sequence accepted, given with replayed. |
| 84 | HighWater int64 `json:"high_water,omitempty"` |
| 85 | // Needs are the bodies still needed, given with incomplete. |
| 86 | Needs []Need `json:"needs,omitempty"` |
| 87 | } |
| 88 | |
| 89 | // Code is a refusal's stable name. A publisher acts on the code; the |
| 90 | // message is for people. |
| 91 | type Code string |
| 92 | |
| 93 | const ( |
| 94 | // CodeUnauthenticated: a request that does not carry the receiver's |
| 95 | // token. |
| 96 | CodeUnauthenticated Code = "unauthenticated" |
| 97 | // CodeTooLarge: a manifest over MaxManifest bytes, a body declared |
| 98 | // larger than the receiver's cap, or bodies whose declared sizes |
| 99 | // together pass the receiver's cap on one upload. |
| 100 | CodeTooLarge Code = "too-large" |
| 101 | // CodeEncoding: a body sent with a Content-Encoding. |
| 102 | CodeEncoding Code = "content-encoding" |
| 103 | // CodeUnreadable: a request body that could not be read. |
| 104 | CodeUnreadable Code = "unreadable" |
| 105 | // CodeBadSignature: a manifest that does not open under the pinned key. |
| 106 | CodeBadSignature Code = "bad-signature" |
| 107 | // CodeBadManifest: a manifest whose text is not a valid manifest. |
| 108 | CodeBadManifest Code = "bad-manifest" |
| 109 | // CodeWrongInstance and CodeWrongDestination: a manifest signed for |
| 110 | // another instance, or for another receiver. |
| 111 | CodeWrongInstance Code = "wrong-instance" |
| 112 | CodeWrongDestination Code = "wrong-destination" |
| 113 | // CodeReplayed: a manifest whose sequence is not greater than the |
| 114 | // greatest accepted. It is a replay of an accepted manifest, or older |
| 115 | // than one. The publisher waits for its clock to pass HighWater. |
| 116 | CodeReplayed Code = "replayed" |
| 117 | // CodeOutOfWindow: a manifest signed more than MaxAge ago, or more than |
| 118 | // MaxAhead ahead of the receiver's clock. |
| 119 | CodeOutOfWindow Code = "out-of-window" |
| 120 | // CodeSuperseded: a manifest older than the one staged, or a body or |
| 121 | // commit for an upload a newer manifest replaced. |
| 122 | CodeSuperseded Code = "superseded" |
| 123 | // CodeNoManifest: a body or commit for a sequence nothing is staged |
| 124 | // for. |
| 125 | CodeNoManifest Code = "no-manifest" |
| 126 | // CodeNotDeclared: a body whose hash the staged manifest does not |
| 127 | // declare. |
| 128 | CodeNotDeclared Code = "not-declared" |
| 129 | // CodeSizeMismatch and CodeHashMismatch: a body whose size or SHA-256 |
| 130 | // is not what the manifest declares. |
| 131 | CodeSizeMismatch Code = "size-mismatch" |
| 132 | CodeHashMismatch Code = "hash-mismatch" |
| 133 | // CodeBadExport: an export.json that is not a valid export, or that |
| 134 | // disagrees with the manifest. |
| 135 | CodeBadExport Code = "bad-export" |
| 136 | // CodeBadBundle: a bundle the acceptance procedure refuses. |
| 137 | CodeBadBundle Code = "bad-bundle" |
| 138 | // CodeIncomplete: a commit before every needed body has verified. |
| 139 | CodeIncomplete Code = "incomplete" |
| 140 | ) |
| 141 | |
| 142 | // Status is the HTTP status a refusal is replied with. |
| 143 | func (c Code) Status() int { |
| 144 | switch c { |
| 145 | case CodeUnauthenticated: |
| 146 | return http.StatusUnauthorized |
| 147 | case CodeTooLarge: |
| 148 | return http.StatusRequestEntityTooLarge |
| 149 | case CodeEncoding: |
| 150 | return http.StatusUnsupportedMediaType |
| 151 | case CodeBadSignature, CodeWrongInstance, CodeWrongDestination, CodeOutOfWindow: |
| 152 | return http.StatusForbidden |
| 153 | case CodeReplayed, CodeSuperseded, CodeIncomplete: |
| 154 | return http.StatusConflict |
| 155 | case CodeNoManifest, CodeNotDeclared: |
| 156 | return http.StatusNotFound |
| 157 | case CodeHashMismatch, CodeBadExport, CodeBadBundle: |
| 158 | return http.StatusUnprocessableEntity |
| 159 | } |
| 160 | return http.StatusBadRequest |
| 161 | } |
| 162 | |
| 163 | // Error is a refusal: its code, and what was wrong. |
| 164 | type Error struct { |
| 165 | Code Code |
| 166 | Message string |
| 167 | // HighWater is set with CodeReplayed. |
| 168 | HighWater int64 |
| 169 | // Needs is set with CodeIncomplete. |
| 170 | Needs []Need |
| 171 | } |
| 172 | |
| 173 | func (e *Error) Error() string { return string(e.Code) + ": " + e.Message } |
| 174 | |
| 175 | // Reply is the refusal as the receiver replies with it. |
| 176 | func (e *Error) Reply() ErrorReply { |
| 177 | return ErrorReply{Schema: ErrorSchema, Code: e.Code, Message: e.Message, HighWater: e.HighWater, Needs: e.Needs} |
| 178 | } |
| 179 | |
| 180 | // CodeOf is the code of the refusal err is or wraps, or "" for none. |
| 181 | func CodeOf(err error) Code { |
| 182 | var e *Error |
| 183 | if errors.As(err, &e) { |
| 184 | return e.Code |
| 185 | } |
| 186 | return "" |
| 187 | } |
| 188 | |
| 189 | func refuse(code Code, format string, args ...any) *Error { |
| 190 | return &Error{Code: code, Message: fmt.Sprintf(format, args...)} |
| 191 | } |