config.go: improve config validation

Also check whether there are no users/records and if records have an
actual user associated with them.
This commit is contained in:
Thomas Preisner 2021-09-11 15:04:45 +02:00
parent 4cb237a05b
commit e15da33296

View file

@ -51,8 +51,13 @@ func LoadConfig(path string) (*Config, error) {
} }
func prepareConfig(cfg *Config) (*Config, error) { func prepareConfig(cfg *Config) (*Config, error) {
// temporary map for preventing duplicate records between users if len(cfg.Users) <= 0 || len(cfg.RRConfigs) <= 0 {
records := make(map[string]bool) return nil, fmt.Errorf("No users or resource record configs present.")
}
// temporary map for detecting duplicated or orphaned entries between
// records and users
globRecords := map[string]bool{}
// populate user map // populate user map
cfg.users = map[string]*User{} cfg.users = map[string]*User{}
@ -68,12 +73,12 @@ func prepareConfig(cfg *Config) (*Config, error) {
} }
for _, record := range user.Records { for _, record := range user.Records {
// check for duplicate records // check for duplicate records
if records[record] { if globRecords[record] {
return nil, fmt.Errorf("Record associated with multiple users detected: %s", record) return nil, fmt.Errorf("Record associated with multiple users detected: %s", record)
} }
// memorize record both in the user as well as in the temporary map // memorize record both in the user as well as in the temporary map
user.records[record] = true user.records[record] = true
records[record] = true globRecords[record] = true
} }
cfg.users[user.Username] = &user cfg.users[user.Username] = &user
} }
@ -81,14 +86,29 @@ func prepareConfig(cfg *Config) (*Config, error) {
// populate record map // populate record map
cfg.rrconfigs = map[string]*RRConfig{} cfg.rrconfigs = map[string]*RRConfig{}
for _, record := range cfg.RRConfigs { for _, record := range cfg.RRConfigs {
if !records[record.Recordname] { // check for duplicate record before verifying association to users
return nil, fmt.Errorf("Record without associated user detected: %s", record.Recordname)
}
if _, ok := cfg.rrconfigs[record.Recordname]; ok { if _, ok := cfg.rrconfigs[record.Recordname]; ok {
return nil, fmt.Errorf("Duplicate record detected: %s", record.Recordname) return nil, fmt.Errorf("Duplicate record detected: %s", record.Recordname)
} }
if !globRecords[record.Recordname] {
return nil, fmt.Errorf("Record without associated user detected: %s", record.Recordname)
}
// need to remove record from globRecords for detecting orphaned records
delete(globRecords, record.Recordname)
cfg.rrconfigs[record.Recordname] = &record cfg.rrconfigs[record.Recordname] = &record
} }
// check whether all records have been processed
if len(globRecords) > 0 {
missing := make([]string, len(globRecords))
i := 0
for k := range globRecords {
missing[i] = k
i++
}
return nil, fmt.Errorf("Records associated to user missing: %v", missing)
}
return cfg, nil return cfg, nil
} }