| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "the-valley/note" |
| 8 | ) |
| 9 | |
| 10 | // pushPolicy is what a project declares about pushes to it, as the host |
| 11 | // declaration exports it (schema/valley.cue): the protected refs and their |
| 12 | // writers from its protection block, and its named ref grants. A project |
| 13 | // that declares no protection block protects nothing, and every other rule |
| 14 | // applies to it unchanged. |
| 15 | type pushPolicy struct { |
| 16 | // Refs are the protected ref patterns. A ref one of them matches takes |
| 17 | // a push only from one of Writers, on top of whatever else its |
| 18 | // namespace requires. |
| 19 | Refs []string `json:"refs"` |
| 20 | Writers []string `json:"writers"` |
| 21 | |
| 22 | // Grants open refs the namespace rules close, each to the principals |
| 23 | // it names. Tags open only this way. |
| 24 | Grants map[string]refGrant `json:"grants"` |
| 25 | } |
| 26 | |
| 27 | // refGrant is one named grant: ref patterns, and who may write them. |
| 28 | type refGrant struct { |
| 29 | Refs []string `json:"refs"` |
| 30 | Writers []string `json:"writers"` |
| 31 | } |
| 32 | |
| 33 | // The namespaces whose rule is fixed rather than declared. |
| 34 | const ( |
| 35 | replacePrefix = "refs/replace/" |
| 36 | notesPrefix = "refs/notes/" |
| 37 | valleyPrefix = "refs/the-valley/" |
| 38 | attestationPrefix = "refs/the-valley/attestations/" |
| 39 | requestPrefix = "refs/the-valley/integration-requests/" |
| 40 | branchPrefix = "refs/heads/" |
| 41 | |
| 42 | // grantRequest is the grant that opens the integration-request |
| 43 | // namespace (dcr-e544f20). |
| 44 | grantRequest = "request" |
| 45 | ) |
| 46 | |
| 47 | // policy is everything one push is judged against. |
| 48 | type policy struct { |
| 49 | project string |
| 50 | push pushPolicy |
| 51 | grants grants |
| 52 | |
| 53 | // verifiers are the keys the host accepts evidence from. An |
| 54 | // attestation is accepted only under one of them. |
| 55 | verifiers []note.VerifierKey |
| 56 | |
| 57 | // repo answers what the decision needs to know about the repository: |
| 58 | // whether a ref is symbolic there, and what a pushed object holds. |
| 59 | repo repository |
| 60 | } |
| 61 | |
| 62 | // update is one line of what git hands a pre-receive hook. |
| 63 | type update struct { |
| 64 | old, new, ref string |
| 65 | } |
| 66 | |
| 67 | // decide returns "" when principal may make the write, and otherwise the |
| 68 | // sentence that refuses it. An empty principal is a key that names none: |
| 69 | // it is nobody's writer and holds no grant, so it writes only what anyone |
| 70 | // may. |
| 71 | // |
| 72 | // A write is allowed only if every rule that applies to the ref allows it, |
| 73 | // checked in this order: |
| 74 | // |
| 75 | // 1. Some refs no push may write, whatever anything declares: replacement |
| 76 | // refs, notes, and the valley's own namespace apart from attestations |
| 77 | // and integration requests. |
| 78 | // 2. A ref that is symbolic in the repository takes no push. git would |
| 79 | // write the ref it points at, and that ref is not the one decided. |
| 80 | // 3. The ref's namespace has its own rule. Topic branches are open. |
| 81 | // Attestations are create-only and must be what their name says. |
| 82 | // Integration requests take the request grant. Every other ref, tags |
| 83 | // included, takes a named grant that opens it to the principal. |
| 84 | // 4. A protected ref also takes a declared writer. |
| 85 | // |
| 86 | // Protection only ever adds a requirement. A writer of a pattern that |
| 87 | // covers integration requests still needs the request grant, and a writer |
| 88 | // of a tag pattern still needs a grant that opens the tag. |
| 89 | func (p policy) decide(principal string, u update) string { |
| 90 | who := principal |
| 91 | if who == "" { |
| 92 | who = "<untagged key>" |
| 93 | } |
| 94 | refuse := func(verb, why string) string { |
| 95 | return fmt.Sprintf("valley: %s may not %s %s — %s", who, verb, u.ref, why) |
| 96 | } |
| 97 | |
| 98 | switch { |
| 99 | case strings.HasPrefix(u.ref, replacePrefix): |
| 100 | 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") |
| 101 | case strings.HasPrefix(u.ref, notesPrefix): |
| 102 | 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") |
| 103 | case strings.HasPrefix(u.ref, valleyPrefix) && |
| 104 | !strings.HasPrefix(u.ref, attestationPrefix) && !strings.HasPrefix(u.ref, requestPrefix): |
| 105 | return refuse("write", fmt.Sprintf("%s is written only by the machinery on the host, and no push may write it", namespace(u.ref))) |
| 106 | } |
| 107 | |
| 108 | switch symbolic, err := p.repo.symbolic(u.ref); { |
| 109 | case err != nil: |
| 110 | return refuse("write", fmt.Sprintf("whether it is a symbolic ref could not be read: %v", err)) |
| 111 | case symbolic: |
| 112 | return refuse("write", "it is a symbolic ref in this repository, and a push to it would write the ref it points at instead") |
| 113 | } |
| 114 | |
| 115 | switch { |
| 116 | case strings.HasPrefix(u.ref, branchPrefix): |
| 117 | // Topic branches are open. |
| 118 | |
| 119 | case strings.HasPrefix(u.ref, attestationPrefix): |
| 120 | if !isZero(u.old) { |
| 121 | return refuse("update", "attestation refs are create-only") |
| 122 | } |
| 123 | if err := p.checkAttestation(u.ref, u.new); err != nil { |
| 124 | return refuse("create", err.Error()) |
| 125 | } |
| 126 | |
| 127 | case strings.HasPrefix(u.ref, requestPrefix): |
| 128 | if principal == "" || !p.grants.holds(principal, grantRequest) { |
| 129 | holder := who + " holds none" |
| 130 | if principal == "" { |
| 131 | holder = "a key that names no principal holds no grant" |
| 132 | } |
| 133 | return refuse("write", fmt.Sprintf("%s takes writes only from a principal holding the %s grant, and %s", requestPrefix, grantRequest, holder)) |
| 134 | } |
| 135 | |
| 136 | default: |
| 137 | names, opened := p.push.openedTo(u.ref) |
| 138 | if principal == "" || !contains(opened, principal) { |
| 139 | closed := namespace(u.ref) |
| 140 | if len(opened) > 0 { |
| 141 | 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, ", "))) |
| 142 | } |
| 143 | return refuse("write", fmt.Sprintf("%s opens only through a named grant, and no grant of %s opens this ref", closed, p.project)) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if p.push.protects(u.ref) && (principal == "" || !contains(p.push.Writers, principal)) { |
| 148 | return refuse("write", fmt.Sprintf("a protected ref of %s (%s)", p.project, p.push.whoMay())) |
| 149 | } |
| 150 | return "" |
| 151 | } |
| 152 | |
| 153 | // protects reports whether ref is one of the project's protected refs. |
| 154 | func (p pushPolicy) protects(ref string) bool { |
| 155 | for _, pattern := range p.Refs { |
| 156 | if match(pattern, ref) { |
| 157 | return true |
| 158 | } |
| 159 | } |
| 160 | return false |
| 161 | } |
| 162 | |
| 163 | // whoMay is what a refusal says about who may write a protected ref |
| 164 | // instead. With no writer declared there is no one, so it says how a |
| 165 | // change lands. |
| 166 | func (p pushPolicy) whoMay() string { |
| 167 | if len(p.Writers) == 0 { |
| 168 | return "no writer is declared — changes land by integration request" |
| 169 | } |
| 170 | return "writers: " + strings.Join(p.Writers, ", ") |
| 171 | } |
| 172 | |
| 173 | // openedTo lists the grants that open ref and the principals they open it |
| 174 | // to, both in name order so a refusal reads the same on every push. |
| 175 | func (p pushPolicy) openedTo(ref string) (names, who []string) { |
| 176 | for _, name := range sortedKeys(p.Grants) { |
| 177 | g := p.Grants[name] |
| 178 | for _, pattern := range g.Refs { |
| 179 | if match(pattern, ref) { |
| 180 | names = append(names, name) |
| 181 | for _, w := range g.Writers { |
| 182 | if !contains(who, w) { |
| 183 | who = append(who, w) |
| 184 | } |
| 185 | } |
| 186 | break |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return names, who |
| 191 | } |
| 192 | |
| 193 | func grantList(names []string) string { |
| 194 | if len(names) == 1 { |
| 195 | return "the grant " + names[0] |
| 196 | } |
| 197 | return "the grants " + strings.Join(names, ", ") |
| 198 | } |
| 199 | |
| 200 | // match reports whether ref matches a declared pattern. `*` matches any |
| 201 | // run of characters, path separators included, which is how the patterns |
| 202 | // are documented (schema/valley.cue). Every other character matches |
| 203 | // itself: the schema refuses the characters a shell glob would read |
| 204 | // specially, because git refuses them in a refname too. |
| 205 | func match(pattern, ref string) bool { |
| 206 | p, r := 0, 0 |
| 207 | star, resume := -1, 0 |
| 208 | for r < len(ref) { |
| 209 | switch { |
| 210 | case p < len(pattern) && pattern[p] == '*': |
| 211 | star, resume = p, r |
| 212 | p++ |
| 213 | case p < len(pattern) && pattern[p] == ref[r]: |
| 214 | p++ |
| 215 | r++ |
| 216 | case star >= 0: |
| 217 | // Let the last star take one more character, and try the |
| 218 | // rest of the pattern again from there. |
| 219 | resume++ |
| 220 | p, r = star+1, resume |
| 221 | default: |
| 222 | return false |
| 223 | } |
| 224 | } |
| 225 | for p < len(pattern) && pattern[p] == '*' { |
| 226 | p++ |
| 227 | } |
| 228 | return p == len(pattern) |
| 229 | } |
| 230 | |
| 231 | // namespace is the part of a refname a refusal names: refs/<kind>/, or |
| 232 | // refs/the-valley/<kind>/ for the valley's own refs. |
| 233 | func namespace(ref string) string { |
| 234 | parts := strings.Split(ref, "/") |
| 235 | n := 2 |
| 236 | if len(parts) > 1 && parts[1] == "the-valley" { |
| 237 | n = 3 |
| 238 | } |
| 239 | if len(parts) <= n { |
| 240 | return ref |
| 241 | } |
| 242 | return strings.Join(parts[:n], "/") + "/" |
| 243 | } |
| 244 | |
| 245 | func isZero(id string) bool { |
| 246 | return id != "" && strings.Trim(id, "0") == "" |
| 247 | } |
| 248 | |
| 249 | func contains(list []string, s string) bool { |
| 250 | for _, x := range list { |
| 251 | if x == s { |
| 252 | return true |
| 253 | } |
| 254 | } |
| 255 | return false |
| 256 | } |