gunk-dev/the-valley

flow-policy.go

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