internal/config/config.go
491 lines · 17561 bytes
1// Package config loads and validates the gitbayd server configuration.
2package config
3
4import (
5 "crypto/ecdsa"
6 "crypto/x509"
7 "encoding/pem"
8 "errors"
9 "fmt"
10 "net"
11 "os"
12 "strconv"
13 "strings"
14 "time"
15
16 "github.com/BurntSushi/toml"
17)
18
19// DefaultWriteRate is the per-account write budget when the config leaves
20// write_rate at zero: generous for a person at a terminal, and a bound on
21// what one account can enqueue — every write also queues notification mail
22// and webhook deliveries.
23const DefaultWriteRate = 60
24
25type Config struct {
26 Server Server `toml:"server"`
27 SSH SSH `toml:"ssh"`
28 HTTP HTTP `toml:"http"`
29 GitDaemon GitDaemon `toml:"git_daemon"`
30 Web Web `toml:"web"`
31 Registration Registration `toml:"registration"`
32 API API `toml:"api"`
33 Webhooks Webhooks `toml:"webhooks"`
34 Pages Pages `toml:"pages"`
35 LFS LFS `toml:"lfs"`
36 Limits Limits `toml:"limits"`
37 Mail Mail `toml:"mail"`
38 Mirrors Mirrors `toml:"mirrors"`
39 Deps Deps `toml:"deps"`
40 Retention Retention `toml:"retention"`
41 Push Push `toml:"push"`
42 // GoImport maps vanity Go module paths to repositories, e.g.
43 // "gitbay.org/gitbay" = "krz/gitbay". Requests carrying ?go-get=1
44 // under a mapped path get a go-import meta tag.
45 GoImport map[string]string `toml:"go_import"`
46}
47
48type Server struct {
49 Root string `toml:"root"`
50 SiteURL string `toml:"site_url"`
51
52 // SourceRepo names the repository this instance develops itself in, as
53 // "owner/name". When set, startup warns if the running build's commit is
54 // not on that repository's default branch. Empty disables the check, which
55 // is right for any instance that does not host its own source.
56 SourceRepo string `toml:"source_repo"`
57}
58
59type SSH struct {
60 Mode string `toml:"mode"` // embedded | system
61 Port int `toml:"port"`
62 HostKeys []string `toml:"host_keys"`
63}
64
65type HTTP struct {
66 Addr string `toml:"addr"`
67 TLS string `toml:"tls"` // acme | files | off
68 CertFile string `toml:"cert_file"`
69 KeyFile string `toml:"key_file"`
70 // ACME (Let's Encrypt by default). Certificates are cached under
71 // server.root/acme. acme_http_addr serves HTTP-01 challenges and
72 // redirects to HTTPS; "off" disables it (TLS-ALPN-01 on the HTTPS
73 // port still works).
74 ACMEEmail string `toml:"acme_email"`
75 ACMEHTTPAddr string `toml:"acme_http_addr"`
76 // TrustedProxies are the addresses or CIDRs of reverse proxies in front
77 // of this process. A request from one of them is attributed to the
78 // last X-Forwarded-For hop that is not itself a trusted proxy; from
79 // anyone else the peer address is the client and the header is
80 // ignored. Empty means no proxy, which is how gitbayd is deployed by
81 // default: it terminates TLS itself.
82 TrustedProxies []string `toml:"trusted_proxies,omitempty"`
83}
84
85type GitDaemon struct {
86 Enabled bool `toml:"enabled"`
87 Port int `toml:"port"`
88}
89
90type Web struct {
91 Mode string `toml:"mode"` // view_only | accounts
92 PasswordAuth bool `toml:"password_auth"`
93 // Title is the instance's display name in the header and page titles.
94 // Empty falls back to the site host.
95 Title string `toml:"title"`
96 // PrivacyNotice is operator-provided text shown on /privacy under the
97 // fixed project-level statement. Plain text; blank paragraphs split.
98 PrivacyNotice string `toml:"privacy_notice"`
99}
100
101type Registration struct {
102 Mode string `toml:"mode"` // closed | invite | open
103 // PendingExpiry is how long a self-registered account may stay
104 // unverified before it is removed, as a duration ("168h"). Empty
105 // keeps such accounts forever.
106 PendingExpiry string `toml:"pending_expiry"`
107 // NotifyAdmin mails the instance's admins when an account becomes
108 // active: an invite redeemed, or an open-mode signup that verified
109 // its address. The unverified row an open signup creates is not
110 // reported — anyone can post the form, so mailing on that would
111 // aim a flood at the admins (#234).
112 NotifyAdmin bool `toml:"notify_admin"`
113}
114
115// PendingExpiryDuration parses PendingExpiry; zero means never.
116func (r Registration) PendingExpiryDuration() time.Duration {
117 d, _ := time.ParseDuration(r.PendingExpiry)
118 return d
119}
120
121// Retention is how long the append-only tables keep a row. Each is a
122// duration string ("2160h"); empty or zero keeps forever, which is what
123// an instance that has never configured this gets. Expired sessions and
124// tokens are swept regardless: they are dead weight the moment they
125// expire and no setting makes them worth keeping.
126type Retention struct {
127 Audit string `toml:"audit"`
128 Events string `toml:"events"`
129 WebhookDeliveries string `toml:"webhook_deliveries"`
130 // Mail is the outbound queue: rows already sent or given up on.
131 Mail string `toml:"mail"`
132 // Push is the outbound device queue: rows already sent or given up on.
133 Push string `toml:"push"`
134}
135
136// Durations parses the five, mapping each to zero when unset or bad.
137func (r Retention) Durations() (audit, events, deliveries, mail, push time.Duration) {
138 parse := func(s string) time.Duration {
139 d, err := time.ParseDuration(s)
140 if err != nil || d < 0 {
141 return 0
142 }
143 return d
144 }
145 return parse(r.Audit), parse(r.Events), parse(r.WebhookDeliveries), parse(r.Mail), parse(r.Push)
146}
147
148// LFS stores large-file objects content-addressed under Root (default
149// <server.root>/lfs). MaxObjectBytes caps a single object; 0 means the
150// 512MB default.
151type LFS struct {
152 Root string `toml:"root"`
153 MaxObjectBytes int64 `toml:"max_object_bytes"`
154}
155
156// Pages serves each public repo's `pages` branch as a static site on
157// <owner>.<domain> — a separate origin, so page-authored scripts never run
158// on the forge's own host. Empty domain disables the feature.
159type Pages struct {
160 Domain string `toml:"domain"`
161}
162
163// API controls the HTTPS/JSON control-plane API (bearer tokens minted over
164// SSH). Off by default: an instance that never enables it has no
165// credential-bearing HTTP surface at all.
166type API struct {
167 Enabled bool `toml:"enabled"`
168}
169
170// Webhooks controls outbound delivery. AllowLocal permits endpoints on
171// loopback/private addresses (off by default: SSRF).
172type Webhooks struct {
173 AllowLocal bool `toml:"allow_local"`
174}
175
176type Mirrors struct {
177 PullIntervalMinutes int `toml:"pull_interval_minutes"`
178}
179
180// Deps configures the dependency-update sweep. It runs only for repos that
181// have opted in with `repo deps enable`, because checking a private repo
182// tells a public registry what it depends on.
183type Deps struct {
184 CheckIntervalHours int `toml:"check_interval_hours"`
185}
186
187type Limits struct {
188 MaxPackBytes int64 `toml:"max_pack_bytes"`
189 MaxBlobBytes int64 `toml:"max_blob_bytes"`
190 MaxAssetBytes int64 `toml:"max_asset_bytes"` // per release asset
191 MaxSnippetBytes int64 `toml:"max_snippet_bytes"` // per snippet file
192 // MaxSnippetsPerUser caps snippets an account may own. 0 means
193 // unlimited, like MaxReposPerUser.
194 MaxSnippetsPerUser int `toml:"max_snippets_per_user"`
195 CloneTimeoutSec int `toml:"clone_timeout"`
196 SSHAuthRate int `toml:"ssh_auth_rate"`
197 // APIRate is sustained JSON-API requests per minute per caller; writes
198 // draw on a tenth of it. 0 uses the default.
199 APIRate int `toml:"api_rate"`
200 // WriteRate is sustained mutating commands per minute per account,
201 // counted in the dispatcher so every surface shares one budget. 0 uses
202 // the default; a negative value turns the limit off.
203 WriteRate int `toml:"write_rate"`
204 // Per-account quotas on what a user owns directly (organizations are
205 // not capped). 0 means unlimited; admin user limits overrides per
206 // account.
207 MaxReposPerUser int `toml:"max_repos_per_user"`
208 MaxBytesPerUser int64 `toml:"max_bytes_per_user"`
209}
210
211type Mail struct {
212 SMTPHost string `toml:"smtp_host"` // host:port (port defaults to 587)
213 From string `toml:"from"`
214 SMTPUser string `toml:"smtp_user,omitempty"`
215 SMTPPass string `toml:"smtp_pass,omitempty"`
216}
217
218// Push is APNs delivery to registered Apple devices. A key belongs to a
219// bundle ID, so an instance pushes to the app built under the topic named
220// here and no other; a self-hoster points this at their own key and their
221// own build.
222type Push struct {
223 Enabled bool `toml:"enabled"`
224 KeyFile string `toml:"key_file"`
225 KeyID string `toml:"key_id"`
226 TeamID string `toml:"team_id"`
227 Topic string `toml:"topic"` // the app's bundle identifier
228 // Environment is a name rather than a URL so a typo cannot aim the
229 // key at a host that is not Apple's.
230 Environment string `toml:"environment"` // production | sandbox
231}
232
233// Host is the APNs endpoint for the configured environment.
234// GITBAY_APNS_HOST overrides it for tests, as GITBAY_SWEEP_TICK does for
235// the retention sweep.
236func (p Push) Host() string {
237 if h := os.Getenv("GITBAY_APNS_HOST"); h != "" {
238 return h
239 }
240 if p.Environment == "sandbox" {
241 return "api.sandbox.push.apple.com"
242 }
243 return "api.push.apple.com"
244}
245
246// LoadAPNSKey reads Apple's .p8 provider key: a PEM-wrapped PKCS#8
247// P-256 private key. Read at startup and validated there, so a
248// misconfigured [push] refuses to start rather than filling a queue
249// nobody is watching.
250func LoadAPNSKey(path string) (*ecdsa.PrivateKey, error) {
251 data, err := os.ReadFile(path)
252 if err != nil {
253 return nil, err
254 }
255 block, _ := pem.Decode(data)
256 if block == nil {
257 return nil, errors.New("not PEM")
258 }
259 any, err := x509.ParsePKCS8PrivateKey(block.Bytes)
260 if err != nil {
261 return nil, err
262 }
263 key, ok := any.(*ecdsa.PrivateKey)
264 if !ok {
265 return nil, errors.New("not an EC private key")
266 }
267 return key, nil
268}
269
270// Default returns the configuration used when a key is absent from the file.
271func Default() Config {
272 return Config{
273 Server: Server{Root: "/var/lib/gitbay"},
274 SSH: SSH{Mode: "embedded", Port: 22},
275 HTTP: HTTP{Addr: ":443", TLS: "acme", ACMEHTTPAddr: ":80"},
276 Web: Web{Mode: "view_only"},
277 Registration: Registration{
278 Mode: "closed",
279 },
280 GitDaemon: GitDaemon{Port: 9418},
281 Mirrors: Mirrors{PullIntervalMinutes: 15},
282 Deps: Deps{CheckIntervalHours: 24},
283 Limits: Limits{
284 MaxPackBytes: 2 << 30, // 2 GiB
285 MaxBlobBytes: 100 << 20,
286 MaxAssetBytes: 512 << 20,
287 MaxSnippetBytes: 1 << 20,
288 CloneTimeoutSec: 3600,
289 SSHAuthRate: 10,
290 APIRate: 120,
291 },
292 }
293}
294
295// Load reads path, applies defaults, and validates. It does not probe the
296// host (see CheckHost) so it is safe in tests and on non-target machines.
297func Load(path string) (Config, error) {
298 cfg := Default()
299 md, err := toml.DecodeFile(path, &cfg)
300 if err != nil {
301 return cfg, err
302 }
303 if u := md.Undecoded(); len(u) > 0 {
304 return cfg, fmt.Errorf("unknown config key %q", u[0].String())
305 }
306 return cfg, cfg.Validate()
307}
308
309func oneOf(field, val string, allowed ...string) error {
310 for _, a := range allowed {
311 if val == a {
312 return nil
313 }
314 }
315 return fmt.Errorf("%s must be one of %v, got %q", field, allowed, val)
316}
317
318// Validate applies the static contradiction checks from the plan.
319func (c Config) Validate() error {
320 var errs []error
321
322 if c.Server.Root == "" {
323 errs = append(errs, errors.New("server.root is required"))
324 }
325 if d := c.Pages.Domain; d != "" {
326 if d == c.SiteHost() {
327 errs = append(errs, errors.New("pages.domain must differ from the site host: pages serve repo-authored scripts, which must not run on the forge's origin"))
328 }
329 if strings.HasSuffix(c.SiteHost(), "."+d) {
330 errs = append(errs, errors.New("pages.domain must not be a parent of the site host"))
331 }
332 }
333 if c.Server.SiteURL == "" {
334 errs = append(errs, errors.New("server.site_url is required"))
335 }
336 if err := oneOf("ssh.mode", c.SSH.Mode, "embedded", "system"); err != nil {
337 errs = append(errs, err)
338 }
339 if c.Registration.PendingExpiry != "" {
340 if d, err := time.ParseDuration(c.Registration.PendingExpiry); err != nil || d <= 0 {
341 errs = append(errs, fmt.Errorf("registration.pending_expiry %q must be a positive duration such as 168h", c.Registration.PendingExpiry))
342 }
343 }
344 if c.Limits.MaxReposPerUser < 0 || c.Limits.MaxBytesPerUser < 0 || c.Limits.MaxSnippetsPerUser < 0 {
345 errs = append(errs, errors.New("limits.max_repos_per_user, max_bytes_per_user and max_snippets_per_user must not be negative"))
346 }
347 if c.Push.Enabled {
348 for _, f := range []struct{ name, val string }{
349 {"push.key_file", c.Push.KeyFile},
350 {"push.key_id", c.Push.KeyID},
351 {"push.team_id", c.Push.TeamID},
352 {"push.topic", c.Push.Topic},
353 } {
354 if f.val == "" {
355 errs = append(errs, fmt.Errorf("%s is required when push.enabled", f.name))
356 }
357 }
358 if err := oneOf("push.environment", c.Push.Environment, "production", "sandbox"); err != nil {
359 errs = append(errs, err)
360 }
361 if c.Push.KeyFile != "" {
362 if _, err := LoadAPNSKey(c.Push.KeyFile); err != nil {
363 errs = append(errs, fmt.Errorf("push.key_file: %w", err))
364 }
365 }
366 }
367 if c.SSH.Port < 1 || c.SSH.Port > 65535 {
368 errs = append(errs, fmt.Errorf("ssh.port %d out of range", c.SSH.Port))
369 }
370 if err := oneOf("http.tls", c.HTTP.TLS, "acme", "files", "off"); err != nil {
371 errs = append(errs, err)
372 }
373 if _, err := c.HTTP.TrustedProxyNets(); err != nil {
374 errs = append(errs, err)
375 }
376 if c.HTTP.TLS == "files" && (c.HTTP.CertFile == "" || c.HTTP.KeyFile == "") {
377 errs = append(errs, errors.New("http.tls = \"files\" requires cert_file and key_file"))
378 }
379 if c.HTTP.TLS == "acme" {
380 host := c.SiteHost()
381 switch {
382 case !strings.HasPrefix(c.Server.SiteURL, "https://"):
383 errs = append(errs, errors.New("http.tls = \"acme\" requires an https:// site_url: certificates are issued for that host"))
384 case host == "" || host == "localhost" || net.ParseIP(host) != nil:
385 errs = append(errs, fmt.Errorf("http.tls = \"acme\" cannot issue a certificate for %q: use a public DNS name in site_url", host))
386 }
387 }
388 if err := oneOf("web.mode", c.Web.Mode, "view_only", "accounts"); err != nil {
389 errs = append(errs, err)
390 }
391 if err := oneOf("registration.mode", c.Registration.Mode, "closed", "invite", "open"); err != nil {
392 errs = append(errs, err)
393 }
394
395 for module, repo := range c.GoImport {
396 host, _, ok := strings.Cut(module, "/")
397 if !ok || host == "" || !strings.Contains(host, ".") {
398 errs = append(errs, fmt.Errorf("go_import key %q must be host/path (e.g. gitbay.org/gitbay)", module))
399 }
400 if parts := strings.Split(repo, "/"); len(parts) != 2 || parts[0] == "" || parts[1] == "" {
401 errs = append(errs, fmt.Errorf("go_import value %q must be owner/name", repo))
402 }
403 }
404
405 // Contradictions.
406 if c.Mail.SMTPHost != "" && c.Mail.From == "" {
407 errs = append(errs, errors.New("[mail] from is required when smtp_host is set"))
408 }
409 if c.Registration.Mode != "closed" && c.Mail.SMTPHost == "" {
410 errs = append(errs, fmt.Errorf(
411 "registration.mode = %q requires [mail] smtp_host: email verification cannot run without SMTP",
412 c.Registration.Mode))
413 }
414 if c.Registration.NotifyAdmin && c.Mail.SMTPHost == "" {
415 errs = append(errs, errors.New(
416 "registration.notify_admin = true requires [mail] smtp_host: there is nowhere to send the notice"))
417 }
418 if c.SSH.Mode == "system" && c.Registration.Mode != "closed" {
419 errs = append(errs, fmt.Errorf(
420 "ssh.mode = \"system\" requires registration.mode = \"closed\": host sshd rejects unknown keys before the dispatcher runs, so registration by unknown key is impossible"))
421 }
422 if c.Web.PasswordAuth && c.Web.Mode == "view_only" {
423 errs = append(errs, errors.New(
424 "web.password_auth = true is meaningless with web.mode = \"view_only\": no login route exists"))
425 }
426 if c.Web.PasswordAuth && c.Web.Mode == "accounts" {
427 errs = append(errs, errors.New(
428 "web.password_auth is not implemented yet; browser sessions are minted over SSH (gitbay web login)"))
429 }
430
431 return errors.Join(errs...)
432}
433
434// SiteHost returns the bare hostname from site_url (no scheme, port, path).
435func (c Config) SiteHost() string {
436 h := strings.TrimPrefix(strings.TrimPrefix(c.Server.SiteURL, "https://"), "http://")
437 h = strings.TrimSuffix(h, "/")
438 if i := strings.IndexByte(h, '/'); i >= 0 {
439 h = h[:i]
440 }
441 if host, _, err := net.SplitHostPort(h); err == nil {
442 return host
443 }
444 return h
445}
446
447// CheckHost performs environment probes that only make sense on the target
448// machine: port availability for the embedded listener and root existence.
449func (c Config) CheckHost() error {
450 var errs []error
451
452 if st, err := os.Stat(c.Server.Root); err != nil {
453 errs = append(errs, fmt.Errorf("server.root: %w", err))
454 } else if !st.IsDir() {
455 errs = append(errs, fmt.Errorf("server.root %q is not a directory", c.Server.Root))
456 }
457
458 if c.SSH.Mode == "embedded" {
459 addr := net.JoinHostPort("", strconv.Itoa(c.SSH.Port))
460 ln, err := net.Listen("tcp", addr)
461 if err != nil {
462 errs = append(errs, fmt.Errorf("ssh.port %d is not bindable (already in use by another daemon?): %w", c.SSH.Port, err))
463 } else {
464 ln.Close()
465 }
466 }
467
468 return errors.Join(errs...)
469}
470
471// TrustedProxyNets parses http.trusted_proxies; a bare address is a /32
472// or /128.
473func (h HTTP) TrustedProxyNets() ([]*net.IPNet, error) {
474 var nets []*net.IPNet
475 for _, p := range h.TrustedProxies {
476 if _, n, err := net.ParseCIDR(p); err == nil {
477 nets = append(nets, n)
478 continue
479 }
480 ip := net.ParseIP(p)
481 if ip == nil {
482 return nil, fmt.Errorf("http.trusted_proxies: %q is not an address or CIDR", p)
483 }
484 bits := 32
485 if ip.To4() == nil {
486 bits = 128
487 }
488 nets = append(nets, &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)})
489 }
490 return nets, nil
491}