| 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 | "io" |
| 11 | "os" |
| 12 | ) |
| 13 | |
| 14 | const usage = `usage: attest <command> [flags] |
| 15 | |
| 16 | run run a repository's checks and store a signed attestation |
| 17 | verify check an attestation's signature and re-check its digests |
| 18 | digest print the valley tree digest of a revision |
| 19 | canonical render a json document into the bytes a signature covers |
| 20 | help print this message |
| 21 | |
| 22 | A statement is signed with a detached SSHSIG signature under the namespace |
| 23 | "` + defaultNamespace + `" and stored at a ref keyed by the subject |
| 24 | digest: ` + refPrefix + `/<tree digest>/<signer fingerprint>. |
| 25 | The host signs; there is no unsigned mode. |
| 26 | |
| 27 | run flags: |
| 28 | --repo DIR repository to attest (default: the git toplevel here) |
| 29 | --rev REV revision whose tree is the subject (default: HEAD) |
| 30 | --check NAME[=ATTR] flake check to run, repeatable; defaults to every |
| 31 | check the flake defines for this system |
| 32 | --command NAME=CMD effectful check to run, repeatable |
| 33 | --environment NAME environment an effectful statement names (default "local") |
| 34 | --key PATH ssh private key the host signs with (required) |
| 35 | --namespace NS SSHSIG namespace (default "` + defaultNamespace + `") |
| 36 | --provenance FILE json run provenance, carried in every statement |
| 37 | --schema FILE attestation schema to vet against |
| 38 | --system SYSTEM flake system (default: this machine's) |
| 39 | --push REMOTE publish: one atomic push of the branch and the refs |
| 40 | --branch REF branch pushed alongside (default: the current branch) |
| 41 | |
| 42 | verify flags: |
| 43 | --statement FILE the statement, as stored |
| 44 | --signature FILE its detached signature |
| 45 | --allowed-signers FILE ssh allowed-signers file |
| 46 | --principal ID expected signer (default: whoever the file says) |
| 47 | --namespace NS SSHSIG namespace (default "` + defaultNamespace + `") |
| 48 | --schema FILE attestation schema to vet against |
| 49 | --repo DIR repository to re-check the recorded digests against |
| 50 | --rev REV revision in it (default: HEAD) |
| 51 | |
| 52 | digest flags: |
| 53 | --repo DIR repository (default: the git toplevel here) |
| 54 | --rev REV revision (default: HEAD) |
| 55 | |
| 56 | canonical takes one json file, or stdin, and writes the canonical bytes |
| 57 | with no trailing newline. The conformance vectors in attest/conformance are |
| 58 | rendered and compared this way. |
| 59 | ` |
| 60 | |
| 61 | func main() { |
| 62 | if len(os.Args) < 2 { |
| 63 | fmt.Fprint(os.Stderr, usage) |
| 64 | os.Exit(2) |
| 65 | } |
| 66 | var err error |
| 67 | switch os.Args[1] { |
| 68 | case "run": |
| 69 | err = cmdRun(os.Args[2:]) |
| 70 | case "verify": |
| 71 | err = cmdVerify(os.Args[2:]) |
| 72 | case "digest": |
| 73 | err = cmdDigest(os.Args[2:]) |
| 74 | case "canonical": |
| 75 | err = cmdCanonical(os.Args[2:]) |
| 76 | case "help", "-h", "--help": |
| 77 | fmt.Print(usage) |
| 78 | return |
| 79 | default: |
| 80 | fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage) |
| 81 | os.Exit(2) |
| 82 | } |
| 83 | if err != nil { |
| 84 | fmt.Fprintf(os.Stderr, "attest: %v\n", err) |
| 85 | os.Exit(1) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // repeatable collects a flag given more than once. |
| 90 | type repeatable []string |
| 91 | |
| 92 | func (r *repeatable) String() string { return fmt.Sprint(*r) } |
| 93 | func (r *repeatable) Set(v string) error { |
| 94 | *r = append(*r, v) |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func newFlagSet(name string) *flag.FlagSet { |
| 99 | fs := flag.NewFlagSet(name, flag.ExitOnError) |
| 100 | fs.Usage = func() { fmt.Fprint(os.Stderr, usage) } |
| 101 | return fs |
| 102 | } |
| 103 | |
| 104 | func cmdDigest(args []string) error { |
| 105 | fs := newFlagSet("digest") |
| 106 | repo := fs.String("repo", "", "repository") |
| 107 | rev := fs.String("rev", "HEAD", "revision") |
| 108 | if err := fs.Parse(args); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | root, err := repoRoot(*repo) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | entries, err := gitTreeEntries(root, *rev) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries)) |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // cmdCanonical exposes the canonicalizer on its own. The canonical bytes |
| 124 | // are an interop contract rather than an internal step — a second attest |
| 125 | // must render them identically or every signature already made stops |
| 126 | // verifying — so the renderer is reachable without composing, signing or |
| 127 | // storing anything, and attest/conformance/ is the fixed set of documents |
| 128 | // it is held to. |
| 129 | func cmdCanonical(args []string) error { |
| 130 | fs := newFlagSet("canonical") |
| 131 | if err := fs.Parse(args); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | var ( |
| 135 | raw []byte |
| 136 | err error |
| 137 | ) |
| 138 | switch fs.NArg() { |
| 139 | case 0: |
| 140 | raw, err = io.ReadAll(os.Stdin) |
| 141 | case 1: |
| 142 | raw, err = os.ReadFile(fs.Arg(0)) |
| 143 | default: |
| 144 | return fmt.Errorf("canonical takes one json file, or none to read stdin") |
| 145 | } |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | out, err := canonicalRender(raw) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | _, err = os.Stdout.Write(out) |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | func repoRoot(dir string) (string, error) { |
| 158 | if dir == "" { |
| 159 | dir = "." |
| 160 | } |
| 161 | return gitLine(dir, "rev-parse", "--show-toplevel") |
| 162 | } |