vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
|
c5f2c5d4
Keenan Nemetz
16d ago
|
1 | package server |
| 2 | ||
| 3 | // The front door's socket is created by the service and used by the `git` account | |
| 4 | // sshd runs, so its mode and its lifecycle are part of the trust boundary. A stale | |
| 5 | // file must not stop a restart, and a live one must not be stolen. | |
| 6 | ||
| 7 | import ( | |
| 8 | "net" | |
| 9 | "os" | |
| 10 | "path/filepath" | |
| 11 | "strings" | |
| 12 | "testing" | |
| 13 | ) | |
| 14 | ||
| 15 | // shortDir returns a directory short enough for a Unix socket path. | |
| 16 | func shortDir(t *testing.T) string { | |
| 17 | t.Helper() | |
| 18 | dir, err := os.MkdirTemp("", "vole-fd-") | |
| 19 | if err != nil { | |
| 20 | t.Fatal(err) | |
| 21 | } | |
| 22 | t.Cleanup(func() { os.RemoveAll(dir) }) | |
| 23 | return dir | |
| 24 | } | |
| 25 | ||
|
8f24ae1f
Keenan Nemetz
12d ago
|
26 | // TestFrontDoorCreatesItsDirectory is the hand-run instance: nothing has created |
| 27 | // /run/vole, and failing with ENOENT says nothing an operator can act on. The | |
| 28 | // directory that does exist is left alone, because its group and setgid bit are | |
| 29 | // the installer's decision. | |
| 30 | func TestFrontDoorCreatesItsDirectory(t *testing.T) { | |
| 31 | t.Parallel() | |
| 32 | base := shortDir(t) | |
| 33 | ||
| 34 | socket := filepath.Join(base, "run", "ssh.sock") | |
| 35 | created, err := ensureFrontDoorDir(socket) | |
| 36 | if err != nil { | |
| 37 | t.Fatalf("create: %v", err) | |
| 38 | } | |
| 39 | if !created { | |
| 40 | t.Error("a missing socket directory was not reported as created") | |
| 41 | } | |
| 42 | dir := filepath.Join(base, "run") | |
| 43 | st, err := os.Stat(dir) | |
| 44 | if err != nil { | |
| 45 | t.Fatal(err) | |
| 46 | } | |
| 47 | if got := st.Mode().Perm(); got != 0o750 { | |
| 48 | t.Errorf("socket directory mode is %o, want 750", got) | |
| 49 | } | |
| 50 | again, err := ensureFrontDoorDir(socket) | |
| 51 | if err != nil || again { | |
| 52 | t.Errorf("an existing socket directory was touched: created=%v err=%v", again, err) | |
| 53 | } | |
| 54 | ||
| 55 | // A file where the directory should be is somebody's file, not a directory to | |
| 56 | // replace. | |
| 57 | notDir := filepath.Join(shortDir(t), "run") | |
| 58 | if err := os.WriteFile(notDir, []byte("x"), 0o600); err != nil { | |
| 59 | t.Fatal(err) | |
| 60 | } | |
| 61 | if _, err := ensureFrontDoorDir(filepath.Join(notDir, "ssh.sock")); err == nil || | |
| 62 | !strings.Contains(err.Error(), "not a directory") { | |
| 63 | t.Errorf("a file in the socket directory's place was not refused: %v", err) | |
| 64 | } | |
| 65 | } | |
| 66 | ||
|
c5f2c5d4
Keenan Nemetz
16d ago
|
67 | func TestFrontDoorSocketIsPrivateAndReplaceable(t *testing.T) { |
| 68 | t.Parallel() | |
| 69 | path := filepath.Join(shortDir(t), "ssh.sock") | |
| 70 | ||
| 71 | ln, err := listenFrontDoor(path) | |
| 72 | if err != nil { | |
| 73 | t.Fatalf("listen: %v", err) | |
| 74 | } | |
| 75 | st, err := os.Stat(path) | |
| 76 | if err != nil { | |
| 77 | t.Fatal(err) | |
| 78 | } | |
| 79 | // The service account and the `git` group may reach it; nobody else. | |
| 80 | if got := st.Mode().Perm(); got != 0o660 { | |
| 81 | t.Errorf("socket mode is %o, want 660", got) | |
| 82 | } | |
| 83 | // A second instance must not take a live socket. | |
| 84 | if second, err := listenFrontDoor(path); err == nil { | |
| 85 | second.Close() | |
| 86 | t.Error("a live socket was taken over") | |
| 87 | } | |
| 88 | ln.Close() | |
| 89 | ||
| 90 | // A socket file whose listener is gone is in the way of the next start, and | |
| 91 | // Close removes it — so make one that survives, the way a killed process would. | |
| 92 | stale, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"}) | |
| 93 | if err != nil { | |
| 94 | t.Fatalf("stale listen: %v", err) | |
| 95 | } | |
| 96 | stale.SetUnlinkOnClose(false) | |
| 97 | stale.Close() | |
| 98 | if _, err := os.Stat(path); err != nil { | |
| 99 | t.Fatalf("the stale socket is missing: %v", err) | |
| 100 | } | |
| 101 | replace, err := listenFrontDoor(path) | |
| 102 | if err != nil { | |
| 103 | t.Fatalf("a stale socket was not replaced: %v", err) | |
| 104 | } | |
| 105 | replace.Close() | |
| 106 | ||
| 107 | // Something that is not a socket is never removed: it is somebody's file. | |
| 108 | file := filepath.Join(shortDir(t), "not-a-socket") | |
| 109 | if err := os.WriteFile(file, []byte("hello"), 0o600); err != nil { | |
| 110 | t.Fatal(err) | |
| 111 | } | |
| 112 | if _, err := listenFrontDoor(file); err == nil || !strings.Contains(err.Error(), "not a socket") { | |
| 113 | t.Errorf("a regular file was treated as a socket: %v", err) | |
| 114 | } | |
| 115 | } |