| 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 | --branch REF branch pushed alongside (default: the current branch) |
| 52 | --timings also append one JSON line per check to the timing |
| 53 | log, $XDG_STATE_HOME/valley/timings.jsonl |
| 54 | (default ~/.local/state/valley/timings.jsonl), a |
| 55 | regular file only its owner can read |
| 56 | --change ID the change ID each timing names |
| 57 | --change-version N the change version each timing names |
| 58 | --cost NAME=CLASS the cost class the policy declares for a check, for |
| 59 | its timing; repeatable |
| 60 | |
| 61 | run evaluates every flake check in one nix evaluation, then builds each |
| 62 | one's derivation in a nix build of its own, up to one per CPU at once, |
| 63 | so the nix daemon builds them side by side. Each check's result is its |
| 64 | own build's, and a check that fails keeps its own build's output as its |
| 65 | log. A check that does not evaluate is an error for that check alone. |
| 66 | The --command checks run after the flake checks, one at a time. |
| 67 | |
| 68 | run prints one time line per check after it finishes: its name, its |
| 69 | result, how long it took, and how it was satisfied. A flake check's time |
| 70 | is its evaluation, which it shares with the others, and its own build. |
| 71 | The flake checks build at the same time, so their times add up to more |
| 72 | than the run took. "ran" means the |
| 73 | command ran or nix built or fetched the output; "cached" means the output |
| 74 | was already in the local nix store; "unknown" means the store was not |
| 75 | asked, which only a run with --timings does, or did not answer within 5s. |
| 76 | A timing is not evidence: nothing signs, stores or pushes it. Timing |
| 77 | output that cannot be written within a second is dropped, and a timing |
| 78 | log that cannot be written is a warning, never a failure. |
| 79 | |
| 80 | verify flags: |
| 81 | --note FILE the signed note, as stored |
| 82 | --known-keys FILE verifier keys the reader accepts, one per line |
| 83 | --signer NAME signer whose signature must be present, repeatable |
| 84 | --schema FILE attestation schema to vet against |
| 85 | --repo DIR repository to re-check the recorded digests against |
| 86 | --rev REV revision in it (default: HEAD) |
| 87 | |
| 88 | digest flags: |
| 89 | --repo DIR repository (default: the git toplevel here) |
| 90 | --rev REV revision (default: HEAD) |
| 91 | |
| 92 | inputs flags: |
| 93 | --repo DIR repository (default: the git toplevel here) |
| 94 | --rev REV revision (default: HEAD) |
| 95 | --check NAME[=ATTR] flake check to digest, repeatable; defaults to every |
| 96 | check the flake defines for this system |
| 97 | --system SYSTEM flake system (default: this machine's) |
| 98 | |
| 99 | inputs evaluates each check over the revision's tree and prints its name, |
| 100 | the digest of its input closure, and the digest of its derivation. It |
| 101 | builds nothing: the integrator compares closure digests to decide whether |
| 102 | a contributor's evidence transfers to a moved base, and check latency has |
| 103 | no business on that path. |
| 104 | |
| 105 | sign and key flags: |
| 106 | --key PATH ed25519 private key |
| 107 | --name NAME the name that key signs under (default: this host's) |
| 108 | |
| 109 | render takes one json file, or stdin, and writes the statement text. Given |
| 110 | statement text instead, it writes that text back โ the form is a fixed |
| 111 | point, so anything render changes was not in it. sign takes statement text |
| 112 | and writes a note, or takes a note and adds a signature line to it. The |
| 113 | conformance vectors in attest/conformance are rendered and verified this |
| 114 | way. |
| 115 | ` |
| 116 | |
| 117 | func main() { |
| 118 | if len(os.Args) < 2 { |
| 119 | fmt.Fprint(os.Stderr, usage) |
| 120 | os.Exit(2) |
| 121 | } |
| 122 | var err error |
| 123 | switch os.Args[1] { |
| 124 | case "run": |
| 125 | err = cmdRun(os.Args[2:]) |
| 126 | case "verify": |
| 127 | err = cmdVerify(os.Args[2:]) |
| 128 | case "digest": |
| 129 | err = cmdDigest(os.Args[2:]) |
| 130 | case "inputs": |
| 131 | err = cmdInputs(os.Args[2:]) |
| 132 | case "render": |
| 133 | err = cmdRender(os.Args[2:]) |
| 134 | case "sign": |
| 135 | err = cmdSign(os.Args[2:]) |
| 136 | case "key": |
| 137 | err = cmdKey(os.Args[2:]) |
| 138 | case "help", "-h", "--help": |
| 139 | fmt.Print(usage) |
| 140 | return |
| 141 | default: |
| 142 | fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage) |
| 143 | os.Exit(2) |
| 144 | } |
| 145 | if err != nil { |
| 146 | fmt.Fprintf(os.Stderr, "attest: %v\n", err) |
| 147 | os.Exit(1) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // repeatable collects a flag given more than once. |
| 152 | type repeatable []string |
| 153 | |
| 154 | func (r *repeatable) String() string { return fmt.Sprint(*r) } |
| 155 | func (r *repeatable) Set(v string) error { |
| 156 | *r = append(*r, v) |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | func newFlagSet(name string) *flag.FlagSet { |
| 161 | fs := flag.NewFlagSet(name, flag.ExitOnError) |
| 162 | fs.Usage = func() { fmt.Fprint(os.Stderr, usage) } |
| 163 | return fs |
| 164 | } |
| 165 | |
| 166 | func cmdDigest(args []string) error { |
| 167 | fs := newFlagSet("digest") |
| 168 | repo := fs.String("repo", "", "repository") |
| 169 | rev := fs.String("rev", "HEAD", "revision") |
| 170 | if err := fs.Parse(args); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | root, err := repoRoot(*repo) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | entries, err := gitTreeEntries(root, *rev) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries)) |
| 182 | return nil |
| 183 | } |
| 184 | |
| 185 | func repoRoot(dir string) (string, error) { |
| 186 | if dir == "" { |
| 187 | dir = "." |
| 188 | } |
| 189 | return gitLine(dir, "rev-parse", "--show-toplevel") |
| 190 | } |