config.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package config
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "math"
  7. "os"
  8. "os/signal"
  9. "path/filepath"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "syscall"
  14. "time"
  15. "dario.cat/mergo"
  16. "github.com/sirupsen/logrus"
  17. "github.com/wadey/synctrace"
  18. "gopkg.in/yaml.v3"
  19. )
  20. type C struct {
  21. path string
  22. files []string
  23. Settings map[string]any
  24. oldSettings map[string]any
  25. callbacks []func(*C)
  26. l *logrus.Logger
  27. reloadLock synctrace.Mutex
  28. }
  29. func NewC(l *logrus.Logger) *C {
  30. return &C{
  31. Settings: make(map[string]any),
  32. l: l,
  33. reloadLock: synctrace.NewMutex("config-reload"),
  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.WithField("config_path", k).WithError(err).Error("Error while marshaling new config")
  95. }
  96. oldVals, err := yaml.Marshal(ov)
  97. if err != nil {
  98. c.l.WithField("config_path", k).WithError(err).Error("Error while marshaling old config")
  99. }
  100. return string(newVals) != string(oldVals)
  101. }
  102. // CatchHUP will listen for the HUP signal in a go routine and reload all configs found in the
  103. // original path provided to Load. The old settings are shallow copied for change detection after the reload.
  104. func (c *C) CatchHUP(ctx context.Context) {
  105. if c.path == "" {
  106. return
  107. }
  108. ch := make(chan os.Signal, 1)
  109. signal.Notify(ch, syscall.SIGHUP)
  110. go func() {
  111. for {
  112. select {
  113. case <-ctx.Done():
  114. signal.Stop(ch)
  115. close(ch)
  116. return
  117. case <-ch:
  118. c.l.Info("Caught HUP, reloading config")
  119. c.ReloadConfig()
  120. }
  121. }
  122. }()
  123. }
  124. func (c *C) ReloadConfig() {
  125. c.reloadLock.Lock()
  126. defer c.reloadLock.Unlock()
  127. c.oldSettings = make(map[string]any)
  128. for k, v := range c.Settings {
  129. c.oldSettings[k] = v
  130. }
  131. err := c.Load(c.path)
  132. if err != nil {
  133. c.l.WithField("config_path", c.path).WithError(err).Error("Error occurred while reloading config")
  134. return
  135. }
  136. for _, v := range c.callbacks {
  137. v(c)
  138. }
  139. }
  140. func (c *C) ReloadConfigString(raw string) error {
  141. c.reloadLock.Lock()
  142. defer c.reloadLock.Unlock()
  143. c.oldSettings = make(map[string]any)
  144. for k, v := range c.Settings {
  145. c.oldSettings[k] = v
  146. }
  147. err := c.LoadString(raw)
  148. if err != nil {
  149. return err
  150. }
  151. for _, v := range c.callbacks {
  152. v(c)
  153. }
  154. return nil
  155. }
  156. // GetString will get the string for k or return the default d if not found or invalid
  157. func (c *C) GetString(k, d string) string {
  158. r := c.Get(k)
  159. if r == nil {
  160. return d
  161. }
  162. return fmt.Sprintf("%v", r)
  163. }
  164. // GetStringSlice will get the slice of strings for k or return the default d if not found or invalid
  165. func (c *C) GetStringSlice(k string, d []string) []string {
  166. r := c.Get(k)
  167. if r == nil {
  168. return d
  169. }
  170. rv, ok := r.([]any)
  171. if !ok {
  172. return d
  173. }
  174. v := make([]string, len(rv))
  175. for i := 0; i < len(v); i++ {
  176. v[i] = fmt.Sprintf("%v", rv[i])
  177. }
  178. return v
  179. }
  180. // GetMap will get the map for k or return the default d if not found or invalid
  181. func (c *C) GetMap(k string, d map[string]any) map[string]any {
  182. r := c.Get(k)
  183. if r == nil {
  184. return d
  185. }
  186. v, ok := r.(map[string]any)
  187. if !ok {
  188. return d
  189. }
  190. return v
  191. }
  192. // GetInt will get the int for k or return the default d if not found or invalid
  193. func (c *C) GetInt(k string, d int) int {
  194. r := c.GetString(k, strconv.Itoa(d))
  195. v, err := strconv.Atoi(r)
  196. if err != nil {
  197. return d
  198. }
  199. return v
  200. }
  201. // GetUint32 will get the uint32 for k or return the default d if not found or invalid
  202. func (c *C) GetUint32(k string, d uint32) uint32 {
  203. r := c.GetInt(k, int(d))
  204. if r < 0 || uint64(r) > uint64(math.MaxUint32) {
  205. return d
  206. }
  207. return uint32(r)
  208. }
  209. // GetBool will get the bool for k or return the default d if not found or invalid
  210. func (c *C) GetBool(k string, d bool) bool {
  211. r := strings.ToLower(c.GetString(k, fmt.Sprintf("%v", d)))
  212. v, err := strconv.ParseBool(r)
  213. if err != nil {
  214. switch r {
  215. case "y", "yes":
  216. return true
  217. case "n", "no":
  218. return false
  219. }
  220. return d
  221. }
  222. return v
  223. }
  224. func AsBool(v any) (value bool, ok bool) {
  225. switch x := v.(type) {
  226. case bool:
  227. return x, true
  228. case string:
  229. switch x {
  230. case "y", "yes":
  231. return true, true
  232. case "n", "no":
  233. return false, true
  234. }
  235. }
  236. return false, false
  237. }
  238. // GetDuration will get the duration for k or return the default d if not found or invalid
  239. func (c *C) GetDuration(k string, d time.Duration) time.Duration {
  240. r := c.GetString(k, "")
  241. v, err := time.ParseDuration(r)
  242. if err != nil {
  243. return d
  244. }
  245. return v
  246. }
  247. func (c *C) Get(k string) any {
  248. return c.get(k, c.Settings)
  249. }
  250. func (c *C) IsSet(k string) bool {
  251. return c.get(k, c.Settings) != nil
  252. }
  253. func (c *C) get(k string, v any) any {
  254. parts := strings.Split(k, ".")
  255. for _, p := range parts {
  256. m, ok := v.(map[string]any)
  257. if !ok {
  258. return nil
  259. }
  260. v, ok = m[p]
  261. if !ok {
  262. return nil
  263. }
  264. }
  265. return v
  266. }
  267. // direct signifies if this is the config path directly specified by the user,
  268. // versus a file/dir found by recursing into that path
  269. func (c *C) resolve(path string, direct bool) error {
  270. i, err := os.Stat(path)
  271. if err != nil {
  272. return nil
  273. }
  274. if !i.IsDir() {
  275. c.addFile(path, direct)
  276. return nil
  277. }
  278. paths, err := readDirNames(path)
  279. if err != nil {
  280. return fmt.Errorf("problem while reading directory %s: %s", path, err)
  281. }
  282. for _, p := range paths {
  283. err := c.resolve(filepath.Join(path, p), false)
  284. if err != nil {
  285. return err
  286. }
  287. }
  288. return nil
  289. }
  290. func (c *C) addFile(path string, direct bool) error {
  291. ext := filepath.Ext(path)
  292. if !direct && ext != ".yaml" && ext != ".yml" {
  293. return nil
  294. }
  295. ap, err := filepath.Abs(path)
  296. if err != nil {
  297. return err
  298. }
  299. c.files = append(c.files, ap)
  300. return nil
  301. }
  302. func (c *C) parseRaw(b []byte) error {
  303. var m map[string]any
  304. err := yaml.Unmarshal(b, &m)
  305. if err != nil {
  306. return err
  307. }
  308. c.Settings = m
  309. return nil
  310. }
  311. func (c *C) parse() error {
  312. var m map[string]any
  313. for _, path := range c.files {
  314. b, err := os.ReadFile(path)
  315. if err != nil {
  316. return err
  317. }
  318. var nm map[string]any
  319. err = yaml.Unmarshal(b, &nm)
  320. if err != nil {
  321. return err
  322. }
  323. // We need to use WithAppendSlice so that firewall rules in separate
  324. // files are appended together
  325. err = mergo.Merge(&nm, m, mergo.WithAppendSlice)
  326. m = nm
  327. if err != nil {
  328. return err
  329. }
  330. }
  331. c.Settings = m
  332. return nil
  333. }
  334. func readDirNames(path string) ([]string, error) {
  335. f, err := os.Open(path)
  336. if err != nil {
  337. return nil, err
  338. }
  339. paths, err := f.Readdirnames(-1)
  340. f.Close()
  341. if err != nil {
  342. return nil, err
  343. }
  344. sort.Strings(paths)
  345. return paths, nil
  346. }