BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / commit_test.go

commit_test.go

fix: commit de pipeline-editor ook in een lege repository
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed a437eecd History
commit_test.go 5.5 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 if _, err := exec.LookPath("unshare"); err != nil {
28 t.Skip("unshare ontbreekt")
29 }
30 base := t.TempDir()
31 home := filepath.Join(base, "home")
32 state := filepath.Join(base, "state", "tmp")
33 bare := filepath.Join(base, "repo.git")
34 if err := os.MkdirAll(home, 0o755); err != nil {
35 t.Fatal(err)
36 }
37 if err := os.MkdirAll(state, 0o755); err != nil {
38 t.Fatal(err)
39 }
40 if err := os.WriteFile(filepath.Join(home, "secret"), []byte("x\n"), 0o644); err != nil {
41 t.Fatal(err)
42 }
43 gitInit(t, bare)
44 hook := "#!/bin/sh\nwhile read old new ref; do\n" +
45 " printf '%s %s\\n' \"$BLUEGIT_ROLE\" \"$ref\" >> " + shellQuote(filepath.Join(base, "roles")) + "\n" +
46 " if [ \"$ref\" = \"refs/heads/master\" ] && [ \"$BLUEGIT_ROLE\" != \"maintainer\" ]; then\n" +
47 " echo \"branch master is protected; only a maintainer may change it\" >&2\n" +
48 " exit 1\n" +
49 " fi\ndone\nexit 0\n"
50 if err := os.WriteFile(filepath.Join(bare, "hooks", "pre-receive"), []byte(hook), 0o755); err != nil {
51 t.Fatal(err)
52 }
53 env := dropEnv(os.Environ(), "HOME=", "TMPDIR=", "GIT_AUTHOR_", "GIT_COMMITTER_")
54 env = append(env,
55 "BLUEGIT_SANDBOX=inner",
56 "BLUEGIT_SANDBOX_BASE="+base,
57 "HOME="+home,
58 "TMPDIR="+home,
59 "GIT_AUTHOR_NAME=hostile",
60 "GIT_AUTHOR_EMAIL=hostile@example.com",
61 "GIT_COMMITTER_NAME=hostile",
62 "GIT_COMMITTER_EMAIL=hostile@example.com",
63 )
64 cmd := exec.Command("unshare", "-U", "-r", "-m", "--", os.Args[0], "-test.run", "^TestCommitOnBranchHardened$", "-test.count=1", "-test.v")
65 cmd.Env = env
66 out, err := cmd.CombinedOutput()
67 if err != nil {
68 t.Fatalf("sandbox: %v\n%s", err, out)
69 }
70 if !strings.Contains(string(out), "sandbox-ok") {
71 t.Fatalf("sandbox zonder bevestiging:\n%s", out)
72 }
73 roles, err := os.ReadFile(filepath.Join(base, "roles"))
74 if err != nil {
75 t.Fatal(err)
76 }
77 if !strings.Contains(string(roles), "maintainer refs/heads/master") {
78 t.Fatalf("hook zag de beheerder niet:\n%s", roles)
79 }
80 if !strings.Contains(string(roles), "developer refs/heads/master") {
81 t.Fatalf("hook zag de ontwikkelaar niet:\n%s", roles)
82 }
83}
84
85func commitSandboxInner(t *testing.T) {
86 if os.Getuid() != 0 {
87 t.Fatalf("uid %d, verwacht de namespace-root", os.Getuid())
88 }
89 base := os.Getenv("BLUEGIT_SANDBOX_BASE")
90 home := filepath.Join(base, "home")
91 bare := filepath.Join(base, "repo.git")
92 if out, err := exec.Command("mount", "-o", "bind,ro", home, home).CombinedOutput(); err != nil {
93 t.Fatalf("mount: %v\n%s", err, out)
94 }
95 if f, err := os.OpenFile(filepath.Join(home, "nope"), os.O_CREATE|os.O_WRONLY, 0o644); err == nil {
96 f.Close()
97 t.Fatal("homedirectory was beschrijfbaar")
98 }
99 os.Setenv("HOME", home)
100 os.Setenv("TMPDIR", home)
101 os.Setenv("GIT_AUTHOR_NAME", "hostile")
102 os.Setenv("GIT_AUTHOR_EMAIL", "hostile@example.com")
103 os.Setenv("GIT_COMMITTER_NAME", "hostile")
104 os.Setenv("GIT_COMMITTER_EMAIL", "hostile@example.com")
105
106 work := filepath.Join(base, "state", "tmp", "edit")
107 body := "image: alpine:latest\n"
108 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"})
109 if err != nil {
110 t.Fatal(err)
111 }
112 got := gitShow(t, bare, "show", "master:blue-ci-cd.yml")
113 if !strings.Contains(got, "alpine:latest") {
114 t.Fatalf("bestand: %s", got)
115 }
116 who := strings.TrimSpace(gitShow(t, bare, "log", "-1", "--format=%an %ae"))
117 if who != "timfa timfa@users.git.timfalken.com" {
118 t.Fatalf("identiteit %q", who)
119 }
120 if out, err := exec.Command("git", "--git-dir", bare, "rev-parse", "--verify", "master^").CombinedOutput(); err == nil {
121 t.Fatalf("eerste commit heeft een parent: %s", out)
122 }
123 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"})
124 if err == nil {
125 t.Fatal("developer committe op beschermde master")
126 }
127 var ie i18n.Error
128 if !errors.As(err, &ie) || ie.Key != "edit.protected" {
129 t.Fatalf("weigering: %v", err)
130 }
131 if strings.Contains(gitShow(t, bare, "show", "master:blue-ci-cd.yml"), "nope") {
132 t.Fatal("master veranderde door de ontwikkelaar")
133 }
134 t.Log("sandbox-ok")
135}
136
137func gitInit(t *testing.T, bare string) {
138 t.Helper()
139 if out, err := exec.Command("git", "init", "--bare", "--initial-branch", "master", bare).CombinedOutput(); err != nil {
140 t.Fatalf("init: %v\n%s", err, out)
141 }
142}
143
144func gitShow(t *testing.T, bare string, args ...string) string {
145 t.Helper()
146 cmd := append([]string{"--git-dir", bare}, args...)
147 out, err := exec.Command("git", cmd...).CombinedOutput()
148 if err != nil {
149 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
150 }
151 return string(out)
152}
153
154func dropEnv(env []string, prefixes ...string) []string {
155 var out []string
156 for _, e := range env {
157 skip := false
158 for _, p := range prefixes {
159 if strings.HasPrefix(e, p) {
160 skip = true
161 break
162 }
163 }
164 if !skip {
165 out = append(out, e)
166 }
167 }
168 return out
169}