gunk-dev/the-valley

transfer.go

sourcee4ada5512f03main's published tip history
7.6 KiB
1package main
2
3// What the integrator writes down when a change lands.
4//
5// Two things, side by side in one attestation ref per subject
6// (dcr-439b771). The contributor's original notes gain the integrator's
7// signature as sibling lines under the text they already carry โ€” several
8// parties attesting to one statement, which is the composition
9// dcr-0de694f already provides. And the integrator's own transfer
10// statement records what it decided: the change, the stream, the base the
11// evidence was produced over, the policy the verdict was derived under, and
12// the per-check verdicts.
13//
14// The two are separate statements because they are about different trees.
15// A contributor's statement is about the tree it was produced over; the
16// transfer statement is about the tree that landed. When nothing
17// intervened those are the same tree and the two notes share one ref.
18
19import (
20 "the-valley/integrator/verdict"
21
22 "encoding/json"
23 "fmt"
24 "os"
25 "os/exec"
26 "path/filepath"
27 "sort"
28 "strings"
29)
30
31const transferNote = "transfer.note"
32
33// landedEvidence is the note set the integrator stores for a landing.
34type landedEvidence struct {
35 // refs maps a ref name to the blobs beneath it, keyed by their path.
36 refs map[string]map[string]string
37}
38
39// composeTransfer builds the transfer statement, vets it against the
40// attestation schema, renders it as the bytes a signature covers, and signs
41// it. Nothing is stored before the schema passes: a statement no verifier
42// could read is not a thing to publish.
43func (in *integrator) composeTransfer(ch verdict.Change, v verdict.Verdict, landedTree, landedCommit, tip, policyDigest string) ([]byte, error) {
44 type digestSet map[string]string
45 type checkTransfer struct {
46 Name string `json:"name"`
47 Rule string `json:"rule"`
48 Evidence digestSet `json:"evidence"`
49 Inputs digestSet `json:"inputs,omitempty"`
50 }
51 checks := make([]checkTransfer, 0, len(v.Checks))
52 for _, cv := range v.Checks {
53 entry := checkTransfer{Name: cv.Name, Rule: cv.Rule, Evidence: digestSet{"sha256": cv.Evidence}}
54 if cv.Inputs != "" {
55 entry.Inputs = digestSet{"valley-inputs-v1": cv.Inputs}
56 }
57 checks = append(checks, entry)
58 }
59 doc := map[string]any{
60 "statementType": "the-valley/attestation/v1",
61 "subject": map[string]any{
62 "primary": "valley-tree-v1",
63 "digest": digestSet{"valley-tree-v1": landedTree, "git-sha1": landedCommit},
64 },
65 "predicateType": predicateTransfer,
66 "predicate": map[string]any{
67 "change": ch.ID,
68 "target": ch.Target,
69 "base": digestSet{"valley-tree-v1": ch.Attested},
70 "policy": digestSet{"sha256": policyDigest, "git-sha1": tip},
71 "checks": checks,
72 },
73 }
74 raw, err := json.Marshal(doc)
75 if err != nil {
76 return nil, err
77 }
78 if err := in.vet(raw); err != nil {
79 return nil, err
80 }
81 text, err := in.pipe(raw, in.attest, "render")
82 if err != nil {
83 return nil, fmt.Errorf("rendering the transfer statement: %w", err)
84 }
85 return in.sign(text)
86}
87
88// vet holds a composed statement to schema/attestation.cue before it is
89// rendered or signed, exactly as attest holds its own.
90func (in *integrator) vet(doc []byte) error {
91 dir, err := os.MkdirTemp("", "valley-integrator-vet")
92 if err != nil {
93 return err
94 }
95 defer os.RemoveAll(dir)
96 if err := os.WriteFile(filepath.Join(dir, "statement.json"), doc, 0o644); err != nil {
97 return err
98 }
99 cmd := exec.Command(in.cue, "vet", "-c", in.attestSchema, "statement.json")
100 cmd.Dir = dir
101 if out, err := cmd.CombinedOutput(); err != nil {
102 return fmt.Errorf("the transfer statement does not satisfy %s:\n%s", in.attestSchema, strings.TrimSpace(string(out)))
103 }
104 return nil
105}
106
107// sign turns statement text into a note, or adds the integrator's
108// signature to a note that already carries one. `attest sign` decides which
109// from the input, so both cases are the same call.
110func (in *integrator) sign(document []byte) ([]byte, error) {
111 return in.pipe(document, in.attest, "sign", "--key", in.key, "--name", in.signerName)
112}
113
114func (in *integrator) pipe(stdin []byte, name string, args ...string) ([]byte, error) {
115 cmd := exec.Command(name, args...)
116 cmd.Stdin = strings.NewReader(string(stdin))
117 var errb strings.Builder
118 cmd.Stderr = &errb
119 out, err := cmd.Output()
120 if err != nil {
121 return nil, fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(errb.String()))
122 }
123 return out, nil
124}
125
126// countersign builds the note set a landing stores: every transferring
127// check's note with the integrator's sibling signature added, keyed by the
128// subject it is about, plus the transfer statement keyed by the tree that
129// landed.
130func (in *integrator) countersign(ch verdict.Change, v verdict.Verdict, l landing, transfer []byte) (landedEvidence, error) {
131 out := landedEvidence{refs: map[string]map[string]string{}}
132 add := func(ref, path string, content []byte) error {
133 blob, err := in.writeBlob(content)
134 if err != nil {
135 return err
136 }
137 if out.refs[ref] == nil {
138 out.refs[ref] = map[string]string{}
139 }
140 out.refs[ref][path] = blob
141 return nil
142 }
143 original := fmt.Sprintf("%s/%s/%s", attestationPrefix, ch.Attested, in.keyHash)
144 for _, cv := range v.Checks {
145 note, ok := l.notes[cv.Name]
146 if !ok || !cv.Transferred {
147 continue
148 }
149 signed, err := in.sign(note)
150 if err != nil {
151 return out, fmt.Errorf("countersigning %s: %w", cv.Name, err)
152 }
153 if err := add(original, cv.Name+"/"+statementNote, signed); err != nil {
154 return out, err
155 }
156 }
157 landed := fmt.Sprintf("%s/%s/%s", attestationPrefix, l.digest, in.keyHash)
158 if err := add(landed, transferNote, transfer); err != nil {
159 return out, err
160 }
161 return out, nil
162}
163
164// store writes the note set into the repository as one ref per subject.
165// Deterministic by construction: a ref's value is a function of the notes
166// beneath it and nothing else.
167func (in *integrator) store(e landedEvidence) ([]string, error) {
168 var written []string
169 for ref := range e.refs {
170 written = append(written, ref)
171 }
172 sort.Strings(written)
173 for _, ref := range written {
174 top, err := in.writeTree(e.refs[ref])
175 if err != nil {
176 return nil, err
177 }
178 if _, err := git(in.repo, "update-ref", ref, top); err != nil {
179 return nil, err
180 }
181 }
182 return written, nil
183}
184
185func (in *integrator) writeBlob(content []byte) (string, error) {
186 cmd := exec.Command("git", "hash-object", "-w", "--stdin")
187 cmd.Dir = in.repo
188 cmd.Stdin = strings.NewReader(string(content))
189 out, err := cmd.Output()
190 if err != nil {
191 return "", fmt.Errorf("storing a note: %w", err)
192 }
193 return strings.TrimSpace(string(out)), nil
194}
195
196// writeTree builds a tree from paths to blob ids, creating a subtree per
197// path segment. Paths here are one or two segments deep โ€” "transfer.note",
198// "<check>/statement.note" โ€” so the recursion is shallow by construction.
199func (in *integrator) writeTree(blobs map[string]string) (string, error) {
200 here := map[string]string{}
201 under := map[string]map[string]string{}
202 for path, blob := range blobs {
203 dir, rest, nested := strings.Cut(path, "/")
204 if !nested {
205 here[path] = blob
206 continue
207 }
208 if under[dir] == nil {
209 under[dir] = map[string]string{}
210 }
211 under[dir][rest] = blob
212 }
213 var lines []string
214 for name, blob := range here {
215 lines = append(lines, fmt.Sprintf("100644 blob %s\t%s", blob, name))
216 }
217 for name, sub := range under {
218 oid, err := in.writeTree(sub)
219 if err != nil {
220 return "", err
221 }
222 lines = append(lines, fmt.Sprintf("040000 tree %s\t%s", oid, name))
223 }
224 sort.Strings(lines)
225 cmd := exec.Command("git", "mktree")
226 cmd.Dir = in.repo
227 cmd.Stdin = strings.NewReader(strings.Join(lines, "\n") + "\n")
228 out, err := cmd.Output()
229 if err != nil {
230 return "", fmt.Errorf("building an attestation tree: %w", err)
231 }
232 return strings.TrimSpace(string(out)), nil
233}