| 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 | render write a json document as the statement text a signature covers |
| 19 | sign sign statement text, or add a signature to a note |
| 20 | key print the verifier key a reader is given for a signing key |
| 21 | help print this message |
| 22 | |
| 23 | A statement is signed as a note: the statement text, a blank line, and one |
| 24 | signature line per signer, each an em-dash, the signer's name, and base64 |
| 25 | of a key hash and a raw Ed25519 signature over the text. Several signers |
| 26 | over one statement are sibling lines under one text. A note is stored at a |
| 27 | ref keyed by the subject digest: |
| 28 | ` + refPrefix + `/<tree digest>/<signer key hash>. |
| 29 | The host signs; there is no unsigned mode. |
| 30 | |
| 31 | run flags: |
| 32 | --repo DIR repository to attest (default: the git toplevel here) |
| 33 | --rev REV revision whose tree is the subject (default: HEAD) |
| 34 | --check NAME[=ATTR] flake check to run, repeatable; defaults to every |
| 35 | check the flake defines for this system |
| 36 | --command NAME=CMD effectful check to run, repeatable |
| 37 | --environment NAME environment an effectful statement names (default "local") |
| 38 | --key PATH ed25519 private key the host signs with (required) |
| 39 | --name NAME the name that key signs under (default: this host's) |
| 40 | --provenance FILE json run provenance, carried in every statement |
| 41 | --schema FILE attestation schema to vet against |
| 42 | --system SYSTEM flake system (default: this machine's) |
| 43 | --push REMOTE publish: one atomic push of the branch and the refs |
| 44 | --branch REF branch pushed alongside (default: the current branch) |
| 45 | |
| 46 | verify flags: |
| 47 | --note FILE the signed note, as stored |
| 48 | --known-keys FILE verifier keys the reader accepts, one per line |
| 49 | --signer NAME signer whose signature must be present, repeatable |
| 50 | --schema FILE attestation schema to vet against |
| 51 | --repo DIR repository to re-check the recorded digests against |
| 52 | --rev REV revision in it (default: HEAD) |
| 53 | |
| 54 | digest flags: |
| 55 | --repo DIR repository (default: the git toplevel here) |
| 56 | --rev REV revision (default: HEAD) |
| 57 | |
| 58 | sign and key flags: |
| 59 | --key PATH ed25519 private key |
| 60 | --name NAME the name that key signs under (default: this host's) |
| 61 | |
| 62 | render takes one json file, or stdin, and writes the statement text. Given |
| 63 | statement text instead, it writes that text back — the form is a fixed |
| 64 | point, so anything render changes was not in it. sign takes statement text |
| 65 | and writes a note, or takes a note and adds a signature line to it. The |
| 66 | conformance vectors in attest/conformance are rendered and verified this |
| 67 | way. |
| 68 | ` |
| 69 | |
| 70 | func main() { |
| 71 | if len(os.Args) < 2 { |
| 72 | fmt.Fprint(os.Stderr, usage) |
| 73 | os.Exit(2) |
| 74 | } |
| 75 | var err error |
| 76 | switch os.Args[1] { |
| 77 | case "run": |
| 78 | err = cmdRun(os.Args[2:]) |
| 79 | case "verify": |
| 80 | err = cmdVerify(os.Args[2:]) |
| 81 | case "digest": |
| 82 | err = cmdDigest(os.Args[2:]) |
| 83 | case "render": |
| 84 | err = cmdRender(os.Args[2:]) |
| 85 | case "sign": |
| 86 | err = cmdSign(os.Args[2:]) |
| 87 | case "key": |
| 88 | err = cmdKey(os.Args[2:]) |
| 89 | case "help", "-h", "--help": |
| 90 | fmt.Print(usage) |
| 91 | return |
| 92 | default: |
| 93 | fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage) |
| 94 | os.Exit(2) |
| 95 | } |
| 96 | if err != nil { |
| 97 | fmt.Fprintf(os.Stderr, "attest: %v\n", err) |
| 98 | os.Exit(1) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // repeatable collects a flag given more than once. |
| 103 | type repeatable []string |
| 104 | |
| 105 | func (r *repeatable) String() string { return fmt.Sprint(*r) } |
| 106 | func (r *repeatable) Set(v string) error { |
| 107 | *r = append(*r, v) |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func newFlagSet(name string) *flag.FlagSet { |
| 112 | fs := flag.NewFlagSet(name, flag.ExitOnError) |
| 113 | fs.Usage = func() { fmt.Fprint(os.Stderr, usage) } |
| 114 | return fs |
| 115 | } |
| 116 | |
| 117 | func cmdDigest(args []string) error { |
| 118 | fs := newFlagSet("digest") |
| 119 | repo := fs.String("repo", "", "repository") |
| 120 | rev := fs.String("rev", "HEAD", "revision") |
| 121 | if err := fs.Parse(args); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | root, err := repoRoot(*repo) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | entries, err := gitTreeEntries(root, *rev) |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries)) |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | func repoRoot(dir string) (string, error) { |
| 137 | if dir == "" { |
| 138 | dir = "." |
| 139 | } |
| 140 | return gitLine(dir, "rev-parse", "--show-toplevel") |
| 141 | } |