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.
| 1 | package ssh |
| 2 | |
| 3 | // One dispatcher, two transports. |
| 4 | // |
| 5 | // The front door does not reimplement any verb: sshd authenticates the key, the |
| 6 | // forced command reaches this process over a Unix socket, and `handleLocal` calls |
| 7 | // the same `exec` the built-in listener calls with the same account and key. The |
| 8 | // test below holds that to its promise by running one command at a time over both |
| 9 | // transports and comparing everything a client would see. |
| 10 | |
| 11 | import ( |
| 12 | "bytes" |
| 13 | "encoding/json" |
| 14 | "errors" |
| 15 | "net" |
| 16 | "os" |
| 17 | "path/filepath" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | "time" |
| 21 | |
| 22 | xssh "golang.org/x/crypto/ssh" |
| 23 | |
| 24 | "rodent.software/vole/ci/runs" |
| 25 | "rodent.software/vole/forge/access" |
| 26 | "rodent.software/vole/forge/channel" |
| 27 | "rodent.software/vole/forge/sshsession" |
| 28 | "rodent.software/vole/internal/sshwire" |
| 29 | ) |
| 30 | |
| 31 | // testConnection is the endpoint sshd reports for the front door's client, and |
| 32 | // the value the token is minted for. |
| 33 | const testConnection = "203.0.113.5 4242 192.0.2.10 22" |
| 34 | |
| 35 | // frontDoorHarness is the usual harness with the stores the front door needs: |
| 36 | // the token table it consumes and a channel store so the channel verb answers |
| 37 | // rather than reporting a missing store. Both are set before the server exists, |
| 38 | // so nothing is reconfigured under a running handler. |
| 39 | func frontDoorHarness(t *testing.T) (*harness, *sshsession.Store) { |
| 40 | t.Helper() |
| 41 | var tokens *sshsession.Store |
| 42 | h := newHarnessWith(t, func(cfg *Config) { |
| 43 | s, err := sshsession.Open(cfg.Identity.Path()) |
| 44 | if err != nil { |
| 45 | t.Fatalf("open tokens: %v", err) |
| 46 | } |
| 47 | cfg.Sessions = s |
| 48 | tokens = s |
| 49 | ch, err := channel.Open(cfg.Identity.Path()) |
| 50 | if err != nil { |
| 51 | t.Fatalf("open channels: %v", err) |
| 52 | } |
| 53 | cfg.Channels = ch |
| 54 | t.Cleanup(func() { |
| 55 | s.Close() |
| 56 | ch.Close() |
| 57 | }) |
| 58 | }) |
| 59 | return h, tokens |
| 60 | } |
| 61 | |
| 62 | // frontDoor starts the local transport on a server the harness already serves |
| 63 | // over SSH. |
| 64 | func frontDoor(t *testing.T, h *harness) string { |
| 65 | t.Helper() |
| 66 | socket := filepath.Join(shortTempDir(t), "ssh.sock") |
| 67 | ln, err := net.Listen("unix", socket) |
| 68 | if err != nil { |
| 69 | t.Fatalf("listen: %v", err) |
| 70 | } |
| 71 | go h.srv.ServeLocal(ln) |
| 72 | t.Cleanup(func() { ln.Close() }) |
| 73 | return socket |
| 74 | } |
| 75 | |
| 76 | // fingerprint is the SHA-256 fingerprint of an authorized_keys line. |
| 77 | func fingerprint(t *testing.T, authorizedKey string) string { |
| 78 | t.Helper() |
| 79 | k, _, _, _, err := xssh.ParseAuthorizedKey([]byte(authorizedKey)) |
| 80 | if err != nil { |
| 81 | t.Fatalf("parse key: %v", err) |
| 82 | } |
| 83 | return xssh.FingerprintSHA256(k) |
| 84 | } |
| 85 | |
| 86 | // shortTempDir returns a directory short enough for a Unix socket path. |
| 87 | func shortTempDir(t *testing.T) string { |
| 88 | t.Helper() |
| 89 | dir, err := os.MkdirTemp("", "vole-ssh-") |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | t.Cleanup(func() { os.RemoveAll(dir) }) |
| 94 | return dir |
| 95 | } |
| 96 | |
| 97 | // runOverSSH runs one command through the built-in listener. |
| 98 | func runOverSSH(t *testing.T, h *harness, keyPath, command, stdin string) (string, string, int) { |
| 99 | t.Helper() |
| 100 | sess, err := h.dial(t, keyPath).NewSession() |
| 101 | if err != nil { |
| 102 | t.Fatalf("new session: %v", err) |
| 103 | } |
| 104 | defer sess.Close() |
| 105 | var out, errOut bytes.Buffer |
| 106 | sess.Stdout, sess.Stderr = &out, &errOut |
| 107 | sess.Stdin = strings.NewReader(stdin) |
| 108 | err = sess.Run(command) |
| 109 | code := 0 |
| 110 | if err != nil { |
| 111 | var exit *xssh.ExitError |
| 112 | if !errors.As(err, &exit) { |
| 113 | t.Fatalf("%q over SSH: %v", command, err) |
| 114 | } |
| 115 | code = exit.ExitStatus() |
| 116 | } |
| 117 | return out.String(), errOut.String(), code |
| 118 | } |
| 119 | |
| 120 | // runOverFrontDoor runs the same command through the local transport, with a |
| 121 | // token minted for the account the request is about. |
| 122 | func runOverFrontDoor(t *testing.T, socket string, tokens *sshsession.Store, tok sshsession.Token, command, stdin string) (string, string, int) { |
| 123 | t.Helper() |
| 124 | tok.Client = testConnection |
| 125 | token, err := tokens.Mint(tok, time.Now().UTC()) |
| 126 | if err != nil { |
| 127 | t.Fatalf("mint: %v", err) |
| 128 | } |
| 129 | var out, errOut bytes.Buffer |
| 130 | code, err := sshwire.Exec(socket, sshwire.Hello{ |
| 131 | Session: token, Command: command, Remote: testConnection, |
| 132 | }, strings.NewReader(stdin), &out, &errOut) |
| 133 | if err != nil { |
| 134 | t.Fatalf("%q over the front door: %v", command, err) |
| 135 | } |
| 136 | return out.String(), errOut.String(), code |
| 137 | } |
| 138 | |
| 139 | // TestBothTransportsAnswerTheSameWay is the equivalence test for the front door. |
| 140 | // |
| 141 | // It covers one command from each kind of handler — the identity query, the |
| 142 | // roster, git's two pack services, the admin API, the run queue, release |
| 143 | // channels, a refused verb, and a job credential that may do less than its |
| 144 | // account — and asserts the bytes and the status are the same on both paths. |
| 145 | func TestBothTransportsAnswerTheSameWay(t *testing.T) { |
| 146 | t.Parallel() |
| 147 | h, tokens := frontDoorHarness(t) |
| 148 | socket := frontDoor(t, h) |
| 149 | |
| 150 | // The admin API needs an admin; the harness's writer becomes one so the |
| 151 | // comparison covers a command that runs rather than one that is refused. |
| 152 | if _, err := h.ident.SetInstanceRole("writer", access.Admin, "test"); err != nil { |
| 153 | t.Fatalf("grant admin: %v", err) |
| 154 | } |
| 155 | |
| 156 | writer, err := h.ident.Find("writer") |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | writerKey := writer.Keys[0].Fingerprint |
| 161 | |
| 162 | // A job credential, so the rules that stop a job administering the instance |
| 163 | // are compared too. |
| 164 | credKey, credPub := keypair(t, filepath.Join(h.dir, "credential")) |
| 165 | credFP := fingerprint(t, credPub) |
| 166 | now := time.Now().UTC() |
| 167 | if err := h.runs.SetRunCredential(runs.RunCredential{ |
| 168 | Fingerprint: credFP, |
| 169 | Account: "writer", |
| 170 | Repo: "demo", |
| 171 | RunID: "run-1", |
| 172 | Expires: now.Add(time.Hour), |
| 173 | }, now); err != nil { |
| 174 | t.Fatalf("set credential: %v", err) |
| 175 | } |
| 176 | |
| 177 | // git-upload-pack has something to advertise once a branch exists. |
| 178 | work := workRepo(t) |
| 179 | if _, errs, err := h.git(work, h.keyPath, "push", h.url(), "main:refs/heads/main"); err != nil { |
| 180 | t.Fatalf("seed push: %v\n%s", err, errs) |
| 181 | } |
| 182 | |
| 183 | account := sshsession.Token{Account: "writer", Key: writerKey} |
| 184 | credential := sshsession.Token{Account: "writer", Key: credFP} |
| 185 | |
| 186 | for _, tc := range []struct { |
| 187 | name string |
| 188 | command string |
| 189 | stdin string |
| 190 | keyPath string |
| 191 | token sshsession.Token |
| 192 | }{ |
| 193 | {"whoami", "vole-whoami", "", h.keyPath, account}, |
| 194 | {"roster", "vole-roster", "", h.keyPath, account}, |
| 195 | {"upload-pack", "git-upload-pack 'demo.git'", "", h.keyPath, account}, |
| 196 | {"receive-pack", "git-receive-pack 'demo.git'", "", h.keyPath, account}, |
| 197 | {"admin list", "vole-admin account list", "", h.keyPath, account}, |
| 198 | {"run list", "vole-run-list demo", "", h.keyPath, account}, |
| 199 | {"channel list", "vole-channel list demo", "", h.keyPath, account}, |
| 200 | {"refused verb", "cat /etc/passwd", "", h.keyPath, account}, |
| 201 | {"credential refusal", "vole-admin account list", "", credKey, credential}, |
| 202 | } { |
| 203 | t.Run(tc.name, func(t *testing.T) { |
| 204 | sshOut, sshErr, sshCode := runOverSSH(t, h, tc.keyPath, tc.command, tc.stdin) |
| 205 | doorOut, doorErr, doorCode := runOverFrontDoor(t, socket, tokens, tc.token, tc.command, tc.stdin) |
| 206 | |
| 207 | if doorOut != sshOut { |
| 208 | t.Errorf("%q: stdout differs\n front door: %q\n ssh: %q", tc.command, doorOut, sshOut) |
| 209 | } |
| 210 | if doorErr != sshErr { |
| 211 | t.Errorf("%q: stderr differs\n front door: %q\n ssh: %q", tc.command, doorErr, sshErr) |
| 212 | } |
| 213 | if doorCode != sshCode { |
| 214 | t.Errorf("%q: the front door exited %d, ssh exited %d", tc.command, doorCode, sshCode) |
| 215 | } |
| 216 | }) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // TestFrontDoorRefusesUnusableTokens covers the answers a request can get before |
| 221 | // any verb runs: an unknown token, a spent one, one minted for another endpoint, |
| 222 | // and an empty command. |
| 223 | func TestFrontDoorRefusesUnusableTokens(t *testing.T) { |
| 224 | t.Parallel() |
| 225 | h, tokens := frontDoorHarness(t) |
| 226 | socket := frontDoor(t, h) |
| 227 | command := "git-upload-pack 'demo.git'" |
| 228 | |
| 229 | exec := func(t *testing.T, hello sshwire.Hello) (string, string, int, error) { |
| 230 | t.Helper() |
| 231 | var out, errOut bytes.Buffer |
| 232 | code, err := sshwire.Exec(socket, hello, nil, &out, &errOut) |
| 233 | return out.String(), errOut.String(), code, err |
| 234 | } |
| 235 | |
| 236 | // Unknown. |
| 237 | if _, _, _, err := exec(t, sshwire.Hello{Session: "not-a-token", Command: command, Remote: testConnection}); err == nil { |
| 238 | t.Error("an unknown token was accepted") |
| 239 | } |
| 240 | |
| 241 | // Minted for another endpoint: refused, and the token is spent rather than |
| 242 | // left available for the rest of its minute. |
| 243 | token, err := tokens.Mint(sshsession.Token{Account: "writer", Client: testConnection}, time.Now().UTC()) |
| 244 | if err != nil { |
| 245 | t.Fatal(err) |
| 246 | } |
| 247 | if _, _, _, err := exec(t, sshwire.Hello{Session: token, Command: command, Remote: "198.51.100.9 4242 192.0.2.10 22"}); err == nil { |
| 248 | t.Error("a token minted for another endpoint was accepted") |
| 249 | } |
| 250 | if _, _, _, err := exec(t, sshwire.Hello{Session: token, Command: command, Remote: testConnection}); err == nil { |
| 251 | t.Error("the refused token was still usable") |
| 252 | } |
| 253 | |
| 254 | // A valid token, spent by the first real request. |
| 255 | token, err = tokens.Mint(sshsession.Token{Account: "writer", Client: testConnection}, time.Now().UTC()) |
| 256 | if err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | if _, _, _, err := exec(t, sshwire.Hello{Session: token, Command: "vole-whoami", Remote: testConnection}); err != nil { |
| 260 | t.Fatalf("a good token was refused: %v", err) |
| 261 | } |
| 262 | if _, _, _, err := exec(t, sshwire.Hello{Session: token, Command: "vole-whoami", Remote: testConnection}); err == nil { |
| 263 | t.Error("a spent token was accepted") |
| 264 | } |
| 265 | |
| 266 | // No command: a connection that asked for a shell. |
| 267 | token, err = tokens.Mint(sshsession.Token{Account: "writer", Client: testConnection}, time.Now().UTC()) |
| 268 | if err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | if _, _, _, err := exec(t, sshwire.Hello{Session: token, Command: "", Remote: testConnection}); err == nil { |
| 272 | t.Error("a request with no command was accepted") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // TestLocalSessionSplitsAndSeparatesStreams checks the transport itself: a write |
| 277 | // larger than one frame arrives whole, and stderr stays out of stdout. |
| 278 | func TestLocalSessionSplitsAndSeparatesStreams(t *testing.T) { |
| 279 | t.Parallel() |
| 280 | server, client := net.Pipe() |
| 281 | sess := &localSession{conn: server} |
| 282 | t.Cleanup(func() { server.Close(); client.Close() }) |
| 283 | |
| 284 | big := bytes.Repeat([]byte("a"), sshwire.MaxFrame*2+7) |
| 285 | go func() { |
| 286 | _, _ = sess.Write(big) |
| 287 | _, _ = sess.Stderr().Write([]byte("diagnostic")) |
| 288 | sess.Exit(3) |
| 289 | }() |
| 290 | |
| 291 | var out, errOut bytes.Buffer |
| 292 | code := -1 |
| 293 | for { |
| 294 | kind, payload, err := sshwire.ReadFrame(client) |
| 295 | if err != nil { |
| 296 | t.Fatalf("read: %v", err) |
| 297 | } |
| 298 | switch kind { |
| 299 | case sshwire.KindStdout: |
| 300 | out.Write(payload) |
| 301 | case sshwire.KindStderr: |
| 302 | errOut.Write(payload) |
| 303 | case sshwire.KindExit: |
| 304 | var exit sshwire.Exit |
| 305 | if err := json.Unmarshal(payload, &exit); err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | code = exit.Code |
| 309 | goto done |
| 310 | } |
| 311 | } |
| 312 | done: |
| 313 | if out.Len() != len(big) || !bytes.Equal(out.Bytes(), big) { |
| 314 | t.Errorf("stdout is %d bytes, want %d", out.Len(), len(big)) |
| 315 | } |
| 316 | if errOut.String() != "diagnostic" { |
| 317 | t.Errorf("stderr %q", errOut.String()) |
| 318 | } |
| 319 | if code != 3 { |
| 320 | t.Errorf("exit %d, want 3", code) |
| 321 | } |
| 322 | } |
| 323 |