| 1 | package main |
| 2 | |
| 3 | // The policy bridge, where this program is coupled to cue and to the |
| 4 | // deriver: the policy cue composes, with the window a check writes down |
| 5 | // and the digest of the whole, and what the deriver makes of a diff under |
| 6 | // it. Both are pinned here rather than assumed. |
| 7 | |
| 8 | import ( |
| 9 | "the-valley/composed" |
| 10 | "the-valley/integrator/verdict" |
| 11 | |
| 12 | "context" |
| 13 | "errors" |
| 14 | "os" |
| 15 | "os/exec" |
| 16 | "path/filepath" |
| 17 | "reflect" |
| 18 | "slices" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | // derivationFloor is a floor of two classes and an `unclassified` set. A |
| 25 | // code path requires c-build and sets c-vm due without requiring it; a |
| 26 | // docs path requires c-docs; a path neither covers requires c-smoke, an |
| 27 | // effectful check. |
| 28 | const derivationFloor = `package verification |
| 29 | floor: { |
| 30 | checks: { |
| 31 | "c-build": {runner: "nix", attribute: "c-build"} |
| 32 | "c-docs": {runner: "nix", attribute: "c-docs"} |
| 33 | "c-vm": {runner: "nix", attribute: "c-vm"} |
| 34 | "c-smoke": {runner: "command", command: "true", validity: "1h"} |
| 35 | } |
| 36 | classes: { |
| 37 | code: {paths: "src/**": true, requires: "c-build": true, follows: "c-vm": true, carry: exact: true} |
| 38 | docs: {paths: "docs/**": true, requires: "c-docs": true, carry: "same-class": true} |
| 39 | } |
| 40 | unclassified: "c-smoke": true |
| 41 | } |
| 42 | ` |
| 43 | |
| 44 | // policyRig is a target repository whose policy is derivationFloor and, |
| 45 | // when given, a project layer of its own, composed by real cue under the |
| 46 | // real schema. |
| 47 | type policyRig struct { |
| 48 | *scratchRepo |
| 49 | source policySource |
| 50 | } |
| 51 | |
| 52 | func newPolicyRig(t *testing.T, project string) *policyRig { |
| 53 | t.Helper() |
| 54 | cue, err := exec.LookPath("cue") |
| 55 | if err != nil { |
| 56 | t.Skip("no cue to compose a policy with; the nix check provides one") |
| 57 | } |
| 58 | schema, err := filepath.Abs(filepath.Join("..", "schema", "verification.cue")) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | floor := t.TempDir() |
| 63 | if err := os.WriteFile(filepath.Join(floor, "floor.cue"), []byte(derivationFloor), 0o644); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | r := &policyRig{scratchRepo: newScratchRepo(t), source: policySource{ |
| 67 | schema: schema, |
| 68 | cue: cue, |
| 69 | layer: composed.Floor{Path: floor}, |
| 70 | projectDir: "policy/project", |
| 71 | }} |
| 72 | files := map[string]string{"seed": "seed\n"} |
| 73 | if project != "" { |
| 74 | files["policy/project/policy.cue"] = "package verification\n" + project |
| 75 | } |
| 76 | r.commitFiles(files) |
| 77 | return r |
| 78 | } |
| 79 | |
| 80 | // commitFiles commits files onto the current branch and returns the commit. |
| 81 | func (r *policyRig) commitFiles(files map[string]string) string { |
| 82 | r.t.Helper() |
| 83 | for path, body := range files { |
| 84 | full := filepath.Join(r.repo, path) |
| 85 | if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { |
| 86 | r.t.Fatal(err) |
| 87 | } |
| 88 | if err := os.WriteFile(full, []byte(body), 0o644); err != nil { |
| 89 | r.t.Fatal(err) |
| 90 | } |
| 91 | } |
| 92 | r.git("add", "--all") |
| 93 | r.git("commit", "--quiet", "-m", "a commit") |
| 94 | return r.git("rev-parse", "HEAD") |
| 95 | } |
| 96 | |
| 97 | // open opens the policy at rev as a request's evaluation does, and ends it |
| 98 | // with the test. |
| 99 | func (r *policyRig) open(rev string) (policySource, *worktree) { |
| 100 | r.t.Helper() |
| 101 | tip := &worktree{repo: r.repo, dir: r.repo, rev: rev} |
| 102 | p, done, err := r.source.open(tip) |
| 103 | r.t.Cleanup(done) |
| 104 | if err != nil { |
| 105 | r.t.Fatal(err) |
| 106 | } |
| 107 | return p, tip |
| 108 | } |
| 109 | |
| 110 | // derive derives from..to under the policy at from. |
| 111 | func (r *policyRig) derive(from, to string) derived { |
| 112 | r.t.Helper() |
| 113 | p, tip := r.open(from) |
| 114 | d, err := p.derive(tip, from, to) |
| 115 | if err != nil { |
| 116 | r.t.Fatal(err) |
| 117 | } |
| 118 | return d |
| 119 | } |
| 120 | |
| 121 | func classSet(names ...string) map[string]bool { |
| 122 | set := map[string]bool{} |
| 123 | for _, n := range names { |
| 124 | set[n] = true |
| 125 | } |
| 126 | return set |
| 127 | } |
| 128 | |
| 129 | // A diff is derived as the policy says: the classes its paths match, the |
| 130 | // checks those classes require with each class as its reason, the |
| 131 | // strictest carry mode among them, and no check a class only follows. |
| 132 | func TestADiffRequiresWhatItsClassesRequire(t *testing.T) { |
| 133 | r := newPolicyRig(t, "") |
| 134 | base := r.git("rev-parse", "HEAD") |
| 135 | head := r.commitFiles(map[string]string{"src/a.go": "package a\n", "src/b.go": "package b\n"}) |
| 136 | d := r.derive(base, head) |
| 137 | want := derived{paths: 2, classes: classSet("code"), requires: map[string][]string{"c-build": {"code"}}, carry: "exact"} |
| 138 | if !reflect.DeepEqual(d, want) { |
| 139 | t.Fatalf("a code change derives %+v, want %+v", d, want) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // A path no class covers is unclassified: the name is among the classes |
| 144 | // matched, and it is the reason for each check the unclassified set |
| 145 | // requires. |
| 146 | func TestAPathNoClassCoversIsUnclassified(t *testing.T) { |
| 147 | r := newPolicyRig(t, "") |
| 148 | base := r.git("rev-parse", "HEAD") |
| 149 | head := r.commitFiles(map[string]string{"docs/a.md": "a\n", "README": "r\n"}) |
| 150 | d := r.derive(base, head) |
| 151 | want := derived{ |
| 152 | paths: 2, |
| 153 | classes: classSet("docs", "unclassified"), |
| 154 | requires: map[string][]string{"c-docs": {"docs"}, "c-smoke": {"unclassified"}}, |
| 155 | carry: "same-class", |
| 156 | } |
| 157 | if !reflect.DeepEqual(d, want) { |
| 158 | t.Fatalf("a docs and unclassified change derives %+v, want %+v", d, want) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // What an intervening landing touched is the classes the deriver matches |
| 163 | // over base..tip, and an effectful observation of a check one of those |
| 164 | // classes requires does not transfer. An unclassified path is touched as |
| 165 | // the class `unclassified`, and a class the policy itself names |
| 166 | // `unclassified` folds into that one name, so each blocks an observation |
| 167 | // the other requires. An intervening docs change blocks neither. |
| 168 | func TestAnInterveningChangeBlocksWhatItsClassesRequire(t *testing.T) { |
| 169 | const odd = `project: checks: "c-odd": {runner: "command", command: "true", validity: "1h"} |
| 170 | project: classes: unclassified: {paths: "odd/**": true, requires: "c-odd": true} |
| 171 | ` |
| 172 | for _, c := range []struct { |
| 173 | name, check string |
| 174 | head, interlude string |
| 175 | blocked bool |
| 176 | }{ |
| 177 | {"an unclassified path blocks the class named unclassified", "c-odd", "odd/a", "README", true}, |
| 178 | {"the class named unclassified blocks the unclassified set", "c-smoke", "README", "odd/b", true}, |
| 179 | {"a docs path blocks neither", "c-odd", "odd/a", "docs/a.md", false}, |
| 180 | } { |
| 181 | t.Run(c.name, func(t *testing.T) { |
| 182 | r := newPolicyRig(t, odd) |
| 183 | base := r.git("rev-parse", "HEAD") |
| 184 | head := r.commitFiles(map[string]string{c.head: "head\n"}) |
| 185 | r.git("checkout", "--quiet", "-b", "target", base) |
| 186 | tip := r.commitFiles(map[string]string{c.interlude: "interlude\n"}) |
| 187 | |
| 188 | p, wt := r.open(tip) |
| 189 | candidate, err := p.derive(wt, base, head) |
| 190 | if err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | required, err := p.required(candidate) |
| 194 | if err != nil { |
| 195 | t.Fatal(err) |
| 196 | } |
| 197 | intervening, err := p.derive(wt, base, tip) |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | if !intervening.classes["unclassified"] && c.blocked { |
| 202 | t.Fatalf("base..tip touched %v, without unclassified", intervening.classes) |
| 203 | } |
| 204 | observed := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) |
| 205 | v := verdict.Decide( |
| 206 | verdict.Change{ID: "c1", Target: "refs/heads/main", Base: base, Head: head, Attested: "attested", Evidence: map[string]verdict.Evidence{ |
| 207 | c.check: {Check: c.check, Kind: verdict.Effectful, Runner: "command", Admissible: verdict.Admissible, Result: "passed", ObservedAt: observed}, |
| 208 | }}, |
| 209 | verdict.Snapshot{ |
| 210 | Apply: verdict.Applied{Clean: true, Tree: "moved"}, |
| 211 | Required: required, |
| 212 | Touched: intervening.classes, |
| 213 | Now: observed.Add(time.Minute), |
| 214 | }) |
| 215 | if len(v.Checks) != 1 || v.Checks[0].Name != c.check { |
| 216 | t.Fatalf("the change requires %+v, want %s alone", v.Checks, c.check) |
| 217 | } |
| 218 | if v.Checks[0].Transferred == c.blocked { |
| 219 | t.Fatalf("with %s intervening, %s transferred %v: %s", c.interlude, c.check, v.Checks[0].Transferred, v.Checks[0].Reason) |
| 220 | } |
| 221 | }) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // A policy that does not compose refuses the request, and leaves no |
| 226 | // materialized layer behind. |
| 227 | func TestAModelThatDoesNotReadIsRefused(t *testing.T) { |
| 228 | r := newPolicyRig(t, `project: checks: "c-build": runner: "telepathy"`+"\n") |
| 229 | tip := &worktree{repo: r.repo, dir: r.repo, rev: r.git("rev-parse", "HEAD")} |
| 230 | p, done, err := r.source.open(tip) |
| 231 | defer done() |
| 232 | if err == nil { |
| 233 | t.Fatal("a policy that does not compose was opened") |
| 234 | } |
| 235 | if p.model != nil { |
| 236 | t.Fatalf("a policy that does not compose was read as %+v", p.model) |
| 237 | } |
| 238 | if _, err := os.Stat(p.project); !os.IsNotExist(err) { |
| 239 | t.Fatalf("the refused request left its project layer behind: %v", err) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // A diff that does not read refuses the derivation, from git and from the |
| 244 | // path reader alike. |
| 245 | func TestADiffThatDoesNotReadIsRefused(t *testing.T) { |
| 246 | r := newPolicyRig(t, "") |
| 247 | base := r.git("rev-parse", "HEAD") |
| 248 | p, tip := r.open(base) |
| 249 | if d, err := p.derive(tip, base, strings.Repeat("0", 40)); err == nil { |
| 250 | t.Fatalf("a diff to a commit that is not there derived %+v", d) |
| 251 | } |
| 252 | p.diff = func(context.Context, string, []string, string, string) ([]string, error) { |
| 253 | return nil, errors.New("truncated diff output") |
| 254 | } |
| 255 | if d, err := p.derive(tip, base, base); err == nil || !strings.Contains(err.Error(), "deriving required checks for") { |
| 256 | t.Fatalf("a diff that does not read derived %+v, %v", d, err) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // A diff with nothing in it requires nothing. |
| 261 | func TestAnEmptyDiffRequiresNothing(t *testing.T) { |
| 262 | repo := newScratchRepo(t) |
| 263 | one := repo.git("rev-parse", "HEAD") |
| 264 | repo.git("commit", "--quiet", "--allow-empty", "-m", "the same tree") |
| 265 | same := repo.git("rev-parse", "HEAD") |
| 266 | p := policySource{model: &composed.Model{}} |
| 267 | d, err := p.derive(&worktree{dir: repo.repo}, one, same) |
| 268 | if err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | if d.paths != 0 || len(d.classes) != 0 || len(d.requires) != 0 || d.carry != "same-paths" { |
| 272 | t.Fatalf("an empty diff derives %+v", d) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // No changed paths are believed only when git agrees. The path reader |
| 277 | // reads none, as one that failed silently would, and the derivation is |
| 278 | // read over two commits with one tree, and refused over two commits whose |
| 279 | // trees differ. |
| 280 | func TestNoChangedPathsAreCheckedAgainstGit(t *testing.T) { |
| 281 | repo := newScratchRepo(t) |
| 282 | one := repo.git("rev-parse", "HEAD") |
| 283 | repo.git("commit", "--quiet", "--allow-empty", "-m", "the same tree") |
| 284 | same := repo.git("rev-parse", "HEAD") |
| 285 | if err := os.WriteFile(filepath.Join(repo.repo, "changed.txt"), []byte("x\n"), 0o644); err != nil { |
| 286 | t.Fatal(err) |
| 287 | } |
| 288 | repo.git("add", "changed.txt") |
| 289 | repo.git("commit", "--quiet", "-m", "a change") |
| 290 | changed := repo.git("rev-parse", "HEAD") |
| 291 | |
| 292 | p := policySource{model: &composed.Model{}, diff: func(context.Context, string, []string, string, string) ([]string, error) { |
| 293 | return nil, nil |
| 294 | }} |
| 295 | tip := &worktree{dir: repo.repo} |
| 296 | if _, err := p.derive(tip, one, same); err != nil { |
| 297 | t.Fatalf("an empty diff read as empty was refused: %v", err) |
| 298 | } |
| 299 | if d, err := p.derive(tip, one, changed); err == nil { |
| 300 | t.Fatalf("a change read as touching nothing derived %+v", d) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // The landing records the digest of the model the request was derived |
| 305 | // under. attest is a stand-in here: it digests the head's tree, which this |
| 306 | // test does not read. |
| 307 | func TestTheLandingRecordsTheModelsDigest(t *testing.T) { |
| 308 | r := newPolicyRig(t, "") |
| 309 | tip := r.git("rev-parse", "HEAD") |
| 310 | head := r.commitFiles(map[string]string{"src/a.go": "package a\n"}) |
| 311 | attest := filepath.Join(t.TempDir(), "attest") |
| 312 | if err := os.WriteFile(attest, []byte("#!/bin/sh\necho valley-tree-v1:"+strings.Repeat("ab", 32)+"\n"), 0o755); err != nil { |
| 313 | t.Fatal(err) |
| 314 | } |
| 315 | in := &integrator{repo: r.repo, attest: attest, signerName: "integrator", policy: r.source} |
| 316 | _, snap, land, err := in.materialize(request{change: "c1", target: "refs/heads/main", head: head}, tip, r.source.layer.Path, newStopwatch()) |
| 317 | if err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | p, _ := r.open(tip) |
| 321 | if land.policy == "" || land.policy != p.model.Digest { |
| 322 | t.Fatalf("the landing records policy %q, and the model's digest is %q", land.policy, p.model.Digest) |
| 323 | } |
| 324 | if !slices.ContainsFunc(snap.Required, func(q verdict.Required) bool { return q.Name == "c-build" }) { |
| 325 | t.Fatalf("the landing was judged over %+v", snap.Required) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // The policy digest is of the whole composed policy, so a class's carry |
| 330 | // mode, the restacker's cap and the approval requirements are in it |
| 331 | // as much as a check is. The layers are composed by the real cue under the |
| 332 | // real schema, with the floor fixed and the project layer varied. |
| 333 | func TestThePolicyDigestCoversCarryTheCapAndApproval(t *testing.T) { |
| 334 | cue, err := exec.LookPath("cue") |
| 335 | if err != nil { |
| 336 | t.Skip("no cue to compose a policy with; the nix check provides one") |
| 337 | } |
| 338 | schema, err := filepath.Abs(filepath.Join("..", "schema", "verification.cue")) |
| 339 | if err != nil { |
| 340 | t.Fatal(err) |
| 341 | } |
| 342 | const floor = `package verification |
| 343 | floor: { |
| 344 | checks: "c-build": {runner: "nix", attribute: "c-build"} |
| 345 | classes: code: {paths: "src/**": true, requires: "c-build": true} |
| 346 | } |
| 347 | ` |
| 348 | digest := func(project string) string { |
| 349 | t.Helper() |
| 350 | p := policySource{schema: schema, cue: cue, instance: t.TempDir(), project: t.TempDir()} |
| 351 | if err := os.WriteFile(filepath.Join(p.instance, "floor.cue"), []byte(floor), 0o644); err != nil { |
| 352 | t.Fatal(err) |
| 353 | } |
| 354 | if err := os.WriteFile(filepath.Join(p.project, "policy.cue"), []byte("package verification\n"+project), 0o644); err != nil { |
| 355 | t.Fatal(err) |
| 356 | } |
| 357 | if err := p.read(); err != nil { |
| 358 | t.Fatal(err) |
| 359 | } |
| 360 | return p.model.Digest |
| 361 | } |
| 362 | |
| 363 | base := digest("") |
| 364 | if again := digest(""); again != base { |
| 365 | t.Fatalf("one policy digested twice gave %s and %s", base, again) |
| 366 | } |
| 367 | // The digest is of the composition, not of the text: writing the cap's |
| 368 | // default composes the same policy. |
| 369 | if same := digest("project: restack: maxAutomaticVersions: 64\n"); same != base { |
| 370 | t.Errorf("writing the default cap moved the digest from %s to %s", base, same) |
| 371 | } |
| 372 | seen := map[string]string{base: "the floor alone"} |
| 373 | for _, c := range []struct{ name, project string }{ |
| 374 | {"a class asking for same-class", `project: classes: code: carry: "same-class": true` + "\n"}, |
| 375 | {"a class asking for exact", "project: classes: code: carry: exact: true\n"}, |
| 376 | {"a lower cap", "project: restack: maxAutomaticVersions: 16\n"}, |
| 377 | // The schema puts no upper bound on the cap. The integrator does |
| 378 | // not read the cap, so it composes and digests one no Go int holds. |
| 379 | {"a cap no int holds", "project: restack: maxAutomaticVersions: 9223372036854775808\n"}, |
| 380 | {"a hardware approval requirement", "project: approval: hardware: true\n"}, |
| 381 | // Only a field left out is absent from the export. Written out, |
| 382 | // false is in it as much as true. |
| 383 | {"an approval requirement", "project: approval: required: true\n"}, |
| 384 | {"an approval requirement written false", "project: approval: required: false\n"}, |
| 385 | } { |
| 386 | sum := digest(c.project) |
| 387 | if other, ok := seen[sum]; ok { |
| 388 | t.Errorf("%s digests the same as %s: %s", c.name, other, sum) |
| 389 | } |
| 390 | seen[sum] = c.name |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | func TestValidityWindowsAreReadAsThePolicyWritesThem(t *testing.T) { |
| 395 | for _, c := range []struct { |
| 396 | in string |
| 397 | want time.Duration |
| 398 | }{ |
| 399 | {"", 0}, |
| 400 | {"30s", 30 * time.Second}, |
| 401 | {"15m", 15 * time.Minute}, |
| 402 | {"6h", 6 * time.Hour}, |
| 403 | {"2d", 48 * time.Hour}, |
| 404 | } { |
| 405 | got, err := parseWindow(c.in) |
| 406 | if err != nil || got != c.want { |
| 407 | t.Errorf("parseWindow(%q) = %v, %v; want %v", c.in, got, err, c.want) |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Each layer's absence defaults in the safe direction of that layer's job, |
| 413 | // and the two directions are opposite (dcr-f41f718). Both are pinned here, |
| 414 | // because the value of the rule is that neither is the other. |
| 415 | |
| 416 | // targetTip commits files onto a scratch repository's main and returns |
| 417 | // the tip as a pass checks it out. |
| 418 | func targetTip(t *testing.T, files map[string]string) (*scratchRepo, *worktree) { |
| 419 | t.Helper() |
| 420 | s := newScratchRepo(t) |
| 421 | for path, body := range files { |
| 422 | full := filepath.Join(s.repo, path) |
| 423 | if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { |
| 424 | t.Fatal(err) |
| 425 | } |
| 426 | if err := os.WriteFile(full, []byte(body), 0o644); err != nil { |
| 427 | t.Fatal(err) |
| 428 | } |
| 429 | } |
| 430 | s.git("add", "--all") |
| 431 | s.git("commit", "--quiet", "--allow-empty", "-m", "the target") |
| 432 | return s, &worktree{repo: s.repo, dir: s.repo, rev: s.git("rev-parse", "HEAD")} |
| 433 | } |
| 434 | |
| 435 | func TestATargetWithNoProjectLayerIsJudgedUnderTheFloorAlone(t *testing.T) { |
| 436 | _, tip := targetTip(t, map[string]string{"policy/project/notes.md": "not a document\n"}) |
| 437 | p := policySource{projectDir: "policy/project"} |
| 438 | done, err := p.openProject(tip) |
| 439 | if err != nil { |
| 440 | t.Fatal(err) |
| 441 | } |
| 442 | defer done() |
| 443 | if p.note == "" { |
| 444 | t.Fatal("the pass said nothing about composing the floor alone") |
| 445 | } |
| 446 | // What the composer is handed is a layer that adds nothing, not a directory that is not there. |
| 447 | docs, err := filepath.Glob(filepath.Join(p.project, "*.cue")) |
| 448 | if err != nil || len(docs) != 1 { |
| 449 | t.Fatalf("the resolved project layer holds %v (%v)", docs, err) |
| 450 | } |
| 451 | if body, err := os.ReadFile(docs[0]); err != nil || strings.TrimSpace(string(body)) != "package verification" { |
| 452 | t.Fatalf("the layer that adds nothing reads %q (%v)", body, err) |
| 453 | } |
| 454 | done() |
| 455 | if _, err := os.Stat(p.project); !os.IsNotExist(err) { |
| 456 | t.Fatalf("the pass left its scratch layer behind: %v", err) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // The project layer is read from the tip's commit, and its directory is a |
| 461 | // name, never a pattern: --project-policy policy/proj* reads that |
| 462 | // directory, and not policy/projX beside it. |
| 463 | func TestATargetsOwnProjectLayerIsReadAtTheTip(t *testing.T) { |
| 464 | for _, dir := range []string{"policy/project", "policy/proj*"} { |
| 465 | t.Run(dir, func(t *testing.T) { |
| 466 | _, tip := targetTip(t, map[string]string{ |
| 467 | dir + "/policy.cue": "package verification // own\n", |
| 468 | "policy/projX/decoy.cue": "package verification // decoy\n", |
| 469 | }) |
| 470 | p := policySource{projectDir: dir} |
| 471 | done, err := p.openProject(tip) |
| 472 | if err != nil { |
| 473 | t.Fatal(err) |
| 474 | } |
| 475 | defer done() |
| 476 | if p.note != "" { |
| 477 | t.Fatalf("an ordinary composition said %q", p.note) |
| 478 | } |
| 479 | entries, err := os.ReadDir(p.project) |
| 480 | if err != nil || len(entries) != 1 || entries[0].Name() != "policy.cue" { |
| 481 | t.Fatalf("the project layer resolved to %s, holding %v (%v)", p.project, entries, err) |
| 482 | } |
| 483 | if body, _ := os.ReadFile(filepath.Join(p.project, "policy.cue")); string(body) != "package verification // own\n" { |
| 484 | t.Fatalf("the project layer reads %q", body) |
| 485 | } |
| 486 | }) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // A project layer that cannot be read refuses the pass. Read as absent, it |
| 491 | // would judge the change under the floor alone, requiring fewer checks |
| 492 | // from a read that never happened. |
| 493 | func TestAProjectLayerThatCannotBeReadIsRefused(t *testing.T) { |
| 494 | for _, lost := range []string{"policy/project", "policy/project/policy.cue"} { |
| 495 | t.Run(lost, func(t *testing.T) { |
| 496 | s, tip := targetTip(t, map[string]string{"policy/project/policy.cue": "package verification\n"}) |
| 497 | oid := s.git("rev-parse", tip.rev+":"+lost) |
| 498 | if err := os.Remove(filepath.Join(s.repo, ".git", "objects", oid[:2], oid[2:])); err != nil { |
| 499 | t.Fatal(err) |
| 500 | } |
| 501 | p := policySource{projectDir: "policy/project"} |
| 502 | done, err := p.openProject(tip) |
| 503 | defer done() |
| 504 | if err == nil { |
| 505 | t.Fatalf("an unreadable project layer resolved to %s, noting %q", p.project, p.note) |
| 506 | } |
| 507 | if !strings.Contains(err.Error(), "policy/project") { |
| 508 | t.Fatalf("the refusal reads %q", err) |
| 509 | } |
| 510 | }) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | func TestAFloorThatIsNotThereIsRefused(t *testing.T) { |
| 515 | // The materialized source, which is the one a test can drive without a |
| 516 | // repository. The repository source refuses the same way, and the e2e |
| 517 | // observes it there. |
| 518 | _, done, err := composed.Floor{Path: t.TempDir()}.Open(context.Background(), "valley-integrator-floor") |
| 519 | defer done() |
| 520 | if err == nil { |
| 521 | t.Fatal("a floor directory with no documents in it was accepted") |
| 522 | } |
| 523 | if !strings.Contains(err.Error(), composed.Floorless) { |
| 524 | t.Fatalf("the refusal reads %q", err) |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // The floor is every *.cue document directly in its directory, whatever |
| 529 | // its name. Listed one name a line, git quotes a name with non-ASCII |
| 530 | // characters in it, and the quoted name does not end in .cue, so a floor |
| 531 | // read that way skipped naïve.cue. The repository sets core.quotePath to |
| 532 | // the default that quotes, and the document is composed into the model |
| 533 | // like any other. |
| 534 | func TestAFloorDocumentWithANonASCIINameIsRead(t *testing.T) { |
| 535 | cue, err := exec.LookPath("cue") |
| 536 | if err != nil { |
| 537 | t.Skip("no cue to compose a policy with; the nix check provides one") |
| 538 | } |
| 539 | schema, err := filepath.Abs(filepath.Join("..", "schema", "verification.cue")) |
| 540 | if err != nil { |
| 541 | t.Fatal(err) |
| 542 | } |
| 543 | s := newScratchRepo(t) |
| 544 | s.git("config", "core.quotePath", "true") |
| 545 | dir := filepath.Join(s.repo, "policy", "instance") |
| 546 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 547 | t.Fatal(err) |
| 548 | } |
| 549 | for name, body := range map[string]string{ |
| 550 | "floor.cue": `package verification |
| 551 | floor: { |
| 552 | checks: "c-build": {runner: "nix", attribute: "c-build"} |
| 553 | classes: code: {paths: "src/**": true, requires: "c-build": true} |
| 554 | } |
| 555 | `, |
| 556 | "naïve.cue": `package verification |
| 557 | floor: checks: "c-lint": {runner: "nix", attribute: "c-lint"} |
| 558 | `, |
| 559 | } { |
| 560 | if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil { |
| 561 | t.Fatal(err) |
| 562 | } |
| 563 | } |
| 564 | s.git("add", "policy") |
| 565 | s.git("commit", "--quiet", "-m", "the floor") |
| 566 | |
| 567 | p := policySource{ |
| 568 | schema: schema, |
| 569 | cue: cue, |
| 570 | layer: composed.Floor{Repo: s.repo, Ref: "refs/heads/main", Dir: "policy/instance"}, |
| 571 | projectDir: "policy/project", |
| 572 | } |
| 573 | p, done, err := p.open(&worktree{repo: s.repo, dir: s.repo, rev: s.git("rev-parse", "HEAD")}) |
| 574 | defer done() |
| 575 | if err != nil { |
| 576 | t.Fatal(err) |
| 577 | } |
| 578 | cat := p.model.Checks |
| 579 | if _, ok := cat["c-lint"]; !ok { |
| 580 | t.Fatalf("the floor's naïve.cue was not read: the model defines %v", cat) |
| 581 | } |
| 582 | if _, ok := cat["c-build"]; !ok { |
| 583 | t.Fatalf("the floor's floor.cue was not read: the model defines %v", cat) |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | func TestARefusalIsReadAsTheSentenceItIs(t *testing.T) { |
| 588 | // attest reports what it checked, then refuses, then elaborates. The |
| 589 | // sentence is the refusal; the elaboration is not. |
| 590 | out := `note /tmp/x.note |
| 591 | text ok (411 bytes signed) |
| 592 | attest: tree digest mismatch: the statement is about a different tree |
| 593 | recorded aaaa |
| 594 | found bbbb |
| 595 | ` |
| 596 | if got := refusalLine(out); got != "tree digest mismatch: the statement is about a different tree" { |
| 597 | t.Fatalf("refusalLine = %q", got) |
| 598 | } |
| 599 | if got := refusalLine("something with no prefix at all\n"); got != "something with no prefix at all" { |
| 600 | t.Fatalf("refusalLine fallback = %q", got) |
| 601 | } |
| 602 | } |