config.go 8.9 KB

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