BlueGit

timfa bluegit

config: auth.mode entra met tenant_id, issuer en startupvalidatie

Bestaande oauth-configs blijven ongewijzigd; base_url-default alleen bij oauth.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-11-12 · 5 days ago

Hash
788b8cb08d6825ade51385f52afd85fdb70d667b
Parent
e0d8bf3a
2 files changed +150 −4
internal/config/config.go +39 −4
@@ -42,9 +42,9 @@ type Config struct {
42 42 proxies *realip.Set
43 43 }
44 44
45 // Auth wijst naar auth.timfalken.com of naar de lokale ontwikkelmodus.
45 // Auth wijst naar auth.timfalken.com, Microsoft Entra ID, of de lokale ontwikkelmodus.
46 46 type Auth struct {
47 Mode string `yaml:"mode"`
47 Mode string `yaml:"mode"` // oauth | entra | dev
48 48 BaseURL string `yaml:"base_url"`
49 49 ClientID string `yaml:"client_id"`
50 50 ClientSecret string `yaml:"client_secret"`
@@ -53,6 +53,11 @@ type Auth struct {
53 53 UserAgent string `yaml:"user_agent"`
54 54 DevUser string `yaml:"dev_user"`
55 55 DevAuthID int64 `yaml:"dev_auth_id"`
56 // TenantID is verplicht bij mode entra (directory-/tenant-id).
57 TenantID string `yaml:"tenant_id"`
58 // Issuer overschrijft de Entra-issuer. Leeg: login.microsoftonline.com/{tenant}/v2.0.
59 // Voor tests of andere clouds (bijv. login.microsoftonline.us).
60 Issuer string `yaml:"issuer"`
56 61 }
57 62
58 63 // Git wijst naar de git-binary en de standaardbranch van nieuwe repositories.
@@ -173,7 +178,8 @@ func (c *Config) applyDefaults() {
173 178 if c.Auth.Mode == "" {
174 179 c.Auth.Mode = "oauth"
175 180 }
176 if c.Auth.BaseURL == "" {
181 // base_url hoort bij oauth (auth.timfalken.com). Entra gebruikt issuer/tenant.
182 if c.Auth.Mode == "oauth" && c.Auth.BaseURL == "" {
177 183 c.Auth.BaseURL = "https://auth.timfalken.com"
178 184 }
179 185 if c.Auth.RedirectPath == "" {
@@ -215,6 +221,8 @@ func (c *Config) applyDefaults() {
215 221 c.Runner.Name = strings.TrimSpace(c.Runner.Name)
216 222 }
217 223 c.Auth.BaseURL = strings.TrimRight(c.Auth.BaseURL, "/")
224 c.Auth.Issuer = strings.TrimRight(strings.TrimSpace(c.Auth.Issuer), "/")
225 c.Auth.TenantID = strings.TrimSpace(c.Auth.TenantID)
218 226 c.BaseURL = strings.TrimRight(c.BaseURL, "/")
219 227 }
220 228
@@ -241,12 +249,25 @@ func (c *Config) Validate() error {
241 249 if c.Auth.BaseURL == "" {
242 250 return fmt.Errorf("auth.base_url ontbreekt")
243 251 }
252 case "entra":
253 if c.Auth.TenantID == "" {
254 return fmt.Errorf("entra vereist auth.tenant_id")
255 }
256 if c.Auth.ClientID == "" || c.Auth.ClientSecret == "" {
257 return fmt.Errorf("entra vereist auth.client_id en een clientgeheim (veld, bestand of BLUEGIT_AUTH_CLIENT_SECRET)")
258 }
259 if c.Auth.Issuer != "" {
260 iu, err := url.Parse(c.Auth.Issuer)
261 if err != nil || (iu.Scheme != "https" && iu.Scheme != "http") || iu.Host == "" {
262 return fmt.Errorf("auth.issuer moet een absolute http(s)-URL zijn")
263 }
264 }
244 265 case "dev":
245 266 if strings.EqualFold(u.Hostname(), "git.timfalken.com") {
246 267 return fmt.Errorf("auth.mode dev is geweigerd voor git.timfalken.com")
247 268 }
248 269 default:
249 return fmt.Errorf("auth.mode moet oauth of dev zijn")
270 return fmt.Errorf("auth.mode moet oauth, entra of dev zijn")
250 271 }
251 272 if c.BehindProxy {
252 273 return fmt.Errorf("behind_proxy doet niets meer; zet trusted_proxies op het IP of CIDR van de ingress en haal behind_proxy weg")
@@ -329,6 +350,14 @@ func (c Config) RedirectURI() string {
329 350 return c.BaseURL + c.Auth.RedirectPath
330 351 }
331 352
353 // EntraIssuer is de OIDC-issuer voor mode entra.
354 func (c Config) EntraIssuer() string {
355 if c.Auth.Issuer != "" {
356 return c.Auth.Issuer
357 }
358 return "https://login.microsoftonline.com/" + c.Auth.TenantID + "/v2.0"
359 }
360
332 361 // Example is een startconfiguratie voor de mini-PC.
333 362 func Example() string {
334 363 return `# BlueGit op de mini-PC. Kopieer naar /etc/bluegit/config.yml.
@@ -354,6 +383,12 @@ auth:
354 383 client_secret_file: /etc/bluegit/client_secret
355 384 redirect_path: /auth/callback
356 385 user_agent: BlueGitAccountClient/0.1
386 # Alternatief: Microsoft Entra ID (OIDC, één tenant). Zet mode op entra
387 # en vul tenant_id + client_id; geheim hetzelfde als hierboven.
388 # mode: entra
389 # tenant_id: 00000000-0000-0000-0000-000000000000
390 # client_id: 11111111-1111-1111-1111-111111111111
391 # issuer blijft leeg, tenzij je een andere cloud of testissuer nodig hebt.
357 392
358 393 git:
359 394 binary: git
@@ -42,9 +42,9 @@ type Config struct {
42 proxies *realip.Set
42 proxies *realip.Set
43}
43}
44
44
45// Auth wijst naar auth.timfalken.com of naar de lokale ontwikkelmodus.
45// Auth wijst naar auth.timfalken.com, Microsoft Entra ID, of de lokale ontwikkelmodus.
46type Auth struct {
46type Auth struct {
47 Mode string `yaml:"mode"`
47 Mode string `yaml:"mode"` // oauth | entra | dev
48 BaseURL string `yaml:"base_url"`
48 BaseURL string `yaml:"base_url"`
49 ClientID string `yaml:"client_id"`
49 ClientID string `yaml:"client_id"`
50 ClientSecret string `yaml:"client_secret"`
50 ClientSecret string `yaml:"client_secret"`
@@ -53,6 +53,11 @@ type Auth struct {
53 UserAgent string `yaml:"user_agent"`
53 UserAgent string `yaml:"user_agent"`
54 DevUser string `yaml:"dev_user"`
54 DevUser string `yaml:"dev_user"`
55 DevAuthID int64 `yaml:"dev_auth_id"`
55 DevAuthID int64 `yaml:"dev_auth_id"`
56 // TenantID is verplicht bij mode entra (directory-/tenant-id).
57 TenantID string `yaml:"tenant_id"`
58 // Issuer overschrijft de Entra-issuer. Leeg: login.microsoftonline.com/{tenant}/v2.0.
59 // Voor tests of andere clouds (bijv. login.microsoftonline.us).
60 Issuer string `yaml:"issuer"`
56}
61}
57
62
58// Git wijst naar de git-binary en de standaardbranch van nieuwe repositories.
63// Git wijst naar de git-binary en de standaardbranch van nieuwe repositories.
@@ -173,7 +178,8 @@ func (c *Config) applyDefaults() {
173 if c.Auth.Mode == "" {
178 if c.Auth.Mode == "" {
174 c.Auth.Mode = "oauth"
179 c.Auth.Mode = "oauth"
175 }
180 }
176 if c.Auth.BaseURL == "" {
181 // base_url hoort bij oauth (auth.timfalken.com). Entra gebruikt issuer/tenant.
182 if c.Auth.Mode == "oauth" && c.Auth.BaseURL == "" {
177 c.Auth.BaseURL = "https://auth.timfalken.com"
183 c.Auth.BaseURL = "https://auth.timfalken.com"
178 }
184 }
179 if c.Auth.RedirectPath == "" {
185 if c.Auth.RedirectPath == "" {
@@ -215,6 +221,8 @@ func (c *Config) applyDefaults() {
215 c.Runner.Name = strings.TrimSpace(c.Runner.Name)
221 c.Runner.Name = strings.TrimSpace(c.Runner.Name)
216 }
222 }
217 c.Auth.BaseURL = strings.TrimRight(c.Auth.BaseURL, "/")
223 c.Auth.BaseURL = strings.TrimRight(c.Auth.BaseURL, "/")
224 c.Auth.Issuer = strings.TrimRight(strings.TrimSpace(c.Auth.Issuer), "/")
225 c.Auth.TenantID = strings.TrimSpace(c.Auth.TenantID)
218 c.BaseURL = strings.TrimRight(c.BaseURL, "/")
226 c.BaseURL = strings.TrimRight(c.BaseURL, "/")
219}
227}
220
228
@@ -241,12 +249,25 @@ func (c *Config) Validate() error {
241 if c.Auth.BaseURL == "" {
249 if c.Auth.BaseURL == "" {
242 return fmt.Errorf("auth.base_url ontbreekt")
250 return fmt.Errorf("auth.base_url ontbreekt")
243 }
251 }
252 case "entra":
253 if c.Auth.TenantID == "" {
254 return fmt.Errorf("entra vereist auth.tenant_id")
255 }
256 if c.Auth.ClientID == "" || c.Auth.ClientSecret == "" {
257 return fmt.Errorf("entra vereist auth.client_id en een clientgeheim (veld, bestand of BLUEGIT_AUTH_CLIENT_SECRET)")
258 }
259 if c.Auth.Issuer != "" {
260 iu, err := url.Parse(c.Auth.Issuer)
261 if err != nil || (iu.Scheme != "https" && iu.Scheme != "http") || iu.Host == "" {
262 return fmt.Errorf("auth.issuer moet een absolute http(s)-URL zijn")
263 }
264 }
244 case "dev":
265 case "dev":
245 if strings.EqualFold(u.Hostname(), "git.timfalken.com") {
266 if strings.EqualFold(u.Hostname(), "git.timfalken.com") {
246 return fmt.Errorf("auth.mode dev is geweigerd voor git.timfalken.com")
267 return fmt.Errorf("auth.mode dev is geweigerd voor git.timfalken.com")
247 }
268 }
248 default:
269 default:
249 return fmt.Errorf("auth.mode moet oauth of dev zijn")
270 return fmt.Errorf("auth.mode moet oauth, entra of dev zijn")
250 }
271 }
251 if c.BehindProxy {
272 if c.BehindProxy {
252 return fmt.Errorf("behind_proxy doet niets meer; zet trusted_proxies op het IP of CIDR van de ingress en haal behind_proxy weg")
273 return fmt.Errorf("behind_proxy doet niets meer; zet trusted_proxies op het IP of CIDR van de ingress en haal behind_proxy weg")
@@ -329,6 +350,14 @@ func (c Config) RedirectURI() string {
329 return c.BaseURL + c.Auth.RedirectPath
350 return c.BaseURL + c.Auth.RedirectPath
330}
351}
331
352
353// EntraIssuer is de OIDC-issuer voor mode entra.
354func (c Config) EntraIssuer() string {
355 if c.Auth.Issuer != "" {
356 return c.Auth.Issuer
357 }
358 return "https://login.microsoftonline.com/" + c.Auth.TenantID + "/v2.0"
359}
360
332// Example is een startconfiguratie voor de mini-PC.
361// Example is een startconfiguratie voor de mini-PC.
333func Example() string {
362func Example() string {
334 return `# BlueGit op de mini-PC. Kopieer naar /etc/bluegit/config.yml.
363 return `# BlueGit op de mini-PC. Kopieer naar /etc/bluegit/config.yml.
@@ -354,6 +383,12 @@ auth:
354 client_secret_file: /etc/bluegit/client_secret
383 client_secret_file: /etc/bluegit/client_secret
355 redirect_path: /auth/callback
384 redirect_path: /auth/callback
356 user_agent: BlueGitAccountClient/0.1
385 user_agent: BlueGitAccountClient/0.1
386 # Alternatief: Microsoft Entra ID (OIDC, één tenant). Zet mode op entra
387 # en vul tenant_id + client_id; geheim hetzelfde als hierboven.
388 # mode: entra
389 # tenant_id: 00000000-0000-0000-0000-000000000000
390 # client_id: 11111111-1111-1111-1111-111111111111
391 # issuer blijft leeg, tenzij je een andere cloud of testissuer nodig hebt.
357
392
358git:
393git:
359 binary: git
394 binary: git
internal/config/config_test.go +111 −0
@@ -102,6 +102,117 @@ func TestDevModeRefusedOnProductionHost(t *testing.T) {
102 102 }
103 103 }
104 104
105 func 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
128 func TestEntraConfig(t *testing.T) {
129 dir := t.TempDir()
130 path := filepath.Join(dir, "config.yml")
131 body := []byte(`base_url: https://git.example.test
132 auth:
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
156 auth:
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
174 func 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 for _, tc := range cases {
196 if err := os.WriteFile(path, []byte(tc.body), 0o600); err != nil {
197 t.Fatal(err)
198 }
199 _, err := Load(path)
200 if err == nil || !strings.Contains(err.Error(), tc.want) {
201 t.Fatalf("body %q: verwacht %q, kreeg %v", tc.body, tc.want, err)
202 }
203 }
204 // Zonder geheim:zelfde fouttekst als oauth.
205 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "")
206 body := []byte("base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n")
207 if err := os.WriteFile(path, body, 0o600); err != nil {
208 t.Fatal(err)
209 }
210 _, err := Load(path)
211 if err == nil || !strings.Contains(err.Error(), "clientgeheim") {
212 t.Fatalf("verwacht clientgeheim-fout, kreeg %v", err)
213 }
214 }
215
105 216 func TestSecretFromEnv(t *testing.T) {
106 217 dir := t.TempDir()
107 218 path := filepath.Join(dir, "config.yml")
@@ -102,6 +102,117 @@ func TestDevModeRefusedOnProductionHost(t *testing.T) {
102 }
102 }
103}
103}
104
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 for _, tc := range cases {
196 if err := os.WriteFile(path, []byte(tc.body), 0o600); err != nil {
197 t.Fatal(err)
198 }
199 _, err := Load(path)
200 if err == nil || !strings.Contains(err.Error(), tc.want) {
201 t.Fatalf("body %q: verwacht %q, kreeg %v", tc.body, tc.want, err)
202 }
203 }
204 // Zonder geheim:zelfde fouttekst als oauth.
205 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "")
206 body := []byte("base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n")
207 if err := os.WriteFile(path, body, 0o600); err != nil {
208 t.Fatal(err)
209 }
210 _, err := Load(path)
211 if err == nil || !strings.Contains(err.Error(), "clientgeheim") {
212 t.Fatalf("verwacht clientgeheim-fout, kreeg %v", err)
213 }
214}
215
105func TestSecretFromEnv(t *testing.T) {
216func TestSecretFromEnv(t *testing.T) {
106 dir := t.TempDir()
217 dir := t.TempDir()
107 path := filepath.Join(dir, "config.yml")
218 path := filepath.Join(dir, "config.yml")