| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "the-valley/approval" |
| 8 | "the-valley/change" |
| 9 | "the-valley/knot" |
| 10 | "the-valley/note" |
| 11 | "the-valley/pushpolicy" |
| 12 | ) |
| 13 | |
| 14 | // pushPolicy is what a project declares about pushes to it, as the host |
| 15 | // declaration exports it (pushpolicy.Policy): the protected refs and their |
| 16 | // writers from its protection block, its named ref grants, and whether it |
| 17 | // retains its change records. A project that declares no protection block |
| 18 | // protects nothing, and every other rule applies to it unchanged. |
| 19 | type pushPolicy = pushpolicy.Policy |
| 20 | |
| 21 | // refGrant is one named grant: ref patterns, and who may write them. |
| 22 | type refGrant = pushpolicy.Grant |
| 23 | |
| 24 | // The namespaces whose rule is fixed rather than declared. |
| 25 | const ( |
| 26 | replacePrefix = "refs/replace/" |
| 27 | notesPrefix = "refs/notes/" |
| 28 | valleyPrefix = "refs/the-valley/" |
| 29 | attestationPrefix = "refs/the-valley/attestations/" |
| 30 | requestPrefix = "refs/the-valley/integration-requests/" |
| 31 | flowDataRef = "refs/the-valley/flow/data" |
| 32 | branchPrefix = "refs/heads/" |
| 33 | |
| 34 | // grantApprove is the grant that opens the integration-request |
| 35 | // namespace (dcr-e544f20). It is also accepted under grantRequest, the |
| 36 | // name it had first: a principal holding either holds it (grants.go). |
| 37 | grantApprove = "approve" |
| 38 | grantRequest = "request" |
| 39 | |
| 40 | // approveGrant is how a refusal names the approve grant, with the |
| 41 | // other name it is accepted under. |
| 42 | approveGrant = grantApprove + " grant (also accepted as " + grantRequest + ")" |
| 43 | |
| 44 | // grantRestack is the host restacker's grant: it adds one version to |
| 45 | // another principal's ready change (record.go), and it opens the |
| 46 | // restack report namespace (report.go). It opens nothing else. |
| 47 | grantRestack = "restack" |
| 48 | ) |
| 49 | |
| 50 | // lookupPrefixes are what git puts before a name it looks up, once the |
| 51 | // name as given is not a ref (git's ref_rev_parse_rules). So a ref the |
| 52 | // valley reads by its full name, when it is missing, is read, fetched and |
| 53 | // pushed to as one of these prefixes followed by that name, or under |
| 54 | // refs/remotes/ with /HEAD after it. |
| 55 | var lookupPrefixes = []string{"refs/", "refs/tags/", "refs/heads/", "refs/remotes/"} |
| 56 | |
| 57 | // lookalike returns the prefix when ref is named like another ref: one of |
| 58 | // lookupPrefixes followed by a full ref name. Every name git tries in |
| 59 | // place of a missing full ref name has this shape, so the test needs no |
| 60 | // list of the refs the valley reads. |
| 61 | func lookalike(ref string) (string, bool) { |
| 62 | for _, prefix := range lookupPrefixes { |
| 63 | if strings.HasPrefix(ref, prefix+"refs/") { |
| 64 | return prefix, true |
| 65 | } |
| 66 | } |
| 67 | return "", false |
| 68 | } |
| 69 | |
| 70 | // policy is everything one push is judged against. |
| 71 | type policy struct { |
| 72 | project string |
| 73 | push pushPolicy |
| 74 | grants grants |
| 75 | |
| 76 | // verifiers are the keys the host accepts evidence from. An |
| 77 | // attestation is accepted only under one of them. |
| 78 | verifiers []note.VerifierKey |
| 79 | |
| 80 | // repo answers what the decision needs to know about the repository: |
| 81 | // what a pushed object holds, and how many change records it has. |
| 82 | repo repository |
| 83 | |
| 84 | // symrefs are the refs the push writes that are symbolic in the |
| 85 | // repository. preReceive reads them all at once, before any update is |
| 86 | // decided. |
| 87 | symrefs map[string]bool |
| 88 | |
| 89 | // recordCreates is how many change records the push creates, and |
| 90 | // existingRecords how many the repository held before it. preReceive |
| 91 | // counts both before any update is decided, and only when the push |
| 92 | // creates a record. |
| 93 | recordCreates, existingRecords int |
| 94 | |
| 95 | // pushed is the value every ref the push writes takes, a delete's |
| 96 | // zero ID included. A decision about one ref reads it for another ref |
| 97 | // the same push writes (besideRequest). |
| 98 | pushed map[string]string |
| 99 | |
| 100 | // requestCreates is how many check requests the push creates under |
| 101 | // the pushing principal's name, and existingRequests how many that |
| 102 | // principal held before it. preReceive counts both the same way. |
| 103 | requestCreates, existingRequests int |
| 104 | } |
| 105 | |
| 106 | // update is one line of what git hands a pre-receive hook. |
| 107 | type update struct { |
| 108 | old, new, ref string |
| 109 | } |
| 110 | |
| 111 | // decide returns "" when principal may make the write, and otherwise the |
| 112 | // sentence that refuses it. An empty principal is a key that names none: |
| 113 | // it is nobody's writer and holds no grant, so it writes only what anyone |
| 114 | // may. |
| 115 | // |
| 116 | // A write is allowed only if every rule that applies to the ref allows it, |
| 117 | // checked in this order: |
| 118 | // |
| 119 | // 1. Some refs no push may write, whatever anything declares: replacement |
| 120 | // refs, notes, and the valley's own namespace apart from attestations, |
| 121 | // integration requests, approvals, change records, restack reports, |
| 122 | // check requests, the plan graph, and refs/the-valley/flow/data. The |
| 123 | // plan graph (refs/the-valley/knots) and refs/the-valley/flow/data are |
| 124 | // each one ref, matched exactly: a ref that only begins with either |
| 125 | // name is in the closed namespace. Nor may a push create or update a |
| 126 | // ref named like another (lookalike), since git would read it in place |
| 127 | // of that ref when that ref is missing. A push may still delete one, by |
| 128 | // the rules below. |
| 129 | // 2. A ref that is symbolic in the repository takes no push. git would |
| 130 | // write the ref it points at, and that ref is not the one decided. |
| 131 | // 3. The ref's namespace has its own rule. Topic branches are open. |
| 132 | // Attestations must be what their name says, and an existing one |
| 133 | // only takes added notes. |
| 134 | // Integration requests take the approve grant. An approval takes the |
| 135 | // approve grant, and holds an approval note about the change its name |
| 136 | // says (checkApproval). A change record moves only by the transitions |
| 137 | // checkRecord allows, and is never deleted from a project that retains |
| 138 | // its records. A restack report takes |
| 139 | // the restack grant, and moves only by the writes checkReport allows. |
| 140 | // A check request takes a named principal under its own name, or a |
| 141 | // holder of the approve grant deleting it, and points at a commit |
| 142 | // (checkCheckRequest). |
| 143 | // The plan graph takes a named principal, and moves only by the writes |
| 144 | // checkGraph allows. |
| 145 | // Every other ref, tags and refs/the-valley/flow/data included, takes a |
| 146 | // named grant that opens it to the principal. |
| 147 | // 4. A protected ref also takes a declared writer. |
| 148 | // |
| 149 | // Protection only ever adds a requirement. A writer of a pattern that |
| 150 | // covers integration requests still needs the approve grant, and a writer |
| 151 | // of a tag pattern still needs a grant that opens the tag. |
| 152 | func (p policy) decide(principal string, u update) string { |
| 153 | who := principal |
| 154 | if who == "" { |
| 155 | who = "<untagged key>" |
| 156 | } |
| 157 | refuse := func(verb, why string) string { |
| 158 | return fmt.Sprintf("valley: %s may not %s %s — %s", who, verb, u.ref, why) |
| 159 | } |
| 160 | |
| 161 | switch { |
| 162 | case strings.HasPrefix(u.ref, replacePrefix): |
| 163 | return refuse("write", "refs/replace/ is closed to every push: a replacement ref makes one object stand in for another wherever git looks it up, so it would change what every reader of the repository sees") |
| 164 | case strings.HasPrefix(u.ref, notesPrefix): |
| 165 | return refuse("write", "refs/notes/ is closed to every push: a note attaches text to an object that every reader's git shows beside it") |
| 166 | case strings.HasPrefix(u.ref, valleyPrefix) && !strings.HasPrefix(u.ref, attestationPrefix) && |
| 167 | !strings.HasPrefix(u.ref, requestPrefix) && !strings.HasPrefix(u.ref, approval.RefPrefix) && !strings.HasPrefix(u.ref, change.RefPrefix) && |
| 168 | !strings.HasPrefix(u.ref, change.RestackPrefix) && !strings.HasPrefix(u.ref, change.CheckRequestPrefix) && u.ref != knot.Ref && u.ref != flowDataRef: |
| 169 | return refuse("write", fmt.Sprintf("%s is written only by the machinery on the host, and no push may write it", namespace(u.ref))) |
| 170 | } |
| 171 | if prefix, ok := lookalike(u.ref); ok && !isZero(u.new) { |
| 172 | verb := "update" |
| 173 | if isZero(u.old) { |
| 174 | verb = "create" |
| 175 | } |
| 176 | return refuse(verb, fmt.Sprintf("no push may create or update a ref named %s followed by a full ref name: when %s is missing, git reads, fetches and pushes to this ref in its place", prefix, strings.TrimPrefix(u.ref, prefix))) |
| 177 | } |
| 178 | |
| 179 | if p.symrefs[u.ref] { |
| 180 | return refuse("write", "it is a symbolic ref in this repository, and a push to it would write the ref it points at instead") |
| 181 | } |
| 182 | |
| 183 | switch { |
| 184 | case strings.HasPrefix(u.ref, branchPrefix): |
| 185 | // Topic branches are open. |
| 186 | |
| 187 | case strings.HasPrefix(u.ref, attestationPrefix): |
| 188 | switch { |
| 189 | case isZero(u.new): |
| 190 | return refuse("delete", "published evidence is never removed, so no push deletes an attestation ref") |
| 191 | case isZero(u.old): |
| 192 | if err := p.checkAttestation(u.ref, u.new); err != nil { |
| 193 | return refuse("create", err.Error()) |
| 194 | } |
| 195 | default: |
| 196 | if err := p.checkAttestationUpdate(u.ref, u.old, u.new); err != nil { |
| 197 | return refuse("update", err.Error()) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | case strings.HasPrefix(u.ref, requestPrefix): |
| 202 | if principal == "" || !p.grants.holds(principal, grantApprove) { |
| 203 | holder := who + " holds none" |
| 204 | if principal == "" { |
| 205 | holder = "a key that names no principal holds no grant" |
| 206 | } |
| 207 | return refuse("write", fmt.Sprintf("%s takes writes only from a principal holding the %s, and %s", requestPrefix, approveGrant, holder)) |
| 208 | } |
| 209 | |
| 210 | case strings.HasPrefix(u.ref, approval.RefPrefix): |
| 211 | if verb, why := p.checkApproval(principal, u); why != "" { |
| 212 | return refuse(verb, why) |
| 213 | } |
| 214 | |
| 215 | case strings.HasPrefix(u.ref, change.RefPrefix): |
| 216 | if verb, why := p.checkRecord(principal, u); why != "" { |
| 217 | return refuse(verb, why) |
| 218 | } |
| 219 | |
| 220 | case strings.HasPrefix(u.ref, change.RestackPrefix): |
| 221 | if verb, why := p.checkReport(principal, u); why != "" { |
| 222 | return refuse(verb, why) |
| 223 | } |
| 224 | |
| 225 | case strings.HasPrefix(u.ref, change.CheckRequestPrefix): |
| 226 | if verb, why := p.checkCheckRequest(principal, u); why != "" { |
| 227 | return refuse(verb, why) |
| 228 | } |
| 229 | |
| 230 | case u.ref == knot.Ref: |
| 231 | if verb, why := p.checkGraph(principal, u); why != "" { |
| 232 | return refuse(verb, why) |
| 233 | } |
| 234 | |
| 235 | default: |
| 236 | names, opened := openedTo(p.push, u.ref) |
| 237 | if principal == "" || !contains(opened, principal) { |
| 238 | closed := namespace(u.ref) |
| 239 | if len(opened) > 0 { |
| 240 | return refuse("write", fmt.Sprintf("%s opens only through a named grant, and %s of %s opens this ref to %s only", closed, grantList(names), p.project, strings.Join(opened, ", "))) |
| 241 | } |
| 242 | return refuse("write", fmt.Sprintf("%s opens only through a named grant, and no grant of %s opens this ref", closed, p.project)) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if p.push.Protects(u.ref) && (principal == "" || !contains(p.push.Writers, principal)) { |
| 247 | return refuse("write", fmt.Sprintf("a protected ref of %s (%s)", p.project, whoMay(p.push))) |
| 248 | } |
| 249 | return "" |
| 250 | } |
| 251 | |
| 252 | // whoMay is what a refusal says about who may write a protected ref |
| 253 | // instead. With no writer declared there is no one, so it says how a |
| 254 | // change lands. |
| 255 | func whoMay(p pushPolicy) string { |
| 256 | if len(p.Writers) == 0 { |
| 257 | return "no writer is declared — changes land by integration request" |
| 258 | } |
| 259 | return "writers: " + strings.Join(p.Writers, ", ") |
| 260 | } |
| 261 | |
| 262 | // openedTo lists the grants that open ref and the principals they open it |
| 263 | // to, both in name order so a refusal reads the same on every push. |
| 264 | func openedTo(p pushPolicy, ref string) (names, who []string) { |
| 265 | for _, name := range sortedKeys(p.Grants) { |
| 266 | g := p.Grants[name] |
| 267 | for _, pattern := range g.Refs { |
| 268 | if pushpolicy.Match(pattern, ref) { |
| 269 | names = append(names, name) |
| 270 | for _, w := range g.Writers { |
| 271 | if !contains(who, w) { |
| 272 | who = append(who, w) |
| 273 | } |
| 274 | } |
| 275 | break |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return names, who |
| 280 | } |
| 281 | |
| 282 | func grantList(names []string) string { |
| 283 | if len(names) == 1 { |
| 284 | return "the grant " + names[0] |
| 285 | } |
| 286 | return "the grants " + strings.Join(names, ", ") |
| 287 | } |
| 288 | |
| 289 | // namespace is the part of a refname a refusal names: refs/<kind>/, or |
| 290 | // refs/the-valley/<kind>/ for the valley's own refs. |
| 291 | func namespace(ref string) string { |
| 292 | parts := strings.Split(ref, "/") |
| 293 | n := 2 |
| 294 | if len(parts) > 1 && parts[1] == "the-valley" { |
| 295 | n = 3 |
| 296 | } |
| 297 | if len(parts) <= n { |
| 298 | return ref |
| 299 | } |
| 300 | return strings.Join(parts[:n], "/") + "/" |
| 301 | } |
| 302 | |
| 303 | func isZero(id string) bool { |
| 304 | return id != "" && strings.Trim(id, "0") == "" |
| 305 | } |
| 306 | |
| 307 | func contains(list []string, s string) bool { |
| 308 | for _, x := range list { |
| 309 | if x == s { |
| 310 | return true |
| 311 | } |
| 312 | } |
| 313 | return false |
| 314 | } |