Skip to content

fix: config_file_watcher.go - root all file reads for safety #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions core/startup/config_file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type configFileHandler struct {

watcher *fsnotify.Watcher

configDir string
appConfig *config.ApplicationConfig
}

Expand All @@ -30,7 +29,6 @@ type configFileHandler struct {
func newConfigFileHandler(appConfig *config.ApplicationConfig) configFileHandler {
c := configFileHandler{
handlers: make(map[string]fileHandler),
configDir: appConfig.DynamicConfigsDir,
appConfig: appConfig,
}
c.Register("api_keys.json", readApiKeysJson(*appConfig), true)
Expand All @@ -45,16 +43,17 @@ func (c *configFileHandler) Register(filename string, handler fileHandler, runNo
}
c.handlers[filename] = handler
if runNow {
c.callHandler(path.Join(c.appConfig.DynamicConfigsDir, filename), handler)
c.callHandler(filename, handler)
}
return nil
}

func (c *configFileHandler) callHandler(filename string, handler fileHandler) {
log.Trace().Str("filename", filename).Msg("reading file for dynamic config update")
fileContent, err := os.ReadFile(filename)
rootedFilePath := filepath.Join(c.appConfig.DynamicConfigsDir, filepath.Clean(filename))
log.Trace().Str("filename", rootedFilePath).Msg("reading file for dynamic config update")
fileContent, err := os.ReadFile(rootedFilePath)
if err != nil && !os.IsNotExist(err) {
log.Error().Err(err).Str("filename", filename).Msg("could not read file")
log.Error().Err(err).Str("filename", rootedFilePath).Msg("could not read file")
}

if err = handler(fileContent, c.appConfig); err != nil {
Expand All @@ -66,7 +65,8 @@ func (c *configFileHandler) Watch() error {
configWatcher, err := fsnotify.NewWatcher()
c.watcher = configWatcher
if err != nil {
log.Fatal().Err(err).Str("configdir", c.configDir).Msg("wnable to create a watcher for configuration directory")
log.Fatal().Err(err).Str("configdir", c.appConfig.DynamicConfigsDir).Msg("unable to create a watcher for configuration directory")

}

if c.appConfig.DynamicConfigsDirPollInterval > 0 {
Expand All @@ -77,7 +77,7 @@ func (c *configFileHandler) Watch() error {
<-ticker.C
for file, handler := range c.handlers {
log.Debug().Str("file", file).Msg("polling config file")
c.callHandler(filepath.Join(c.appConfig.DynamicConfigsDir, file), handler)
c.callHandler(file, handler)
}
}
}()
Expand All @@ -97,7 +97,7 @@ func (c *configFileHandler) Watch() error {
continue
}

c.callHandler(event.Name, handler)
c.callHandler(filepath.Base(event.Name), handler)
}
case err, ok := <-c.watcher.Errors:
log.Error().Err(err).Msg("config watcher error received")
Expand Down