config.yaml: make config_dir and notification_dir optional

This commit is contained in:
marco 2025-05-02 16:41:17 +02:00
parent d10067e772
commit 65a5c29dc0
7 changed files with 20 additions and 17 deletions

View file

@ -67,6 +67,7 @@ func (cli *cliConfig) template() string {
- Configuration Folder : {{.ConfigPaths.ConfigDir}}
- Data Folder : {{.ConfigPaths.DataDir}}
- Hub Folder : {{.ConfigPaths.HubDir}}
- Notification Folder : {{.ConfigPaths.NotificationDir}}
- Simulation File : {{.ConfigPaths.SimulationFilePath}}
{{- end }}

View file

@ -70,7 +70,7 @@ func (cli *cliNotifications) NewCommand() *cobra.Command {
return fmt.Errorf("loading api client: %w", err)
}
return require.Notifications(cfg)
return nil
},
}

View file

@ -74,14 +74,6 @@ func DB(c *csconfig.Config) error {
return nil
}
func Notifications(c *csconfig.Config) error {
if c.ConfigPaths.NotificationDir == "" {
return errors.New("config_paths.notification_dir is not set in crowdsec config")
}
return nil
}
func HubDownloader(ctx context.Context, c *csconfig.Config) *cwhub.Downloader {
// set branch in config, and log if necessary
branch := HubBranch(ctx, c)

View file

@ -32,10 +32,6 @@ func initAPIServer(ctx context.Context, cConfig *csconfig.Config) (*apiserver.AP
return nil, errors.New("plugins are enabled, but the plugin_config section is missing in the configuration")
}
if cConfig.ConfigPaths.NotificationDir == "" {
return nil, errors.New("plugins are enabled, but config_paths.notification_dir is not defined")
}
if cConfig.ConfigPaths.PluginDir == "" {
return nil, errors.New("plugins are enabled, but config_paths.plugin_dir is not defined")
}

View file

@ -23,6 +23,10 @@ func (c *Config) loadConfigurationPaths() error {
return errors.New("no configuration paths provided")
}
if c.ConfigPaths.ConfigDir == "" {
c.ConfigPaths.ConfigDir = filepath.Dir(c.FilePath)
}
if c.ConfigPaths.DataDir == "" {
return errors.New("please provide a data directory with the 'data_dir' directive in the 'config_paths' section")
}
@ -35,6 +39,10 @@ func (c *Config) loadConfigurationPaths() error {
c.ConfigPaths.HubIndexFile = filepath.Join(c.ConfigPaths.HubDir, ".index.json")
}
if c.ConfigPaths.NotificationDir == "" {
c.ConfigPaths.NotificationDir = filepath.Join(c.ConfigPaths.ConfigDir, "notifications")
}
if c.ConfigPaths.PatternDir == "" {
c.ConfigPaths.PatternDir = filepath.Join(c.ConfigPaths.ConfigDir, "patterns")
}

View file

@ -147,6 +147,11 @@ teardown() {
# defaults
config_set 'del(.config_paths.config_dir)'
rune -0 cscli config show --key Config.ConfigPaths.ConfigDir
assert_output "$configdir"
echo "$config" > "$CONFIG_YAML"
config_set 'del(.config_paths.hub_dir)'
rune -0 cscli hub list
rune -0 cscli config show --key Config.ConfigPaths.HubDir

View file

@ -12,6 +12,9 @@ setup_file() {
PROFILES_PATH=$(config_get '.api.server.profiles_path')
export PROFILES_PATH
CONFIG_DIR=$(dirname "$CONFIG_YAML")
export CONFIG_DIR
}
teardown_file() {
@ -110,10 +113,8 @@ teardown() {
@test "config.yaml: missing config_paths.notification_dir" {
config_set 'del(.config_paths.notification_dir)'
config_set "$PROFILES_PATH" '.notifications=["http_default"]'
rune -0 wait-for \
--err "api server init: plugins are enabled, but config_paths.notification_dir is not defined" \
"$CROWDSEC"
rune -0 cscli config show --key Config.ConfigPaths.NotificationDir
assert_output "$CONFIG_DIR/notifications"
}
@test "config.yaml: missing config_paths.plugin_dir" {