| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | |
| 11 | "the-valley/note/statement" |
| 12 | ) |
| 13 | |
| 14 | // The statement of dcr-0de694f, in the shape schema/attestation.cue fixes. |
| 15 | // The schema is the specification; these types exist to compose a document |
| 16 | // that satisfies it, and every composed statement is vetted against the |
| 17 | // schema before anything is done with it. |
| 18 | |
| 19 | const ( |
| 20 | statementType = statement.Type |
| 21 | predicatePure = statement.PredicatePure |
| 22 | predicateEffect = statement.PredicateEffect |
| 23 | refPrefix = "refs/the-valley/attestations" |
| 24 | |
| 25 | // signerSuffix completes a host's signer name. A note names its key by |
| 26 | // a string and a hash, and the string a valley host uses is its fully |
| 27 | // qualified name, a slash, and what it is signing: |
| 28 | // valley-host.example.org attesting is |
| 29 | // "valley-host.example.org/attestations". The host part says |
| 30 | // which machine to go and ask about a key; the suffix keeps a key |
| 31 | // signing attestations distinct as a verifier key from the same key |
| 32 | // signing anything else, because the name is inside the key hash. |
| 33 | signerSuffix = "/attestations" |
| 34 | ) |
| 35 | |
| 36 | // defaultSignerName is the name this machine signs under: its own name and |
| 37 | // the suffix. A machine whose hostname is not its fully qualified name |
| 38 | // produces a name that is still unique to it and still says which machine |
| 39 | // it was, and --name is how a host that knows better says so. |
| 40 | func defaultSignerName() string { |
| 41 | host, err := os.Hostname() |
| 42 | if err != nil || host == "" { |
| 43 | return "localhost" + signerSuffix |
| 44 | } |
| 45 | return host + signerSuffix |
| 46 | } |
| 47 | |
| 48 | type document struct { |
| 49 | StatementType string `json:"statementType"` |
| 50 | Subject subject `json:"subject"` |
| 51 | PredicateType string `json:"predicateType"` |
| 52 | Predicate predicate `json:"predicate"` |
| 53 | Provenance json.RawMessage `json:"provenance,omitempty"` |
| 54 | } |
| 55 | |
| 56 | // subject is the digest set of dcr-0de694f: the content-addressed tree |
| 57 | // digest under `primary`, and whatever advisory names accompany it. |
| 58 | type subject struct { |
| 59 | Primary string `json:"primary"` |
| 60 | Digest map[string]string `json:"digest"` |
| 61 | } |
| 62 | |
| 63 | type predicate struct { |
| 64 | Check checkRun `json:"check"` |
| 65 | Result string `json:"result"` |
| 66 | |
| 67 | // Pure only — what a verifier re-derives against. |
| 68 | Inputs map[string]string `json:"inputs,omitempty"` |
| 69 | Derivation map[string]string `json:"derivation,omitempty"` |
| 70 | Output map[string]string `json:"output,omitempty"` |
| 71 | |
| 72 | // Effectful only — what the notarization is a notarization of. |
| 73 | Environment string `json:"environment,omitempty"` |
| 74 | ObservedAt string `json:"observedAt,omitempty"` |
| 75 | } |
| 76 | |
| 77 | type checkRun struct { |
| 78 | Name string `json:"name"` |
| 79 | Runner string `json:"runner"` |
| 80 | Attribute string `json:"attribute,omitempty"` |
| 81 | Command string `json:"command,omitempty"` |
| 82 | } |
| 83 | |
| 84 | // loadProvenance reads the caller-supplied provenance for a run: the |
| 85 | // harness, the model, digests of the prompt and the context, and the |
| 86 | // delegation chain as recorded. Nothing in this repository collects any of |
| 87 | // it today, so it arrives from whoever runs attest or not at all. |
| 88 | // |
| 89 | // The bytes are carried into the statement without being interpreted here. |
| 90 | // #Provenance in schema/attestation.cue says what the field may hold, and |
| 91 | // vetting the composed statement is what enforces it — a second opinion in |
| 92 | // Go would be a second specification to keep in step. |
| 93 | func loadProvenance(path string) (json.RawMessage, error) { |
| 94 | if path == "" { |
| 95 | return nil, nil |
| 96 | } |
| 97 | raw, err := os.ReadFile(path) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | if !json.Valid(raw) { |
| 102 | return nil, fmt.Errorf("%s is not json", path) |
| 103 | } |
| 104 | return json.RawMessage(raw), nil |
| 105 | } |
| 106 | |
| 107 | // vetStatement validates a statement, as JSON, against |
| 108 | // schema/attestation.cue. Nothing is signed, stored or pushed before this |
| 109 | // passes: a statement no verifier could read is not a thing to publish. |
| 110 | // |
| 111 | // JSON is what cue reads, and the written form of a statement carries |
| 112 | // exactly what its JSON does, so vetting one settles the other. Composing |
| 113 | // runs the gate before writing the statement out; verifying runs it after |
| 114 | // reading the statement back. |
| 115 | func vetStatement(schema string, doc []byte) error { |
| 116 | dir, err := os.MkdirTemp("", "valley-attest-vet") |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | defer os.RemoveAll(dir) |
| 121 | if err := os.WriteFile(filepath.Join(dir, "statement.json"), doc, 0o644); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | // Run in the temporary directory, so cue's errors point at |
| 125 | // "./statement.json" rather than at a path the reader never sees. |
| 126 | cmd := exec.Command("cue", "vet", "-c", schema, "statement.json") |
| 127 | cmd.Dir = dir |
| 128 | cmd.SysProcAttr = dieWithParent() |
| 129 | out, err := cmd.CombinedOutput() |
| 130 | if err != nil { |
| 131 | return fmt.Errorf("statement does not satisfy %s:\n%s", schema, strings.TrimSpace(string(out))) |
| 132 | } |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | // vetStatements vets several statements in one cue run, which checks each |
| 137 | // file against the schema on its own (cue help vet). It says only whether |
| 138 | // every one passed: vetStatement says why one does not. |
| 139 | func vetStatements(schema string, docs [][]byte) error { |
| 140 | dir, err := os.MkdirTemp("", "valley-attest-vet") |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | defer os.RemoveAll(dir) |
| 145 | args := []string{"vet", "-c", schema} |
| 146 | for i, doc := range docs { |
| 147 | name := fmt.Sprintf("statement-%d.json", i+1) |
| 148 | if err := os.WriteFile(filepath.Join(dir, name), doc, 0o644); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | args = append(args, name) |
| 152 | } |
| 153 | cmd := exec.Command("cue", args...) |
| 154 | cmd.Dir = dir |
| 155 | cmd.SysProcAttr = dieWithParent() |
| 156 | if out, err := cmd.CombinedOutput(); err != nil { |
| 157 | return fmt.Errorf("statements do not satisfy %s:\n%s", schema, strings.TrimSpace(string(out))) |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // schemaPath resolves the attestation schema: the flag, else the path the |
| 163 | // packaged wrapper sets, else the schema in the repository being attested. |
| 164 | func schemaPath(flag, repo string) (string, error) { |
| 165 | for _, p := range []string{flag, os.Getenv("VALLEY_ATTEST_SCHEMA"), filepath.Join(repo, "schema", "attestation.cue")} { |
| 166 | if p == "" { |
| 167 | continue |
| 168 | } |
| 169 | if _, err := os.Stat(p); err == nil { |
| 170 | return filepath.Abs(p) |
| 171 | } |
| 172 | } |
| 173 | return "", fmt.Errorf("no attestation schema found; pass --schema or set VALLEY_ATTEST_SCHEMA") |
| 174 | } |
| 175 | |
| 176 | // attestationRef is where a note is stored: a ref keyed by the subject |
| 177 | // digest, then by the key hash of the signer that wrote it. The key hash |
| 178 | // segment is what lets several hosts publish about one subject without |
| 179 | // racing — each writes a ref only it writes, which takes added notes and |
| 180 | // keeps every note it holds — and it is the same eight hex digits a reader |
| 181 | // sees in that host's verifier key. It names which key wrote the ref and |
| 182 | // is not itself evidence of anything; two hosts colliding on four bytes |
| 183 | // lose a push and forge nothing, because what is checked is the note. |
| 184 | func attestationRef(subjectDigest, keyHash string) string { |
| 185 | return fmt.Sprintf("%s/%s/%s", refPrefix, subjectDigest, keyHash) |
| 186 | } |