| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | // cmdRun is the whole protocol in order: digest the subject tree, run the |
| 12 | // checks, compose a statement per check, vet each against the schema, sign |
| 13 | // each, store them at one ref keyed by the subject digest, and publish |
| 14 | // with a single atomic push. Any step failing stops the run before the |
| 15 | // next — a failed check publishes nothing, and neither does a statement |
| 16 | // the schema rejects. |
| 17 | func cmdRun(args []string) error { |
| 18 | fs := newFlagSet("run") |
| 19 | repo := fs.String("repo", "", "repository") |
| 20 | rev := fs.String("rev", "HEAD", "revision") |
| 21 | key := fs.String("key", "", "ssh private key") |
| 22 | namespace := fs.String("namespace", defaultNamespace, "SSHSIG namespace") |
| 23 | environment := fs.String("environment", "local", "environment an effectful statement names") |
| 24 | provenanceFile := fs.String("provenance", "", "json run provenance") |
| 25 | schemaFlag := fs.String("schema", "", "attestation schema") |
| 26 | system := fs.String("system", nixSystem(), "flake system") |
| 27 | push := fs.String("push", "", "remote to publish to") |
| 28 | branch := fs.String("branch", "", "branch pushed alongside") |
| 29 | var checks, commands repeatable |
| 30 | fs.Var(&checks, "check", "flake check to run") |
| 31 | fs.Var(&commands, "command", "effectful check to run") |
| 32 | if err := fs.Parse(args); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | root, err := repoRoot(*repo) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | schema, err := schemaPath(*schemaFlag, root) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | prov, err := loadProvenance(*provenanceFile) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | // The signer is checked before any work is done: a run that can only |
| 50 | // end in "no key" should say so before it builds anything. |
| 51 | if *key == "" { |
| 52 | return fmt.Errorf("no signing key: pass --key. The host signs; an unsigned statement is not an attestation") |
| 53 | } |
| 54 | signerHex, signerSSH, err := keyFingerprint(*key) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | subj, err := composeSubject(root, *rev) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | fmt.Printf("subject %s:%s\n", subj.Primary, subj.Digest[subj.Primary]) |
| 64 | for _, alg := range sortedKeys(subj.Digest) { |
| 65 | if alg != subj.Primary { |
| 66 | fmt.Printf(" %s:%s (advisory)\n", alg, subj.Digest[alg]) |
| 67 | } |
| 68 | } |
| 69 | fmt.Printf("signer %s\n", signerSSH) |
| 70 | |
| 71 | tree, err := os.MkdirTemp("", "valley-attest-tree") |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | defer os.RemoveAll(tree) |
| 76 | if err := exportTree(root, *rev, tree); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | specs, err := checkSpecs(tree, *system, checks, commands) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | if len(specs) == 0 { |
| 85 | return fmt.Errorf("no checks to run") |
| 86 | } |
| 87 | |
| 88 | outcomes := make([]checkOutcome, 0, len(specs)) |
| 89 | failed := 0 |
| 90 | for _, spec := range specs { |
| 91 | var out checkOutcome |
| 92 | if spec.runner() == "nix" { |
| 93 | out, err = runPure(tree, *system, spec) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | } else { |
| 98 | out = runEffectful(tree, spec) |
| 99 | } |
| 100 | result := "passed" |
| 101 | if !out.passed { |
| 102 | result = "failed" |
| 103 | failed++ |
| 104 | } |
| 105 | fmt.Printf("check %-24s %-8s %s\n", spec.name, spec.runner(), result) |
| 106 | outcomes = append(outcomes, out) |
| 107 | } |
| 108 | if failed > 0 { |
| 109 | for _, out := range outcomes { |
| 110 | if !out.passed && out.log != "" { |
| 111 | fmt.Fprintf(os.Stderr, "\n--- %s ---\n%s\n", out.spec.name, out.log) |
| 112 | } |
| 113 | } |
| 114 | return fmt.Errorf("%d of %d checks failed; nothing published", failed, len(specs)) |
| 115 | } |
| 116 | |
| 117 | // Compose, vet, sign. Every statement is vetted before any of them is |
| 118 | // signed, so a malformed one cannot leave a half-signed set behind. |
| 119 | canonicals := make([][]byte, len(outcomes)) |
| 120 | for i, out := range outcomes { |
| 121 | st := composeStatement(subj, out, *environment, prov) |
| 122 | canonical, err := canonicalize(st) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | if err := vetStatement(schema, canonical); err != nil { |
| 127 | return fmt.Errorf("%s: %w\nnothing published", out.spec.name, err) |
| 128 | } |
| 129 | canonicals[i] = canonical |
| 130 | } |
| 131 | |
| 132 | subtrees := map[string]string{} |
| 133 | for i, out := range outcomes { |
| 134 | sig, err := signDetached(*key, *namespace, canonicals[i]) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | stBlob, err := writeBlob(root, canonicals[i]) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | sigBlob, err := writeBlob(root, sig) |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | sub, err := mkTree(root, map[string]string{"statement.json": stBlob, "statement.sig": sigBlob}, nil) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | subtrees[out.spec.name] = sub |
| 151 | } |
| 152 | top, err := mkTree(root, nil, subtrees) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | ref := attestationRef(subj.Digest[subj.Primary], signerHex) |
| 157 | if _, err := git(root, "update-ref", ref, top); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | fmt.Printf("stored %s -> %s\n", ref, top) |
| 161 | |
| 162 | if *push == "" { |
| 163 | return nil |
| 164 | } |
| 165 | head := *branch |
| 166 | if head == "" { |
| 167 | head, err = gitLine(root, "symbolic-ref", "--short", "HEAD") |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("no branch to push: %w", err) |
| 170 | } |
| 171 | } |
| 172 | head = strings.TrimPrefix(head, "refs/heads/") |
| 173 | branchSpec := fmt.Sprintf("refs/heads/%s:refs/heads/%s", head, head) |
| 174 | refSpec := ref + ":" + ref |
| 175 | out, err := git(root, "push", "--atomic", *push, branchSpec, refSpec) |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | fmt.Printf("pushed %s --atomic %s %s\n%s", *push, branchSpec, refSpec, out) |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | // composeSubject builds the digest set: the content-addressed tree digest |
| 184 | // as the member that IS the identity, and the commit as an advisory member |
| 185 | // for finding the change. |
| 186 | func composeSubject(root, rev string) (subject, error) { |
| 187 | entries, err := gitTreeEntries(root, rev) |
| 188 | if err != nil { |
| 189 | return subject{}, err |
| 190 | } |
| 191 | commit, err := gitLine(root, "rev-parse", rev+"^{commit}") |
| 192 | if err != nil { |
| 193 | return subject{}, err |
| 194 | } |
| 195 | subj := subject{Primary: treeScheme, Digest: map[string]string{treeScheme: treeDigest(entries)}} |
| 196 | switch len(commit) { |
| 197 | case 40: |
| 198 | subj.Digest["git-sha1"] = commit |
| 199 | case 64: |
| 200 | subj.Digest["git-sha256"] = commit |
| 201 | default: |
| 202 | return subject{}, fmt.Errorf("unexpected object id %q", commit) |
| 203 | } |
| 204 | return subj, nil |
| 205 | } |
| 206 | |
| 207 | func composeStatement(subj subject, out checkOutcome, environment string, prov json.RawMessage) statement { |
| 208 | result := "failed" |
| 209 | if out.passed { |
| 210 | result = "passed" |
| 211 | } |
| 212 | st := statement{ |
| 213 | StatementType: statementType, |
| 214 | Subject: subj, |
| 215 | Provenance: prov, |
| 216 | Predicate: predicate{ |
| 217 | Check: checkRun{ |
| 218 | Name: out.spec.name, |
| 219 | Runner: out.spec.runner(), |
| 220 | Attribute: out.spec.attribute, |
| 221 | Command: out.spec.command, |
| 222 | }, |
| 223 | Result: result, |
| 224 | }, |
| 225 | } |
| 226 | if out.spec.runner() == "nix" { |
| 227 | st.PredicateType = predicatePure |
| 228 | st.Predicate.Inputs = map[string]string{inputsScheme: out.inputs} |
| 229 | st.Predicate.Derivation = map[string]string{"sha256": out.derivation} |
| 230 | st.Predicate.Output = map[string]string{treeScheme: out.output} |
| 231 | } else { |
| 232 | st.PredicateType = predicateEffect |
| 233 | st.Predicate.Environment = environment |
| 234 | st.Predicate.ObservedAt = out.observedAt |
| 235 | } |
| 236 | return st |
| 237 | } |
| 238 | |
| 239 | // checkSpecs resolves what to run. Naming no check means the repo's whole |
| 240 | // canonical set: every check the flake defines for this system. |
| 241 | func checkSpecs(tree, system string, checks, commands repeatable) ([]checkSpec, error) { |
| 242 | var specs []checkSpec |
| 243 | for _, c := range checks { |
| 244 | name, attr, found := strings.Cut(c, "=") |
| 245 | if !found { |
| 246 | attr = name |
| 247 | } |
| 248 | specs = append(specs, checkSpec{name: name, attribute: attr}) |
| 249 | } |
| 250 | for _, c := range commands { |
| 251 | name, command, found := strings.Cut(c, "=") |
| 252 | if !found { |
| 253 | return nil, fmt.Errorf("--command %q: expected NAME=COMMAND", c) |
| 254 | } |
| 255 | specs = append(specs, checkSpec{name: name, command: command}) |
| 256 | } |
| 257 | if len(specs) > 0 { |
| 258 | return specs, nil |
| 259 | } |
| 260 | names, err := flakeChecks(tree, system) |
| 261 | if err != nil { |
| 262 | return nil, err |
| 263 | } |
| 264 | for _, n := range names { |
| 265 | specs = append(specs, checkSpec{name: n, attribute: n}) |
| 266 | } |
| 267 | return specs, nil |
| 268 | } |
| 269 | |
| 270 | func sortedKeys(m map[string]string) []string { |
| 271 | keys := make([]string, 0, len(m)) |
| 272 | for k := range m { |
| 273 | keys = append(keys, k) |
| 274 | } |
| 275 | sort.Strings(keys) |
| 276 | return keys |
| 277 | } |