Skip to content

Commit 981d282

Browse files
enhance: return err if notification has no plugin type (#3638)
* enhance: return err if plugin has no type * fix + reduce error verbosity --------- Co-authored-by: marco <[email protected]>
1 parent 7ff6288 commit 981d282

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
lines changed

cmd/crowdsec/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func initAPIServer(ctx context.Context, cConfig *csconfig.Config) (*apiserver.AP
3838

3939
err = pluginBroker.Init(ctx, cConfig.PluginConfig, cConfig.API.Server.Profiles, cConfig.ConfigPaths)
4040
if err != nil {
41-
return nil, fmt.Errorf("unable to run plugin broker: %w", err)
41+
return nil, fmt.Errorf("plugin broker: %w", err)
4242
}
4343

4444
log.Info("initiated plugin broker")

pkg/csplugin/broker.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func (pb *PluginBroker) Init(ctx context.Context, pluginCfg *csconfig.PluginCfg,
8484
pb.pluginsTypesToDispatch = make(map[string]struct{})
8585

8686
if err := pb.loadConfig(configPaths.NotificationDir); err != nil {
87-
return fmt.Errorf("while loading plugin config: %w", err)
87+
return fmt.Errorf("loading config: %w", err)
8888
}
8989

9090
if err := pb.loadPlugins(ctx, configPaths.PluginDir); err != nil {
91-
return fmt.Errorf("while loading plugin: %w", err)
91+
return fmt.Errorf("loading plugin: %w", err)
9292
}
9393

9494
pb.watcher = PluginWatcher{}
@@ -409,8 +409,12 @@ func ParsePluginConfigFile(path string) ([]PluginConfig, error) {
409409
dec := yaml.NewDecoder(yamlFile)
410410
dec.SetStrict(true)
411411

412+
idx := -1
413+
412414
for {
413-
pc := PluginConfig{}
415+
var pc PluginConfig
416+
417+
idx += 1
414418

415419
err = dec.Decode(&pc)
416420
if err != nil {
@@ -420,11 +424,17 @@ func ParsePluginConfigFile(path string) ([]PluginConfig, error) {
420424

421425
return nil, fmt.Errorf("while decoding %s got error %s", path, err)
422426
}
427+
423428
// if the yaml document is empty, skip
424429
if reflect.DeepEqual(pc, PluginConfig{}) {
425430
continue
426431
}
427432

433+
if pc.Type == "" {
434+
return nil, fmt.Errorf("field 'type' missing in %s (position %d)", path, idx)
435+
436+
}
437+
428438
parsedConfigs = append(parsedConfigs, pc)
429439
}
430440

test/bats/70_plugin_http.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ setup_file() {
1919

2020
# https://mikefarah.gitbook.io/yq/operators/env-variable-operators
2121
config_set "$(config_get '.config_paths.notification_dir')/http.yaml" '
22+
.type="http" |
2223
.url=strenv(MOCK_URL) |
2324
.group_wait="5s" |
2425
.group_threshold=2

test/bats/71_plugin_dummy.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ setup_file() {
1717
DUMMY_YAML="$(config_get '.config_paths.notification_dir')/dummy.yaml"
1818

1919
config_set "$DUMMY_YAML" '
20+
.type="dummy" |
2021
.group_wait="5s" |
2122
.group_threshold=2 |
2223
.output_file=strenv(tempfile) |

test/bats/72_plugin_badconfig.bats

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,39 @@ teardown() {
3838
config_set '.plugin_config.user="" | .plugin_config.group="nogroup"'
3939
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
4040
rune -0 wait-for \
41-
--err "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: both plugin user and group must be set" \
41+
--err "api server init: plugin broker: loading plugin: while getting process attributes: both plugin user and group must be set" \
4242
"$CROWDSEC"
4343
}
4444

4545
@test "misconfigured plugin, only group is empty" {
4646
config_set '(.plugin_config.user="nobody") | (.plugin_config.group="")'
4747
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
4848
rune -0 wait-for \
49-
--err "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: both plugin user and group must be set" \
49+
--err "api server init: plugin broker: loading plugin: while getting process attributes: both plugin user and group must be set" \
5050
"$CROWDSEC"
5151
}
5252

5353
@test "misconfigured plugin, user does not exist" {
5454
config_set '(.plugin_config.user="userdoesnotexist") | (.plugin_config.group="groupdoesnotexist")'
5555
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
5656
rune -0 wait-for \
57-
--err "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: user: unknown user userdoesnotexist" \
57+
--err "api server init: plugin broker: loading plugin: while getting process attributes: user: unknown user userdoesnotexist" \
5858
"$CROWDSEC"
5959
}
6060

6161
@test "misconfigured plugin, group does not exist" {
6262
config_set '(.plugin_config.user=strenv(USER)) | (.plugin_config.group="groupdoesnotexist")'
6363
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
6464
rune -0 wait-for \
65-
--err "api server init: unable to run plugin broker: while loading plugin: while getting process attributes: group: unknown group groupdoesnotexist" \
65+
--err "api server init: plugin broker: loading plugin: while getting process attributes: group: unknown group groupdoesnotexist" \
6666
"$CROWDSEC"
6767
}
6868

6969
@test "bad plugin name" {
7070
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
7171
cp "$PLUGIN_DIR"/notification-http "$PLUGIN_DIR"/badname
7272
rune -0 wait-for \
73-
--err "api server init: unable to run plugin broker: while loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}" \
73+
--err "api server init: plugin broker: loading plugin: plugin name ${PLUGIN_DIR}/badname is invalid. Name should be like {type-name}" \
7474
"$CROWDSEC"
7575
}
7676

@@ -90,15 +90,15 @@ teardown() {
9090
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
9191
chmod g+w "$PLUGIN_DIR"/notification-http
9292
rune -0 wait-for \
93-
--err "api server init: unable to run plugin broker: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is group writable, group writable plugins are invalid" \
93+
--err "api server init: plugin broker: loading plugin: plugin at ${PLUGIN_DIR}/notification-http is group writable, group writable plugins are invalid" \
9494
"$CROWDSEC"
9595
}
9696

9797
@test "bad plugin permission (world writable)" {
9898
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
9999
chmod o+w "$PLUGIN_DIR"/notification-http
100100
rune -0 wait-for \
101-
--err "api server init: unable to run plugin broker: while loading plugin: plugin at ${PLUGIN_DIR}/notification-http is world writable, world writable plugins are invalid" \
101+
--err "api server init: plugin broker: loading plugin: plugin at ${PLUGIN_DIR}/notification-http is world writable, world writable plugins are invalid" \
102102
"$CROWDSEC"
103103
}
104104

@@ -124,10 +124,22 @@ teardown() {
124124
"$CROWDSEC"
125125
}
126126

127-
@test "unable to run plugin broker: while reading plugin config" {
127+
@test "plugin broker: missing notification dir" {
128128
config_set '.config_paths.notification_dir="/this/path/does/not/exist"'
129129
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
130130
rune -0 wait-for \
131-
--err "api server init: unable to run plugin broker: while loading plugin config: open /this/path/does/not/exist: no such file or directory" \
131+
--err "api server init: plugin broker: loading config: open /this/path/does/not/exist: no such file or directory" \
132132
"$CROWDSEC"
133133
}
134+
135+
@test "misconfigured notification: missing plugin type" {
136+
rune -0 yq -i 'del(.type)' "$CONFIG_DIR/notifications/http.yaml"
137+
# enable a notification, otherwise plugins are ignored
138+
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
139+
# the slack plugin may fail or not, but we just need the logs
140+
config_set '.common.log_media="stdout"'
141+
rune wait-for \
142+
--err "api server init: plugin broker: loading plugin config" \
143+
"$CROWDSEC"
144+
assert_stderr --partial "field 'type' missing in $CONFIG_DIR/notifications/http.yaml (position 0)"
145+
}

test/bats/73_plugin_formatting.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ setup_file() {
1717
# the $alert is not a shell variable
1818
# shellcheck disable=SC2016
1919
config_set "$DUMMY_YAML" '
20+
.type="dummy" |
2021
.group_wait="5s" |
2122
.group_threshold=2 |
2223
.output_file=strenv(tempfile) |

0 commit comments

Comments
 (0)