config.go 8.0 KB

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