gunk-dev/the-valley

statement.go

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