浏览代码

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

mmetc 2 年之前
父节点
当前提交
a4eee41fd7
共有 2 个文件被更改,包括 23 次插入5 次删除
  1. 9 5
      pkg/csplugin/broker.go
  2. 14 0
      test/bats/72_plugin_badconfig.bats

+ 9 - 5
pkg/csplugin/broker.go

@@ -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 {
-			if !pb.profilesContainPlugin(pluginConfig.Name) {
-				continue
-			}
 			setRequiredFields(&pluginConfig)
 			setRequiredFields(&pluginConfig)
 			if _, ok := pb.pluginConfigByName[pluginConfig.Name]; ok {
 			if _, ok := pb.pluginConfigByName[pluginConfig.Name]; ok {
-				log.Warnf("several configs for notification %s found  ", pluginConfig.Name)
+				log.Warningf("notification '%s' is defined multiple times", pluginConfig.Name)
 			}
 			}
 			pb.pluginConfigByName[pluginConfig.Name] = pluginConfig
 			pb.pluginConfigByName[pluginConfig.Name] = pluginConfig
+			if !pb.profilesContainPlugin(pluginConfig.Name) {
+				continue
+			}
 		}
 		}
 	}
 	}
 	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) {

+ 14 - 0
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}"
     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