| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "net" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // A push is refused until the host converges, so what reads as a push has |
| 13 | // to be everything git-shell would run as receive-pack. git-shell turns |
| 14 | // "git" and any whitespace into "git-" before it looks the command up. |
| 15 | func TestEverySpellingOfReceivePackIsAPush(t *testing.T) { |
| 16 | for command, push := range map[string]bool{ |
| 17 | "git-receive-pack 'project.git'": true, |
| 18 | "git receive-pack 'project.git'": true, |
| 19 | "git\treceive-pack 'project.git'": true, |
| 20 | "git-receive-pack": true, |
| 21 | "git-upload-pack 'project.git'": false, |
| 22 | "git upload-pack 'project.git'": false, |
| 23 | "git-upload-archive 'project.git'": false, |
| 24 | "env": false, |
| 25 | } { |
| 26 | if got := isPush(command); got != push { |
| 27 | t.Errorf("isPush(%q) = %v, want %v", command, got, push) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // The telemetry command is the shell's own: refused unless the host |
| 33 | // serves telemetry, the key names a principal, the command parses and the |
| 34 | // principal holds the grant; otherwise relayed to the collector as one |
| 35 | // request line naming the principal the shell derived, whatever the |
| 36 | // command said. |
| 37 | func TestTheTelemetryCommandIsRelayedOnlyWhole(t *testing.T) { |
| 38 | dir := t.TempDir() |
| 39 | grantsFile := filepath.Join(dir, "grants") |
| 40 | os.WriteFile(grantsFile, []byte("observe operator\nreport alice\n"), 0o600) |
| 41 | socket := filepath.Join(dir, "sock") |
| 42 | l, err := net.Listen("unix", socket) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | defer l.Close() |
| 47 | got := make(chan string, 8) |
| 48 | go func() { |
| 49 | for { |
| 50 | c, err := l.Accept() |
| 51 | if err != nil { |
| 52 | return |
| 53 | } |
| 54 | line, _ := bufio.NewReader(c).ReadString('\n') |
| 55 | got <- line |
| 56 | c.Write([]byte(`{"kind":"end"}` + "\n")) |
| 57 | c.Close() |
| 58 | } |
| 59 | }() |
| 60 | run := func(cmd, principal, sock string) (int, string) { |
| 61 | var out, errs strings.Builder |
| 62 | status := telemetryCommand(cmd, principal, sock, grantsFile, strings.NewReader(""), &out, &errs) |
| 63 | return status, out.String() + errs.String() |
| 64 | } |
| 65 | for _, c := range []struct{ cmd, principal, sock, why string }{ |
| 66 | {"valley-telemetry sources", "operator", "", "serves no telemetry"}, |
| 67 | {"valley-telemetry sources", "", socket, "names a principal"}, |
| 68 | {"valley-telemetry sources; sh", "operator", socket, "not a telemetry operation"}, |
| 69 | {"valley-telemetry read --source=/etc/shadow", "operator", socket, "not a source name"}, |
| 70 | {"valley-telemetry sources", "alice", socket, "does not hold the telemetry observe grant"}, |
| 71 | {"valley-telemetry report", "operator", socket, "does not hold the telemetry report grant"}, |
| 72 | } { |
| 73 | if status, said := run(c.cmd, c.principal, c.sock); status == 0 || !strings.Contains(said, c.why) { |
| 74 | t.Errorf("%q as %q: %d %q", c.cmd, c.principal, status, said) |
| 75 | } |
| 76 | } |
| 77 | select { |
| 78 | case line := <-got: |
| 79 | t.Fatalf("a refused command reached the collector: %s", line) |
| 80 | default: |
| 81 | } |
| 82 | if status, said := run("valley-telemetry read --source=worker --logs", "operator", socket); status != 0 || !strings.Contains(said, `"end"`) { |
| 83 | t.Fatalf("relayed: %d %q", status, said) |
| 84 | } |
| 85 | if line := <-got; line != `{"v":1,"principal":"operator","op":"read","source":"worker","logs":true}`+"\n" { |
| 86 | t.Fatalf("the collector was sent %q", line) |
| 87 | } |
| 88 | } |