| 1 | // attest composes, signs, stores and verifies a valley attestation: a |
| 2 | // host-signed statement that a check ran over a tree with a result |
| 3 | // (dcr-0de694f). The statement's shape is schema/attestation.cue; this |
| 4 | // program only composes documents that satisfy it. |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "flag" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | ) |
| 12 | |
| 13 | const usage = `usage: attest <command> [flags] |
| 14 | |
| 15 | run run a repository's checks and store a signed attestation |
| 16 | verify check a note's signatures and re-check its digests |
| 17 | digest print the valley tree digest of a revision |
| 18 | inputs print a pure check's input-closure digest, building nothing |
| 19 | render write a json document as the statement text a signature covers |
| 20 | sign sign statement text, or add a signature to a note |
| 21 | key print the verifier key a reader is given for a signing key |
| 22 | help print this message |
| 23 | |
| 24 | A statement is signed as a note: the statement text, a blank line, and one |
| 25 | signature line per signer, each an em-dash, the signer's name, and base64 |
| 26 | of a key hash and a raw Ed25519 signature over the text. Several signers |
| 27 | over one statement are sibling lines under one text. A note is stored at a |
| 28 | ref keyed by the subject digest: |
| 29 | ` + refPrefix + `/<tree digest>/<signer key hash>. |
| 30 | The host signs; there is no unsigned mode. |
| 31 | |
| 32 | Every repository read โ the tree digested, exported and verified โ is made |
| 33 | with git's replacement refs off, and with no GIT_ variable inherited but |
| 34 | safe.directory entries and no user or system configuration read. A |
| 35 | refs/replace/* in the repository cannot make a revision read as some other |
| 36 | tree. A push to a remote keeps the person's own configuration. |
| 37 | |
| 38 | run flags: |
| 39 | --repo DIR repository to attest (default: the git toplevel here) |
| 40 | --rev REV revision whose tree is the subject (default: HEAD) |
| 41 | --check NAME[=ATTR] flake check to run, repeatable; defaults to every |
| 42 | check the flake defines for this system |
| 43 | --command NAME=CMD effectful check to run, repeatable |
| 44 | --environment NAME environment an effectful statement names (default "local") |
| 45 | --key PATH ed25519 private key the host signs with (required) |
| 46 | --name NAME the name that key signs under (default: this host's) |
| 47 | --provenance FILE json run provenance, carried in every statement |
| 48 | --schema FILE attestation schema to vet against |
| 49 | --system SYSTEM flake system (default: this machine's) |
| 50 | --push REMOTE publish: one atomic push of the branch and the refs, |
| 51 | adding this run's notes to a ref the remote holds |
| 52 | --branch REF branch pushed alongside (default: the current branch) |
| 53 | --timings also append one JSON line per check to the timing |
| 54 | log, $XDG_STATE_HOME/valley/timings.jsonl |
| 55 | (default ~/.local/state/valley/timings.jsonl), a |
| 56 | regular file only its owner can read |
| 57 | --change ID the change ID each timing names |
| 58 | --change-version N the change version each timing names |
| 59 | --cost NAME=CLASS the cost class the policy declares for a check, for |
| 60 | its timing; repeatable |
| 61 | --slots N how many nix builds run at once (default: one per |
| 62 | CPU this process may run on) |
| 63 | --gc-roots DIR root every output the flake checks build in DIR, |
| 64 | so nix's garbage collection keeps them |
| 65 | --eval-reservations DIR |
| 66 | admit the flake checks' evaluation by memory, among |
| 67 | the runs that share DIR (needs --eval-budget) |
| 68 | --eval-estimate N the peak memory the evaluation is expected to |
| 69 | need, in bytes (default: the whole budget) |
| 70 | --eval-budget N the memory, in bytes, that the evaluations |
| 71 | admitted through DIR share |
| 72 | --eval-peak FILE write the evaluation's peak memory, in bytes, and |
| 73 | how it ended, to FILE once it ends |
| 74 | |
| 75 | run evaluates every flake check in one nix evaluation, then builds each |
| 76 | one's derivation in a nix build of its own, up to --slots of them at |
| 77 | once, so the nix daemon builds them side by side. Each check's result is its |
| 78 | own build's, and a check that fails keeps its own build's output as its |
| 79 | log. A check that does not evaluate is an error for that check alone. |
| 80 | The --command checks run after the flake checks, one at a time. |
| 81 | |
| 82 | With --eval-reservations, the evaluation first makes a reservation in |
| 83 | DIR, a file named by attest's pid that holds its estimate, and waits |
| 84 | until its estimate plus those of every reservation made before it fit |
| 85 | the budget, or until none was made before it. It removes the reservation |
| 86 | before the first build. So the evaluations of runs that share DIR never |
| 87 | expect more memory together than the budget, an evaluation whose |
| 88 | estimate exceeds the budget runs alone, and the builds still overlap. A |
| 89 | check's time does not count the wait. attest and its nix evaluations hold |
| 90 | a lock on the reservation until they end, and on Linux each nix |
| 91 | evaluation is killed when attest dies. A reservation that no running |
| 92 | process holds does not count, so a run killed during its evaluation |
| 93 | leaves nothing that counts once its evaluation is gone. So does a |
| 94 | reservation file that is not a valid reservation. A reservation that |
| 95 | cannot be made, or reservations that cannot be read, end the run with an |
| 96 | error before anything is evaluated. |
| 97 | |
| 98 | An evaluation's peak memory is the largest maximum resident set size of |
| 99 | the nix evaluations it ran. Its outcome is ok when every one of them |
| 100 | succeeded, killed when a signal ended one, and failed otherwise. |
| 101 | --eval-peak writes both on one line, such as "734003200 ok". |
| 102 | |
| 103 | run prints one time line per check after it finishes: its name, its |
| 104 | result, how long it took, and how it was satisfied. A flake check's time |
| 105 | is its evaluation, which it shares with the others, and its own build. |
| 106 | The flake checks build at the same time, so their times add up to more |
| 107 | than the run took. "ran" means the |
| 108 | command ran or nix built or fetched the output; "cached" means the output |
| 109 | was already in the local nix store; "unknown" means the store was not |
| 110 | asked, which only a run with --timings does, or did not answer within 5s. |
| 111 | A timing is not evidence: nothing signs, stores or pushes it. Timing |
| 112 | output that cannot be written within a second is dropped, and a timing |
| 113 | log that cannot be written is a warning, never a failure. |
| 114 | |
| 115 | With --gc-roots, every output of every flake check that builds, or was |
| 116 | already in the store, gets an indirect garbage-collector root in DIR: a |
| 117 | symlink named by the output's store path basename, registered with the |
| 118 | nix daemon. An output is rooted once however many trees use it. Each run |
| 119 | that uses an output makes its symlink afresh, so the symlink's |
| 120 | modification time is when the output was last used. Removing the symlink |
| 121 | lets nix collect the output. A root that cannot be added is a warning, |
| 122 | never a failure. Without --gc-roots, run roots nothing. |
| 123 | |
| 124 | verify flags: |
| 125 | --note FILE the signed note, as stored |
| 126 | --known-keys FILE verifier keys the reader accepts, one per line |
| 127 | --signer NAME signer whose signature must be present, repeatable |
| 128 | --schema FILE attestation schema to vet against |
| 129 | --repo DIR repository to re-check the recorded digests against |
| 130 | --rev REV revision in it (default: HEAD) |
| 131 | |
| 132 | digest flags: |
| 133 | --repo DIR repository (default: the git toplevel here) |
| 134 | --rev REV revision (default: HEAD) |
| 135 | |
| 136 | inputs flags: |
| 137 | --repo DIR repository (default: the git toplevel here) |
| 138 | --rev REV revision (default: HEAD) |
| 139 | --check NAME[=ATTR] flake check to digest, repeatable; defaults to every |
| 140 | check the flake defines for this system |
| 141 | --system SYSTEM flake system (default: this machine's) |
| 142 | |
| 143 | inputs evaluates each check over the revision's tree and prints its name, |
| 144 | the digest of its input closure, and the digest of its derivation. It |
| 145 | builds nothing: the integrator compares closure digests to decide whether |
| 146 | a contributor's evidence transfers to a moved base, and check latency has |
| 147 | no business on that path. Several checks are evaluated together, in one |
| 148 | nix evaluation where nix allows it, and printed in the order given. A |
| 149 | check that does not evaluate ends the command with its error, after the |
| 150 | lines of the checks before it. |
| 151 | |
| 152 | sign and key flags: |
| 153 | --key PATH ed25519 private key |
| 154 | --name NAME the name that key signs under (default: this host's) |
| 155 | |
| 156 | render takes one json file, or stdin, and writes the statement text. Given |
| 157 | statement text instead, it writes that text back โ the form is a fixed |
| 158 | point, so anything render changes was not in it. sign takes statement text |
| 159 | and writes a note, or takes a note and adds a signature line to it. The |
| 160 | conformance vectors in attest/conformance are rendered and verified this |
| 161 | way. |
| 162 | ` |
| 163 | |
| 164 | func main() { |
| 165 | if len(os.Args) < 2 { |
| 166 | fmt.Fprint(os.Stderr, usage) |
| 167 | os.Exit(2) |
| 168 | } |
| 169 | var err error |
| 170 | switch os.Args[1] { |
| 171 | case "run": |
| 172 | err = cmdRun(os.Args[2:]) |
| 173 | case "verify": |
| 174 | err = cmdVerify(os.Args[2:]) |
| 175 | case "digest": |
| 176 | err = cmdDigest(os.Args[2:]) |
| 177 | case "inputs": |
| 178 | err = cmdInputs(os.Args[2:]) |
| 179 | case "render": |
| 180 | err = cmdRender(os.Args[2:]) |
| 181 | case "sign": |
| 182 | err = cmdSign(os.Args[2:]) |
| 183 | case "key": |
| 184 | err = cmdKey(os.Args[2:]) |
| 185 | case "help", "-h", "--help": |
| 186 | fmt.Print(usage) |
| 187 | return |
| 188 | default: |
| 189 | fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage) |
| 190 | os.Exit(2) |
| 191 | } |
| 192 | if err != nil { |
| 193 | fmt.Fprintf(os.Stderr, "attest: %v\n", err) |
| 194 | os.Exit(1) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // repeatable collects a flag given more than once. |
| 199 | type repeatable []string |
| 200 | |
| 201 | func (r *repeatable) String() string { return fmt.Sprint(*r) } |
| 202 | func (r *repeatable) Set(v string) error { |
| 203 | *r = append(*r, v) |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | func newFlagSet(name string) *flag.FlagSet { |
| 208 | fs := flag.NewFlagSet(name, flag.ExitOnError) |
| 209 | fs.Usage = func() { fmt.Fprint(os.Stderr, usage) } |
| 210 | return fs |
| 211 | } |
| 212 | |
| 213 | func cmdDigest(args []string) error { |
| 214 | fs := newFlagSet("digest") |
| 215 | repo := fs.String("repo", "", "repository") |
| 216 | rev := fs.String("rev", "HEAD", "revision") |
| 217 | if err := fs.Parse(args); err != nil { |
| 218 | return err |
| 219 | } |
| 220 | root, err := repoRoot(*repo) |
| 221 | if err != nil { |
| 222 | return err |
| 223 | } |
| 224 | entries, err := gitTreeEntries(root, *rev) |
| 225 | if err != nil { |
| 226 | return err |
| 227 | } |
| 228 | fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries)) |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | func repoRoot(dir string) (string, error) { |
| 233 | if dir == "" { |
| 234 | dir = "." |
| 235 | } |
| 236 | return gitLine(dir, "rev-parse", "--show-toplevel") |
| 237 | } |