gunk-dev/the-valley

statement.go

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