[core] Add validation for rule UUIDs to be unique

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-12-18 14:48:57 +01:00
parent 114c9e9039
commit 61a3facb64
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 33 additions and 10 deletions

View file

@ -15,6 +15,7 @@ import (
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/Luzifer/go_helpers/v2/str"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
@ -104,16 +105,8 @@ func loadConfig(filename string) error {
return errors.Wrap(err, "parsing config")
}
if len(tmpConfig.Channels) == 0 {
log.Warn("Loaded config with empty channel list")
}
if len(tmpConfig.Rules) == 0 {
log.Warn("Loaded config with empty ruleset")
}
if err = tmpConfig.validateRuleActions(); err != nil {
return errors.Wrap(err, "validating rule actions")
if err = tmpConfig.runLoadChecks(); err != nil {
return errors.Wrap(err, "running load-checks on config")
}
configLock.Lock()
@ -324,6 +317,30 @@ func (c *configFile) fixMissingUUIDs() {
}
}
func (c *configFile) runLoadChecks() (err error) {
if len(c.Channels) == 0 {
log.Warn("Loaded config with empty channel list")
}
if len(c.Rules) == 0 {
log.Warn("Loaded config with empty ruleset")
}
var seen []string
for _, r := range c.Rules {
if r.UUID != "" && str.StringInSlice(r.UUID, seen) {
return errors.New("duplicate rule UUIDs found")
}
seen = append(seen, r.UUID)
}
if err = c.validateRuleActions(); err != nil {
return errors.Wrap(err, "validating rule actions")
}
return nil
}
func (c *configFile) updateAutoMessagesFromConfig(old *configFile) {
for idx, nam := range c.AutoMessages {
// By default assume last message to be sent now

View file

@ -99,6 +99,12 @@ func configEditorRulesAdd(w http.ResponseWriter, r *http.Request) {
}
if err := patchConfig(cfg.Config, user, "", "Add rule", func(c *configFile) error {
for _, r := range c.Rules {
if r.UUID == msg.UUID {
return errors.New("rule already exists (UUID duplicate)")
}
}
c.Rules = append(c.Rules, msg)
return nil
}); err != nil {