Skip to content

Commit ce6018f

Browse files
authored
config.yaml: make config_dir and notification_dir optional (#3606)
1 parent ec553b3 commit ce6018f

File tree

7 files changed

+34
-30
lines changed

7 files changed

+34
-30
lines changed

cmd/crowdsec-cli/cliconfig/show.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (cli *cliConfig) showKey(key string) error {
2525
}
2626

2727
opts := []expr.Option{}
28-
opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...)
28+
opts = append(opts, exprhelpers.GetExprOptions(map[string]any{})...)
2929
opts = append(opts, expr.Env(Env{}))
3030

3131
program, err := expr.Compile(key, opts...)
@@ -44,7 +44,7 @@ func (cli *cliConfig) showKey(key string) error {
4444
// that would break compatibility with previous versions
4545
switch output.(type) {
4646
case string:
47-
fmt.Println(output)
47+
fmt.Fprintln(os.Stdout, output)
4848
default:
4949
litter.Dump(output)
5050
}
@@ -54,7 +54,7 @@ func (cli *cliConfig) showKey(key string) error {
5454
return fmt.Errorf("failed to serialize configuration: %w", err)
5555
}
5656

57-
fmt.Println(string(data))
57+
fmt.Fprintln(os.Stdout, string(data))
5858
}
5959

6060
return nil
@@ -67,6 +67,7 @@ func (cli *cliConfig) template() string {
6767
- Configuration Folder : {{.ConfigPaths.ConfigDir}}
6868
- Data Folder : {{.ConfigPaths.DataDir}}
6969
- Hub Folder : {{.ConfigPaths.HubDir}}
70+
- Notification Folder : {{.ConfigPaths.NotificationDir}}
7071
- Simulation File : {{.ConfigPaths.SimulationFilePath}}
7172
{{- end }}
7273
@@ -216,14 +217,14 @@ func (cli *cliConfig) show() error {
216217
return fmt.Errorf("failed to serialize configuration: %w", err)
217218
}
218219

219-
fmt.Println(string(data))
220+
fmt.Fprintln(os.Stdout, string(data))
220221
case "raw":
221222
data, err := yaml.Marshal(cfg)
222223
if err != nil {
223224
return fmt.Errorf("failed to serialize configuration: %w", err)
224225
}
225226

226-
fmt.Println(string(data))
227+
fmt.Fprintln(os.Stdout, string(data))
227228
}
228229

229230
return nil

cmd/crowdsec-cli/clinotifications/notifications.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (cli *cliNotifications) NewCommand() *cobra.Command {
7070
return fmt.Errorf("loading api client: %w", err)
7171
}
7272

73-
return require.Notifications(cfg)
73+
return nil
7474
},
7575
}
7676

