1package config
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strconv"
8 "strings"
9 "time"
10
11 "github.com/caarlos0/env/v8"
12 "github.com/charmbracelet/soft-serve/server/sshutils"
13 "golang.org/x/crypto/ssh"
14 "gopkg.in/yaml.v3"
15)
16
17// SSHConfig is the configuration for the SSH server.
18type SSHConfig struct {
19 // ListenAddr is the address on which the SSH server will listen.
20 ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
21
22 // PublicURL is the public URL of the SSH server.
23 PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
24
25 // KeyPath is the path to the SSH server's private key.
26 KeyPath string `env:"KEY_PATH" yaml:"key_path"`
27
28 // ClientKeyPath is the path to the server's client private key.
29 ClientKeyPath string `env:"CLIENT_KEY_PATH" yaml:"client_key_path"`
30
31 // MaxTimeout is the maximum number of seconds a connection can take.
32 MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout"`
33
34 // IdleTimeout is the number of seconds a connection can be idle before it is closed.
35 IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
36}
37
38// GitConfig is the Git daemon configuration for the server.
39type GitConfig struct {
40 // ListenAddr is the address on which the Git daemon will listen.
41 ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
42
43 // PublicURL is the public URL of the Git daemon server.
44 PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
45
46 // MaxTimeout is the maximum number of seconds a connection can take.
47 MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout"`
48
49 // IdleTimeout is the number of seconds a connection can be idle before it is closed.
50 IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
51
52 // MaxConnections is the maximum number of concurrent connections.
53 MaxConnections int `env:"MAX_CONNECTIONS" yaml:"max_connections"`
54}
55
56// HTTPConfig is the HTTP configuration for the server.
57type HTTPConfig struct {
58 // ListenAddr is the address on which the HTTP server will listen.
59 ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
60
61 // TLSKeyPath is the path to the TLS private key.
62 TLSKeyPath string `env:"TLS_KEY_PATH" yaml:"tls_key_path"`
63
64 // TLSCertPath is the path to the TLS certificate.
65 TLSCertPath string `env:"TLS_CERT_PATH" yaml:"tls_cert_path"`
66
67 // PublicURL is the public URL of the HTTP server.
68 PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
69}
70
71// StatsConfig is the configuration for the stats server.
72type StatsConfig struct {
73 // ListenAddr is the address on which the stats server will listen.
74 ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
75}
76
77// LogConfig is the logger configuration.
78type LogConfig struct {
79 // Format is the format of the logs.
80 // Valid values are "json", "logfmt", and "text".
81 Format string `env:"FORMAT" yaml:"format"`
82
83 // Time format for the log `ts` field.
84 // Format must be described in Golang's time format.
85 TimeFormat string `env:"TIME_FORMAT" yaml:"time_format"`
86
87 // Path to a file to write logs to.
88 // If not set, logs will be written to stderr.
89 Path string `env:"PATH" yaml:"path"`
90}
91
92// DBConfig is the database connection configuration.
93type DBConfig struct {
94 // Driver is the driver for the database.
95 Driver string `env:"DRIVER" yaml:"driver"`
96
97 // DataSource is the database data source name.
98 DataSource string `env:"DATA_SOURCE" yaml:"data_source"`
99}
100
101// LFSConfig is the configuration for Git LFS.
102type LFSConfig struct {
103 // Enabled is whether or not Git LFS is enabled.
104 Enabled bool `env:"ENABLED" yaml:"enabled"`
105
106 // SSHEnabled is whether or not Git LFS over SSH is enabled.
107 // This is only used if LFS is enabled.
108 SSHEnabled bool `env:"SSH_ENABLED" yaml:"ssh_enabled"`
109}
110
111// Config is the configuration for Soft Serve.
112type Config struct {
113 // Name is the name of the server.
114 Name string `env:"NAME" yaml:"name"`
115
116 // SSH is the configuration for the SSH server.
117 SSH SSHConfig `envPrefix:"SSH_" yaml:"ssh"`
118
119 // Git is the configuration for the Git daemon.
120 Git GitConfig `envPrefix:"GIT_" yaml:"git"`
121
122 // HTTP is the configuration for the HTTP server.
123 HTTP HTTPConfig `envPrefix:"HTTP_" yaml:"http"`
124
125 // Stats is the configuration for the stats server.
126 Stats StatsConfig `envPrefix:"STATS_" yaml:"stats"`
127
128 // Log is the logger configuration.
129 Log LogConfig `envPrefix:"LOG_" yaml:"log"`
130
131 // DB is the database configuration.
132 DB DBConfig `envPrefix:"DB_" yaml:"db"`
133
134 // LFS is the configuration for Git LFS.
135 LFS LFSConfig `envPrefix:"LFS_" yaml:"lfs"`
136
137 // InitialAdminKeys is a list of public keys that will be added to the list of admins.
138 InitialAdminKeys []string `env:"INITIAL_ADMIN_KEYS" envSeparator:"\n" yaml:"initial_admin_keys"`
139
140 // DataPath is the path to the directory where Soft Serve will store its data.
141 DataPath string `env:"DATA_PATH" yaml:"-"`
142}
143
144// Environ returns the config as a list of environment variables.
145func (c *Config) Environ() []string {
146 envs := []string{}
147 if c == nil {
148 return envs
149 }
150
151 // TODO: do this dynamically
152 envs = append(envs, []string{
153 fmt.Sprintf("SOFT_SERVE_DATA_PATH=%s", c.DataPath),
154 fmt.Sprintf("SOFT_SERVE_NAME=%s", c.Name),
155 fmt.Sprintf("SOFT_SERVE_INITIAL_ADMIN_KEYS=%s", strings.Join(c.InitialAdminKeys, "\n")),
156 fmt.Sprintf("SOFT_SERVE_SSH_LISTEN_ADDR=%s", c.SSH.ListenAddr),
157 fmt.Sprintf("SOFT_SERVE_SSH_PUBLIC_URL=%s", c.SSH.PublicURL),
158 fmt.Sprintf("SOFT_SERVE_SSH_KEY_PATH=%s", c.SSH.KeyPath),
159 fmt.Sprintf("SOFT_SERVE_SSH_CLIENT_KEY_PATH=%s", c.SSH.ClientKeyPath),
160 fmt.Sprintf("SOFT_SERVE_SSH_MAX_TIMEOUT=%d", c.SSH.MaxTimeout),
161 fmt.Sprintf("SOFT_SERVE_SSH_IDLE_TIMEOUT=%d", c.SSH.IdleTimeout),
162 fmt.Sprintf("SOFT_SERVE_GIT_LISTEN_ADDR=%s", c.Git.ListenAddr),
163 fmt.Sprintf("SOFT_SERVE_GIT_PUBLIC_URL=%s", c.Git.PublicURL),
164 fmt.Sprintf("SOFT_SERVE_GIT_MAX_TIMEOUT=%d", c.Git.MaxTimeout),
165 fmt.Sprintf("SOFT_SERVE_GIT_IDLE_TIMEOUT=%d", c.Git.IdleTimeout),
166 fmt.Sprintf("SOFT_SERVE_GIT_MAX_CONNECTIONS=%d", c.Git.MaxConnections),
167 fmt.Sprintf("SOFT_SERVE_HTTP_LISTEN_ADDR=%s", c.HTTP.ListenAddr),
168 fmt.Sprintf("SOFT_SERVE_HTTP_TLS_KEY_PATH=%s", c.HTTP.TLSKeyPath),
169 fmt.Sprintf("SOFT_SERVE_HTTP_TLS_CERT_PATH=%s", c.HTTP.TLSCertPath),
170 fmt.Sprintf("SOFT_SERVE_HTTP_PUBLIC_URL=%s", c.HTTP.PublicURL),
171 fmt.Sprintf("SOFT_SERVE_STATS_LISTEN_ADDR=%s", c.Stats.ListenAddr),
172 fmt.Sprintf("SOFT_SERVE_LOG_FORMAT=%s", c.Log.Format),
173 fmt.Sprintf("SOFT_SERVE_LOG_TIME_FORMAT=%s", c.Log.TimeFormat),
174 fmt.Sprintf("SOFT_SERVE_DB_DRIVER=%s", c.DB.Driver),
175 fmt.Sprintf("SOFT_SERVE_DB_DATA_SOURCE=%s", c.DB.DataSource),
176 fmt.Sprintf("SOFT_SERVE_LFS_ENABLED=%t", c.LFS.Enabled),
177 fmt.Sprintf("SOFT_SERVE_LFS_SSH_ENABLED=%t", c.LFS.SSHEnabled),
178 }...)
179
180 return envs
181}
182
183// IsDebug returns true if the server is running in debug mode.
184func IsDebug() bool {
185 debug, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_DEBUG"))
186 return debug
187}
188
189// IsVerbose returns true if the server is running in verbose mode.
190// Verbose mode is only enabled if debug mode is enabled.
191func IsVerbose() bool {
192 verbose, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_VERBOSE"))
193 return IsDebug() && verbose
194}
195
196// parseFile parses the given file as a configuration file.
197// The file must be in YAML format.
198func parseFile(cfg *Config, path string) error {
199 f, err := os.Open(path)
200 if err != nil {
201 return err
202 }
203
204 defer f.Close() // nolint: errcheck
205 if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
206 return fmt.Errorf("decode config: %w", err)
207 }
208
209 return cfg.Validate()
210}
211
212// ParseFile parses the config from the default file path.
213// This also calls Validate() on the config.
214func (c *Config) ParseFile() error {
215 return parseFile(c, c.ConfigPath())
216}
217
218// parseEnv parses the environment variables as a configuration file.
219func parseEnv(cfg *Config) error {
220 // Merge initial admin keys from both config file and environment variables.
221 initialAdminKeys := append([]string{}, cfg.InitialAdminKeys...)
222
223 // Override with environment variables
224 if err := env.ParseWithOptions(cfg, env.Options{
225 Prefix: "SOFT_SERVE_",
226 }); err != nil {
227 return fmt.Errorf("parse environment variables: %w", err)
228 }
229
230 // Merge initial admin keys from environment variables.
231 if initialAdminKeysEnv := os.Getenv("SOFT_SERVE_INITIAL_ADMIN_KEYS"); initialAdminKeysEnv != "" {
232 cfg.InitialAdminKeys = append(cfg.InitialAdminKeys, initialAdminKeys...)
233 }
234
235 return cfg.Validate()
236}
237
238// ParseEnv parses the config from the environment variables.
239// This also calls Validate() on the config.
240func (c *Config) ParseEnv() error {
241 return parseEnv(c)
242}
243
244// Parse parses the config from the default file path and environment variables.
245// This also calls Validate() on the config.
246func (c *Config) Parse() error {
247 if err := c.ParseFile(); err != nil {
248 return err
249 }
250
251 return c.ParseEnv()
252}
253
254// writeConfig writes the configuration to the given file.
255func writeConfig(cfg *Config, path string) error {
256 if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
257 return err
258 }
259 return os.WriteFile(path, []byte(newConfigFile(cfg)), 0o644) // nolint: errcheck, gosec
260}
261
262// WriteConfig writes the configuration to the default file.
263func (c *Config) WriteConfig() error {
264 return writeConfig(c, c.ConfigPath())
265}
266
267// DefaultDataPath returns the path to the data directory.
268// It uses the SOFT_SERVE_DATA_PATH environment variable if set, otherwise it
269// uses "data".
270func DefaultDataPath() string {
271 dp := os.Getenv("SOFT_SERVE_DATA_PATH")
272 if dp == "" {
273 dp = "data"
274 }
275
276 return dp
277}
278
279// ConfigPath returns the path to the config file.
280func (c *Config) ConfigPath() string { // nolint:revive
281 return filepath.Join(c.DataPath, "config.yaml")
282}
283
284func exist(path string) bool {
285 _, err := os.Stat(path)
286 return err == nil
287}
288
289// Exist returns true if the config file exists.
290func (c *Config) Exist() bool {
291 return exist(filepath.Join(c.DataPath, "config.yaml"))
292}
293
294// DefaultConfig returns the default Config. All the path values are relative
295// to the data directory.
296// Use Validate() to validate the config and ensure absolute paths.
297func DefaultConfig() *Config {
298 return &Config{
299 Name: "Soft Serve",
300 DataPath: DefaultDataPath(),
301 SSH: SSHConfig{
302 ListenAddr: ":23231",
303 PublicURL: "ssh://localhost:23231",
304 KeyPath: filepath.Join("ssh", "soft_serve_host_ed25519"),
305 ClientKeyPath: filepath.Join("ssh", "soft_serve_client_ed25519"),
306 MaxTimeout: 0,
307 IdleTimeout: 10 * 60, // 10 minutes
308 },
309 Git: GitConfig{
310 ListenAddr: ":9418",
311 PublicURL: "git://localhost",
312 MaxTimeout: 0,
313 IdleTimeout: 3,
314 MaxConnections: 32,
315 },
316 HTTP: HTTPConfig{
317 ListenAddr: ":23232",
318 PublicURL: "http://localhost:23232",
319 },
320 Stats: StatsConfig{
321 ListenAddr: "localhost:23233",
322 },
323 Log: LogConfig{
324 Format: "text",
325 TimeFormat: time.DateTime,
326 },
327 DB: DBConfig{
328 Driver: "sqlite",
329 DataSource: "soft-serve.db" +
330 "?_pragma=busy_timeout(5000)&_pragma=foreign_keys(1)",
331 },
332 LFS: LFSConfig{
333 Enabled: true,
334 SSHEnabled: true,
335 },
336 }
337}
338
339// Validate validates the configuration.
340// It updates the configuration with absolute paths.
341func (c *Config) Validate() error {
342 // Use absolute paths
343 if !filepath.IsAbs(c.DataPath) {
344 dp, err := filepath.Abs(c.DataPath)
345 if err != nil {
346 return err
347 }
348 c.DataPath = dp
349 }
350
351 c.SSH.PublicURL = strings.TrimSuffix(c.SSH.PublicURL, "/")
352 c.HTTP.PublicURL = strings.TrimSuffix(c.HTTP.PublicURL, "/")
353
354 if c.SSH.KeyPath != "" && !filepath.IsAbs(c.SSH.KeyPath) {
355 c.SSH.KeyPath = filepath.Join(c.DataPath, c.SSH.KeyPath)
356 }
357
358 if c.SSH.ClientKeyPath != "" && !filepath.IsAbs(c.SSH.ClientKeyPath) {
359 c.SSH.ClientKeyPath = filepath.Join(c.DataPath, c.SSH.ClientKeyPath)
360 }
361
362 if c.HTTP.TLSKeyPath != "" && !filepath.IsAbs(c.HTTP.TLSKeyPath) {
363 c.HTTP.TLSKeyPath = filepath.Join(c.DataPath, c.HTTP.TLSKeyPath)
364 }
365
366 if c.HTTP.TLSCertPath != "" && !filepath.IsAbs(c.HTTP.TLSCertPath) {
367 c.HTTP.TLSCertPath = filepath.Join(c.DataPath, c.HTTP.TLSCertPath)
368 }
369
370 if strings.HasPrefix(c.DB.Driver, "sqlite") && !filepath.IsAbs(c.DB.DataSource) {
371 c.DB.DataSource = filepath.Join(c.DataPath, c.DB.DataSource)
372 }
373
374 // Validate keys
375 pks := make([]string, 0)
376 for _, key := range parseAuthKeys(c.InitialAdminKeys) {
377 ak := sshutils.MarshalAuthorizedKey(key)
378 pks = append(pks, ak)
379 }
380
381 c.InitialAdminKeys = pks
382
383 return nil
384}
385
386// parseAuthKeys parses authorized keys from either file paths or string authorized_keys.
387func parseAuthKeys(aks []string) []ssh.PublicKey {
388 exist := make(map[string]struct{}, 0)
389 pks := make([]ssh.PublicKey, 0)
390 for _, key := range aks {
391 if bts, err := os.ReadFile(key); err == nil {
392 // key is a file
393 key = strings.TrimSpace(string(bts))
394 }
395
396 if pk, _, err := sshutils.ParseAuthorizedKey(key); err == nil {
397 if _, ok := exist[key]; !ok {
398 pks = append(pks, pk)
399 exist[key] = struct{}{}
400 }
401 }
402 }
403 return pks
404}
405
406// AdminKeys returns the server admin keys.
407func (c *Config) AdminKeys() []ssh.PublicKey {
408 return parseAuthKeys(c.InitialAdminKeys)
409}