config.go 9.8 KB

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