| 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 18 | const ( |
| 19 | idApp = "9da6809f038df92138c131cd16bed76d" |
| 20 | idSite = "273d5668702918ea7ae079de374db96b" |
| 21 | idValley = "f83927d5a4ed068cdd02a8cdaa09f26d" |
| 22 | key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA" |
| 23 | ) |
| 24 | |
| 25 | func 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 | |
| 35 | func 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. |
| 47 | func 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. |
| 70 | func 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 | |
| 82 | func 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 | // The report names each project a reader lacks a consent for, and which: |
| 88 | // a project with no main, no policy/read, or one that does not name the |
| 89 | // reader lacks its own consent; a project no grant covers lacks the |
| 90 | // instance's. |
| 91 | func TestCoverage(t *testing.T) { |
| 92 | needTool(t, "git") |
| 93 | needTool(t, "cue") |
| 94 | root := t.TempDir() |
| 95 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 96 | t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(root, "gitconfig")) |
| 97 | t.Setenv("HOME", root) |
| 98 | schemas, err := filepath.Abs(filepath.Join("..", "..", "..", "schema")) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | srv := filepath.Join(root, "srv") |
| 103 | |
| 104 | instance := policy("valley", idValley, "operator: true") |
| 105 | instance["identity/registry.cue"] = `package identity |
| 106 | |
| 107 | boundaries: { |
| 108 | registry: kind: "registry" |
| 109 | web: {kind: "edge-read", destination: "example.org"} |
| 110 | } |
| 111 | genesis: "operator" |
| 112 | principals: operator: { |
| 113 | kind: "human" |
| 114 | keys: [{class: "ssh-ed25519", bound: "hardware", public: "` + key + `"}] |
| 115 | grants: { |
| 116 | govern: boundary: "registry" |
| 117 | read: {boundary: "web", projects: "all"} |
| 118 | } |
| 119 | } |
| 120 | anyone: grants: read: {boundary: "web", projects: {app: "` + idApp + `", site: "` + idSite + `"}} |
| 121 | ` |
| 122 | valley := project(t, srv, "valley", instance) |
| 123 | project(t, srv, "app", policy("app", idApp, "anyone: true, operator: true")) |
| 124 | project(t, srv, "site", policy("site", idSite, "anyone: true")) |
| 125 | project(t, srv, "docs", map[string]string{"README.md": "# docs\n"}) |
| 126 | project(t, srv, "fresh", nil) |
| 127 | list := hostGit(t, "app\ndocs\nfresh\nsite\nvalley\n", "-C", valley, "hash-object", "-w", "--stdin") |
| 128 | hostGit(t, "", "-C", valley, "update-ref", "refs/the-valley/projects", list) |
| 129 | |
| 130 | for _, c := range []struct { |
| 131 | reader string |
| 132 | want []string |
| 133 | }{ |
| 134 | {"operator", []string{ |
| 135 | "docs: no project consent (its main holds no policy/read)", |
| 136 | "fresh: no project consent (it has no main)", |
| 137 | "site: no project consent (its policy/read does not name operator)", |
| 138 | "operator may read 2 of 5 projects at example.org", |
| 139 | }}, |
| 140 | {"anyone", []string{ |
| 141 | "docs: no project consent (its main holds no policy/read); no instance grant (no read grant of anyone at example.org names docs)", |
| 142 | "fresh: no project consent (it has no main); no instance grant (no read grant of anyone at example.org names fresh)", |
| 143 | "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 + ")", |
| 144 | "anyone may read 2 of 5 projects at example.org", |
| 145 | }}, |
| 146 | } { |
| 147 | var stdout, stderr bytes.Buffer |
| 148 | code := run(context.Background(), []string{"--instance-repo", valley, "--destination", "example.org", |
| 149 | "--reader", c.reader, "--read-schema", filepath.Join(schemas, "read.cue"), |
| 150 | "--identity-schema", filepath.Join(schemas, "identity.cue")}, &stdout, &stderr, time.Now()) |
| 151 | if code != 0 { |
| 152 | t.Fatalf("%s: exited %d: %s", c.reader, code, stderr.String()) |
| 153 | } |
| 154 | if got := strings.Split(strings.TrimSpace(stdout.String()), "\n"); strings.Join(got, "\n") != strings.Join(c.want, "\n") { |
| 155 | t.Errorf("%s: the report is\n%s\nwant\n%s", c.reader, strings.Join(got, "\n"), strings.Join(c.want, "\n")) |
| 156 | } |
| 157 | } |
| 158 | } |