gunk-dev/the-valley

main_test.go

source8c40ee5f164cmain's published tip history
10.3 KiB
1package main
2
3import (
4 "bytes"
5 "context"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "sort"
10 "strings"
11 "testing"
12 "time"
13)
14
15// The command runs over a real valley: bare repositories made with real
16// git, read under the real schemas with the real cue.
17
18const (
19 idApp = "9da6809f038df92138c131cd16bed76d"
20 idSite = "273d5668702918ea7ae079de374db96b"
21 idValley = "f83927d5a4ed068cdd02a8cdaa09f26d"
22 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"
23)
24
25func needTool(t *testing.T, name string) {
26 t.Helper()
27 if _, err := exec.LookPath(name); err != nil {
28 if os.Getenv("VALLEY_TEST_TOOLS") == "required" {
29 t.Fatalf("no %s on PATH, and VALLEY_TEST_TOOLS=required", name)
30 }
31 t.Skipf("no %s on PATH; the nix check provides one", name)
32 }
33}
34
35func hostGit(t *testing.T, stdin string, args ...string) string {
36 t.Helper()
37 cmd := exec.Command("git", args...)
38 cmd.Stdin = strings.NewReader(stdin)
39 out, err := cmd.Output()
40 if err != nil {
41 t.Fatalf("git %s: %v", strings.Join(args, " "), err)
42 }
43 return strings.TrimSpace(string(out))
44}
45
46// tree writes a tree holding the files, by path, into repo.
47func tree(t *testing.T, repo string, files map[string]string) string {
48 t.Helper()
49 dirs := map[string]map[string]string{}
50 var listing []string
51 for path, body := range files {
52 if first, rest, nested := strings.Cut(path, "/"); nested {
53 if dirs[first] == nil {
54 dirs[first] = map[string]string{}
55 }
56 dirs[first][rest] = body
57 continue
58 }
59 listing = append(listing, "100644 blob "+hostGit(t, body, "-C", repo, "hash-object", "-w", "--stdin")+"\t"+path)
60 }
61 for name, sub := range dirs {
62 listing = append(listing, "040000 tree "+tree(t, repo, sub)+"\t"+name)
63 }
64 sort.Strings(listing)
65 return hostGit(t, strings.Join(listing, "\n")+"\n", "-C", repo, "mktree")
66}
67
68// project makes a bare repository beside the others, with a main holding
69// the files unless files is nil.
70func project(t *testing.T, srv, name string, files map[string]string) string {
71 t.Helper()
72 repo := filepath.Join(srv, name+".git")
73 hostGit(t, "", "init", "--quiet", "--bare", repo)
74 if files != nil {
75 commit := hostGit(t, "", "-C", repo, "-c", "user.name=test", "-c", "user.email=test@example.org",
76 "commit-tree", tree(t, repo, files), "-m", name)
77 hostGit(t, "", "-C", repo, "update-ref", "refs/heads/main", commit)
78 }
79 return repo
80}
81
82func policy(name, identity, readers string) map[string]string {
83 return map[string]string{"policy/read/read.cue": "package read\n\nproject: \"" + name +
84 "\"\nidentity: \"" + identity + "\"\nreaders: {" + readers + "}\n"}
85}
86
87// registry is the instance's identity registry: the operator governs it
88// and reads every project, and anyone reads the projects in scope.
89func registry(scope string) string {
90 return `package identity
91
92boundaries: {
93 registry: kind: "registry"
94 web: {kind: "edge-read", destination: "example.org"}
95}
96genesis: "operator"
97principals: operator: {
98 kind: "human"
99 keys: [{class: "ssh-ed25519", bound: "hardware", public: "` + key + `"}]
100 grants: {
101 govern: boundary: "registry"
102 read: {boundary: "web", projects: "all"}
103 }
104}
105anyone: grants: read: {boundary: "web", projects: ` + scope + `}
106`
107}
108
109// coverageValley makes a valley in a new directory: the instance repository
110// valley, whose registry on main lets anyone read app and site, and the
111// projects app, site, docs and fresh beside it. It returns the instance
112// repository and the schemas' directory.
113func coverageValley(t *testing.T) (string, string) {
114 t.Helper()
115 needTool(t, "git")
116 needTool(t, "cue")
117 root := t.TempDir()
118 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
119 t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(root, "gitconfig"))
120 t.Setenv("HOME", root)
121 schemas, err := filepath.Abs(filepath.Join("..", "..", "..", "schema"))
122 if err != nil {
123 t.Fatal(err)
124 }
125 srv := filepath.Join(root, "srv")
126
127 instance := policy("valley", idValley, "operator: true")
128 instance["identity/registry.cue"] = registry(`{app: "` + idApp + `", site: "` + idSite + `"}`)
129 valley := project(t, srv, "valley", instance)
130 project(t, srv, "app", policy("app", idApp, "anyone: true, operator: true"))
131 project(t, srv, "site", policy("site", idSite, "anyone: true"))
132 project(t, srv, "docs", map[string]string{"README.md": "# docs\n"})
133 project(t, srv, "fresh", nil)
134 list := hostGit(t, "app\ndocs\nfresh\nsite\nvalley\n", "-C", valley, "hash-object", "-w", "--stdin")
135 hostGit(t, "", "-C", valley, "update-ref", "refs/the-valley/projects", list)
136 return valley, schemas
137}
138
139// report runs the command over the valley for the reader, with the flags
140// given after the others, and fails the test unless it exits 0.
141func report(t *testing.T, valley, schemas, reader string, more ...string) (stdout, stderr string) {
142 t.Helper()
143 var out, errs bytes.Buffer
144 code := run(context.Background(), append([]string{"--instance-repo", valley, "--destination", "example.org",
145 "--reader", reader, "--read-schema", filepath.Join(schemas, "read.cue"),
146 "--identity-schema", filepath.Join(schemas, "identity.cue")}, more...), &out, &errs, time.Now())
147 if code != 0 {
148 t.Fatalf("%s: exited %d: %s", reader, code, errs.String())
149 }
150 return out.String(), errs.String()
151}
152
153func sameReport(t *testing.T, what, stdout string, want []string) {
154 t.Helper()
155 if got := strings.Split(strings.TrimSpace(stdout), "\n"); strings.Join(got, "\n") != strings.Join(want, "\n") {
156 t.Errorf("%s: the report is\n%s\nwant\n%s", what, strings.Join(got, "\n"), strings.Join(want, "\n"))
157 }
158}
159
160// The report names each project a reader lacks a consent for, and which:
161// a project with no main, no policy/read, or one that does not name the
162// reader lacks its own consent; a project no grant covers lacks the
163// instance's.
164func TestCoverage(t *testing.T) {
165 valley, schemas := coverageValley(t)
166 for _, c := range []struct {
167 reader string
168 want []string
169 }{
170 {"operator", []string{
171 "docs: no project consent (its main holds no policy/read)",
172 "fresh: no project consent (it has no main)",
173 "site: no project consent (its policy/read does not name operator)",
174 "operator may read 2 of 5 projects at example.org",
175 }},
176 {"anyone", []string{
177 "docs: no project consent (its main holds no policy/read); no instance grant (no read grant of anyone at example.org names docs)",
178 "fresh: no project consent (it has no main); no instance grant (no read grant of anyone at example.org names fresh)",
179 "valley: no project consent (its policy/read does not name anyone); no instance grant (no read grant of anyone at example.org covers valley with the identity " + idValley + ")",
180 "anyone may read 2 of 5 projects at example.org",
181 }},
182 } {
183 stdout, _ := report(t, valley, schemas, c.reader)
184 sameReport(t, c.reader, stdout, c.want)
185 }
186}
187
188// The registry is read at --registry-ref, the ref the identity compiler
189// reads it at. A grant revoked on that ref alone is revoked, whatever main
190// still says, and an instance repository with no such ref grants no read,
191// whatever another ref git would take the name for grants.
192func TestRegistryRef(t *testing.T) {
193 valley, schemas := coverageValley(t)
194 main := hostGit(t, "", "-C", valley, "rev-parse", "main")
195 revoked := map[string]string{"identity/registry.cue": registry(`{app: "` + idApp + `"}`)}
196 for name, body := range policy("valley", idValley, "operator: true") {
197 revoked[name] = body
198 }
199 commit := hostGit(t, "", "-C", valley, "-c", "user.name=test", "-c", "user.email=test@example.org",
200 "commit-tree", tree(t, valley, revoked), "-p", main, "-m", "valley: site's grant to anyone is revoked")
201 hostGit(t, "", "-C", valley, "update-ref", "refs/heads/registry", commit)
202
203 noGrant := func(name string) string {
204 return name + ": no project consent (its main holds no policy/read); no instance grant (no read grant of anyone at example.org names " + name + ")"
205 }
206 stdout, _ := report(t, valley, schemas, "anyone", "--registry-ref", "refs/heads/registry")
207 sameReport(t, "anyone at refs/heads/registry", stdout, []string{
208 noGrant("docs"),
209 "fresh: no project consent (it has no main); no instance grant (no read grant of anyone at example.org names fresh)",
210 "site: no instance grant (no read grant of anyone at example.org covers site with the identity " + idSite + ")",
211 "valley: no project consent (its policy/read does not name anyone); no instance grant (no read grant of anyone at example.org covers valley with the identity " + idValley + ")",
212 "anyone may read 1 of 5 projects at example.org",
213 })
214 if stdout, _ := report(t, valley, schemas, "anyone"); !strings.HasSuffix(strings.TrimSpace(stdout), "anyone may read 2 of 5 projects at example.org") {
215 t.Errorf("at main, site's grant is not revoked, and the report is\n%s", stdout)
216 }
217
218 stdout, stderr := report(t, valley, schemas, "operator", "--registry-ref", "refs/heads/none")
219 if !strings.Contains(stderr, "holds no refs/heads/none, so it has no registry, and grants no read") ||
220 !strings.HasSuffix(strings.TrimSpace(stdout), "operator may read 0 of 5 projects at example.org") {
221 t.Errorf("with no registry ref, the report is\n%s\nand the command said\n%s", stdout, stderr)
222 }
223
224 // With refs/heads/registry gone, git rev-parse would resolve the name
225 // to refs/heads/refs/heads/registry, a branch any pusher can make, here
226 // granting anyone read of app and site. The registry is read at the
227 // ref named exactly, so there is none.
228 hostGit(t, "", "-C", valley, "update-ref", "-d", "refs/heads/registry")
229 hostGit(t, "", "-C", valley, "update-ref", "refs/heads/refs/heads/registry", main)
230 stdout, stderr = report(t, valley, schemas, "anyone", "--registry-ref", "refs/heads/registry")
231 if !strings.Contains(stderr, "holds no refs/heads/registry, so it has no registry, and grants no read") ||
232 !strings.HasSuffix(strings.TrimSpace(stdout), "anyone may read 0 of 5 projects at example.org") {
233 t.Errorf("with refs/heads/registry gone, the report is\n%s\nand the command said\n%s", stdout, stderr)
234 }
235
236 for _, ref := range []string{"main", "refs/heads/main~1", "refs/heads/../main", "refs/heads/registry.lock", "refs/heads/a b", "refs//main"} {
237 var out, errs bytes.Buffer
238 if code := run(context.Background(), []string{"--instance-repo", valley, "--destination", "example.org",
239 "--reader", "anyone", "--registry-ref", ref, "--read-schema", filepath.Join(schemas, "read.cue"),
240 "--identity-schema", filepath.Join(schemas, "identity.cue")}, &out, &errs, time.Now()); code != 2 {
241 t.Errorf("--registry-ref %q: exited %d, want 2", ref, code)
242 }
243 }
244}