| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"log/slog" |
| 8 |
"math" |
| 9 |
"os" |
| 10 |
"os/signal" |
| 11 |
"path/filepath" |
| 12 |
"sort" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"syscall" |
| 17 |
"time" |
| 18 |
|
| 19 |
"dario.cat/mergo" |
| 20 |
"go.yaml.in/yaml/v3" |
| 21 |
) |
| 22 |
|
| 23 |
type C struct { |
| 24 |
path string |
| 25 |
files []string |
| 26 |
Settings map[string]any |
| 27 |
oldSettings map[string]any |
| 28 |
callbacks []func(*C) |
| 29 |
l *slog.Logger |
| 30 |
reloadLock sync.Mutex |
| 31 |
} |
| 32 |
|
| 33 |
func NewC(l *slog.Logger) *C { |
| 34 |
return &C{ |
| 35 |
Settings: make(map[string]any), |
| 36 |
l: l, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Load will find all yaml files within path and load them in lexical order |
| 41 |
func (c *C) Load(path string) error { |
| 42 |
c.path = path |
| 43 |
c.files = make([]string, 0) |
| 44 |
|
| 45 |
err := c.resolve(path, true) |
| 46 |
if err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
if len(c.files) == 0 { |
| 51 |
return fmt.Errorf("no config files found at %s", path) |
| 52 |
} |
| 53 |
|
| 54 |
sort.Strings(c.files) |
| 55 |
|
| 56 |
err = c.parse() |
| 57 |
if err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
|
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
func (c *C) LoadString(raw string) error { |
| 65 |
if raw == "" { |
| 66 |
return errors.New("Empty configuration") |
| 67 |
} |
| 68 |
return c.parseRaw([]byte(raw)) |
| 69 |
} |
| 70 |
|
| 71 |
// RegisterReloadCallback stores a function to be called when a config reload is triggered. The functions registered |
| 72 |
// here should decide if they need to make a change to the current process before making the change. HasChanged can be |
| 73 |
// used to help decide if a change is necessary. |
| 74 |
// These functions should return quickly or spawn their own go routine if they will take a while |
| 75 |
func (c *C) RegisterReloadCallback(f func(*C)) { |
| 76 |
c.callbacks = append(c.callbacks, f) |
| 77 |
} |
| 78 |
|
| 79 |
// InitialLoad returns true if this is the first load of the config, and ReloadConfig has not been called yet. |
| 80 |
func (c *C) InitialLoad() bool { |
| 81 |
return c.oldSettings == nil |
| 82 |
} |
| 83 |
|
| 84 |
// HasChanged checks if the underlying structure of the provided key has changed after a config reload. The value of |
| 85 |
// k in both the old and new settings will be serialized, the result of the string comparison is returned. |
| 86 |
// If k is an empty string the entire config is tested. |
| 87 |
// It's important to note that this is very rudimentary and susceptible to configuration ordering issues indicating |
| 88 |
// there is change when there actually wasn't any. |
| 89 |
func (c *C) HasChanged(k string) bool { |
| 90 |
if c.oldSettings == nil { |
| 91 |
return false |
| 92 |
} |
| 93 |
|
| 94 |
var ( |
| 95 |
nv any |
| 96 |
ov any |
| 97 |
) |
| 98 |
|
| 99 |
if k == "" { |
| 100 |
nv = c.Settings |
| 101 |
ov = c.oldSettings |
| 102 |
k = "all settings" |
| 103 |
} else { |
| 104 |
nv = c.get(k, c.Settings) |
| 105 |
ov = c.get(k, c.oldSettings) |
| 106 |
} |
| 107 |
|
| 108 |
newVals, err := yaml.Marshal(nv) |
| 109 |
if err != nil { |
| 110 |
c.l.Error("Error while marshaling new config", |
| 111 |
"config_path", k, |
| 112 |
"error", err, |
| 113 |
) |
| 114 |
} |
| 115 |
|
| 116 |
oldVals, err := yaml.Marshal(ov) |
| 117 |
if err != nil { |
| 118 |
c.l.Error("Error while marshaling old config", |
| 119 |
"config_path", k, |
| 120 |
"error", err, |
| 121 |
) |
| 122 |
} |
| 123 |
|
| 124 |
return string(newVals) != string(oldVals) |
| 125 |
} |
| 126 |
|
| 127 |
// CatchHUP will listen for the HUP signal in a go routine and reload all configs found in the |
| 128 |
// original path provided to Load. The old settings are shallow copied for change detection after the reload. |
| 129 |
func (c *C) CatchHUP(ctx context.Context) { |
| 130 |
if c.path == "" { |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
ch := make(chan os.Signal, 1) |
| 135 |
signal.Notify(ch, syscall.SIGHUP) |
| 136 |
|
| 137 |
go func() { |
| 138 |
for { |
| 139 |
select { |
| 140 |
case <-ctx.Done(): |
| 141 |
signal.Stop(ch) |
| 142 |
close(ch) |
| 143 |
return |
| 144 |
case <-ch: |
| 145 |
c.l.Info("Caught HUP, reloading config") |
| 146 |
c.ReloadConfig() |
| 147 |
} |
| 148 |
} |
| 149 |
}() |
| 150 |
} |
| 151 |
|
| 152 |
func (c *C) ReloadConfig() { |
| 153 |
c.reloadLock.Lock() |
| 154 |
defer c.reloadLock.Unlock() |
| 155 |
|
| 156 |
c.oldSettings = make(map[string]any) |
| 157 |
for k, v := range c.Settings { |
| 158 |
c.oldSettings[k] = v |
| 159 |
} |
| 160 |
|
| 161 |
err := c.Load(c.path) |
| 162 |
if err != nil { |
| 163 |
c.l.Error("Error occurred while reloading config", |
| 164 |
"config_path", c.path, |
| 165 |
"error", err, |
| 166 |
) |
| 167 |
return |
| 168 |
} |
| 169 |
|
| 170 |
for _, v := range c.callbacks { |
| 171 |
v(c) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func (c *C) ReloadConfigString(raw string) error { |
| 176 |
c.reloadLock.Lock() |
| 177 |
defer c.reloadLock.Unlock() |
| 178 |
|
| 179 |
c.oldSettings = make(map[string]any) |
| 180 |
for k, v := range c.Settings { |
| 181 |
c.oldSettings[k] = v |
| 182 |
} |
| 183 |
|
| 184 |
err := c.LoadString(raw) |
| 185 |
if err != nil { |
| 186 |
return err |
| 187 |
} |
| 188 |
|
| 189 |
for _, v := range c.callbacks { |
| 190 |
v(c) |
| 191 |
} |
| 192 |
|
| 193 |
return nil |
| 194 |
} |
| 195 |
|
| 196 |
// GetString will get the string for k or return the default d if not found or invalid |
| 197 |
func (c *C) GetString(k, d string) string { |
| 198 |
r := c.Get(k) |
| 199 |
if r == nil { |
| 200 |
return d |
| 201 |
} |
| 202 |
|
| 203 |
return fmt.Sprintf("%v", r) |
| 204 |
} |
| 205 |
|
| 206 |
// GetStringSlice will get the slice of strings for k or return the default d if not found or invalid |
| 207 |
func (c *C) GetStringSlice(k string, d []string) []string { |
| 208 |
r := c.Get(k) |
| 209 |
if r == nil { |
| 210 |
return d |
| 211 |
} |
| 212 |
|
| 213 |
rv, ok := r.([]any) |
| 214 |
if !ok { |
| 215 |
return d |
| 216 |
} |
| 217 |
|
| 218 |
v := make([]string, len(rv)) |
| 219 |
for i := 0; i < len(v); i++ { |
| 220 |
v[i] = fmt.Sprintf("%v", rv[i]) |
| 221 |
} |
| 222 |
|
| 223 |
return v |
| 224 |
} |
| 225 |
|
| 226 |
// GetMap will get the map for k or return the default d if not found or invalid |
| 227 |
func (c *C) GetMap(k string, d map[string]any) map[string]any { |
| 228 |
r := c.Get(k) |
| 229 |
if r == nil { |
| 230 |
return d |
| 231 |
} |
| 232 |
|
| 233 |
v, ok := r.(map[string]any) |
| 234 |
if !ok { |
| 235 |
return d |
| 236 |
} |
| 237 |
|
| 238 |
return v |
| 239 |
} |
| 240 |
|
| 241 |
// GetInt will get the int for k or return the default d if not found or invalid |
| 242 |
func (c *C) GetInt(k string, d int) int { |
| 243 |
r := c.GetString(k, strconv.Itoa(d)) |
| 244 |
v, err := strconv.Atoi(r) |
| 245 |
if err != nil { |
| 246 |
return d |
| 247 |
} |
| 248 |
|
| 249 |
return v |
| 250 |
} |
| 251 |
|
| 252 |
// GetUint32 will get the uint32 for k or return the default d if not found or invalid |
| 253 |
func (c *C) GetUint32(k string, d uint32) uint32 { |
| 254 |
r := c.GetInt(k, int(d)) |
| 255 |
if r < 0 || uint64(r) > uint64(math.MaxUint32) { |
| 256 |
return d |
| 257 |
} |
| 258 |
return uint32(r) |
| 259 |
} |
| 260 |
|
| 261 |
// GetBool will get the bool for k or return the default d if not found or invalid |
| 262 |
func (c *C) GetBool(k string, d bool) bool { |
| 263 |
r := strings.ToLower(c.GetString(k, fmt.Sprintf("%v", d))) |
| 264 |
v, err := strconv.ParseBool(r) |
| 265 |
if err != nil { |
| 266 |
switch r { |
| 267 |
case "y", "yes": |
| 268 |
return true |
| 269 |
case "n", "no": |
| 270 |
return false |
| 271 |
} |
| 272 |
return d |
| 273 |
} |
| 274 |
|
| 275 |
return v |
| 276 |
} |
| 277 |
|
| 278 |
func AsBool(v any) (value bool, ok bool) { |
| 279 |
switch x := v.(type) { |
| 280 |
case bool: |
| 281 |
return x, true |
| 282 |
case string: |
| 283 |
switch x { |
| 284 |
case "y", "yes": |
| 285 |
return true, true |
| 286 |
case "n", "no": |
| 287 |
return false, true |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return false, false |
| 292 |
} |
| 293 |
|
| 294 |
// GetDuration will get the duration for k or return the default d if not found or invalid |
| 295 |
func (c *C) GetDuration(k string, d time.Duration) time.Duration { |
| 296 |
r := c.GetString(k, "") |
| 297 |
v, err := time.ParseDuration(r) |
| 298 |
if err != nil { |
| 299 |
return d |
| 300 |
} |
| 301 |
return v |
| 302 |
} |
| 303 |
|
| 304 |
func (c *C) Get(k string) any { |
| 305 |
return c.get(k, c.Settings) |
| 306 |
} |
| 307 |
|
| 308 |
func (c *C) IsSet(k string) bool { |
| 309 |
return c.get(k, c.Settings) != nil |
| 310 |
} |
| 311 |
|
| 312 |
func (c *C) get(k string, v any) any { |
| 313 |
parts := strings.Split(k, ".") |
| 314 |
for _, p := range parts { |
| 315 |
m, ok := v.(map[string]any) |
| 316 |
if !ok { |
| 317 |
return nil |
| 318 |
} |
| 319 |
|
| 320 |
v, ok = m[p] |
| 321 |
if !ok { |
| 322 |
return nil |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
return v |
| 327 |
} |
| 328 |
|
| 329 |
// direct signifies if this is the config path directly specified by the user, |
| 330 |
// versus a file/dir found by recursing into that path |
| 331 |
func (c *C) resolve(path string, direct bool) error { |
| 332 |
i, err := os.Stat(path) |
| 333 |
if err != nil { |
| 334 |
return nil |
| 335 |
} |
| 336 |
|
| 337 |
if !i.IsDir() { |
| 338 |
c.addFile(path, direct) |
| 339 |
return nil |
| 340 |
} |
| 341 |
|
| 342 |
paths, err := readDirNames(path) |
| 343 |
if err != nil { |
| 344 |
return fmt.Errorf("problem while reading directory %s: %s", path, err) |
| 345 |
} |
| 346 |
|
| 347 |
for _, p := range paths { |
| 348 |
err := c.resolve(filepath.Join(path, p), false) |
| 349 |
if err != nil { |
| 350 |
return err |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
return nil |
| 355 |
} |
| 356 |
|
| 357 |
func (c *C) addFile(path string, direct bool) error { |
| 358 |
ext := filepath.Ext(path) |
| 359 |
|
| 360 |
if !direct && ext != ".yaml" && ext != ".yml" { |
| 361 |
return nil |
| 362 |
} |
| 363 |
|
| 364 |
ap, err := filepath.Abs(path) |
| 365 |
if err != nil { |
| 366 |
return err |
| 367 |
} |
| 368 |
|
| 369 |
c.files = append(c.files, ap) |
| 370 |
return nil |
| 371 |
} |
| 372 |
|
| 373 |
func (c *C) parseRaw(b []byte) error { |
| 374 |
var m map[string]any |
| 375 |
|
| 376 |
err := yaml.Unmarshal(b, &m) |
| 377 |
if err != nil { |
| 378 |
return err |
| 379 |
} |
| 380 |
|
| 381 |
c.Settings = m |
| 382 |
return nil |
| 383 |
} |
| 384 |
|
| 385 |
func (c *C) parse() error { |
| 386 |
var m map[string]any |
| 387 |
|
| 388 |
for _, path := range c.files { |
| 389 |
b, err := os.ReadFile(path) |
| 390 |
if err != nil { |
| 391 |
return err |
| 392 |
} |
| 393 |
|
| 394 |
var nm map[string]any |
| 395 |
err = yaml.Unmarshal(b, &nm) |
| 396 |
if err != nil { |
| 397 |
return err |
| 398 |
} |
| 399 |
|
| 400 |
// We need to use WithAppendSlice so that firewall rules in separate |
| 401 |
// files are appended together |
| 402 |
err = mergo.Merge(&nm, m, mergo.WithAppendSlice) |
| 403 |
m = nm |
| 404 |
if err != nil { |
| 405 |
return err |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
c.Settings = m |
| 410 |
return nil |
| 411 |
} |
| 412 |
|
| 413 |
func readDirNames(path string) ([]string, error) { |
| 414 |
f, err := os.Open(path) |
| 415 |
if err != nil { |
| 416 |
return nil, err |
| 417 |
} |
| 418 |
|
| 419 |
paths, err := f.Readdirnames(-1) |
| 420 |
f.Close() |
| 421 |
if err != nil { |
| 422 |
return nil, err |
| 423 |
} |
| 424 |
|
| 425 |
sort.Strings(paths) |
| 426 |
return paths, nil |
| 427 |
} |