BlueGit

timfa bluegit

Files

bluegit / internal / config / config.go

config.go

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