| 1 | package main |
| 2 | |
| 3 | // The inputs a verdict reads besides the request, the target and the |
| 4 | // attestation refs, and the record that remembers them. |
| 5 | // |
| 6 | // The outcome record keys a standing answer by the request's head, the |
| 7 | // target's tip and the evidence fingerprint. A verdict also reads the |
| 8 | // floor and which signers count, and either can change while those three |
| 9 | // stay where they are: the registry accepts a signer, or the group lands |
| 10 | // a floor that requires less. So the integrator remembers what it read of |
| 11 | // them too, in a record of its own beside the outcome record, and an |
| 12 | // answer stands only while both records match the pass. |
| 13 | // |
| 14 | // The inputs live in their own record, not in the outcome record, because |
| 15 | // the outcome record is read outside the controller (rebase/outcome.go), |
| 16 | // and its readers compare its evidence fingerprint with one they compute |
| 17 | // from the attestation refs alone. Neither the floor nor the |
| 18 | // known-signers file is something they can read. |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "crypto/sha256" |
| 23 | "encoding/hex" |
| 24 | "fmt" |
| 25 | "os" |
| 26 | "path/filepath" |
| 27 | "strings" |
| 28 | ) |
| 29 | |
| 30 | // inputsPrefix is where the inputs records live, one per request, named as |
| 31 | // its outcome record is. |
| 32 | const inputsPrefix = "refs/the-valley/integration-inputs" |
| 33 | |
| 34 | // unreadInputs is what a pass records as its inputs when one of them did |
| 35 | // not read. It is not a digest, so it never equals the inputs of a pass |
| 36 | // that read them all. Only a failure stands under it. |
| 37 | const unreadInputs = "unread" |
| 38 | |
| 39 | // inputs is what one pass read of the floor and the accepted signers. |
| 40 | type inputs struct { |
| 41 | // digest is the fingerprint the inputs record holds, or unreadInputs. |
| 42 | digest string |
| 43 | // floor is the floor's documents as a directory of *.cue, opened once |
| 44 | // for the pass, so the verdict composes the documents digest was |
| 45 | // taken over. "" when the floor did not read. |
| 46 | floor string |
| 47 | // err is why an input did not read. A request it would have judged |
| 48 | // fails instead. |
| 49 | err error |
| 50 | } |
| 51 | |
| 52 | // inputState reads the inputs for one pass, and returns what ends the |
| 53 | // pass's hold on the floor. The digest is SHA-256 over these lines: |
| 54 | // |
| 55 | // - floor <name> <sha256>, for every *.cue document of the floor, the |
| 56 | // same documents the verdict composes, by name; |
| 57 | // - known-signers <sha256> of the known-signers file as it reads now; |
| 58 | // - signer-projects <sha256> of the signer projects file as read at |
| 59 | // start, or signer-projects none when there is none; |
| 60 | // - project <name> and signer <name>, which say where a signer counts |
| 61 | // and which signature is the integrator's own. |
| 62 | // |
| 63 | // The floor and the known-signers file are read before any request is |
| 64 | // judged. The verdict reads the floor from the copy taken here. attest |
| 65 | // reads the known-signers file again when it verifies a note, so a file |
| 66 | // rewritten during the pass may be judged at its new bytes and recorded |
| 67 | // at its old ones. The next pass then reads a different digest and judges |
| 68 | // again; the reverse, an old answer recorded under new bytes, cannot |
| 69 | // happen. |
| 70 | func (in *integrator) inputState() (inputs, func()) { |
| 71 | h := sha256.New() |
| 72 | floor, done, err := in.policy.layer.Open(context.Background(), "valley-integrator-floor") |
| 73 | if err != nil { |
| 74 | return inputs{digest: unreadInputs, err: err}, done |
| 75 | } |
| 76 | // Open refuses a floor with no *.cue in it, so docs is never empty. |
| 77 | // The directory's entries are listed, in name order, rather than |
| 78 | // globbed, so a floor whose path holds a glob metacharacter is read |
| 79 | // where it is. |
| 80 | entries, err := os.ReadDir(floor) |
| 81 | var docs []string |
| 82 | for _, e := range entries { |
| 83 | if strings.HasSuffix(e.Name(), ".cue") { |
| 84 | docs = append(docs, e.Name()) |
| 85 | } |
| 86 | } |
| 87 | for _, doc := range docs { |
| 88 | var data []byte |
| 89 | if data, err = os.ReadFile(filepath.Join(floor, doc)); err != nil { |
| 90 | break |
| 91 | } |
| 92 | fmt.Fprintf(h, "floor %s %x\n", doc, sha256.Sum256(data)) |
| 93 | } |
| 94 | if err != nil { |
| 95 | done() |
| 96 | return inputs{digest: unreadInputs, err: fmt.Errorf("reading the floor: %w", err)}, func() {} |
| 97 | } |
| 98 | keys, err := os.ReadFile(in.knownSigners) |
| 99 | if err != nil { |
| 100 | done() |
| 101 | return inputs{digest: unreadInputs, err: fmt.Errorf("reading the known-signers file: %w", err)}, func() {} |
| 102 | } |
| 103 | fmt.Fprintf(h, "known-signers %x\n", sha256.Sum256(keys)) |
| 104 | if in.signerProjectsData == nil { |
| 105 | fmt.Fprintf(h, "signer-projects none\n") |
| 106 | } else { |
| 107 | fmt.Fprintf(h, "signer-projects %x\n", sha256.Sum256(in.signerProjectsData)) |
| 108 | } |
| 109 | fmt.Fprintf(h, "project %s\nsigner %s\n", in.project, in.signerName) |
| 110 | return inputs{digest: hex.EncodeToString(h.Sum(nil)), floor: floor}, done |
| 111 | } |
| 112 | |
| 113 | func (in *integrator) inputsRef(r request) string { |
| 114 | return inputsPrefix + "/" + strings.TrimPrefix(r.ref, requestPrefix+"/") |
| 115 | } |
| 116 | |
| 117 | // readInputs reads the inputs the request's outcome record was written |
| 118 | // under, or "" when that cannot be told. The inputs record names the blob |
| 119 | // of the outcome record it was written with. An outcome record written |
| 120 | // without one, as a landing's, a voiding's, or one from before inputs |
| 121 | // were remembered, leaves the inputs record naming another blob, and |
| 122 | // that reads as "". |
| 123 | func (in *integrator) readInputs(r request) string { |
| 124 | outcome, ok, err := resolveRef(in.repo, in.memoRef(r)) |
| 125 | if err != nil || !ok { |
| 126 | return "" |
| 127 | } |
| 128 | oid, ok, err := resolveRef(in.repo, in.inputsRef(r)) |
| 129 | if err != nil || !ok { |
| 130 | return "" |
| 131 | } |
| 132 | out, ok := gitTry(in.repo, "cat-file", "blob", oid) |
| 133 | if !ok { |
| 134 | return "" |
| 135 | } |
| 136 | digest, bound, ok := strings.Cut(strings.TrimSpace(out), " ") |
| 137 | if !ok || bound != outcome || digest == "" { |
| 138 | return "" |
| 139 | } |
| 140 | return digest |
| 141 | } |
| 142 | |
| 143 | // writeAnswer writes a request's outcome record and its inputs record in |
| 144 | // one ref transaction, so neither moves without the other. |
| 145 | func (in *integrator) writeAnswer(r request, value, digest string) error { |
| 146 | outcome, err := in.writeBlob([]byte(value + "\n")) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | record, err := in.writeBlob([]byte(digest + " " + outcome + "\n")) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | stdin := fmt.Sprintf("start\nupdate %s %s\nupdate %s %s\nprepare\ncommit\n", |
| 155 | in.memoRef(r), outcome, in.inputsRef(r), record) |
| 156 | _, err = gitInput(in.repo, []byte(stdin), "update-ref", "--stdin") |
| 157 | return err |
| 158 | } |