| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "the-valley/approval" |
| 8 | "the-valley/change" |
| 9 | ) |
| 10 | |
| 11 | // An approval is one signed note stored as a blob at |
| 12 | // refs/the-valley/approvals/<target>/c-<change ID> (approval.Ref): an |
| 13 | // approver's statement that one recorded head of the change may land. A |
| 14 | // new approval replaces the one before it. |
| 15 | // |
| 16 | // The hook holds an approval to its form, and to the integration request |
| 17 | // it lands beside when it is written (besideRequest). Whether the key that |
| 18 | // signed it is an approval key, whether the request beside it still |
| 19 | // matches it, and whether the approval lets anything land, are for |
| 20 | // whatever reads it to decide. |
| 21 | |
| 22 | // approvalBudget bounds the one object an approval ref points at. |
| 23 | var approvalBudget = budget{subject: "an approval", fileSize: approval.MaxNote, bytes: approval.MaxNote} |
| 24 | |
| 25 | // approval reads the blob id names, held to approval.MaxNote bytes before |
| 26 | // its content is asked for. |
| 27 | func (gitRepository) approval(id string) ([]byte, error) { |
| 28 | objects, err := openObjectsWithin(approvalBudget) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | defer objects.close() |
| 33 | return objects.read(id, "blob", approval.MaxNote) |
| 34 | } |
| 35 | |
| 36 | // checkApproval decides a write to an approval ref, and returns the verb |
| 37 | // and the reason that refuse it, or "" for the reason when the write is |
| 38 | // allowed. A principal holding the approve grant creates, replaces and |
| 39 | // deletes one; nobody else writes one, whatever change they own. The ref's |
| 40 | // target is main, as a change record's is, and what it points at is a blob |
| 41 | // of at most approval.MaxNote bytes holding an approval note |
| 42 | // (approval.ParseNote) about the change and the target its ref names, and |
| 43 | // about the head the change's request files (besideRequest). |
| 44 | func (p policy) checkApproval(principal string, u update) (verb, why string) { |
| 45 | target, id, ok := approval.ParseRef(u.ref) |
| 46 | if !ok || target != recordTarget { |
| 47 | return "write", fmt.Sprintf("an approval's ref is %s%s/c-<change ID>, with the change ID as 32 letters from k to z", approval.RefPrefix, recordTarget) |
| 48 | } |
| 49 | verb = "update" |
| 50 | switch { |
| 51 | case isZero(u.new): |
| 52 | verb = "delete" |
| 53 | case isZero(u.old): |
| 54 | verb = "create" |
| 55 | } |
| 56 | if principal == "" || !p.grants.holds(principal, grantApprove) { |
| 57 | holder := principal + " holds none" |
| 58 | if principal == "" { |
| 59 | holder = "a key that names no principal holds no grant" |
| 60 | } |
| 61 | return verb, fmt.Sprintf("%s takes writes only from a principal holding the %s, and %s", approval.RefPrefix, approveGrant, holder) |
| 62 | } |
| 63 | if verb == "delete" { |
| 64 | return verb, "" |
| 65 | } |
| 66 | body, err := p.repo.approval(u.new) |
| 67 | if err != nil { |
| 68 | return verb, "it " + err.Error() |
| 69 | } |
| 70 | s, err := approval.ParseNote(body) |
| 71 | switch { |
| 72 | case err != nil: |
| 73 | return verb, "it is not an approval note: " + err.Error() |
| 74 | case s.Change != id: |
| 75 | return verb, fmt.Sprintf("it approves the change %s, and its ref names %s", s.Change, id) |
| 76 | case s.Target != target: |
| 77 | return verb, fmt.Sprintf("it approves landing on %s, and its ref names %s", s.Target, target) |
| 78 | } |
| 79 | return verb, p.besideRequest(target, id, s) |
| 80 | } |
| 81 | |
| 82 | // besideRequest compares an approval with the integration request of its |
| 83 | // change that it lands beside: the request this push writes, or else the |
| 84 | // one the repository holds. The approval must name the head that request |
| 85 | // files, and a version the change's record gives that head: the record |
| 86 | // this push writes, or else the one the repository holds. An approval |
| 87 | // with no request beside it is not compared. |
| 88 | // |
| 89 | // It returns the reason that refuses the approval, or "". It reads no |
| 90 | // signature and decides nothing about landing. |
| 91 | // |
| 92 | // It compares the pair only as this push writes it, against refs read |
| 93 | // before git installs any of the push, so a matched pair is not kept |
| 94 | // matched. Two pushes that each write a request and its approval |
| 95 | // atomically conflict at the approval's ref, whose old value git checks, |
| 96 | // and one fails whole. Two that write them without --atomic can each win |
| 97 | // one ref, leaving one's approval beside the other's request. A later |
| 98 | // push that writes the request alone moves it away from an approval |
| 99 | // already admitted. valley approve pushes the pair atomically. A |
| 100 | // mismatched pair is not an approval: what compares the signed statement |
| 101 | // with the request at landing is the landing gate (not built). |
| 102 | func (p policy) besideRequest(target, id string, s approval.Statement) string { |
| 103 | request := requestPrefix + target + "/" + change.RequestName(id) |
| 104 | head, ok, err := p.current(request) |
| 105 | switch { |
| 106 | case err != nil: |
| 107 | return "the integration request it lands beside cannot be read: " + err.Error() |
| 108 | case !ok: |
| 109 | return "" |
| 110 | case s.Head != change.HeadPrefix+head: |
| 111 | return fmt.Sprintf("it approves the head %s, and the integration request beside it files %s", |
| 112 | strings.TrimPrefix(s.Head, change.HeadPrefix), head) |
| 113 | } |
| 114 | commit, ok, err := p.current(change.Ref(target, id)) |
| 115 | switch { |
| 116 | case err != nil: |
| 117 | return "the change's record cannot be read: " + err.Error() |
| 118 | case !ok: |
| 119 | return fmt.Sprintf("it approves v%d, and the repository holds no record of the change", s.Version) |
| 120 | } |
| 121 | rc, err := p.repo.record(commit) |
| 122 | if err != nil { |
| 123 | return "the change's record " + err.Error() |
| 124 | } |
| 125 | r, err := change.Parse(rc.file) |
| 126 | if err != nil { |
| 127 | return "the change's record does not read: " + err.Error() |
| 128 | } |
| 129 | for _, v := range r.Versions { |
| 130 | if v.N == s.Version && v.Head == s.Head { |
| 131 | return "" |
| 132 | } |
| 133 | } |
| 134 | return fmt.Sprintf("it approves v%d at %s, and the change's record gives v%d no such head", s.Version, head, s.Version) |
| 135 | } |
| 136 | |
| 137 | // current is the value a ref holds once this push is in: what the push |
| 138 | // writes to it, or else what the repository holds. ok is false when the |
| 139 | // push deletes the ref, or neither writes nor holds one. |
| 140 | func (p policy) current(ref string) (string, bool, error) { |
| 141 | if v, pushed := p.pushed[ref]; pushed { |
| 142 | return v, !isZero(v), nil |
| 143 | } |
| 144 | return p.repo.ref(ref) |
| 145 | } |