BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / commit_test.go

commit_test.go

fix: laat go-tests in Steve slagen zonder namespaces of smart HTTP
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
4bb9fce0 History
commit_test.go 6.0 KiB Download Raw
1package gitrepo
2
3import (
4 "context"
5 "errors"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/i18n"
14)
15
16// TestCommitOnBranchHardened bootst de system-service na: een andere uid
17// via een user-namespace, een alleen-lezen homedirectory, geen git-identiteit
18// in de omgeving, en een lege repository zonder master.
19func TestCommitOnBranchHardened(t *testing.T) {
20 if os.Getenv("BLUEGIT_SANDBOX") == "inner" {
21 commitSandboxInner(t)
22 return
23 }
24 if _, err := exec.LookPath("git"); err != nil {
25 t.Skip("git ontbreekt")
26 }
27 skipWithoutUserNS(t)
28 base := t.TempDir()
29 home := filepath.Join(base, "home")
30 state := filepath.Join(base, "state", "tmp")
31 bare := filepath.Join(base, "repo.git")
32 if err := os.MkdirAll(home, 0o755); err != nil {
33 t.Fatal(err)
34 }
35 if err := os.MkdirAll(state, 0o755); err != nil {
36 t.Fatal(err)
37 }
38 if err := os.WriteFile(filepath.Join(home, "secret"), []byte("x\n"), 0o644); err != nil {
39 t.Fatal(err)
40 }
41 gitInit(t, bare)
42 hook := "#!/bin/sh\nwhile read old new ref; do\n" +
43 " printf '%s %s\\n' \"$BLUEGIT_ROLE\" \"$ref\" >> " + shellQuote(filepath.Join(base, "roles")) + "\n" +
44 " if [ \"$ref\" = \"refs/heads/master\" ] && [ \"$BLUEGIT_ROLE\" != \"maintainer\" ]; then\n" +
45 " echo \"branch master is protected; only a maintainer may change it\" >&2\n" +
46 " exit 1\n" +
47 " fi\ndone\nexit 0\n"
48 if err := os.WriteFile(filepath.Join(bare, "hooks", "pre-receive"), []byte(hook), 0o755); err != nil {
49 t.Fatal(err)
50 }
51 env := dropEnv(os.Environ(), "HOME=", "TMPDIR=", "GIT_AUTHOR_", "GIT_COMMITTER_")
52 env = append(env,
53 "BLUEGIT_SANDBOX=inner",
54 "BLUEGIT_SANDBOX_BASE="+base,
55 "HOME="+home,
56 "TMPDIR="+home,
57 "GIT_AUTHOR_NAME=hostile",
58 "GIT_AUTHOR_EMAIL=hostile@example.com",
59 "GIT_COMMITTER_NAME=hostile",
60 "GIT_COMMITTER_EMAIL=hostile@example.com",
61 )
62 cmd := exec.Command("unshare", "-U", "-r", "-m", "--", os.Args[0], "-test.run", "^TestCommitOnBranchHardened$", "-test.count=1", "-test.v")
63 cmd.Env = env
64 out, err := cmd.CombinedOutput()
65 if err != nil {
66 t.Fatalf("sandbox: %v\n%s", err, out)
67 }
68 if !strings.Contains(string(out), "sandbox-ok") {
69 t.Fatalf("sandbox zonder bevestiging:\n%s", out)
70 }
71 roles, err := os.ReadFile(filepath.Join(base, "roles"))
72 if err != nil {
73 t.Fatal(err)
74 }
75 if !strings.Contains(string(roles), "maintainer refs/heads/master") {
76 t.Fatalf("hook zag de beheerder niet:\n%s", roles)
77 }
78 if !strings.Contains(string(roles), "developer refs/heads/master") {
79 t.Fatalf("hook zag de ontwikkelaar niet:\n%s", roles)
80 }
81}
82
83func commitSandboxInner(t *testing.T) {
84 if os.Getuid() != 0 {
85 t.Fatalf("uid %d, verwacht de namespace-root", os.Getuid())
86 }
87 base := os.Getenv("BLUEGIT_SANDBOX_BASE")
88 home := filepath.Join(base, "home")
89 bare := filepath.Join(base, "repo.git")
90 if out, err := exec.Command("mount", "-o", "bind,ro", home, home).CombinedOutput(); err != nil {
91 t.Fatalf("mount: %v\n%s", err, out)
92 }
93 if f, err := os.OpenFile(filepath.Join(home, "nope"), os.O_CREATE|os.O_WRONLY, 0o644); err == nil {
94 f.Close()
95 t.Fatal("homedirectory was beschrijfbaar")
96 }
97 os.Setenv("HOME", home)
98 os.Setenv("TMPDIR", home)
99 os.Setenv("GIT_AUTHOR_NAME", "hostile")
100 os.Setenv("GIT_AUTHOR_EMAIL", "hostile@example.com")
101 os.Setenv("GIT_COMMITTER_NAME", "hostile")
102 os.Setenv("GIT_COMMITTER_EMAIL", "hostile@example.com")
103
104 work := filepath.Join(base, "state", "tmp", "edit")
105 body := "image: alpine:latest\n"
106 err := CommitOnBranch(context.Background(), "git", bare, work, "master", "timfa", "timfa@users.git.timfalken.com", "Add blue-ci-cd.yml", "", map[string]string{"blue-ci-cd.yml": body}, access.RoleMaintainer, []string{"master"})
107 if err != nil {
108 t.Fatal(err)
109 }
110 got := gitShow(t, bare, "show", "master:blue-ci-cd.yml")
111 if !strings.Contains(got, "alpine:latest") {
112 t.Fatalf("bestand: %s", got)
113 }
114 who := strings.TrimSpace(gitShow(t, bare, "log", "-1", "--format=%an %ae"))
115 if who != "timfa timfa@users.git.timfalken.com" {
116 t.Fatalf("identiteit %q", who)
117 }
118 if out, err := exec.Command("git", "--git-dir", bare, "rev-parse", "--verify", "master^").CombinedOutput(); err == nil {
119 t.Fatalf("eerste commit heeft een parent: %s", out)
120 }
121 err = CommitOnBranch(context.Background(), "git", bare, work, "master", "ada", "ada@users.git.timfalken.com", "ada op master", "", map[string]string{"blue-ci-cd.yml": "image: nope\n"}, access.RoleDeveloper, []string{"master"})
122 if err == nil {
123 t.Fatal("developer committe op beschermde master")
124 }
125 var ie i18n.Error
126 if !errors.As(err, &ie) || ie.Key != "edit.protected" {
127 t.Fatalf("weigering: %v", err)
128 }
129 if strings.Contains(gitShow(t, bare, "show", "master:blue-ci-cd.yml"), "nope") {
130 t.Fatal("master veranderde door de ontwikkelaar")
131 }
132 t.Log("sandbox-ok")
133}
134
135func gitInit(t *testing.T, bare string) {
136 t.Helper()
137 if out, err := exec.Command("git", "init", "--bare", "--initial-branch", "master", bare).CombinedOutput(); err != nil {
138 t.Fatalf("init: %v\n%s", err, out)
139 }
140}
141
142func gitShow(t *testing.T, bare string, args ...string) string {
143 t.Helper()
144 cmd := append([]string{"--git-dir", bare}, args...)
145 out, err := exec.Command("git", cmd...).CombinedOutput()
146 if err != nil {
147 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
148 }
149 return string(out)
150}
151
152func dropEnv(env []string, prefixes ...string) []string {
153 var out []string
154 for _, e := range env {
155 skip := false
156 for _, p := range prefixes {
157 if strings.HasPrefix(e, p) {
158 skip = true
159 break
160 }
161 }
162 if !skip {
163 out = append(out, e)
164 }
165 }
166 return out
167}
168
169// skipWithoutUserNS slaat de test over als unshare ontbreekt of als de
170// kernel user namespaces weigert. In een container is de binary er vaak
171// wel, maar geeft unshare(CLONE_NEWUSER|CLONE_NEWNS) EPERM.
172func skipWithoutUserNS(t *testing.T) {
173 t.Helper()
174 if _, err := exec.LookPath("unshare"); err != nil {
175 t.Skip("unshare ontbreekt")
176 }
177 out, err := exec.Command("unshare", "-U", "-r", "-m", "true").CombinedOutput()
178 if err != nil {
179 t.Skipf("user namespaces niet beschikbaar: %v: %s", err, out)
180 }
181}