-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
204 lines (159 loc) · 4.46 KB
/
config.go
File metadata and controls
204 lines (159 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"cmp"
"errors"
"fmt"
"io/fs"
"log/slog"
"net"
"os"
"path/filepath"
"time"
"github.com/pelletier/go-toml/v2"
)
type resolvedServer struct {
listenAddr string
sessionTTL time.Duration
}
type Server struct {
LogLevel string `json:"log_level,omitempty" toml:"log_level,commented"`
DBPath string `json:"database_path,omitempty" toml:"database_path,commented"`
Password string `json:"password,omitempty" toml:"password,commented"`
ListenAddr string `json:"listen_addr,omitempty" toml:"listen_addr,commented"`
CertFile string `json:"cert_file,omitempty" toml:"cert_file,commented"`
KeyFile string `json:"key_file,omitempty" toml:"key_file,commented"`
SessionTTL string `json:"session_ttl,omitempty" toml:"session_ttl,commented"`
resolvedServer
}
type Config struct {
Server Server `json:"server,omitempty" toml:"server,commented"`
Endpoints []Endpoint `json:"endpoints,omitempty" toml:"endpoints,commented"`
configPath string
sha string
}
func (c *Config) validate() error {
uid := os.Getuid()
if c.Server.ListenAddr == "" {
return errors.New("listen_addr must not be empty")
}
if c.Server.Password == "" {
return errors.New("server password must not be empty")
}
if c.Server.ListenAddr != "" {
if _, _, err := net.SplitHostPort(c.Server.ListenAddr); err != nil {
return fmt.Errorf("listen_addr must be host:port or :port: %v", err)
}
}
_, err := parseLogLevel(c.Server.LogLevel)
if err != nil {
return fmt.Errorf("invalid log level: %v", err)
}
if c.Server.SessionTTL != "" {
if _, err := time.ParseDuration(c.Server.SessionTTL); err != nil {
return fmt.Errorf("invalid session ttl duration %q", c.Server.SessionTTL)
}
}
seen := make(map[string]struct{}, len(c.Endpoints))
for i, e := range c.Endpoints {
if err := e.validate(); err != nil {
return fmt.Errorf("endpoint[%d]: %v", i, err)
}
if (e.UID != 0 || e.GID != 0) && uid != 0 {
return fmt.Errorf("cannot set UID/GID for endpoint %q: must run as root to drop privileges (current uid=%d, requested uid=%d gid=%d)", e.Path, uid, e.UID, e.GID)
}
if e.NoAuth {
logger.Warn("endpoint registered without password protection (unsafe mode enabled)",
"path", e.Path,
"index", i,
)
}
if _, dup := seen[e.Path]; dup {
return fmt.Errorf("duplicate endpoint: %s", e.Path)
}
seen[e.Path] = struct{}{}
}
return nil
}
func (c *Config) resolve() error {
if c == nil {
return errors.New("cannot set defaults on nil config")
}
c.Server.listenAddr = cmp.Or(c.Server.ListenAddr, defaultListenAddr)
c.Server.sessionTTL = defaultSessionTTL
if c.Server.SessionTTL != "" {
t, _ := time.ParseDuration(c.Server.SessionTTL) // validated at [Config.validate]
c.Server.sessionTTL = t
}
return nil
}
func (c *Config) redact() *Config {
if c == nil {
return nil
}
redacted := *c
redacted.Endpoints = append([]Endpoint(nil), redacted.Endpoints...)
if redacted.Server.Password != "" {
redacted.Server.Password = redact
}
return &redacted
}
func (c *Config) complete() {
for i := range c.Endpoints {
c.Endpoints[i].resolve()
}
}
func defaultConfigPath() (string, error) {
home, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(home, defaultConfigName), nil
}
func parseFileConfig(path string) (*Config, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("config: stat file: %v", err)
}
if fi.Mode().Perm() != 0o600 {
return nil, fmt.Errorf("config: %q has invalid permissions: got %04o, expected 0600", path, fi.Mode().Perm())
}
raw, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
var config Config
if err := toml.Unmarshal(raw, &config); err != nil {
return nil, fmt.Errorf("config: parse file: %v", err)
}
return &config, nil
}
func loadFileConfig(path string) (*Config, error) {
if path == "" {
return nil, errors.New("config path must be set")
}
c, err := parseFileConfig(path)
if err != nil {
if path != "" || !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("load config %s: %v", path, err)
}
c = &Config{}
}
if err := c.validate(); err != nil {
return nil, err
}
if err := c.resolve(); err != nil {
return nil, err
}
c.complete()
return c, nil
}
func parseLogLevel(s string) (slog.Level, error) {
if s == "" {
return slog.LevelInfo, nil
}
var lvl slog.Level
if err := lvl.UnmarshalText([]byte(s)); err != nil {
return lvl, err
}
return lvl, nil
}