| 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 an attestation's signature and re-check its digests |
| 17 | digest print the valley tree digest of a revision |
| 18 | help print this message |
| 19 | |
| 20 | A statement is signed with a detached SSHSIG signature under the namespace |
| 21 | "` + defaultNamespace + `" and stored at a ref keyed by the subject |
| 22 | digest: ` + refPrefix + `/<tree digest>/<signer fingerprint>. |
| 23 | The host signs; there is no unsigned mode. |
| 24 | |
| 25 | run flags: |
| 26 | --repo DIR repository to attest (default: the git toplevel here) |
| 27 | --rev REV revision whose tree is the subject (default: HEAD) |
| 28 | --check NAME[=ATTR] flake check to run, repeatable; defaults to every |
| 29 | check the flake defines for this system |
| 30 | --command NAME=CMD effectful check to run, repeatable |
| 31 | --environment NAME environment an effectful statement names (default "local") |
| 32 | --key PATH ssh private key the host signs with (required) |
| 33 | --namespace NS SSHSIG namespace (default "` + defaultNamespace + `") |
| 34 | --provenance FILE json run provenance, carried in every statement |
| 35 | --schema FILE attestation schema to vet against |
| 36 | --system SYSTEM flake system (default: this machine's) |
| 37 | --push REMOTE publish: one atomic push of the branch and the refs |
| 38 | --branch REF branch pushed alongside (default: the current branch) |
| 39 | |
| 40 | verify flags: |
| 41 | --statement FILE the statement, as stored |
| 42 | --signature FILE its detached signature |
| 43 | --allowed-signers FILE ssh allowed-signers file |
| 44 | --principal ID expected signer (default: whoever the file says) |
| 45 | --namespace NS SSHSIG namespace (default "` + defaultNamespace + `") |
| 46 | --schema FILE attestation schema to vet against |
| 47 | --repo DIR repository to re-check the recorded digests against |
| 48 | --rev REV revision in it (default: HEAD) |
| 49 | |
| 50 | digest flags: |
| 51 | --repo DIR repository (default: the git toplevel here) |
| 52 | --rev REV revision (default: HEAD) |
| 53 | ` |
| 54 | |
| 55 | func main() { |
| 56 | if len(os.Args) < 2 { |
| 57 | fmt.Fprint(os.Stderr, usage) |
| 58 | os.Exit(2) |
| 59 | } |
| 60 | var err error |
| 61 | switch os.Args[1] { |
| 62 | case "run": |
| 63 | err = cmdRun(os.Args[2:]) |
| 64 | case "verify": |
| 65 | err = cmdVerify(os.Args[2:]) |
| 66 | case "digest": |
| 67 | err = cmdDigest(os.Args[2:]) |
| 68 | case "help", "-h", "--help": |
| 69 | fmt.Print(usage) |
| 70 | return |
| 71 | default: |
| 72 | fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage) |
| 73 | os.Exit(2) |
| 74 | } |
| 75 | if err != nil { |
| 76 | fmt.Fprintf(os.Stderr, "attest: %v\n", err) |
| 77 | os.Exit(1) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // repeatable collects a flag given more than once. |
| 82 | type repeatable []string |
| 83 | |
| 84 | func (r *repeatable) String() string { return fmt.Sprint(*r) } |
| 85 | func (r *repeatable) Set(v string) error { |
| 86 | *r = append(*r, v) |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func newFlagSet(name string) *flag.FlagSet { |
| 91 | fs := flag.NewFlagSet(name, flag.ExitOnError) |
| 92 | fs.Usage = func() { fmt.Fprint(os.Stderr, usage) } |
| 93 | return fs |
| 94 | } |
| 95 | |
| 96 | func cmdDigest(args []string) error { |
| 97 | fs := newFlagSet("digest") |
| 98 | repo := fs.String("repo", "", "repository") |
| 99 | rev := fs.String("rev", "HEAD", "revision") |
| 100 | if err := fs.Parse(args); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | root, err := repoRoot(*repo) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | entries, err := gitTreeEntries(root, *rev) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries)) |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | func repoRoot(dir string) (string, error) { |
| 116 | if dir == "" { |
| 117 | dir = "." |
| 118 | } |
| 119 | return gitLine(dir, "rev-parse", "--show-toplevel") |
| 120 | } |