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