log.Warning if a notification is configured twice (#2240)

This commit is contained in:
mmetc 2023-06-02 14:41:50 +02:00 committed by GitHub
parent 228e4f9acc
commit a4eee41fd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"reflect"
"strings" "strings"
"sync" "sync"
"text/template" "text/template"
@ -183,14 +184,14 @@ func (pb *PluginBroker) loadConfig(path string) error {
return err return err
} }
for _, pluginConfig := range pluginConfigs { for _, pluginConfig := range pluginConfigs {
setRequiredFields(&pluginConfig)
if _, ok := pb.pluginConfigByName[pluginConfig.Name]; ok {
log.Warningf("notification '%s' is defined multiple times", pluginConfig.Name)
}
pb.pluginConfigByName[pluginConfig.Name] = pluginConfig
if !pb.profilesContainPlugin(pluginConfig.Name) { if !pb.profilesContainPlugin(pluginConfig.Name) {
continue continue
} }
setRequiredFields(&pluginConfig)
if _, ok := pb.pluginConfigByName[pluginConfig.Name]; ok {
log.Warnf("several configs for notification %s found ", pluginConfig.Name)
}
pb.pluginConfigByName[pluginConfig.Name] = pluginConfig
} }
} }
err = pb.verifyPluginConfigsWithProfile() err = pb.verifyPluginConfigsWithProfile()
@ -358,6 +359,10 @@ func ParsePluginConfigFile(path string) ([]PluginConfig, error) {
} }
return []PluginConfig{}, fmt.Errorf("while decoding %s got error %s", path, err) return []PluginConfig{}, fmt.Errorf("while decoding %s got error %s", path, err)
} }
// if the yaml document is empty, skip
if reflect.DeepEqual(pc, PluginConfig{}) {
continue
}
parsedConfigs = append(parsedConfigs, pc) parsedConfigs = append(parsedConfigs, pc)
} }
return parsedConfigs, nil return parsedConfigs, nil
@ -371,7 +376,6 @@ func setRequiredFields(pluginCfg *PluginConfig) {
if pluginCfg.TimeOut == time.Second*0 { if pluginCfg.TimeOut == time.Second*0 {
pluginCfg.TimeOut = time.Second * 5 pluginCfg.TimeOut = time.Second * 5
} }
} }
func getUUID() (string, error) { func getUUID() (string, error) {

View file

@ -67,6 +67,20 @@ teardown() {
assert_stderr --partial "api server init: unable to run local API: while loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}" assert_stderr --partial "api server init: unable to run local API: while loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}"
} }
@test "duplicate notification config" {
CONFIG_DIR=$(dirname "$CONFIG_YAML")
# email_default has two configurations
rune -0 yq -i '.name="email_default"' "$CONFIG_DIR/notifications/http.yaml"
# enable a notification, otherwise plugins are ignored
config_set "${PROFILES_PATH}" '.notifications=["slack_default"]'
# we want to check the logs
config_set '.common.log_media="stdout"'
# the command will fail because slack_deault is not working
run -1 --separate-stderr timeout 2s "${CROWDSEC}"
# but we have what we wanted
assert_stderr --partial "notification 'email_default' is defined multiple times"
}
@test "bad plugin permission (group writable)" { @test "bad plugin permission (group writable)" {
config_set "${PROFILES_PATH}" '.notifications=["http_default"]' config_set "${PROFILES_PATH}" '.notifications=["http_default"]'
chmod g+w "${PLUGIN_DIR}"/notification-http chmod g+w "${PLUGIN_DIR}"/notification-http