e2e/deploykey_test.go

v1.21.0
gitbay/e2e/deploykey_test.go history · blame · raw

115 lines · 4670 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	// Its push closes the bound repository's issue whether the message
 81	// names it bare or by full path (#213).
 82	for _, title := range []string{"'bare'", "'full path'"} {
 83		if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", title); code != 0 {
 84			t.Fatalf("issue create: %s", errOut)
 85		}
 86	}
 87	mustGit(t, rwDir, rwEnv, "commit", "-q", "--allow-empty", "-m", "ci push\n\nCloses #1, closes alice/app#2")
 88	mustGit(t, rwDir, rwEnv, "push", "-q", "origin", "main")
 89	for _, n := range []string{"1", "2"} {
 90		if out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", n, "--json"); !strings.Contains(out, `"state":"closed"`) {
 91			t.Fatalf("deploy key push did not close issue %s: %s", n, out)
 92		}
 93	}
 94
 95	// The binding survives an owner rename (keys bind to the repo ID).
 96	if _, errOut, code = inst.ssh(t, aliceKey, "", "org", "create", "moved"); code != 0 {
 97		t.Fatalf("org create: %s", errOut)
 98	}
 99	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "transfer", "alice/app", "moved"); code != 0 {
100		t.Fatalf("transfer: %s", errOut)
101	}
102	mustGit(t, t.TempDir(), roEnv, "clone", inst.sshURL("moved/app"))
103
104	// List shows both with modes; removal severs access immediately.
105	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "deploy-key", "list", "moved/app")
106	if !strings.Contains(out, "ro") || !strings.Contains(out, "rw") {
107		t.Fatalf("deploy-key list: %s", out)
108	}
109	if _, errOut, code = inst.ssh(t, aliceKey, "", "repo", "deploy-key", "remove", "moved/app", roFP); code != 0 {
110		t.Fatalf("remove: %s", errOut)
111	}
112	if out, code := gitRun(t, t.TempDir(), roEnv, "clone", inst.sshURL("moved/app")); code == 0 {
113		t.Fatalf("removed deploy key still works:\n%s", out)
114	}
115}