BlueGit

timfa bluegit

Files

bluegit / internal / config / config.go

config.go

feat: noem de ingebouwde runner Steve
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed f32ad8a5 History
config.go 9.2 KiB Download Raw
1// Package config leest het YAML-configuratiebestand van BlueGit.
2package config
3
4import (
5 "fmt"
6 "net"
7 "net/url"
8 "os"
9 "strconv"
10 "strings"
11
12 "gitlab.com/Timfa/bluegit/internal/realip"
13 "gopkg.in/yaml.v3"
14)
15
16// Config is de installatie op de mini-PC.
17type Config struct {
18 BaseURL string `yaml:"base_url"`
19 Listen string `yaml:"listen"`
20 // StatePath is de kleine map met database, sleutel en het gekozen
21 // opslagpad. Leeg wordt /var/lib/bluegit.
22 StatePath string `yaml:"state_path"`
23 // StoragePath is optioneel. Leeg betekent dat de beheerder het pad
24 // in de webinterface kiest. Er is geen standaardpad.
25 StoragePath string `yaml:"storage_path"`
26 TrustedProxies []string `yaml:"trusted_proxies"`
27 // BehindProxy is het oude veld. Het wordt geweigerd: één boolean
28 // vertrouwt ofwel iedereen, ofwel niemand. De lijst hierboven beslist.
29 BehindProxy bool `yaml:"behind_proxy"`
30 Auth Auth `yaml:"auth"`
31 Git Git `yaml:"git"`
32 SSH SSH `yaml:"ssh"`
33 Runner Runner `yaml:"runner"`
34 CI CI `yaml:"ci"`
35
36 proxies *realip.Set
37}
38
39// Auth wijst naar auth.timfalken.com of naar de lokale ontwikkelmodus.
40type Auth struct {
41 Mode string `yaml:"mode"`
42 BaseURL string `yaml:"base_url"`
43 ClientID string `yaml:"client_id"`
44 ClientSecret string `yaml:"client_secret"`
45 ClientSecretFile string `yaml:"client_secret_file"`
46 RedirectPath string `yaml:"redirect_path"`
47 UserAgent string `yaml:"user_agent"`
48 DevUser string `yaml:"dev_user"`
49 DevAuthID int64 `yaml:"dev_auth_id"`
50}
51
52// Git wijst naar de git-binary en de standaardbranch van nieuwe repositories.
53type Git struct {
54 Binary string `yaml:"binary"`
55 DefaultBranch string `yaml:"default_branch"`
56 HookBinary string `yaml:"hook_binary"`
57}
58
59// SSH is de git-over-SSH-listener en het adres dat in clone-URL's staat.
60// Listen is waar dit proces bindt. PublicHost en PublicPort zijn wat
61// een gebruiker ziet; de ingress kan daar een andere poort voor gebruiken.
62type SSH struct {
63 Enabled bool `yaml:"enabled"`
64 Listen string `yaml:"listen"`
65 PublicHost string `yaml:"public_host"`
66 PublicPort int `yaml:"public_port"`
67}
68
69// Runner is de ingebouwde Docker-runner.
70type Runner struct {
71 Name string `yaml:"name"`
72}
73
74// CI stuurt de Docker-runner.
75type CI struct {
76 Enabled bool `yaml:"enabled"`
77 Docker string `yaml:"docker_binary"`
78 Concurrency int `yaml:"concurrency"`
79 Network string `yaml:"network"`
80 TimeoutSec int `yaml:"timeout_seconds"`
81}
82
83// Load leest path en vult ontbrekende waarden aan.
84func Load(path string) (Config, error) {
85 raw, err := os.ReadFile(path)
86 if err != nil {
87 return Config{}, fmt.Errorf("config lezen: %w", err)
88 }
89 var c Config
90 if err := yaml.Unmarshal(raw, &c); err != nil {
91 return Config{}, fmt.Errorf("config: %w", err)
92 }
93 c.applyDefaults()
94 if v := os.Getenv("BLUEGIT_STATE_PATH"); v != "" {
95 c.StatePath = v
96 }
97 if v := os.Getenv("BLUEGIT_STORAGE_PATH"); v != "" {
98 c.StoragePath = v
99 }
100 if v := os.Getenv("BLUEGIT_LISTEN"); v != "" {
101 c.Listen = v
102 }
103 if v := os.Getenv("BLUEGIT_AUTH_CLIENT_SECRET"); v != "" {
104 c.Auth.ClientSecret = v
105 }
106 if c.Auth.ClientSecret == "" && c.Auth.ClientSecretFile != "" {
107 b, err := os.ReadFile(c.Auth.ClientSecretFile)
108 if err != nil {
109 return Config{}, fmt.Errorf("clientgeheim: %w", err)
110 }
111 c.Auth.ClientSecret = strings.TrimSpace(string(b))
112 }
113 if err := c.Validate(); err != nil {
114 return Config{}, err
115 }
116 return c, nil
117}
118
119func (c *Config) applyDefaults() {
120 if c.Listen == "" {
121 c.Listen = "127.0.0.1:8080"
122 }
123 if c.StatePath == "" {
124 c.StatePath = "/var/lib/bluegit"
125 }
126 if c.Auth.Mode == "" {
127 c.Auth.Mode = "oauth"
128 }
129 if c.Auth.BaseURL == "" {
130 c.Auth.BaseURL = "https://auth.timfalken.com"
131 }
132 if c.Auth.RedirectPath == "" {
133 c.Auth.RedirectPath = "/auth/callback"
134 }
135 if c.Auth.UserAgent == "" {
136 c.Auth.UserAgent = "BlueGitAccountClient/0.1"
137 }
138 if c.Auth.DevUser == "" {
139 c.Auth.DevUser = "tim"
140 }
141 if c.Auth.DevAuthID == 0 {
142 c.Auth.DevAuthID = 1
143 }
144 if c.Git.Binary == "" {
145 c.Git.Binary = "git"
146 }
147 if c.Git.DefaultBranch == "" {
148 c.Git.DefaultBranch = "master"
149 }
150 if c.SSH.Listen == "" {
151 c.SSH.Listen = "0.0.0.0:2222"
152 }
153 if c.CI.Docker == "" {
154 c.CI.Docker = "docker"
155 }
156 if c.CI.Concurrency <= 0 {
157 c.CI.Concurrency = 1
158 }
159 if c.CI.Network == "" {
160 c.CI.Network = "bridge"
161 }
162 if c.CI.TimeoutSec <= 0 {
163 c.CI.TimeoutSec = 1800
164 }
165 if strings.TrimSpace(c.Runner.Name) == "" {
166 c.Runner.Name = "Steve"
167 } else {
168 c.Runner.Name = strings.TrimSpace(c.Runner.Name)
169 }
170 c.Auth.BaseURL = strings.TrimRight(c.Auth.BaseURL, "/")
171 c.BaseURL = strings.TrimRight(c.BaseURL, "/")
172}
173
174// Validate keurt een configuratie goed of af.
175func (c *Config) Validate() error {
176 if c.BaseURL == "" {
177 return fmt.Errorf("base_url ontbreekt")
178 }
179 u, err := url.Parse(c.BaseURL)
180 if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
181 return fmt.Errorf("base_url moet een absolute http(s)-URL zijn")
182 }
183 if strings.TrimSpace(c.StatePath) == "" {
184 return fmt.Errorf("state_path ontbreekt")
185 }
186 if !strings.HasPrefix(c.Auth.RedirectPath, "/") {
187 return fmt.Errorf("auth.redirect_path moet met / beginnen")
188 }
189 switch c.Auth.Mode {
190 case "oauth":
191 if c.Auth.ClientID == "" || c.Auth.ClientSecret == "" {
192 return fmt.Errorf("oauth vereist auth.client_id en een clientgeheim (veld, bestand of BLUEGIT_AUTH_CLIENT_SECRET)")
193 }
194 if c.Auth.BaseURL == "" {
195 return fmt.Errorf("auth.base_url ontbreekt")
196 }
197 case "dev":
198 if strings.EqualFold(u.Hostname(), "git.timfalken.com") {
199 return fmt.Errorf("auth.mode dev is geweigerd voor git.timfalken.com")
200 }
201 default:
202 return fmt.Errorf("auth.mode moet oauth of dev zijn")
203 }
204 if c.BehindProxy {
205 return fmt.Errorf("behind_proxy doet niets meer; zet trusted_proxies op het IP of CIDR van de ingress en haal behind_proxy weg")
206 }
207 set, err := realip.Parse(c.TrustedProxies)
208 if err != nil {
209 return err
210 }
211 c.proxies = set
212 if c.SSH.PublicPort < 0 || c.SSH.PublicPort > 65535 {
213 return fmt.Errorf("ssh.public_port moet tussen 0 en 65535 liggen")
214 }
215 if h := strings.TrimSpace(c.SSH.PublicHost); h != "" {
216 if strings.Contains(h, "://") || strings.ContainsAny(h, " /") || (strings.Contains(h, ":") && net.ParseIP(h) == nil) {
217 return fmt.Errorf("ssh.public_host moet een hostnaam of IP zijn, zonder schema of poort")
218 }
219 }
220 if _, _, err := net.SplitHostPort(c.SSH.Listen); err != nil {
221 return fmt.Errorf("ssh.listen moet host:poort zijn")
222 }
223 return nil
224}
225
226// ProxySet is de geparsede trusted_proxies-lijst. Nil betekent dat
227// niemand vertrouwd wordt.
228func (c Config) ProxySet() *realip.Set { return c.proxies }
229
230// SSHPublicHost is de host in een SSH-clone-URL.
231// Leeg in de config: de host van base_url.
232func (c Config) SSHPublicHost() string {
233 if h := strings.TrimSpace(c.SSH.PublicHost); h != "" {
234 return h
235 }
236 u, err := url.Parse(c.BaseURL)
237 if err != nil {
238 return ""
239 }
240 return u.Hostname()
241}
242
243// SSHPublicPort is de poort in een SSH-clone-URL.
244// 0 in de config: de poort van ssh.listen.
245func (c Config) SSHPublicPort() int {
246 if c.SSH.PublicPort > 0 {
247 return c.SSH.PublicPort
248 }
249 _, port, err := net.SplitHostPort(c.SSH.Listen)
250 if err != nil {
251 return 22
252 }
253 n, err := strconv.Atoi(port)
254 if err != nil || n <= 0 {
255 return 22
256 }
257 return n
258}
259
260// CloneSSH is de URL die een gebruiker kloont. Leeg als SSH uit staat.
261func (c Config) CloneSSH(repoPath string) string {
262 if !c.SSH.Enabled {
263 return ""
264 }
265 host := c.SSHPublicHost()
266 if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
267 host = "[" + host + "]"
268 }
269 return fmt.Sprintf("ssh://git@%s:%d/%s.git", host, c.SSHPublicPort(), repoPath)
270}
271
272// RedirectURI is de callback die bij de auth-server geregistreerd moet staan.
273func (c Config) RedirectURI() string {
274 return c.BaseURL + c.Auth.RedirectPath
275}
276
277// Example is een startconfiguratie voor de mini-PC.
278func Example() string {
279 return `# BlueGit op de mini-PC. Kopieer naar /etc/bluegit/config.yml.
280# Plain HTTP. TLS en de publieke SSH-poort doet de ingress, zie docs/INGRESS.md.
281base_url: https://git.timfalken.com
282listen: 0.0.0.0:8080
283# Database, sleutel en het gekozen opslagpad. Dit is niet de datadisk.
284state_path: /var/lib/bluegit
285# storage_path blijft leeg. De installatiebeheerder kiest de datadisk
286# bij de eerste aanmelding. Er is geen standaardpad.
287# Alleen deze adressen mogen X-Forwarded-For en X-Real-IP zetten.
288# Zet hier het IP van de ingress, zoals de mini-PC die verbinding ziet.
289trusted_proxies:
290 - 10.0.0.2
291
292auth:
293 mode: oauth
294 base_url: https://auth.timfalken.com
295 # Het domein is de client_id. Registreer het pad /auth/callback bij /admin.
296 client_id: git.timfalken.com
297 # Laat leeg en zet BLUEGIT_AUTH_CLIENT_SECRET, of gebruik client_secret_file.
298 client_secret: ""
299 client_secret_file: /etc/bluegit/client_secret
300 redirect_path: /auth/callback
301 user_agent: BlueGitAccountClient/0.1
302
303git:
304 binary: git
305 default_branch: master
306
307ssh:
308 enabled: true
309 # Waar dit proces git-SSH aanneemt. De SSH van het besturingssysteem blijft vrij.
310 listen: 0.0.0.0:2222
311 # Wat gebruikers in de clone-URL zien. De ingress stuurt poort 22 hiernaartoe.
312 public_host: git.timfalken.com
313 public_port: 22
314
315runner:
316 name: Steve
317
318ci:
319 enabled: true
320 docker_binary: docker
321 concurrency: 1
322 network: bridge
323 timeout_seconds: 1800
324`
325}