From f8821b898234bd7b77b1da150ab812252205e400 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:22:43 +0200 Subject: [PATCH 01/11] fix dev config --- cmd/crowdsec-cli/config.go | 101 ------------------------------------- cmd/crowdsec-cli/main.go | 36 ++++++------- config/dev.yaml | 1 + config/prod.yaml | 1 + pkg/csconfig/config.go | 1 + 5 files changed, 19 insertions(+), 121 deletions(-) diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/config.go index c29647e07..a0db7cab2 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/config.go @@ -1,12 +1,8 @@ package main import ( - "bufio" "fmt" - "io/ioutil" - "os" "path" - "strings" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -24,61 +20,6 @@ type cliConfig struct { dbPath string } -func interactiveCfg() error { - var err error - reader := bufio.NewReader(os.Stdin) - fmt.Print("crowdsec installation directory (default: /etc/crowdsec/config/): ") - config.InstallFolder, err = reader.ReadString('\n') - config.InstallFolder = strings.Replace(config.InstallFolder, "\n", "", -1) //CRLF to LF (windows) - if config.InstallFolder == "" { - config.InstallFolder = "/etc/crowdsec/config/" - } - if err != nil { - log.Fatalf("failed to read input : %v", err.Error()) - } - - fmt.Print("crowdsec backend plugin directory (default: /etc/crowdsec/plugin/backend): ") - config.BackendPluginFolder, err = reader.ReadString('\n') - config.BackendPluginFolder = strings.Replace(config.BackendPluginFolder, "\n", "", -1) //CRLF to LF (windows) - if config.BackendPluginFolder == "" { - config.BackendPluginFolder = "/etc/crowdsec/plugin/backend" - } - if err != nil { - log.Fatalf("failed to read input : %v", err.Error()) - } - if err := writeCfg(); err != nil { - log.Fatalf("failed writting configuration file : %s", err) - } - return nil -} - -func writeCfg() error { - - if config.configFolder == "" { - return fmt.Errorf("config dir is unset") - } - - config.hubFolder = config.configFolder + "/hub/" - if _, err := os.Stat(config.hubFolder); os.IsNotExist(err) { - - log.Warningf("creating skeleton!") - if err := os.MkdirAll(config.hubFolder, os.ModePerm); err != nil { - return fmt.Errorf("failed to create missing directory : '%s'", config.hubFolder) - } - } - out := path.Join(config.configFolder, "/config") - configYaml, err := yaml.Marshal(&config) - if err != nil { - return fmt.Errorf("failed marshaling config: %s", err) - } - err = ioutil.WriteFile(out, configYaml, 0644) - if err != nil { - return fmt.Errorf("failed to write to %s : %s", out, err) - } - log.Infof("wrote config to %s ", out) - return nil -} - func NewConfigCmd() *cobra.Command { var cmdConfig = &cobra.Command{ @@ -112,47 +53,5 @@ If no commands are specified, config is in interactive mode.`, }, } cmdConfig.AddCommand(cmdConfigShow) - var cmdConfigInterctive = &cobra.Command{ - Use: "prompt", - Short: "Prompt for configuration values in an interactive fashion", - Long: `Start interactive configuration of cli. It will successively ask for install dir, db path.`, - Args: cobra.ExactArgs(0), - Run: func(cmd *cobra.Command, args []string) { - err := interactiveCfg() - if err != nil { - log.Fatalf("Failed to run interactive config : %s", err) - } - log.Warningf("Configured, please run update.") - }, - } - cmdConfig.AddCommand(cmdConfigInterctive) - var cmdConfigInstalldir = &cobra.Command{ - Use: "installdir [value]", - Short: `Configure installation directory`, - Long: `Configure the installation directory of crowdsec, such as /etc/crowdsec/config/`, - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - config.InstallFolder = args[0] - if err := writeCfg(); err != nil { - log.Fatalf("failed writting configuration: %s", err) - } - }, - } - cmdConfig.AddCommand(cmdConfigInstalldir) - - var cmdConfigBackendFolder = &cobra.Command{ - Use: "backend [value]", - Short: `Configure installation directory`, - Long: `Configure the backend plugin directory of crowdsec, such as /etc/crowdsec/plugins/backend`, - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - config.BackendPluginFolder = args[0] - if err := writeCfg(); err != nil { - log.Fatalf("failed writting configuration: %s", err) - } - }, - } - cmdConfig.AddCommand(cmdConfigBackendFolder) - return cmdConfig } diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 07c3474df..9d3ed77d0 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -1,18 +1,17 @@ package main import ( - "io/ioutil" "os/user" "path/filepath" "strings" + "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/cwhub" "github.com/crowdsecurity/crowdsec/pkg/cwversion" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/cobra/doc" - "gopkg.in/yaml.v2" ) var dbg_lvl, nfo_lvl, wrn_lvl, err_lvl bool @@ -37,6 +36,13 @@ func initConfig() { log.SetLevel(log.ErrorLevel) } + config.configFolder = filepath.Join(filepath.Clean(csConfig.csCliFolder)) + + csConfig := csconfig.NewCrowdSecConfig() + if err := csConfig.GetOPT(); err != nil { + log.Fatalf(err.Error()) + } + if strings.HasPrefix(config.configFolder, "~/") { usr, err := user.Current() if err != nil { @@ -45,23 +51,14 @@ func initConfig() { config.configFolder = usr.HomeDir + "/" + config.configFolder[2:] } /*read config*/ - buf, err := ioutil.ReadFile(filepath.Clean(config.configFolder + "/config")) - if err != nil { - log.Infof("Failed to open config %s : %s", filepath.Clean(config.configFolder+"/config"), err) - } else { - err = yaml.UnmarshalStrict(buf, &config) - if err != nil { - log.Fatalf("Failed to parse config %s : %s, please configure", filepath.Clean(config.configFolder+"/config"), err) - } - config.InstallFolder = filepath.Clean(config.InstallFolder) - config.hubFolder = filepath.Clean(config.configFolder + "/hub/") - config.BackendPluginFolder = filepath.Clean(config.BackendPluginFolder) - // - cwhub.Installdir = config.InstallFolder - cwhub.Cfgdir = config.configFolder - cwhub.Hubdir = config.hubFolder - config.configured = true - } + config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder), "./config/") + config.hubFolder = filepath.Clean(config.configFolder + "/hub/") + config.BackendPluginFolder = filepath.Clean(csConfig.OutputConfig.BackendFolder) + // + cwhub.Installdir = config.InstallFolder + cwhub.Cfgdir = config.configFolder + cwhub.Hubdir = config.hubFolder + config.configured = true } func main() { @@ -112,7 +109,6 @@ API interaction: rootCmd.AddCommand(cmdVersion) //rootCmd.PersistentFlags().BoolVarP(&config.simulation, "simulate", "s", false, "No action; perform a simulation of events that would occur based on the current arguments.") - rootCmd.PersistentFlags().StringVarP(&config.configFolder, "config-dir", "c", "/etc/crowdsec/cscli/", "Configuration directory to use.") rootCmd.PersistentFlags().StringVarP(&config.output, "output", "o", "human", "Output format : human, json, raw.") rootCmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug.") rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.") diff --git a/config/dev.yaml b/config/dev.yaml index deee7f972..e80ae4c56 100644 --- a/config/dev.yaml +++ b/config/dev.yaml @@ -2,6 +2,7 @@ working_dir: "." data_dir: "./data" config_dir: "./config" pid_dir: "./" +cscli_dir: "./config/crowdsec-cli" log_dir: "./logs" log_mode: "stdout" log_level: info diff --git a/config/prod.yaml b/config/prod.yaml index e1dce77c6..d2337c6e2 100644 --- a/config/prod.yaml +++ b/config/prod.yaml @@ -3,6 +3,7 @@ data_dir: ${DATA} config_dir: ${CFG} pid_dir: ${PID} log_dir: /var/log/ +cscli_dir: ${CFG}/cscli log_mode: file log_level: info profiling: false diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 1f6ef3658..62111cb3a 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -31,6 +31,7 @@ type CrowdSec struct { Profiling bool `yaml:"profiling,omitempty"` //true -> enable runtime profiling SQLiteFile string `yaml:"sqlite_path,omitempty"` //path to sqlite output APIMode bool `yaml:"apimode,omitempty"` //true -> enable api push + csCliFolder string `yaml:"cscli_dir"` //cscli folder Linter bool Prometheus bool HTTPListen string `yaml:"http_listen,omitempty"` From 68ccbd1f69984ba9d25b3e60ec193d7002cb02c6 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:23:37 +0200 Subject: [PATCH 02/11] fix --- cmd/crowdsec-cli/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 9d3ed77d0..0a0fa4e2d 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -36,12 +36,11 @@ func initConfig() { log.SetLevel(log.ErrorLevel) } - config.configFolder = filepath.Join(filepath.Clean(csConfig.csCliFolder)) - csConfig := csconfig.NewCrowdSecConfig() if err := csConfig.GetOPT(); err != nil { log.Fatalf(err.Error()) } + config.configFolder = filepath.Join(filepath.Clean(csConfig.csCliFolder)) if strings.HasPrefix(config.configFolder, "~/") { usr, err := user.Current() From 74dd723ebf17db989071a9a456e57d6d6d45715b Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:24:54 +0200 Subject: [PATCH 03/11] fix --- cmd/crowdsec-cli/main.go | 2 +- pkg/csconfig/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 0a0fa4e2d..4805960a8 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -40,7 +40,7 @@ func initConfig() { if err := csConfig.GetOPT(); err != nil { log.Fatalf(err.Error()) } - config.configFolder = filepath.Join(filepath.Clean(csConfig.csCliFolder)) + config.configFolder = filepath.Join(filepath.Clean(csConfig.CsCliFolder)) if strings.HasPrefix(config.configFolder, "~/") { usr, err := user.Current() diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 62111cb3a..37356b8b8 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -31,7 +31,7 @@ type CrowdSec struct { Profiling bool `yaml:"profiling,omitempty"` //true -> enable runtime profiling SQLiteFile string `yaml:"sqlite_path,omitempty"` //path to sqlite output APIMode bool `yaml:"apimode,omitempty"` //true -> enable api push - csCliFolder string `yaml:"cscli_dir"` //cscli folder + CsCliFolder string `yaml:"cscli_dir"` //cscli folder Linter bool Prometheus bool HTTPListen string `yaml:"http_listen,omitempty"` From ccb4b65db941df8c4790f1cbd8bad99b56a056e6 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:27:02 +0200 Subject: [PATCH 04/11] fix --- wizard.sh | 9 --------- 1 file changed, 9 deletions(-) diff --git a/wizard.sh b/wizard.sh index 74ecd3d86..451ea08f7 100755 --- a/wizard.sh +++ b/wizard.sh @@ -289,8 +289,6 @@ install_crowdsec() { CFG=${CROWDSEC_CONFIG_PATH} PID=${PID_DIR} BIN=${CROWDSEC_BIN_INSTALLED} envsubst < ./config/crowdsec.service > "${SYSTEMD_PATH_FILE}" install_bins systemctl daemon-reload - log_info "Default cscli config generation" - configure_cli } update_bins() { @@ -353,12 +351,6 @@ uninstall_crowdsec() { log_info "crowdsec successfully uninstalled" } -# configure token and crowdsec configuration path for cscli -configure_cli() { - ${CSCLI_BIN_INSTALLED} config installdir "$CROWDSEC_CONFIG_PATH" || log_err "unable to configure ${CSCLI_BIN_INSTALLED} crowdsec configuration path" - ${CSCLI_BIN_INSTALLED} config backend "$CROWDSEC_BACKEND_FOLDER" || log_err "unable to configure ${CSCLI_BIN_INSTALLED} backend folder" - -} setup_cron_pull() { cp ./config/crowdsec_pull /etc/cron.d/ @@ -437,7 +429,6 @@ main() { log_info "installing crowdsec" install_crowdsec log_info "configuring ${CSCLI_BIN_INSTALLED}" - configure_cli ${CSCLI_BIN_INSTALLED} update > /dev/null 2>&1 || (log_err "fail to update crowdsec hub. exiting" && exit 1) # detect running services From ae191f3426d27bb68f3838fdf0d5c083c00720cb Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:29:13 +0200 Subject: [PATCH 05/11] debug --- cmd/crowdsec-cli/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 4805960a8..cc96affbb 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -58,6 +58,8 @@ func initConfig() { cwhub.Cfgdir = config.configFolder cwhub.Hubdir = config.hubFolder config.configured = true + log.Infof("CONFIG : %+v\n", config) + log.Infof("CSCFONg : %+v \n", csConfig) } func main() { From 176d5197f6b6940a71f90b3d9e75f81827082cde Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Fri, 22 May 2020 18:31:46 +0200 Subject: [PATCH 06/11] debug --- cmd/crowdsec-cli/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index cc96affbb..c3414b8c9 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -49,6 +49,9 @@ func initConfig() { } config.configFolder = usr.HomeDir + "/" + config.configFolder[2:] } + + log.Infof("CONFIG : %+v\n", config) + log.Infof("CSCFONg : %+v \n", csConfig) /*read config*/ config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder), "./config/") config.hubFolder = filepath.Clean(config.configFolder + "/hub/") @@ -58,8 +61,7 @@ func initConfig() { cwhub.Cfgdir = config.configFolder cwhub.Hubdir = config.hubFolder config.configured = true - log.Infof("CONFIG : %+v\n", config) - log.Infof("CSCFONg : %+v \n", csConfig) + } func main() { From 6757fa3cee844bc0a959fca92a1a16c66aecfafc Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 18:15:59 +0200 Subject: [PATCH 07/11] merge --- cmd/crowdsec-cli/api.go | 2 +- cmd/crowdsec-cli/backup-restore.go | 14 +++++++------- cmd/crowdsec-cli/ban.go | 1 - cmd/crowdsec-cli/config.go | 20 ++++++++++---------- cmd/crowdsec-cli/main.go | 16 ++++++++-------- pkg/csconfig/config.go | 18 ++++++++++++++++++ 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/cmd/crowdsec-cli/api.go b/cmd/crowdsec-cli/api.go index 355faa043..6ea5e1639 100644 --- a/cmd/crowdsec-cli/api.go +++ b/cmd/crowdsec-cli/api.go @@ -158,7 +158,7 @@ cscli api credentials # Display your API credentials return err } - err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile)) + err = outputCTX.LoadAPIConfig(path.Join(config.installFolder, apiConfigFile)) if err != nil { return err } diff --git a/cmd/crowdsec-cli/backup-restore.go b/cmd/crowdsec-cli/backup-restore.go index f0509c76f..818d50e69 100644 --- a/cmd/crowdsec-cli/backup-restore.go +++ b/cmd/crowdsec-cli/backup-restore.go @@ -151,7 +151,7 @@ func restoreFromDirectory(source string) error { continue } stage := file.Name() - stagedir := fmt.Sprintf("%s/%s/%s/", config.InstallFolder, itype, stage) + stagedir := fmt.Sprintf("%s/%s/%s/", config.installFolder, itype, stage) log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir) if err = os.MkdirAll(stagedir, os.ModePerm); err != nil { return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err) @@ -188,7 +188,7 @@ func restoreFromDirectory(source string) error { /* Restore acquis */ - yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.InstallFolder) + yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.installFolder) bac := fmt.Sprintf("%s/acquis.yaml", source) if err = copyFile(bac, yamlAcquisFile); err != nil { return fmt.Errorf("failed copy %s to %s : %s", bac, yamlAcquisFile, err) @@ -202,7 +202,7 @@ func restoreAPICreds(source string) error { var err error /*check existing configuration*/ - apiyaml := path.Join(config.InstallFolder, apiConfigFile) + apiyaml := path.Join(config.installFolder, apiConfigFile) api := &cwapi.ApiCtx{} if err = api.LoadConfig(apiyaml); err != nil { @@ -332,7 +332,7 @@ func backupToDirectory(target string) error { /* Backup acquis */ - yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.InstallFolder) + yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.installFolder) bac := fmt.Sprintf("%s/acquis.yaml", target) if err = copyFile(yamlAcquisFile, bac); err != nil { return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) @@ -341,7 +341,7 @@ func backupToDirectory(target string) error { /* Backup default.yaml */ - defyaml := fmt.Sprintf("%s/default.yaml", config.InstallFolder) + defyaml := fmt.Sprintf("%s/default.yaml", config.installFolder) bac = fmt.Sprintf("%s/default.yaml", target) if err = copyFile(defyaml, bac); err != nil { return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) @@ -354,8 +354,8 @@ func backupToDirectory(target string) error { log.Fatalf("no API output context, won't save api credentials") } outputCTX.API = &cwapi.ApiCtx{} - if err = outputCTX.API.LoadConfig(path.Join(config.InstallFolder, apiConfigFile)); err != nil { - return fmt.Errorf("unable to load api config %s : %s", path.Join(config.InstallFolder, apiConfigFile), err) + if err = outputCTX.API.LoadConfig(path.Join(config.installFolder, apiConfigFile)); err != nil { + return fmt.Errorf("unable to load api config %s : %s", path.Join(config.installFolder, apiConfigFile), err) } credsYaml, err := json.Marshal(&outputCTX.API.Creds) if err != nil { diff --git a/cmd/crowdsec-cli/ban.go b/cmd/crowdsec-cli/ban.go index 8c852a093..8de21a97c 100644 --- a/cmd/crowdsec-cli/ban.go +++ b/cmd/crowdsec-cli/ban.go @@ -190,7 +190,6 @@ You can add/delete/list or flush current bans in your local ban DB.`, return nil }, } - cmdBan.PersistentFlags().StringVar(&config.dbPath, "db", "", "Set path to SQLite DB.") cmdBan.PersistentFlags().StringVar(&remediationType, "remediation", "ban", "Set specific remediation type : ban|slow|captcha") cmdBan.Flags().SortFlags = false cmdBan.PersistentFlags().SortFlags = false diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/config.go index a0db7cab2..494199e4e 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/config.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "path" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -12,12 +11,13 @@ import ( /*CliCfg is the cli configuration structure, might be unexported*/ type cliConfig struct { configured bool - configFolder string `yaml:"cliconfig,omitempty"` /*overload ~/.cscli/*/ - output string /*output is human, json*/ - hubFolder string - InstallFolder string `yaml:"installdir"` /*/etc/crowdsec/*/ - BackendPluginFolder string `yaml:"backend"` - dbPath string + ConfigFilePath string `yaml:"config_file"` + configFolder string + output string + HubFolder string `yaml:"hub_folder"` + installFolder string + BackendPluginFolder string `yaml:"backend_folder"` + DataFolder string `yaml:"data_folder"` } func NewConfigCmd() *cobra.Command { @@ -39,8 +39,9 @@ If no commands are specified, config is in interactive mode.`, Run: func(cmd *cobra.Command, args []string) { if config.output == "json" { log.WithFields(log.Fields{ - "installdir": config.InstallFolder, - "cliconfig": path.Join(config.configFolder, "/config"), + "crowdsec_configuration_file": config.ConfigFilePath, + "backend_folder": config.BackendPluginFolder, + "data_folder": config.DataFolder, }).Warning("Current config") } else { x, err := yaml.Marshal(config) @@ -48,7 +49,6 @@ If no commands are specified, config is in interactive mode.`, log.Fatalf("failed to marshal current configuration : %v", err) } fmt.Printf("%s", x) - fmt.Printf("#cliconfig: %s", path.Join(config.configFolder, "/config")) } }, } diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index c3414b8c9..df9bf84e5 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -37,7 +37,7 @@ func initConfig() { } csConfig := csconfig.NewCrowdSecConfig() - if err := csConfig.GetOPT(); err != nil { + if err := csConfig.GetCliConfig(&config.ConfigFilePath); err != nil { log.Fatalf(err.Error()) } config.configFolder = filepath.Join(filepath.Clean(csConfig.CsCliFolder)) @@ -50,18 +50,16 @@ func initConfig() { config.configFolder = usr.HomeDir + "/" + config.configFolder[2:] } - log.Infof("CONFIG : %+v\n", config) - log.Infof("CSCFONg : %+v \n", csConfig) /*read config*/ - config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder), "./config/") - config.hubFolder = filepath.Clean(config.configFolder + "/hub/") + config.installFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder)) + config.HubFolder = filepath.Clean(config.configFolder + "/hub/") config.BackendPluginFolder = filepath.Clean(csConfig.OutputConfig.BackendFolder) + config.DataFolder = filepath.Clean(csConfig.DataFolder) // - cwhub.Installdir = config.InstallFolder + cwhub.Installdir = config.installFolder cwhub.Cfgdir = config.configFolder - cwhub.Hubdir = config.hubFolder + cwhub.Hubdir = config.HubFolder config.configured = true - } func main() { @@ -112,6 +110,8 @@ API interaction: rootCmd.AddCommand(cmdVersion) //rootCmd.PersistentFlags().BoolVarP(&config.simulation, "simulate", "s", false, "No action; perform a simulation of events that would occur based on the current arguments.") + rootCmd.PersistentFlags().StringVarP(&config.ConfigFilePath, "config", "c", "/etc/crowdsec/default.yaml", "path to crowdsec config file (default: /etc/crowdsec/default.yaml)") + rootCmd.PersistentFlags().StringVarP(&config.output, "output", "o", "human", "Output format : human, json, raw.") rootCmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug.") rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.") diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 37356b8b8..067b1a7e8 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -60,6 +60,24 @@ func NewCrowdSecConfig() *CrowdSec { } } +func (c *CrowdSec) GetCliConfig(configFile *string) error { + /*overriden by cfg file*/ + if *configFile != "" { + rcfg, err := ioutil.ReadFile(*configFile) + if err != nil { + return fmt.Errorf("read '%s' : %s", *configFile, err) + } + if err := yaml.UnmarshalStrict(rcfg, c); err != nil { + return fmt.Errorf("parse '%s' : %s", *configFile, err) + } + if c.AcquisitionFile == "" { + c.AcquisitionFile = filepath.Clean(c.ConfigFolder + "/acquis.yaml") + } + } + return nil + +} + // GetOPT return flags parsed from command line func (c *CrowdSec) GetOPT() error { From 38a750d3df60a26611463a643f34d970fbf26837 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 18:30:47 +0200 Subject: [PATCH 08/11] fix --- cmd/crowdsec-cli/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index df9bf84e5..97513456a 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -110,7 +110,7 @@ API interaction: rootCmd.AddCommand(cmdVersion) //rootCmd.PersistentFlags().BoolVarP(&config.simulation, "simulate", "s", false, "No action; perform a simulation of events that would occur based on the current arguments.") - rootCmd.PersistentFlags().StringVarP(&config.ConfigFilePath, "config", "c", "/etc/crowdsec/default.yaml", "path to crowdsec config file (default: /etc/crowdsec/default.yaml)") + rootCmd.PersistentFlags().StringVarP(&config.ConfigFilePath, "config", "c", "/etc/crowdsec/config/default.yaml", "path to crowdsec config file (default: /etc/crowdsec/config/default.yaml)") rootCmd.PersistentFlags().StringVarP(&config.output, "output", "o", "human", "Output format : human, json, raw.") rootCmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug.") From 087549e2ba7c01f386c7d852a02ffb2c9df64565 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 18:40:00 +0200 Subject: [PATCH 09/11] fix wizard --- wizard.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wizard.sh b/wizard.sh index 451ea08f7..cd3063d31 100755 --- a/wizard.sh +++ b/wizard.sh @@ -22,6 +22,7 @@ CROWDSEC_CONFIG_PATH="/etc/crowdsec" CROWDSEC_CONFIG_PATH="${CROWDSEC_CONFIG_PATH}/config" CROWDSEC_LOG_FILE="/var/log/crowdsec.log" CROWDSEC_BACKEND_FOLDER="/etc/crowdsec/plugins/backend" +CSCLI_FOLDER="/etc/crowdsec/config/cscli" CROWDSEC_BIN="./cmd/crowdsec/crowdsec" CSCLI_BIN="./cmd/crowdsec-cli/cscli" @@ -274,7 +275,8 @@ install_crowdsec() { mkdir -p "${CROWDSEC_CONFIG_PATH}/patterns" || exit mkdir -p "${CROWDSEC_BACKEND_FOLDER}" || exit - mkdir -p "${CROWDSEC_PLUGIN_BACKEND_DIR}" || exit + mkdir -p "${CROWDSEC_PLUGIN_BACKEND_DIR}" || exit + mkdir -p "${CSCLI_FOLDER}" || exit (cd ./plugins && find . -type f -name "*.so" -exec install -Dm 644 {} "${CROWDSEC_PLUGIN_DIR}/{}" \; && cd ../) || exit cp -r ./config/plugins/backend/* "${CROWDSEC_BACKEND_FOLDER}" || exit From 1f91bd8af0e2baed9d3668e608efb44f7a2aa2c4 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 19:17:03 +0200 Subject: [PATCH 10/11] make var public --- cmd/crowdsec-cli/api.go | 2 +- cmd/crowdsec-cli/backup-restore.go | 14 +++++++------- cmd/crowdsec-cli/config.go | 2 +- cmd/crowdsec-cli/main.go | 4 ++-- pkg/cwhub/hubMgmt.go | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/crowdsec-cli/api.go b/cmd/crowdsec-cli/api.go index 6ea5e1639..355faa043 100644 --- a/cmd/crowdsec-cli/api.go +++ b/cmd/crowdsec-cli/api.go @@ -158,7 +158,7 @@ cscli api credentials # Display your API credentials return err } - err = outputCTX.LoadAPIConfig(path.Join(config.installFolder, apiConfigFile)) + err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile)) if err != nil { return err } diff --git a/cmd/crowdsec-cli/backup-restore.go b/cmd/crowdsec-cli/backup-restore.go index 818d50e69..f0509c76f 100644 --- a/cmd/crowdsec-cli/backup-restore.go +++ b/cmd/crowdsec-cli/backup-restore.go @@ -151,7 +151,7 @@ func restoreFromDirectory(source string) error { continue } stage := file.Name() - stagedir := fmt.Sprintf("%s/%s/%s/", config.installFolder, itype, stage) + stagedir := fmt.Sprintf("%s/%s/%s/", config.InstallFolder, itype, stage) log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir) if err = os.MkdirAll(stagedir, os.ModePerm); err != nil { return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err) @@ -188,7 +188,7 @@ func restoreFromDirectory(source string) error { /* Restore acquis */ - yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.installFolder) + yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.InstallFolder) bac := fmt.Sprintf("%s/acquis.yaml", source) if err = copyFile(bac, yamlAcquisFile); err != nil { return fmt.Errorf("failed copy %s to %s : %s", bac, yamlAcquisFile, err) @@ -202,7 +202,7 @@ func restoreAPICreds(source string) error { var err error /*check existing configuration*/ - apiyaml := path.Join(config.installFolder, apiConfigFile) + apiyaml := path.Join(config.InstallFolder, apiConfigFile) api := &cwapi.ApiCtx{} if err = api.LoadConfig(apiyaml); err != nil { @@ -332,7 +332,7 @@ func backupToDirectory(target string) error { /* Backup acquis */ - yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.installFolder) + yamlAcquisFile := fmt.Sprintf("%s/acquis.yaml", config.InstallFolder) bac := fmt.Sprintf("%s/acquis.yaml", target) if err = copyFile(yamlAcquisFile, bac); err != nil { return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) @@ -341,7 +341,7 @@ func backupToDirectory(target string) error { /* Backup default.yaml */ - defyaml := fmt.Sprintf("%s/default.yaml", config.installFolder) + defyaml := fmt.Sprintf("%s/default.yaml", config.InstallFolder) bac = fmt.Sprintf("%s/default.yaml", target) if err = copyFile(defyaml, bac); err != nil { return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) @@ -354,8 +354,8 @@ func backupToDirectory(target string) error { log.Fatalf("no API output context, won't save api credentials") } outputCTX.API = &cwapi.ApiCtx{} - if err = outputCTX.API.LoadConfig(path.Join(config.installFolder, apiConfigFile)); err != nil { - return fmt.Errorf("unable to load api config %s : %s", path.Join(config.installFolder, apiConfigFile), err) + if err = outputCTX.API.LoadConfig(path.Join(config.InstallFolder, apiConfigFile)); err != nil { + return fmt.Errorf("unable to load api config %s : %s", path.Join(config.InstallFolder, apiConfigFile), err) } credsYaml, err := json.Marshal(&outputCTX.API.Creds) if err != nil { diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/config.go index 494199e4e..92d2f6248 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/config.go @@ -15,7 +15,7 @@ type cliConfig struct { configFolder string output string HubFolder string `yaml:"hub_folder"` - installFolder string + InstallFolder string BackendPluginFolder string `yaml:"backend_folder"` DataFolder string `yaml:"data_folder"` } diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 97513456a..7ab1d227b 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -51,12 +51,12 @@ func initConfig() { } /*read config*/ - config.installFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder)) + config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder)) config.HubFolder = filepath.Clean(config.configFolder + "/hub/") config.BackendPluginFolder = filepath.Clean(csConfig.OutputConfig.BackendFolder) config.DataFolder = filepath.Clean(csConfig.DataFolder) // - cwhub.Installdir = config.installFolder + cwhub.Installdir = config.InstallFolder cwhub.Cfgdir = config.configFolder cwhub.Hubdir = config.HubFolder config.configured = true diff --git a/pkg/cwhub/hubMgmt.go b/pkg/cwhub/hubMgmt.go index 4729bb5d1..3b735963f 100644 --- a/pkg/cwhub/hubMgmt.go +++ b/pkg/cwhub/hubMgmt.go @@ -42,8 +42,8 @@ type ItemVersion struct { //Item can be : parsed, scenario, collection type Item struct { /*descriptive info*/ - Type string `yaml:"type,omitempty"` //parser|postoverflows|scenario|collection(|enrich) - Stage string `json:"stage" yaml:"type,omitempty,omitempty"` //Stage for parser|postoverflow : s00-raw/s01-... + Type string `yaml:"type,omitempty"` //parser|postoverflows|scenario|collection(|enrich) + Stage string `json:"stage" yaml:"stage,omitempty,omitempty"` //Stage for parser|postoverflow : s00-raw/s01-... Name string //as seen in .config.json, usually "author/name" FileName string //the filename, ie. apache2-logs.yaml Description string `yaml:"description,omitempty"` //as seen in .config.json From 3dfc9de409f86942ba6bf3e71fda1490db574d39 Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 19:19:56 +0200 Subject: [PATCH 11/11] fix --- cmd/crowdsec-cli/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 7ab1d227b..189848be6 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -40,7 +40,7 @@ func initConfig() { if err := csConfig.GetCliConfig(&config.ConfigFilePath); err != nil { log.Fatalf(err.Error()) } - config.configFolder = filepath.Join(filepath.Clean(csConfig.CsCliFolder)) + config.configFolder = filepath.Clean(csConfig.CsCliFolder) if strings.HasPrefix(config.configFolder, "~/") { usr, err := user.Current() @@ -51,7 +51,7 @@ func initConfig() { } /*read config*/ - config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder)) + config.InstallFolder = filepath.Clean(csConfig.ConfigFolder) config.HubFolder = filepath.Clean(config.configFolder + "/hub/") config.BackendPluginFolder = filepath.Clean(csConfig.OutputConfig.BackendFolder) config.DataFolder = filepath.Clean(csConfig.DataFolder)