| 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 | defaultNamespace = "the-valley.attestation.v1" |
| 22 | refPrefix = "refs/the-valley/attestations" |
| 23 | ) |
| 24 | |
| 25 | type statement struct { |
| 26 | StatementType string `json:"statementType"` |
| 27 | Subject subject `json:"subject"` |
| 28 | PredicateType string `json:"predicateType"` |
| 29 | Predicate predicate `json:"predicate"` |
| 30 | Provenance json.RawMessage `json:"provenance,omitempty"` |
| 31 | } |
| 32 | |
| 33 | // subject is the digest set of dcr-0de694f: the content-addressed tree |
| 34 | // digest under `primary`, and whatever advisory names accompany it. |
| 35 | type subject struct { |
| 36 | Primary string `json:"primary"` |
| 37 | Digest map[string]string `json:"digest"` |
| 38 | } |
| 39 | |
| 40 | type predicate struct { |
| 41 | Check checkRun `json:"check"` |
| 42 | Result string `json:"result"` |
| 43 | |
| 44 | // Pure only — what a verifier re-derives against. |
| 45 | Inputs map[string]string `json:"inputs,omitempty"` |
| 46 | Derivation map[string]string `json:"derivation,omitempty"` |
| 47 | Output map[string]string `json:"output,omitempty"` |
| 48 | |
| 49 | // Effectful only — what the notarization is a notarization of. |
| 50 | Environment string `json:"environment,omitempty"` |
| 51 | ObservedAt string `json:"observedAt,omitempty"` |
| 52 | } |
| 53 | |
| 54 | type checkRun struct { |
| 55 | Name string `json:"name"` |
| 56 | Runner string `json:"runner"` |
| 57 | Attribute string `json:"attribute,omitempty"` |
| 58 | Command string `json:"command,omitempty"` |
| 59 | } |
| 60 | |
| 61 | // loadProvenance reads the caller-supplied provenance for a run: the |
| 62 | // harness, the model, digests of the prompt and the context, and the |
| 63 | // delegation chain as recorded. Nothing in this repository collects any of |
| 64 | // it today, so it arrives from whoever runs attest or not at all. |
| 65 | // |
| 66 | // The bytes are carried into the statement without being interpreted here. |
| 67 | // #Provenance in schema/attestation.cue says what the field may hold, and |
| 68 | // vetting the composed statement is what enforces it — a second opinion in |
| 69 | // Go would be a second specification to keep in step. |
| 70 | func loadProvenance(path string) (json.RawMessage, error) { |
| 71 | if path == "" { |
| 72 | return nil, nil |
| 73 | } |
| 74 | raw, err := os.ReadFile(path) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | if !json.Valid(raw) { |
| 79 | return nil, fmt.Errorf("%s is not json", path) |
| 80 | } |
| 81 | return json.RawMessage(raw), nil |
| 82 | } |
| 83 | |
| 84 | // vetStatement validates canonical statement bytes against |
| 85 | // schema/attestation.cue. Nothing is signed, stored or pushed before this |
| 86 | // passes: a statement no verifier could read is not a thing to publish. |
| 87 | func vetStatement(schema string, canonical []byte) error { |
| 88 | dir, err := os.MkdirTemp("", "valley-attest-vet") |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | defer os.RemoveAll(dir) |
| 93 | if err := os.WriteFile(filepath.Join(dir, "statement.json"), canonical, 0o644); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | // Run in the temporary directory, so cue's errors point at |
| 97 | // "./statement.json" rather than at a path the reader never sees. |
| 98 | cmd := exec.Command("cue", "vet", "-c", schema, "statement.json") |
| 99 | cmd.Dir = dir |
| 100 | out, err := cmd.CombinedOutput() |
| 101 | if err != nil { |
| 102 | return fmt.Errorf("statement does not satisfy %s:\n%s", schema, strings.TrimSpace(string(out))) |
| 103 | } |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | // schemaPath resolves the attestation schema: the flag, else the path the |
| 108 | // packaged wrapper sets, else the schema in the repository being attested. |
| 109 | func schemaPath(flag, repo string) (string, error) { |
| 110 | for _, p := range []string{flag, os.Getenv("VALLEY_ATTEST_SCHEMA"), filepath.Join(repo, "schema", "attestation.cue")} { |
| 111 | if p == "" { |
| 112 | continue |
| 113 | } |
| 114 | if _, err := os.Stat(p); err == nil { |
| 115 | return filepath.Abs(p) |
| 116 | } |
| 117 | } |
| 118 | return "", fmt.Errorf("no attestation schema found; pass --schema or set VALLEY_ATTEST_SCHEMA") |
| 119 | } |
| 120 | |
| 121 | // attestationRef is where a statement is stored: a ref keyed by the |
| 122 | // subject digest, then by the signer. The signer segment is what lets |
| 123 | // several parties attest to one subject side by side — each writes a ref |
| 124 | // only it writes, which an integrator can hold create-only. |
| 125 | func attestationRef(subjectDigest, signer string) string { |
| 126 | return fmt.Sprintf("%s/%s/%s", refPrefix, subjectDigest, signer) |
| 127 | } |