internal/config/config.go
176 lines · 4993 bytes
1// Package config loads and validates the forged server configuration.
2package config
3
4import (
5 "errors"
6 "fmt"
7 "net"
8 "os"
9 "strconv"
10
11 "github.com/BurntSushi/toml"
12)
13
14type Config struct {
15 Server Server `toml:"server"`
16 SSH SSH `toml:"ssh"`
17 HTTP HTTP `toml:"http"`
18 GitDaemon GitDaemon `toml:"git_daemon"`
19 Web Web `toml:"web"`
20 Registration Registration `toml:"registration"`
21 Limits Limits `toml:"limits"`
22 Mail Mail `toml:"mail"`
23}
24
25type Server struct {
26 Root string `toml:"root"`
27 SiteURL string `toml:"site_url"`
28}
29
30type SSH struct {
31 Mode string `toml:"mode"` // embedded | system
32 Port int `toml:"port"`
33 HostKeys []string `toml:"host_keys"`
34}
35
36type HTTP struct {
37 Addr string `toml:"addr"`
38 TLS string `toml:"tls"` // acme | files | off
39}
40
41type GitDaemon struct {
42 Enabled bool `toml:"enabled"`
43 Port int `toml:"port"`
44}
45
46type Web struct {
47 Mode string `toml:"mode"` // view_only | accounts
48 PasswordAuth bool `toml:"password_auth"`
49}
50
51type Registration struct {
52 Mode string `toml:"mode"` // closed | invite | open
53}
54
55type Limits struct {
56 MaxPackBytes int64 `toml:"max_pack_bytes"`
57 MaxBlobBytes int64 `toml:"max_blob_bytes"`
58 CloneTimeoutSec int `toml:"clone_timeout"`
59 SSHAuthRate int `toml:"ssh_auth_rate"`
60}
61
62type Mail struct {
63 SMTPHost string `toml:"smtp_host"`
64 From string `toml:"from"`
65}
66
67// Default returns the configuration used when a key is absent from the file.
68func Default() Config {
69 return Config{
70 Server: Server{Root: "/var/lib/forge"},
71 SSH: SSH{Mode: "embedded", Port: 22},
72 HTTP: HTTP{Addr: ":443", TLS: "acme"},
73 Web: Web{Mode: "view_only"},
74 Registration: Registration{
75 Mode: "closed",
76 },
77 GitDaemon: GitDaemon{Port: 9418},
78 Limits: Limits{
79 MaxPackBytes: 2 << 30, // 2 GiB
80 MaxBlobBytes: 100 << 20,
81 CloneTimeoutSec: 3600,
82 SSHAuthRate: 10,
83 },
84 }
85}
86
87// Load reads path, applies defaults, and validates. It does not probe the
88// host (see CheckHost) so it is safe in tests and on non-target machines.
89func Load(path string) (Config, error) {
90 cfg := Default()
91 md, err := toml.DecodeFile(path, &cfg)
92 if err != nil {
93 return cfg, err
94 }
95 if u := md.Undecoded(); len(u) > 0 {
96 return cfg, fmt.Errorf("unknown config key %q", u[0].String())
97 }
98 return cfg, cfg.Validate()
99}
100
101func oneOf(field, val string, allowed ...string) error {
102 for _, a := range allowed {
103 if val == a {
104 return nil
105 }
106 }
107 return fmt.Errorf("%s must be one of %v, got %q", field, allowed, val)
108}
109
110// Validate applies the static contradiction checks from the plan.
111func (c Config) Validate() error {
112 var errs []error
113
114 if c.Server.Root == "" {
115 errs = append(errs, errors.New("server.root is required"))
116 }
117 if c.Server.SiteURL == "" {
118 errs = append(errs, errors.New("server.site_url is required"))
119 }
120 if err := oneOf("ssh.mode", c.SSH.Mode, "embedded", "system"); err != nil {
121 errs = append(errs, err)
122 }
123 if c.SSH.Port < 1 || c.SSH.Port > 65535 {
124 errs = append(errs, fmt.Errorf("ssh.port %d out of range", c.SSH.Port))
125 }
126 if err := oneOf("http.tls", c.HTTP.TLS, "acme", "files", "off"); err != nil {
127 errs = append(errs, err)
128 }
129 if err := oneOf("web.mode", c.Web.Mode, "view_only", "accounts"); err != nil {
130 errs = append(errs, err)
131 }
132 if err := oneOf("registration.mode", c.Registration.Mode, "closed", "invite", "open"); err != nil {
133 errs = append(errs, err)
134 }
135
136 // Contradictions.
137 if c.Registration.Mode != "closed" && c.Mail.SMTPHost == "" {
138 errs = append(errs, fmt.Errorf(
139 "registration.mode = %q requires [mail] smtp_host: email verification cannot run without SMTP",
140 c.Registration.Mode))
141 }
142 if c.SSH.Mode == "system" && c.Registration.Mode != "closed" {
143 errs = append(errs, fmt.Errorf(
144 "ssh.mode = \"system\" requires registration.mode = \"closed\": host sshd rejects unknown keys before the dispatcher runs, so registration by unknown key is impossible"))
145 }
146 if c.Web.PasswordAuth && c.Web.Mode == "view_only" {
147 errs = append(errs, errors.New(
148 "web.password_auth = true is meaningless with web.mode = \"view_only\": no login route exists"))
149 }
150
151 return errors.Join(errs...)
152}
153
154// CheckHost performs environment probes that only make sense on the target
155// machine: port availability for the embedded listener and root existence.
156func (c Config) CheckHost() error {
157 var errs []error
158
159 if st, err := os.Stat(c.Server.Root); err != nil {
160 errs = append(errs, fmt.Errorf("server.root: %w", err))
161 } else if !st.IsDir() {
162 errs = append(errs, fmt.Errorf("server.root %q is not a directory", c.Server.Root))
163 }
164
165 if c.SSH.Mode == "embedded" {
166 addr := net.JoinHostPort("", strconv.Itoa(c.SSH.Port))
167 ln, err := net.Listen("tcp", addr)
168 if err != nil {
169 errs = append(errs, fmt.Errorf("ssh.port %d is not bindable (already in use by another daemon?): %w", c.SSH.Port, err))
170 } else {
171 ln.Close()
172 }
173 }
174
175 return errors.Join(errs...)
176}