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"`