e2e/ssh_test.go

v1.34.0
gitbay/e2e/ssh_test.go history · blame · raw

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