| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "the-valley/knot" |
| 8 | ) |
| 9 | |
| 10 | // The plan graph is a project's plan as data: knots at one ref, |
| 11 | // refs/the-valley/knots, with its own root history and one commit per |
| 12 | // operation (the knot module). Any named principal may write it, and no |
| 13 | // review stands between a write and the ref, so the hook is what holds |
| 14 | // every write to the graph's rules. |
| 15 | // |
| 16 | // The bounds on reading it follow from the graph's limits (knot/ |
| 17 | // contract.go): the widest tree is one knot's comments, the walk visits |
| 18 | // every knot and every comment, and the hook stops reading a push's graph |
| 19 | // after knot.HookDeadline. |
| 20 | var graphBudget = budget{ |
| 21 | subject: "the plan graph", depth: knot.MaxDepth, entriesInTree: knot.MaxTreeEntries, entries: knot.MaxEntries, |
| 22 | treeSize: knot.MaxTreeSize, fileSize: knot.MaxKnotSize, bytes: knot.MaxReadBytes, deadline: knot.HookDeadline, |
| 23 | } |
| 24 | |
| 25 | // graphReader reads the objects of one push's graph, within graphBudget. |
| 26 | type graphReader interface { |
| 27 | // commit reads the graph commit id names. |
| 28 | commit(id string) (knot.Commit, error) |
| 29 | // tree lists every tree and file below the tree id names. |
| 30 | tree(id string) ([]knot.Entry, error) |
| 31 | // blob reads the file id names, at most max bytes. |
| 32 | blob(id string, max int) ([]byte, error) |
| 33 | // overdue is nil while the reading's deadline has not passed, and the |
| 34 | // refusal that says so once it has. |
| 35 | overdue() error |
| 36 | close() |
| 37 | } |
| 38 | |
| 39 | type streamGraph struct{ objects *objectStream } |
| 40 | |
| 41 | func (gitRepository) graph() (graphReader, error) { |
| 42 | objects, err := openObjectsWithin(graphBudget) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | return streamGraph{objects}, nil |
| 47 | } |
| 48 | |
| 49 | func (g streamGraph) close() { g.objects.close() } |
| 50 | |
| 51 | func (g streamGraph) overdue() error { |
| 52 | if !g.objects.expired() { |
| 53 | return nil |
| 54 | } |
| 55 | b := g.objects.budget |
| 56 | return fmt.Errorf("the hook could not finish checking it within the %s it spends on %s", b.deadline, b.subject) |
| 57 | } |
| 58 | |
| 59 | func (g streamGraph) commit(id string) (knot.Commit, error) { |
| 60 | body, err := g.objects.read(id, "commit", knot.MaxCommitSize) |
| 61 | if err != nil { |
| 62 | return knot.Commit{}, fmt.Errorf("%s %v", id, err) |
| 63 | } |
| 64 | c, err := knot.ParseCommit(body, len(id)/2) |
| 65 | if err != nil { |
| 66 | return knot.Commit{}, fmt.Errorf("%s: %v", id, err) |
| 67 | } |
| 68 | return c, nil |
| 69 | } |
| 70 | |
| 71 | // tree walks the tree within the budget, and lists what it holds for the |
| 72 | // knot module to judge: the walk refuses only what would cross a bound, |
| 73 | // and the module refuses every entry that is not the graph's layout. |
| 74 | func (g streamGraph) tree(id string) ([]knot.Entry, error) { |
| 75 | var entries []knot.Entry |
| 76 | w := walk{objects: g.objects, hashSize: len(id) / 2, visit: func(at, mode, child string) error { |
| 77 | entries = append(entries, knot.Entry{Path: at, Mode: mode, OID: child}) |
| 78 | return nil |
| 79 | }} |
| 80 | if err := w.tree(id, "", 1); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return entries, nil |
| 84 | } |
| 85 | |
| 86 | func (g streamGraph) blob(id string, max int) ([]byte, error) { |
| 87 | return g.objects.read(id, "blob", max) |
| 88 | } |
| 89 | |
| 90 | // checkGraph decides a write to refs/the-valley/knots, and returns the |
| 91 | // verb and the reason that refuse it, or "" for the reason when the write |
| 92 | // is allowed. |
| 93 | // |
| 94 | // - Any named principal may write it. A key that names none may not. |
| 95 | // - The push that creates the ref brings one commit with no parent. An |
| 96 | // update brings one commit whose only parent is the tip it replaces, |
| 97 | // so a push is one operation, and the ref never rewinds. No push |
| 98 | // deletes the ref: its history is the record of every write, and a |
| 99 | // graph that must be recovered is recovered on the host. |
| 100 | // - The commit names the pushing principal as its committer, and carries |
| 101 | // a Valley-Op trailer. |
| 102 | // - Its tree is exactly the graph's layout, and every knot and comment |
| 103 | // the commit adds or changes holds to the module's rules, against the |
| 104 | // graph before it (knot.Push): no knot or comment is deleted, a knot |
| 105 | // keeps its id, type, created and created_by, a new knot's created_by |
| 106 | // and a new comment's author name the pusher, an edited comment keeps |
| 107 | // its author and names the pusher in edited_by, every edge resolves, |
| 108 | // and no edge forms a cycle. |
| 109 | // - A comment is edited by its author, or by a holder of the approve |
| 110 | // grant, under either of its names. The module checks what an edit |
| 111 | // records, and the hook decides who may make it, since the grants are |
| 112 | // the host's. |
| 113 | func (p policy) checkGraph(principal string, u update) (verb, why string) { |
| 114 | switch { |
| 115 | case principal == "": |
| 116 | return "write", "the plan graph takes writes only from a named principal, and a key that names no principal is none" |
| 117 | case isZero(u.new): |
| 118 | return "delete", "the plan graph is never deleted: its history is the record of every write to it, and a graph that must be recovered is recovered on the host" |
| 119 | } |
| 120 | verb = "update" |
| 121 | if isZero(u.old) { |
| 122 | verb = "create" |
| 123 | } |
| 124 | g, err := p.repo.graph() |
| 125 | if err != nil { |
| 126 | return verb, "the graph could not be read: " + err.Error() |
| 127 | } |
| 128 | defer g.close() |
| 129 | c, err := g.commit(u.new) |
| 130 | if err != nil { |
| 131 | return verb, "its new tip " + err.Error() |
| 132 | } |
| 133 | switch { |
| 134 | case verb == "create" && len(c.Parents) != 0: |
| 135 | return verb, fmt.Sprintf("the commit that creates the graph has no parent, and this one has %d", len(c.Parents)) |
| 136 | case verb == "update" && (len(c.Parents) != 1 || c.Parents[0] != u.old): |
| 137 | return verb, fmt.Sprintf("an update of the graph is one commit whose only parent is the tip it replaces, %s, and this one's parents are %s", u.old, parentList(c.Parents)) |
| 138 | case c.Committer != principal: |
| 139 | return verb, fmt.Sprintf("its commit names %s as its committer, and %s is pushing it: every commit on the graph is committed by the principal that pushes it", c.Committer, principal) |
| 140 | } |
| 141 | entries, err := g.tree(c.Tree) |
| 142 | if err != nil { |
| 143 | return verb, "its tree: " + err.Error() |
| 144 | } |
| 145 | after, err := knot.Layout(entries) |
| 146 | if err != nil { |
| 147 | return verb, err.Error() |
| 148 | } |
| 149 | var before *knot.Index |
| 150 | if verb == "update" { |
| 151 | old, err := g.commit(u.old) |
| 152 | if err != nil { |
| 153 | return verb, "the tip it replaces, " + err.Error() |
| 154 | } |
| 155 | entries, err := g.tree(old.Tree) |
| 156 | if err != nil { |
| 157 | return verb, "the tip it replaces: its tree: " + err.Error() |
| 158 | } |
| 159 | if before, err = knot.Layout(entries); err != nil { |
| 160 | return verb, "the tip it replaces: " + err.Error() |
| 161 | } |
| 162 | } |
| 163 | mayEdit := func(string) error { |
| 164 | if p.grants.holds(principal, grantApprove) { |
| 165 | return nil |
| 166 | } |
| 167 | return fmt.Errorf("a comment is edited by its author, or by a holder of the %s", approveGrant) |
| 168 | } |
| 169 | if err := (knot.Push{Old: before, New: after, Read: g.blob, Pusher: principal, MayEdit: mayEdit}).Check(); err != nil { |
| 170 | return verb, err.Error() |
| 171 | } |
| 172 | // A read past the deadline is refused as it fails, but a read that |
| 173 | // began in time can end late, and the work between reads takes time |
| 174 | // too: the push is admitted only if the whole check ended in time. |
| 175 | if err := g.overdue(); err != nil { |
| 176 | return verb, err.Error() |
| 177 | } |
| 178 | return verb, "" |
| 179 | } |
| 180 | |
| 181 | func parentList(parents []string) string { |
| 182 | if len(parents) == 0 { |
| 183 | return "none" |
| 184 | } |
| 185 | return strings.Join(parents, ", ") |
| 186 | } |