| 1 | package change |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "sort" |
| 7 | ) |
| 8 | |
| 9 | // AttestationRefs is the namespace the integrator keeps evidence under. |
| 10 | const AttestationRefs = "refs/the-valley/attestations/" |
| 11 | |
| 12 | // The bounds an attestation ref's tree is read within. The push hook |
| 13 | // refuses a pushed tree past any of them (valleyhook/repo.go), and the |
| 14 | // integrator refuses to write one (integrator/transfer.go), so every tree |
| 15 | // the integrator writes can be pushed to another host. The byte bounds |
| 16 | // count what the hook's walk reads, each tree object as git stores it. |
| 17 | const ( |
| 18 | AttestationDepth = 4 // trees from the ref's tree down, the ref's tree counted as 1 |
| 19 | AttestationTreeEntries = 64 // entries in any one tree |
| 20 | AttestationEntries = 256 // entries in all the ref's trees together |
| 21 | AttestationTreeBytes = 16 << 10 // bytes in any one tree object |
| 22 | AttestationNoteBytes = 64 << 10 // bytes in any one note |
| 23 | AttestationBytes = 1 << 20 // bytes in every tree object and note the walk reads |
| 24 | ) |
| 25 | |
| 26 | // An attestation ref the integrator writes holds its transfer statements, |
| 27 | // one per landing of the ref's tree. Each statement sits at |
| 28 | // TransferPath(<landed commit>), under TransferDir at the top of the tree. |
| 29 | // A ref written before the integrator kept one statement per landing holds |
| 30 | // a single statement at TransferNote, at the top of the tree, instead. |
| 31 | const ( |
| 32 | TransferDir = "transfer.d" |
| 33 | TransferNote = "transfer.note" |
| 34 | ) |
| 35 | |
| 36 | // TransferPath is the path of the transfer statement for a landing as |
| 37 | // commit, a full object ID: the commit's first two hex digits, its next |
| 38 | // two, and the rest, as transfer.d/<2 hex>/<2 hex>/<rest>.note. The two |
| 39 | // levels of directories keep the number of entries in any one tree small |
| 40 | // however many times one tree lands. |
| 41 | func TransferPath(commit string) string { |
| 42 | return TransferDir + "/" + commit[:2] + "/" + commit[2:4] + "/" + commit[4:] + ".note" |
| 43 | } |
| 44 | |
| 45 | // EvidenceFingerprint is the evidence state the integrator writes into an |
| 46 | // outcome record (integrator/refs.go): the first 16 hex digits of the |
| 47 | // SHA-256 over every attestation ref on origin, one "<ref> <oid>" line |
| 48 | // each, every line ending in a newline, in byte order. An outcome record |
| 49 | // with a different fingerprint was written about other evidence. |
| 50 | // |
| 51 | // The integrator, valley status and the restacker each compute it from |
| 52 | // what they read, so it is defined once, here. |
| 53 | func EvidenceFingerprint(attestations []string) string { |
| 54 | sorted := append([]string(nil), attestations...) |
| 55 | sort.Strings(sorted) |
| 56 | h := sha256.New() |
| 57 | for _, line := range sorted { |
| 58 | h.Write([]byte(line + "\n")) |
| 59 | } |
| 60 | return hex.EncodeToString(h.Sum(nil))[:16] |
| 61 | } |
| 62 | |
| 63 | // SelectNotes picks the note each check is judged by, from every note |
| 64 | // stored for one tree. notes must be in the order the integrator reads |
| 65 | // them: the attestation refs in byte order, and each ref's notes in the |
| 66 | // order git ls-tree -r lists them. check names the check a note says it |
| 67 | // is about, and verifies says whether the note verifies. |
| 68 | // |
| 69 | // The first note for a check that verifies is the one it is judged by, |
| 70 | // and no later note replaces it, whatever it records. Until one verifies, |
| 71 | // each note read replaces the one before, so the last refusal stands. |
| 72 | // |
| 73 | // The integrator judges a request by this choice (integrator/evidence.go), |
| 74 | // and approve reuses evidence only where this choice is evidence it |
| 75 | // accepts (status/coverage.go), so the two never judge a check by |
| 76 | // different notes. |
| 77 | func SelectNotes[N any](notes []N, check func(N) string, verifies func(N) bool) map[string]N { |
| 78 | picked := map[string]N{} |
| 79 | for _, n := range notes { |
| 80 | name := check(n) |
| 81 | if old, seen := picked[name]; seen && verifies(old) { |
| 82 | continue |
| 83 | } |
| 84 | picked[name] = n |
| 85 | } |
| 86 | return picked |
| 87 | } |