@@ -176,7 +176,7 @@ func (cli *cliNotifications) newListCmd() *cobra.Command {
176176
if err != nil {
177177
return fmt.Errorf("failed to serialize notification configuration: %w", err)
178178
}
179-
fmt.Printf("%s", string(x))
179+
fmt.Fprint(os.Stdout, string(x))
180180
} else if cfg.Cscli.Output == "raw" {
181181
csvwriter := csv.NewWriter(os.Stdout)
182182
err := csvwriter.Write([]string{"Name", "Type", "Profile name"})
@@ -223,19 +223,19 @@ func (cli *cliNotifications) newInspectCmd() *cobra.Command {
223223
return fmt.Errorf("plugin '%s' does not exist or is not active", args[0])
224224
}
225225
if cfg.Cscli.Output == "human" || cfg.Cscli.Output == "raw" {
226-
fmt.Printf(" - %15s: %15s\n", "Type", ncfg.Config.Type)
227-
fmt.Printf(" - %15s: %15s\n", "Name", ncfg.Config.Name)
228-
fmt.Printf(" - %15s: %15s\n", "Timeout", ncfg.Config.TimeOut)
229-
fmt.Printf(" - %15s: %15s\n", "Format", ncfg.Config.Format)
226+
fmt.Fprintf(os.Stdout, " - %15s: %15s\n", "Type", ncfg.Config.Type)
227+
fmt.Fprintf(os.Stdout, " - %15s: %15s\n", "Name", ncfg.Config.Name)
228+
fmt.Fprintf(os.Stdout, " - %15s: %15s\n", "Timeout", ncfg.Config.TimeOut)
229+
fmt.Fprintf(os.Stdout, " - %15s: %15s\n", "Format", ncfg.Config.Format)
230230
for k, v := range ncfg.Config.Config {
231-
fmt.Printf(" - %15s: %15v\n", k, v)
231+
fmt.Fprintf(os.Stdout, " - %15s: %15v\n", k, v)
232232
}
233233
} else if cfg.Cscli.Output == "json" {
234234
x, err := json.MarshalIndent(cfg, "", " ")
235235
if err != nil {
236236
return fmt.Errorf("failed to serialize notification configuration: %w", err)
237237
}
238-
fmt.Printf("%s", string(x))
238+
fmt.Fprint(os.Stdout, string(x))
239239
}
240240

241241
return nil

cmd/crowdsec-cli/require/require.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ func DB(c *csconfig.Config) error {
7474
return nil
7575
}
7676

77-
func Notifications(c *csconfig.Config) error {
78-
if c.ConfigPaths.NotificationDir == "" {
79-
return errors.New("config_paths.notification_dir is not set in crowdsec config")
80-
}
81-
82-
return nil
83-
}
84-
8577
func HubDownloader(ctx context.Context, c *csconfig.Config) *cwhub.Downloader {
8678
// set branch in config, and log if necessary
8779
branch := HubBranch(ctx, c)

cmd/crowdsec/api.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ func initAPIServer(ctx context.Context, cConfig *csconfig.Config) (*apiserver.AP
3232
return nil, errors.New("plugins are enabled, but the plugin_config section is missing in the configuration")
3333
}
3434

35-
if cConfig.ConfigPaths.NotificationDir == "" {
36-
return nil, errors.New("plugins are enabled, but config_paths.notification_dir is not defined")
37-
}
38-
3935
if cConfig.ConfigPaths.PluginDir == "" {
4036
return nil, errors.New("plugins are enabled, but config_paths.plugin_dir is not defined")
4137
}

pkg/csconfig/config_paths.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ type ConfigurationPaths struct {
1919

2020
func (c *Config) loadConfigurationPaths() error {
2121
var err error
22+
2223
if c.ConfigPaths == nil {
2324
return errors.New("no configuration paths provided")
2425
}
2526

27+
if c.ConfigPaths.ConfigDir == "" {
28+
c.ConfigPaths.ConfigDir = filepath.Dir(c.FilePath)
29+
}
30+
2631
if c.ConfigPaths.DataDir == "" {
2732
return errors.New("please provide a data directory with the 'data_dir' directive in the 'config_paths' section")
2833
}
@@ -35,6 +40,10 @@ func (c *Config) loadConfigurationPaths() error {
3540
c.ConfigPaths.HubIndexFile = filepath.Join(c.ConfigPaths.HubDir, ".index.json")
3641
}
3742

43+
if c.ConfigPaths.NotificationDir == "" {
44+
c.ConfigPaths.NotificationDir = filepath.Join(c.ConfigPaths.ConfigDir, "notifications")
45+
}
46+
3847
if c.ConfigPaths.PatternDir == "" {
3948
c.ConfigPaths.PatternDir = filepath.Join(c.ConfigPaths.ConfigDir, "patterns")
4049
}
@@ -53,6 +62,7 @@ func (c *Config) loadConfigurationPaths() error {
5362
if *k == "" {
5463
continue
5564
}
65+
5666
*k, err = filepath.Abs(*k)
5767
if err != nil {
5868
return fmt.Errorf("failed to get absolute path of '%s': %w", *k, err)

test/bats/01_cscli.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ teardown() {
147147

148148
# defaults
149149

150+
config_set 'del(.config_paths.config_dir)'
151+
rune -0 cscli config show --key Config.ConfigPaths.ConfigDir
152+
assert_output "$configdir"
153+
echo "$config" > "$CONFIG_YAML"
154+
150155
config_set 'del(.config_paths.hub_dir)'
151156
rune -0 cscli hub list
152157
rune -0 cscli config show --key Config.ConfigPaths.HubDir

test/bats/72_plugin_badconfig.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ setup_file() {
1212

1313
PROFILES_PATH=$(config_get '.api.server.profiles_path')
1414
export PROFILES_PATH
15+
16+
CONFIG_DIR=$(dirname "$CONFIG_YAML")
17+
export CONFIG_DIR
1518
}
1619

1720
teardown_file() {
@@ -72,7 +75,6 @@ teardown() {
7275
}
7376

7477
@test "duplicate notification config" {
75-
CONFIG_DIR=$(dirname "$CONFIG_YAML")
7678
# email_default has two configurations
7779
rune -0 yq -i '.name="email_default"' "$CONFIG_DIR/notifications/http.yaml"
7880
# enable a notification, otherwise plugins are ignored
@@ -110,10 +112,8 @@ teardown() {
110112

111113
@test "config.yaml: missing config_paths.notification_dir" {
112114
config_set 'del(.config_paths.notification_dir)'
113-
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
114-
rune -0 wait-for \
115-
--err "api server init: plugins are enabled, but config_paths.notification_dir is not defined" \
116-
"$CROWDSEC"
115+
rune -0 cscli config show --key Config.ConfigPaths.NotificationDir
116+
assert_output "$CONFIG_DIR/notifications"
117117
}
118118

119119
@test "config.yaml: missing config_paths.plugin_dir" {

0 commit comments

Comments
 (0)