BlueGit

timfa bluegit

Files

bluegit / internal / config / config.go

config.go

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