From 6757fa3cee844bc0a959fca92a1a16c66aecfafc Mon Sep 17 00:00:00 2001 From: AlteredCoder Date: Sun, 24 May 2020 18:15:59 +0200 Subject: [PATCH] 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 {