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