From a4eee41fd79178951d2ff3ba81542f940eafeb6f Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:41:50 +0200 Subject: [PATCH] log.Warning if a notification is configured twice (#2240) --- pkg/csplugin/broker.go | 16 ++++++++++------ test/bats/72_plugin_badconfig.bats | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/csplugin/broker.go b/pkg/csplugin/broker.go index 46c7ac6ed..79cd445e7 100644 --- a/pkg/csplugin/broker.go +++ b/pkg/csplugin/broker.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "reflect" "strings" "sync" "text/template" @@ -183,14 +184,14 @@ func (pb *PluginBroker) loadConfig(path string) error { return err } 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) { 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() @@ -358,6 +359,10 @@ func ParsePluginConfigFile(path string) ([]PluginConfig, error) { } 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) } return parsedConfigs, nil @@ -371,7 +376,6 @@ func setRequiredFields(pluginCfg *PluginConfig) { if pluginCfg.TimeOut == time.Second*0 { pluginCfg.TimeOut = time.Second * 5 } - } func getUUID() (string, error) { diff --git a/test/bats/72_plugin_badconfig.bats b/test/bats/72_plugin_badconfig.bats index 8f9709bac..c216067f9 100644 --- a/test/bats/72_plugin_badconfig.bats +++ b/test/bats/72_plugin_badconfig.bats @@ -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}" } +@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)" { config_set "${PROFILES_PATH}" '.notifications=["http_default"]' chmod g+w "${PLUGIN_DIR}"/notification-http