| 1 | // Command flow-policy holds the project policy's classification of the |
| 2 | // paths around klaus (flow/): what a change to one path requires, as `valley |
| 3 | // checks --names` derives it from the policy composed over a floor. |
| 4 | // |
| 5 | // It lays the policy out as an instance and a project layer, seeds one git |
| 6 | // repository with every path below, and for each path commits a change to |
| 7 | // it alone and compares the checks the change requires with what the path |
| 8 | // must, or must not, require. Any mismatch is printed and exits 1. |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "flag" |
| 13 | "fmt" |
| 14 | "os" |
| 15 | "os/exec" |
| 16 | "path/filepath" |
| 17 | "slices" |
| 18 | "strings" |
| 19 | ) |
| 20 | |
| 21 | var flowChecks = []string{"flow-build", "flow-e2e", "flow-unit", "flow-vet"} |
| 22 | |
| 23 | // policyChecks are what a change to the policy requires: its own |
| 24 | // verification. |
| 25 | var policyChecks = []string{"cue-vet", "derive-unit", "own-policy", "policy-deriver"} |
| 26 | |
| 27 | type scenario struct { |
| 28 | path string |
| 29 | exactly []string // when set, the change requires these and nothing else |
| 30 | requires []string |
| 31 | excludes []string |
| 32 | } |
| 33 | |
| 34 | func scenarios() []scenario { |
| 35 | s := []scenario{ |
| 36 | // klaus's Go, module files and fixtures: its four checks, and for |
| 37 | // what is Go, the dependency boundary and its Darwin build. |
| 38 | {path: "flow/internal/cmd/root.go", exactly: append([]string{"go-dependencies", "valley-darwin-build"}, flowChecks...)}, |
| 39 | {path: "flow/go.mod", exactly: append([]string{"go-dependencies", "valley-darwin-build"}, flowChecks...)}, |
| 40 | {path: "flow/go.sum", exactly: append([]string{"go-dependencies", "valley-darwin-build"}, flowChecks...)}, |
| 41 | {path: "flow/internal/scan/testdata/log.txt", exactly: flowChecks}, |
| 42 | // klaus's prose, which none of them reads: the floor's prose check |
| 43 | // alone. |
| 44 | {path: "flow/docs/guide.md", exactly: []string{"prose-format"}}, |
| 45 | {path: "flow/README.md", exactly: []string{"prose-format"}}, |
| 46 | // Their definition, and the packaging and inputs the checks share, |
| 47 | // which this check reads too, and which keep the checks of their |
| 48 | // other classes. |
| 49 | {path: "nix/checks/flow.nix", requires: flowChecks, excludes: []string{"flow-policy"}}, |
| 50 | {path: "nix/packages.nix", requires: append([]string{"flow-policy", "valley-darwin-eval", "web-reader-browser-e2e", "web-push-e2e"}, flowChecks...)}, |
| 51 | {path: "nix/checks.nix", requires: append([]string{"flow-policy", "module-eval"}, flowChecks...)}, |
| 52 | {path: "flake.nix", requires: append([]string{"flow-policy", "module-eval"}, flowChecks...)}, |
| 53 | {path: "flake.lock", requires: append([]string{"flow-policy", "module-eval"}, flowChecks...)}, |
| 54 | // The checks' helper: the three checks that run it, not go vet. |
| 55 | {path: "nix/checks/flow-checker/flow-checker.go", requires: []string{"flow-build", "flow-unit", "flow-e2e"}, excludes: []string{"flow-vet"}}, |
| 56 | // An unrelated module requires none of them. |
| 57 | {path: "sigverify/keys.go", requires: []string{"sigverify-unit"}, excludes: flowChecks}, |
| 58 | // The policy requires its own verification, and this check, which |
| 59 | // also reads the example floor and docs template, and no other |
| 60 | // example floor. |
| 61 | {path: "policy/project/policy.cue", requires: append([]string{"flow-policy"}, policyChecks...)}, |
| 62 | {path: "examples/policy/instance/floor-format.cue", requires: []string{"flow-policy"}}, |
| 63 | {path: "examples/policy/instance/floor-other.cue", excludes: []string{"flow-policy"}}, |
| 64 | // This check's program requires this check. |
| 65 | {path: "nix/checks/flow-policy-checker/flow-policy.go", requires: []string{"flow-policy"}}, |
| 66 | {path: "nix/checks/flow-policy.nix", requires: []string{"flow-policy"}}, |
| 67 | } |
| 68 | // The valley command the end-to-end suite runs and this check derives |
| 69 | // with: its own source, the modules it builds with, its help texts, the |
| 70 | // PIV signer, the delta vectors, the attestation helper on its PATH, and |
| 71 | // the schemas they carry. Each requires flow-e2e and flow-policy, and |
| 72 | // none of klaus's other checks. |
| 73 | for _, p := range []string{ |
| 74 | "status/command.go", |
| 75 | "status/help/usage.txt", |
| 76 | "approval/approval.go", |
| 77 | "approval/piv/valley-piv-signer.c", |
| 78 | "attest/main.go", |
| 79 | "change/change.go", |
| 80 | "composed/composed.go", |
| 81 | "delta/delta.go", |
| 82 | "delta/testdata/vectors.py", |
| 83 | "delta/testdata/valid/empty.delta", |
| 84 | "derive/derive.go", |
| 85 | "knot/knot.go", |
| 86 | "knot/cmd/knotcheck/main.go", |
| 87 | "note/note.go", |
| 88 | "note/statement/statement.go", |
| 89 | "rebase/rebase.go", |
| 90 | "selfupdate/selfupdate.go", |
| 91 | "telemetry/telemetry.go", |
| 92 | "telemetry/valley-telemetry/main.go", |
| 93 | "schema/verification.cue", |
| 94 | "schema/attestation.cue", |
| 95 | } { |
| 96 | s = append(s, scenario{path: p, requires: []string{"flow-e2e", "flow-policy"}, excludes: []string{"flow-build", "flow-unit", "flow-vet"}}) |
| 97 | } |
| 98 | // Files beside them that the valley command's build leaves out: the |
| 99 | // approval command and its fixtures, the plan's golden files, the |
| 100 | // attestation vectors, and prose. |
| 101 | for _, p := range []string{ |
| 102 | "approval/cmd/approval/main.go", |
| 103 | "approval/testdata/statement.json", |
| 104 | "status/testdata/plan/one.txt", |
| 105 | "attest/conformance/vectors.json", |
| 106 | "delta/testdata/README.md", |
| 107 | "status/notes.md", |
| 108 | } { |
| 109 | s = append(s, scenario{path: p, excludes: []string{"flow-e2e", "flow-policy"}}) |
| 110 | } |
| 111 | return s |
| 112 | } |
| 113 | |
| 114 | func main() { |
| 115 | schema := flag.String("schema", "", "the verification schema") |
| 116 | floor := flag.String("floor", "", "the instance floor") |
| 117 | template := flag.String("template", "", "an instance template composed beside the floor") |
| 118 | project := flag.String("project", "", "the project layer's directory") |
| 119 | flag.Parse() |
| 120 | failed, err := run(*schema, *floor, *template, *project) |
| 121 | if err != nil { |
| 122 | fmt.Fprintln(os.Stderr, "flow-policy:", err) |
| 123 | os.Exit(2) |
| 124 | } |
| 125 | if failed > 0 { |
| 126 | fmt.Fprintf(os.Stderr, "flow-policy: %d paths classified wrongly\n", failed) |
| 127 | os.Exit(1) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func run(schema, floor, template, project string) (int, error) { |
| 132 | tmp, err := os.MkdirTemp("", "flow-policy") |
| 133 | if err != nil { |
| 134 | return 0, err |
| 135 | } |
| 136 | defer os.RemoveAll(tmp) |
| 137 | instance, layer := filepath.Join(tmp, "policy", "instance"), filepath.Join(tmp, "policy", "project") |
| 138 | for _, c := range []struct{ src, dst string }{{floor, filepath.Join(instance, "floor.cue")}, {template, filepath.Join(instance, "template.cue")}} { |
| 139 | if err := copyFile(c.src, c.dst); err != nil { |
| 140 | return 0, err |
| 141 | } |
| 142 | } |
| 143 | cues, err := filepath.Glob(filepath.Join(project, "*.cue")) |
| 144 | if err != nil || len(cues) == 0 { |
| 145 | return 0, fmt.Errorf("no project layer in %s", project) |
| 146 | } |
| 147 | for _, c := range cues { |
| 148 | if err := copyFile(c, filepath.Join(layer, filepath.Base(c))); err != nil { |
| 149 | return 0, err |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | repo := filepath.Join(tmp, "tree") |
| 154 | env := append(os.Environ(), "HOME="+tmp, "GIT_CONFIG_NOSYSTEM=1", |
| 155 | "GIT_AUTHOR_NAME=valley-check", "GIT_AUTHOR_EMAIL=valley-check@localhost", |
| 156 | "GIT_COMMITTER_NAME=valley-check", "GIT_COMMITTER_EMAIL=valley-check@localhost") |
| 157 | run := func(name string, args ...string) (string, error) { |
| 158 | cmd := exec.Command(name, args...) |
| 159 | cmd.Dir, cmd.Env = repo, env |
| 160 | out, err := cmd.CombinedOutput() |
| 161 | if err != nil { |
| 162 | return "", fmt.Errorf("%s %s: %v\n%s", name, strings.Join(args, " "), err, out) |
| 163 | } |
| 164 | return string(out), nil |
| 165 | } |
| 166 | if err := os.MkdirAll(repo, 0o755); err != nil { |
| 167 | return 0, err |
| 168 | } |
| 169 | if _, err := run("git", "init", "-q", "-b", "main"); err != nil { |
| 170 | return 0, err |
| 171 | } |
| 172 | all := scenarios() |
| 173 | for _, s := range all { |
| 174 | if err := write(filepath.Join(repo, s.path), "seed\n"); err != nil { |
| 175 | return 0, err |
| 176 | } |
| 177 | } |
| 178 | if _, err := run("git", "add", "-A"); err != nil { |
| 179 | return 0, err |
| 180 | } |
| 181 | if _, err := run("git", "commit", "-q", "-m", "base"); err != nil { |
| 182 | return 0, err |
| 183 | } |
| 184 | |
| 185 | failed := 0 |
| 186 | for _, s := range all { |
| 187 | if _, err := run("git", "checkout", "-q", "-B", "change", "main"); err != nil { |
| 188 | return 0, err |
| 189 | } |
| 190 | if err := write(filepath.Join(repo, s.path), "edit\n"); err != nil { |
| 191 | return 0, err |
| 192 | } |
| 193 | if _, err := run("git", "commit", "-q", "-am", s.path); err != nil { |
| 194 | return 0, err |
| 195 | } |
| 196 | out, err := run("valley", "checks", "--schema", schema, "--instance", instance, "--project", layer, "--names", "main", "HEAD") |
| 197 | if err != nil { |
| 198 | return 0, err |
| 199 | } |
| 200 | got := strings.Fields(out) |
| 201 | slices.Sort(got) |
| 202 | if problem := s.judge(got); problem != "" { |
| 203 | fmt.Printf("✗ %s: %s; requires %s\n", s.path, problem, strings.Join(got, " ")) |
| 204 | failed++ |
| 205 | continue |
| 206 | } |
| 207 | fmt.Printf("✓ %s: %s\n", s.path, strings.Join(got, " ")) |
| 208 | } |
| 209 | return failed, nil |
| 210 | } |
| 211 | |
| 212 | // judge says how got, the sorted checks a change to s.path requires, |
| 213 | // differs from what s demands, or "". |
| 214 | func (s scenario) judge(got []string) string { |
| 215 | if s.exactly != nil { |
| 216 | want := slices.Clone(s.exactly) |
| 217 | slices.Sort(want) |
| 218 | if !slices.Equal(got, want) { |
| 219 | return "want exactly " + strings.Join(want, " ") |
| 220 | } |
| 221 | } |
| 222 | var missing, extra []string |
| 223 | for _, c := range s.requires { |
| 224 | if !slices.Contains(got, c) { |
| 225 | missing = append(missing, c) |
| 226 | } |
| 227 | } |
| 228 | for _, c := range s.excludes { |
| 229 | if slices.Contains(got, c) { |
| 230 | extra = append(extra, c) |
| 231 | } |
| 232 | } |
| 233 | var problems []string |
| 234 | if len(missing) > 0 { |
| 235 | problems = append(problems, "missing "+strings.Join(missing, " ")) |
| 236 | } |
| 237 | if len(extra) > 0 { |
| 238 | problems = append(problems, "must not require "+strings.Join(extra, " ")) |
| 239 | } |
| 240 | return strings.Join(problems, "; ") |
| 241 | } |
| 242 | |
| 243 | func write(path, content string) error { |
| 244 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 245 | return err |
| 246 | } |
| 247 | return os.WriteFile(path, []byte(content), 0o644) |
| 248 | } |
| 249 | |
| 250 | func copyFile(src, dst string) error { |
| 251 | b, err := os.ReadFile(src) |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
| 255 | return write(dst, string(b)) |
| 256 | } |