gunk-dev/the-valley

statement.go

source89496f97a023main's published tip history
5.8 KiB
1package main
2
3import (
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
19const (
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.
40func defaultSignerName() string {
41 host, err := os.Hostname()
42 if err != nil || host == "" {
43 return "localhost" + signerSuffix
44 }
45 return host + signerSuffix
46}
47
48type 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.
58type subject struct {
59 Primary string `json:"primary"`
60 Digest map[string]string `json:"digest"`
61}
62
63type 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
77type 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.
93func 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.
115func 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 out, err := cmd.CombinedOutput()
129 if err != nil {
130 return fmt.Errorf("statement does not satisfy %s:\n%s", schema, strings.TrimSpace(string(out)))
131 }
132 return nil
133}
134
135// schemaPath resolves the attestation schema: the flag, else the path the
136// packaged wrapper sets, else the schema in the repository being attested.
137func schemaPath(flag, repo string) (string, error) {
138 for _, p := range []string{flag, os.Getenv("VALLEY_ATTEST_SCHEMA"), filepath.Join(repo, "schema", "attestation.cue")} {
139 if p == "" {
140 continue
141 }
142 if _, err := os.Stat(p); err == nil {
143 return filepath.Abs(p)
144 }
145 }
146 return "", fmt.Errorf("no attestation schema found; pass --schema or set VALLEY_ATTEST_SCHEMA")
147}
148
149// attestationRef is where a note is stored: a ref keyed by the subject
150// digest, then by the key hash of the signer that wrote it. The key hash
151// segment is what lets several hosts publish about one subject without
152// racing — each writes a ref only it writes, which takes added notes and
153// keeps every note it holds — and it is the same eight hex digits a reader
154// sees in that host's verifier key. It names which key wrote the ref and
155// is not itself evidence of anything; two hosts colliding on four bytes
156// lose a push and forge nothing, because what is checked is the note.
157func attestationRef(subjectDigest, keyHash string) string {
158 return fmt.Sprintf("%s/%s/%s", refPrefix, subjectDigest, keyHash)
159}