e2e/deploykey_test.go

0caaaedf8fa3d930b6b4fb83e249b1e0e3265c0a
gitbay/e2e/deploykey_test.go history · blame · raw

103 lines · 4061 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"os"
  6	"path/filepath"
  7	"strings"
  8	"testing"
  9)
 10
 11func TestDeployKeys(t *testing.T) {
 12	inst := startInstance(t)
 13	aliceKey := inst.newKey(t, "alice")
 14	bobKey := inst.newKey(t, "bob")
 15	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 16	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
 17
 18	// Private repo with content.
 19	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app", "--private"); code != 0 {
 20		t.Fatalf("repo create: %s", errOut)
 21	}
 22	work := t.TempDir()
 23	env := inst.gitEnv(aliceKey)
 24	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
 25	dir := filepath.Join(work, "w")
 26	os.WriteFile(filepath.Join(dir, "app.txt"), []byte("v1\n"), 0o644)
 27	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 28	mustGit(t, dir, env, "add", ".")
 29	mustGit(t, dir, env, "commit", "-q", "-m", "init")
 30	mustGit(t, dir, env, "push", "-q", "origin", "main")
 31
 32	// Bind a read-only deploy key; non-admins cannot.
 33	roKey := inst.newKey(t, "ci-ro")
 34	roPub, _ := os.ReadFile(roKey + ".pub")
 35	if _, _, code := inst.ssh(t, bobKey, string(roPub), "repo", "deploy-key", "add", "alice/app"); code != 3 {
 36		t.Fatalf("non-admin deploy-key add on private repo: want not-found parity")
 37	}
 38	out, errOut, code := inst.ssh(t, aliceKey, string(roPub), "repo", "deploy-key", "add", "alice/app", "--json")
 39	if code != 0 {
 40		t.Fatalf("deploy-key add: %s", errOut)
 41	}
 42	var env2 struct {
 43		Data struct {
 44			Fingerprint string `json:"fingerprint"`
 45		} `json:"data"`
 46	}
 47	json.Unmarshal([]byte(out), &env2)
 48	roFP := env2.Data.Fingerprint
 49
 50	// The ro key clones the private repo but cannot push, cannot touch any
 51	// other repo, and cannot run control commands.
 52	roEnv := inst.gitEnv(roKey)
 53	roWork := t.TempDir()
 54	mustGit(t, roWork, roEnv, "clone", inst.sshURL("alice/app"), "w")
 55	roDir := filepath.Join(roWork, "w")
 56	mustGit(t, roDir, roEnv, "commit", "-q", "--allow-empty", "-m", "try")
 57	if out, code := gitRun(t, roDir, roEnv, "push", "origin", "main"); code == 0 {
 58		t.Fatalf("ro deploy key pushed:\n%s", out)
 59	}
 60	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/other", "--private"); code != 0 {
 61		t.Fatal("other repo failed")
 62	}
 63	if out, code := gitRun(t, t.TempDir(), roEnv, "clone", inst.sshURL("alice/other")); code == 0 || !strings.Contains(out, "repository not found") {
 64		t.Fatalf("deploy key crossed repos: %d\n%s", code, out)
 65	}
 66	if _, _, code := inst.ssh(t, roKey, "", "whoami"); code != 4 {
 67		t.Fatal("deploy key ran a control command")
 68	}
 69
 70	// An rw key pushes.
 71	rwKey := inst.newKey(t, "ci-rw")
 72	rwPub, _ := os.ReadFile(rwKey + ".pub")
 73	if _, errOut, code = inst.ssh(t, aliceKey, string(rwPub), "repo", "deploy-key", "add", "alice/app", "--rw"); code != 0 {
 74		t.Fatalf("rw add: %s", errOut)
 75	}
 76	rwEnv := inst.gitEnv(rwKey)
 77	rwWork := t.TempDir()
 78	mustGit(t, rwWork, rwEnv, "clone", inst.sshURL("alice/app"), "w")
 79	rwDir := filepath.Join(rwWork, "w")
 80	mustGit(t, rwDir, rwEnv, "commit", "-q", "--allow-empty", "-m", "ci push")
 81	mustGit(t, rwDir, rwEnv, "push", "-q", "origin", "main")
 82
 83	// The binding survives an owner rename (keys bind to the repo ID).
 84	if _, errOut, code = inst.ssh(t, aliceKey, "", "org", "create", "moved"); code != 0 {
 85		t.Fatalf("org create: %s", errOut)
 86	}
 87	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "transfer", "alice/app", "moved"); code != 0 {
 88		t.Fatalf("transfer: %s", errOut)
 89	}
 90	mustGit(t, t.TempDir(), roEnv, "clone", inst.sshURL("moved/app"))
 91
 92	// List shows both with modes; removal severs access immediately.
 93	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "deploy-key", "list", "moved/app")
 94	if !strings.Contains(out, "ro") || !strings.Contains(out, "rw") {
 95		t.Fatalf("deploy-key list: %s", out)
 96	}
 97	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "deploy-key", "remove", "moved/app", roFP); code != 0 {
 98		t.Fatalf("remove: %s", errOut)
 99	}
100	if out, code := gitRun(t, t.TempDir(), roEnv, "clone", inst.sshURL("moved/app")); code == 0 {
101		t.Fatalf("removed deploy key still works:\n%s", out)
102	}
103}