e2e/ssh_test.go

f8b976a97290a20d552056a999511f5d27d8e8ec
gitbay/e2e/ssh_test.go history · blame · raw

334 lines · 10783 bytes

  1// Package e2e drives a real gitbayd with the real ssh and git clients.
  2package e2e
  3
  4import (
  5	"encoding/json"
  6	"fmt"
  7	"io"
  8	"math/rand/v2"
  9	"net"
 10	"os"
 11	"os/exec"
 12	"path/filepath"
 13	"strings"
 14	"sync/atomic"
 15	"testing"
 16)
 17
 18type instance struct {
 19	gitbayd  string // path to built binary
 20	runner   string // path to built gitbay-runner (CI tests)
 21	root     string
 22	config   string
 23	port     int
 24	httpPort int
 25	gitPort  int
 26	proc     *exec.Cmd
 27	sshDir   string      // per-user client keys live here
 28	keyFile  string      // server.secret_key_file, outside root
 29	stderr   *tailBuffer // the end of the first serve's stderr
 30}
 31
 32// nextPort hands out candidate ports. Seeded randomly so two test processes
 33// on one machine — `go test ./...` runs packages concurrently — start in
 34// different places.
 35var nextPort = func() *atomic.Int32 {
 36	var n atomic.Int32
 37	n.Store(int32(20000 + rand.IntN(20000)))
 38	return &n
 39}()
 40
 41// freePorts reserves n distinct ports, counting up rather than asking the
 42// kernel for :0.
 43//
 44// :0 cannot be made safe once tests run in parallel. A port is picked by
 45// binding, reading the number back and closing, and between that close and
 46// the bind inside gitbayd the kernel is free to hand the same port to
 47// another instance picking at that moment. The loser does not fail
 48// cleanly: waitForPort only asks whether something is listening, so a test
 49// whose port was taken talks to a different test's server and reports
 50// whatever that one says.
 51//
 52// A counter cannot collide within a process, whatever the interleaving.
 53// Each candidate is still bind-tested, which skips ports other programs
 54// hold. An outside process taking one in the close-to-bind window remains
 55// possible, as it was before; that race is not ours to close.
 56func freePorts(t *testing.T, n int) []int {
 57	t.Helper()
 58	ports := make([]int, 0, n)
 59	for len(ports) < n {
 60		p := int(nextPort.Add(1))
 61		if p > 60000 {
 62			t.Fatal("ran out of ports")
 63		}
 64		ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", p))
 65		if err != nil {
 66			continue // somebody else has it
 67		}
 68		ln.Close()
 69		ports = append(ports, p)
 70	}
 71	return ports
 72}
 73
 74func freePort(t *testing.T) int {
 75	t.Helper()
 76	return freePorts(t, 1)[0]
 77}
 78
 79func startInstance(t *testing.T) *instance {
 80	return startInstanceWith(t, "")
 81}
 82
 83// startInstanceWith appends extra TOML to the instance config.
 84func startInstanceWith(t *testing.T, extra string) *instance {
 85	t.Helper()
 86	ports := freePorts(t, 3)
 87	inst := &instance{
 88		gitbayd:  buildGitbayd(t),
 89		root:     t.TempDir(),
 90		port:     ports[0],
 91		httpPort: ports[1],
 92		gitPort:  ports[2],
 93		sshDir:   t.TempDir(),
 94	}
 95	inst.keyFile = filepath.Join(t.TempDir(), "secret.key")
 96	inst.config = filepath.Join(inst.root, "config.toml")
 97	cfg := fmt.Sprintf(`
 98[server]
 99root = %q
100site_url = "https://gitbay.test"
101secret_key_file = %q
102[ssh]
103port = %d
104[http]
105addr = "127.0.0.1:%d"
106tls = "off"
107[git_daemon]
108enabled = true
109port = %d
110`, inst.root, inst.keyFile, inst.port, inst.httpPort, inst.gitPort)
111	cfg += extra + "\n"
112	if err := os.WriteFile(inst.config, []byte(cfg), 0o600); err != nil {
113		t.Fatal(err)
114	}
115
116	inst.admin(t, "admin", "secrets", "init")
117
118	inst.stderr = &tailBuffer{max: 16 << 10}
119	inst.proc = exec.Command(inst.gitbayd, "--config", inst.config, "serve")
120	inst.proc.Stderr = io.MultiWriter(os.Stderr, inst.stderr)
121	if err := inst.proc.Start(); err != nil {
122		t.Fatal(err)
123	}
124	t.Cleanup(func() {
125		inst.proc.Process.Kill()
126		inst.proc.Wait()
127	})
128
129	// Every listener, not just SSH: the HTTP and git ones come up in their
130	// own goroutines, and a test whose first act is an HTTP request used
131	// to race them and be refused.
132	inst.waitListening(t, inst.port, inst.httpPort, inst.gitPort)
133	return inst
134}
135
136// admin runs a gitbayd admin command against the instance's database.
137func (i *instance) admin(t *testing.T, args ...string) string {
138	t.Helper()
139	cmd := exec.Command(i.gitbayd, append([]string{"--config", i.config}, args...)...)
140	out, err := cmd.CombinedOutput()
141	if err != nil {
142		t.Fatalf("gitbayd %v: %v\n%s", args, err, out)
143	}
144	return string(out)
145}
146
147// forgedAdminErr runs an admin command expected to fail, returning output.
148func (i *instance) forgedAdminErr(t *testing.T, args ...string) string {
149	t.Helper()
150	cmd := exec.Command(i.gitbayd, append([]string{"--config", i.config}, args...)...)
151	out, err := cmd.CombinedOutput()
152	if err == nil {
153		t.Fatalf("gitbayd %v unexpectedly succeeded:\n%s", args, out)
154	}
155	return string(out)
156}
157
158// newKey generates a client keypair and returns the private key path.
159func (i *instance) newKey(t *testing.T, name string) string {
160	t.Helper()
161	priv := filepath.Join(i.sshDir, name)
162	cmd := exec.Command("ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", name, "-f", priv)
163	if out, err := cmd.CombinedOutput(); err != nil {
164		t.Fatalf("ssh-keygen: %v\n%s", err, out)
165	}
166	return priv
167}
168
169// sshCmd is the ssh invocation ssh runs, for a test that reads the output
170// as it arrives.
171func (i *instance) sshCmd(key string, args ...string) *exec.Cmd {
172	base := []string{
173		"-p", fmt.Sprint(i.port),
174		"-i", key,
175		"-o", "IdentitiesOnly=yes",
176		"-o", "StrictHostKeyChecking=no",
177		"-o", "UserKnownHostsFile=" + filepath.Join(i.sshDir, "known_hosts"),
178		"-o", "BatchMode=yes",
179		"git@127.0.0.1",
180	}
181	return exec.Command("ssh", append(base, args...)...)
182}
183
184// ssh runs the real OpenSSH client against the instance with the given key.
185func (i *instance) ssh(t *testing.T, key string, stdin string, args ...string) (string, string, int) {
186	t.Helper()
187	cmd := i.sshCmd(key, args...)
188	if stdin != "" {
189		cmd.Stdin = strings.NewReader(stdin)
190	}
191	var out, errOut strings.Builder
192	cmd.Stdout = &out
193	cmd.Stderr = &errOut
194	err := cmd.Run()
195	code := 0
196	if ee, ok := err.(*exec.ExitError); ok {
197		code = ee.ExitCode()
198	} else if err != nil {
199		t.Fatalf("ssh: %v", err)
200	}
201	return out.String(), errOut.String(), code
202}
203
204// sshTerm is ssh with a leading --term=<v> on the command line, as the
205// CLI sends it at a terminal: OpenSSH's ControlMaster does not forward a
206// new session's SetEnv, so the term travels in argv instead. An empty
207// term sends nothing.
208func (i *instance) sshTerm(t *testing.T, key, term string, args ...string) (string, string, int) {
209	t.Helper()
210	if term != "" {
211		// "--" stops the local ssh client from parsing --term=... as one of
212		// its own options; it is not part of the remote command line.
213		args = append([]string{"--", "--term=" + term}, args...)
214	}
215	cmd := i.sshCmd(key, args...)
216	var out, errOut strings.Builder
217	cmd.Stdout, cmd.Stderr = &out, &errOut
218	err := cmd.Run()
219	code := 0
220	if ee, ok := err.(*exec.ExitError); ok {
221		code = ee.ExitCode()
222	} else if err != nil {
223		t.Fatalf("ssh: %v", err)
224	}
225	return out.String(), errOut.String(), code
226}
227
228func TestControlPlaneOverBareSSH(t *testing.T) {
229	t.Parallel()
230	inst := startInstance(t)
231
232	aliceKey := inst.newKey(t, "alice")
233	inst.admin(t, "admin", "user", "create", "alice",
234		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
235
236	// whoami --json from bare OpenSSH.
237	out, errOut, code := inst.ssh(t, aliceKey, "", "whoami", "--json")
238	if code != 0 {
239		t.Fatalf("whoami exit %d, stderr: %s", code, errOut)
240	}
241	var env struct {
242		ProtocolVersion int `json:"protocol_version"`
243		Data            struct {
244			Username string `json:"username"`
245			KeyScope string `json:"key_scope"`
246		} `json:"data"`
247	}
248	if err := json.Unmarshal([]byte(out), &env); err != nil {
249		t.Fatalf("whoami output not JSON: %v\n%s", err, out)
250	}
251	if env.Data.Username != "alice" || env.ProtocolVersion != 1 || env.Data.KeyScope != "full" {
252		t.Fatalf("whoami = %+v", env)
253	}
254
255	// Unknown key is refused at auth.
256	strangerKey := inst.newKey(t, "stranger")
257	_, _, code = inst.ssh(t, strangerKey, "", "whoami")
258	if code == 0 {
259		t.Fatal("unknown key was authenticated")
260	}
261
262	// keys add over stdin, then list shows both.
263	secondKey := inst.newKey(t, "alice2")
264	pub, _ := os.ReadFile(secondKey + ".pub")
265	out, errOut, code = inst.ssh(t, aliceKey, string(pub), "keys", "add", "--scope", "git")
266	if code != 0 {
267		t.Fatalf("keys add exit %d, stderr: %s", code, errOut)
268	}
269	out, _, code = inst.ssh(t, aliceKey, "", "keys", "list")
270	if code != 0 || len(strings.Split(strings.TrimSpace(out), "\n")) != 2 {
271		t.Fatalf("keys list exit %d:\n%s", code, out)
272	}
273	// The key's comment (ssh-keygen -C) is its label; keys label renames it.
274	if !strings.Contains(out, "\tgit\talice2\t") {
275		t.Fatalf("keys list lacks the comment as label:\n%s", out)
276	}
277	secondFP := strings.Fields(strings.Split(strings.TrimSpace(out), "\n")[1])[0]
278	if _, errOut, code := inst.ssh(t, aliceKey, "", "keys", "label", secondFP, "'build box'"); code != 0 {
279		t.Fatalf("keys label exit %d, stderr: %s", code, errOut)
280	}
281	out, _, _ = inst.ssh(t, aliceKey, "", "keys", "list")
282	if !strings.Contains(out, "\tgit\tbuild box\t") {
283		t.Fatalf("keys list after label:\n%s", out)
284	}
285
286	// The git-scoped key authenticates but is denied control commands.
287	out, errOut, code = inst.ssh(t, secondKey, "", "whoami")
288	if code != 4 {
289		t.Fatalf("git-scoped whoami: exit %d (want 4), stdout %q stderr %q", code, out, errOut)
290	}
291	if !strings.Contains(errOut, "does not allow control commands") {
292		t.Fatalf("scope denial message missing: %q", errOut)
293	}
294
295	// Duplicate key registration: bob cannot claim alice's key, and the
296	// message is the exact spec text, naming no account.
297	bobKey := inst.newKey(t, "bob")
298	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
299	alicePub, _ := os.ReadFile(aliceKey + ".pub")
300	_, errOut, code = inst.ssh(t, bobKey, string(alicePub), "keys", "add")
301	if code != 1 {
302		t.Fatalf("duplicate key add: exit %d, want 1", code)
303	}
304	want := "that key is already registered to another account; remove it there first or use a different key"
305	if !strings.Contains(errOut, want) {
306		t.Fatalf("duplicate key message = %q, want %q", errOut, want)
307	}
308	if strings.Contains(errOut, "alice") {
309		t.Fatalf("duplicate key message leaks account name: %q", errOut)
310	}
311
312	// Arguments with spaces survive the tokenizer round trip.
313	_, errOut, code = inst.ssh(t, aliceKey, "", "keys", "remove", "'no such fingerprint'")
314	if code != 3 {
315		t.Fatalf("keys remove with spaced arg: exit %d (want 3), stderr %q", code, errOut)
316	}
317}
318
319// freePort used to close its listener before returning, so the kernel was
320// free to hand the same port to the next call. An instance asks for three in
321// a row and then fails to bind its second listener, which surfaces as an
322// unrelated test timing out on "gitbayd did not start listening".
323func TestFreePortsAreDistinct(t *testing.T) {
324	t.Parallel()
325	for round := 0; round < 50; round++ {
326		seen := map[int]bool{}
327		for _, p := range freePorts(t, 8) {
328			if seen[p] {
329				t.Fatalf("round %d: port %d issued twice in one request", round, p)
330			}
331			seen[p] = true
332		}
333	}
334}