sys,fw: return errors when loading configurations

This commit is contained in:
Gustavo Iñiguez Goia 2023-07-01 23:13:31 +02:00
parent f1ef00a5fd
commit edc9b17010
Failed to generate hash of commit

View file

@ -151,37 +151,45 @@ func (c *Config) NewSystemFwConfig(preLoadCb, reLoadCb func()) (*Config, error)
return c, nil
}
func (c *Config) SetFile(file string) {
c.file = file
}
// LoadDiskConfiguration reads and loads the firewall configuration from disk
func (c *Config) LoadDiskConfiguration(reload bool) {
func (c *Config) LoadDiskConfiguration(reload bool) error {
c.Lock()
defer c.Unlock()
raw, err := ioutil.ReadFile(c.file)
if err != nil {
log.Error("Error reading firewall configuration from disk %s: %s", c.file, err)
return
return err
}
c.loadConfiguration(raw)
if err = c.loadConfiguration(raw); err != nil {
return err
}
// we need to monitor the configuration file for changes, regardless if it's
// malformed or not.
c.watcher.Remove(c.file)
if err := c.watcher.Add(c.file); err != nil {
log.Error("Could not watch firewall configuration: %s", err)
return
return err
}
if reload {
c.reloadCallback()
return
return nil
}
go c.monitorConfigWorker()
return nil
}
// loadConfigutation reads the system firewall rules from disk.
// Then the rules are added based on the configuration defined.
func (c *Config) loadConfiguration(rawConfig []byte) {
func (c *Config) loadConfiguration(rawConfig []byte) error {
c.SysConfig.Lock()
defer c.SysConfig.Unlock()
@ -191,9 +199,11 @@ func (c *Config) loadConfiguration(rawConfig []byte) {
if err := json.Unmarshal(rawConfig, &c.SysConfig); err != nil {
// we only log the parser error, giving the user a chance to write a valid config
log.Error("Error parsing firewall configuration %s: %s", c.file, err)
return
return err
}
log.Info("fw configuration loaded")
return nil
}
// SaveConfiguration saves configuration to disk.