BlueGit

timfa bluegit

Files

bluegit / internal / config / config_test.go

config_test.go

config_test.go 11.0 KiB Download Raw
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestPresetConfig(t *testing.T) {
11 dir := t.TempDir()
12 path := filepath.Join(dir, "config.yml")
13 docker := filepath.Join(dir, "Dockerfile")
14 if err := os.WriteFile(docker, []byte("FROM alpine:3.20\nRUN apk add --no-cache git\n"), 0o644); err != nil {
15 t.Fatal(err)
16 }
17 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\nci:\n presets:\n bluegit/node:\n from: node:22-bookworm\n team/tools:\n dockerfile: Dockerfile\n team/plain:\n image: alpine:3.20\n")
18 if err := os.WriteFile(path, body, 0o600); err != nil {
19 t.Fatal(err)
20 }
21 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
22 cfg, err := Load(path)
23 if err != nil {
24 t.Fatal(err)
25 }
26 if cfg.CI.Presets["team/tools"].Dockerfile != docker {
27 t.Fatalf("pad %s", cfg.CI.Presets["team/tools"].Dockerfile)
28 }
29 if cfg.CI.Presets["bluegit/node"].From != "node:22-bookworm" || cfg.CI.Presets["team/plain"].Image != "alpine:3.20" {
30 t.Fatalf("%+v", cfg.CI.Presets)
31 }
32 bad := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\nci:\n presets:\n node:22:\n image: alpine\n")
33 if err := os.WriteFile(path, bad, 0o600); err != nil {
34 t.Fatal(err)
35 }
36 if _, err := Load(path); err == nil {
37 t.Fatal("een tag in de aliasnaam hoort te falen")
38 }
39}
40
41func TestRunnerName(t *testing.T) {
42 dir := t.TempDir()
43 path := filepath.Join(dir, "config.yml")
44 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
45 if err := os.WriteFile(path, body, 0o600); err != nil {
46 t.Fatal(err)
47 }
48 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
49 cfg, err := Load(path)
50 if err != nil {
51 t.Fatal(err)
52 }
53 if cfg.Runner.Name != "Steve" {
54 t.Fatalf("standaard %q", cfg.Runner.Name)
55 }
56 body = []byte("base_url: https://git.example.test\nrunner:\n name: \" Ada \"\nauth:\n mode: oauth\n client_id: git.example.test\n")
57 if err := os.WriteFile(path, body, 0o600); err != nil {
58 t.Fatal(err)
59 }
60 cfg, err = Load(path)
61 if err != nil {
62 t.Fatal(err)
63 }
64 if cfg.Runner.Name != "Ada" {
65 t.Fatalf("naam %q", cfg.Runner.Name)
66 }
67}
68
69func TestStoragePathOptional(t *testing.T) {
70 dir := t.TempDir()
71 path := filepath.Join(dir, "config.yml")
72 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
73 if err := os.WriteFile(path, body, 0o600); err != nil {
74 t.Fatal(err)
75 }
76 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
77 cfg, err := Load(path)
78 if err != nil {
79 t.Fatal(err)
80 }
81 if cfg.StoragePath != "" {
82 t.Fatalf("geen standaardpad verwacht, kreeg %q", cfg.StoragePath)
83 }
84 if cfg.StatePath != "/var/lib/bluegit" {
85 t.Fatal(cfg.StatePath)
86 }
87 if strings.Contains(Example(), "/mnt/storage") {
88 t.Fatal("voorbeeldconfiguratie bevat nog een vast schijfpad")
89 }
90}
91
92func TestDevModeRefusedOnProductionHost(t *testing.T) {
93 dir := t.TempDir()
94 path := filepath.Join(dir, "config.yml")
95 body := []byte("base_url: https://git.timfalken.com\nstorage_path: /mnt/storage/bluegit\nauth:\n mode: dev\n")
96 if err := os.WriteFile(path, body, 0o600); err != nil {
97 t.Fatal(err)
98 }
99 _, err := Load(path)
100 if err == nil || !strings.Contains(err.Error(), "dev") {
101 t.Fatalf("verwacht weigering, kreeg %v", err)
102 }
103}
104
105func TestOAuthDefaultsUnchanged(t *testing.T) {
106 dir := t.TempDir()
107 path := filepath.Join(dir, "config.yml")
108 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
109 if err := os.WriteFile(path, body, 0o600); err != nil {
110 t.Fatal(err)
111 }
112 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
113 cfg, err := Load(path)
114 if err != nil {
115 t.Fatal(err)
116 }
117 if cfg.Auth.Mode != "oauth" || cfg.Auth.BaseURL != "https://auth.timfalken.com" {
118 t.Fatalf("%+v", cfg.Auth)
119 }
120 if cfg.Auth.RedirectPath != "/auth/callback" || cfg.Auth.UserAgent != "BlueGitAccountClient/0.1" {
121 t.Fatalf("%+v", cfg.Auth)
122 }
123 if cfg.Auth.TenantID != "" || cfg.Auth.Issuer != "" {
124 t.Fatalf("entra-velden horen leeg: %+v", cfg.Auth)
125 }
126}
127
128func TestEntraConfig(t *testing.T) {
129 dir := t.TempDir()
130 path := filepath.Join(dir, "config.yml")
131 body := []byte(`base_url: https://git.example.test
132auth:
133 mode: entra
134 tenant_id: 00000000-0000-0000-0000-000000000001
135 client_id: 11111111-1111-1111-1111-111111111111
136`)
137 if err := os.WriteFile(path, body, 0o600); err != nil {
138 t.Fatal(err)
139 }
140 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "entra-geheim")
141 cfg, err := Load(path)
142 if err != nil {
143 t.Fatal(err)
144 }
145 if cfg.Auth.BaseURL != "" {
146 t.Fatalf("entra zet geen auth.timfalken-default, kreeg %q", cfg.Auth.BaseURL)
147 }
148 if cfg.EntraIssuer() != "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000001/v2.0" {
149 t.Fatal(cfg.EntraIssuer())
150 }
151 if cfg.Auth.ClientSecret != "entra-geheim" || cfg.RedirectURI() != "https://git.example.test/auth/callback" {
152 t.Fatalf("%+v", cfg.Auth)
153 }
154
155 body = []byte(`base_url: https://git.example.test
156auth:
157 mode: entra
158 tenant_id: 00000000-0000-0000-0000-000000000001
159 client_id: 11111111-1111-1111-1111-111111111111
160 issuer: https://login.test.example/t/v2.0
161`)
162 if err := os.WriteFile(path, body, 0o600); err != nil {
163 t.Fatal(err)
164 }
165 cfg, err = Load(path)
166 if err != nil {
167 t.Fatal(err)
168 }
169 if cfg.EntraIssuer() != "https://login.test.example/t/v2.0" {
170 t.Fatal(cfg.EntraIssuer())
171 }
172}
173
174func TestEntraValidation(t *testing.T) {
175 dir := t.TempDir()
176 path := filepath.Join(dir, "config.yml")
177 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
178 cases := []struct {
179 body string
180 want string
181 }{
182 {
183 body: "base_url: https://git.example.test\nauth:\n mode: entra\n client_id: app\n",
184 want: "tenant_id",
185 },
186 {
187 body: "base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n",
188 want: "client_id",
189 },
190 {
191 body: "base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n issuer: niet-een-url\n",
192 want: "issuer",
193 },
194 {
195 body: "base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n issuer: http://login.microsoftonline.com/t/v2.0\n",
196 want: "HTTPS",
197 },
198 }
199 for _, tc := range cases {
200 if err := os.WriteFile(path, []byte(tc.body), 0o600); err != nil {
201 t.Fatal(err)
202 }
203 _, err := Load(path)
204 if err == nil || !strings.Contains(err.Error(), tc.want) {
205 t.Fatalf("body %q: verwacht %q, kreeg %v", tc.body, tc.want, err)
206 }
207 }
208 // Zonder geheim:zelfde fouttekst als oauth.
209 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "")
210 body := []byte("base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n")
211 if err := os.WriteFile(path, body, 0o600); err != nil {
212 t.Fatal(err)
213 }
214 _, err := Load(path)
215 if err == nil || !strings.Contains(err.Error(), "clientgeheim") {
216 t.Fatalf("verwacht clientgeheim-fout, kreeg %v", err)
217 }
218}
219
220func TestEntraIssuerHTTPLoopback(t *testing.T) {
221 dir := t.TempDir()
222 path := filepath.Join(dir, "config.yml")
223 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
224 body := []byte(`base_url: https://git.example.test
225auth:
226 mode: entra
227 tenant_id: t1
228 client_id: app
229 issuer: http://127.0.0.1:8081/t/v2.0
230`)
231 if err := os.WriteFile(path, body, 0o600); err != nil {
232 t.Fatal(err)
233 }
234 cfg, err := Load(path)
235 if err != nil {
236 t.Fatal(err)
237 }
238 if cfg.EntraIssuer() != "http://127.0.0.1:8081/t/v2.0" {
239 t.Fatal(cfg.EntraIssuer())
240 }
241}
242
243func TestSecretFromEnv(t *testing.T) {
244 dir := t.TempDir()
245 path := filepath.Join(dir, "config.yml")
246 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\n")
247 if err := os.WriteFile(path, body, 0o600); err != nil {
248 t.Fatal(err)
249 }
250 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "uit-de-omgeving")
251 cfg, err := Load(path)
252 if err != nil {
253 t.Fatal(err)
254 }
255 if cfg.Auth.ClientSecret != "uit-de-omgeving" || cfg.Git.DefaultBranch != "master" {
256 t.Fatalf("branch %s geheim %s", cfg.Git.DefaultBranch, cfg.Auth.ClientSecret)
257 }
258 if cfg.RedirectURI() != "https://git.example.test/auth/callback" {
259 t.Fatal(cfg.RedirectURI())
260 }
261 if cfg.SSHPublicHost() != "git.example.test" || cfg.SSHPublicPort() != 2222 {
262 t.Fatalf("ssh %s:%d", cfg.SSHPublicHost(), cfg.SSHPublicPort())
263 }
264 if cfg.CloneSSH("tim/demo") != "" {
265 t.Fatal("ssh staat uit, dus geen clone-URL")
266 }
267}
268
269func TestPublicSSHAndTrustedProxy(t *testing.T) {
270 dir := t.TempDir()
271 path := filepath.Join(dir, "config.yml")
272 body := []byte(`base_url: https://git.timfalken.com
273storage_path: ` + dir + `
274listen: 0.0.0.0:8080
275trusted_proxies: ["10.0.0.2", "192.168.0.0/16"]
276auth:
277 mode: oauth
278 client_id: git.timfalken.com
279ssh:
280 enabled: true
281 listen: 0.0.0.0:2222
282 public_host: git.timfalken.com
283 public_port: 22
284`)
285 if err := os.WriteFile(path, body, 0o600); err != nil {
286 t.Fatal(err)
287 }
288 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
289 cfg, err := Load(path)
290 if err != nil {
291 t.Fatal(err)
292 }
293 if cfg.CloneSSH("tim/demo") != "git@git.timfalken.com:tim/demo.git" {
294 t.Fatal(cfg.CloneSSH("tim/demo"))
295 }
296 if cfg.ProxySet() == nil || cfg.Listen != "0.0.0.0:8080" {
297 t.Fatalf("%+v", cfg)
298 }
299}
300
301func TestBehindProxyFlagRejected(t *testing.T) {
302 dir := t.TempDir()
303 path := filepath.Join(dir, "config.yml")
304 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nbehind_proxy: true\nauth:\n mode: oauth\n client_id: git.example.test\n")
305 if err := os.WriteFile(path, body, 0o600); err != nil {
306 t.Fatal(err)
307 }
308 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
309 _, err := Load(path)
310 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
311 t.Fatalf("verwacht weigering van behind_proxy, kreeg %v", err)
312 }
313}
314
315func TestBadProxyRejected(t *testing.T) {
316 dir := t.TempDir()
317 path := filepath.Join(dir, "config.yml")
318 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\ntrusted_proxies: [ingress.local]\nauth:\n mode: oauth\n client_id: git.example.test\n")
319 if err := os.WriteFile(path, body, 0o600); err != nil {
320 t.Fatal(err)
321 }
322 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
323 _, err := Load(path)
324 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
325 t.Fatalf("verwacht weigering, kreeg %v", err)
326 }
327 body = []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\nssh:\n enabled: true\n public_host: git.example.test:22\n")
328 path = filepath.Join(dir, "port.yml")
329 if err := os.WriteFile(path, body, 0o600); err != nil {
330 t.Fatal(err)
331 }
332 _, err = Load(path)
333 if err == nil || !strings.Contains(err.Error(), "public_host") {
334 t.Fatalf("verwacht weigering van een poort in de host, kreeg %v", err)
335 }
336}
337
338func TestCloneSSHForms(t *testing.T) {
339 c := Config{SSH: SSH{Enabled: true, PublicHost: "git.example.test", PublicPort: 22}}
340 if got := c.CloneSSH("tim/demo"); got != "git@git.example.test:tim/demo.git" {
341 t.Fatal(got)
342 }
343 c.SSH.PublicPort = 2222
344 if got := c.CloneSSH("tim/demo"); got != "ssh://git@git.example.test:2222/tim/demo.git" {
345 t.Fatal(got)
346 }
347 c.SSH.PublicHost, c.SSH.PublicPort = "::1", 22
348 if got := c.CloneSSH("tim/demo"); got != "git@[::1]:tim/demo.git" {
349 t.Fatal(got)
350 }
351}