This commit is contained in:
AlteredCoder 2020-05-24 18:15:59 +02:00
parent 176d5197f6
commit 6757fa3cee
6 changed files with 44 additions and 27 deletions

View file

@ -158,7 +158,7 @@ cscli api credentials # Display your API credentials
return err return err
} }
err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile)) err = outputCTX.LoadAPIConfig(path.Join(config.installFolder, apiConfigFile))
if err != nil { if err != nil {
return err return err
} }

View file

@ -151,7 +151,7 @@ func restoreFromDirectory(source string) error {
continue continue
} }
stage := file.Name() 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) log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)
if err = os.MkdirAll(stagedir, os.ModePerm); err != nil { if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err) return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err)
@ -188,7 +188,7 @@ func restoreFromDirectory(source string) error {
/* /*
Restore acquis 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) bac := fmt.Sprintf("%s/acquis.yaml", source)
if err = copyFile(bac, yamlAcquisFile); err != nil { if err = copyFile(bac, yamlAcquisFile); err != nil {
return fmt.Errorf("failed copy %s to %s : %s", bac, yamlAcquisFile, err) return fmt.Errorf("failed copy %s to %s : %s", bac, yamlAcquisFile, err)
@ -202,7 +202,7 @@ func restoreAPICreds(source string) error {
var err error var err error
/*check existing configuration*/ /*check existing configuration*/
apiyaml := path.Join(config.InstallFolder, apiConfigFile) apiyaml := path.Join(config.installFolder, apiConfigFile)
api := &cwapi.ApiCtx{} api := &cwapi.ApiCtx{}
if err = api.LoadConfig(apiyaml); err != nil { if err = api.LoadConfig(apiyaml); err != nil {
@ -332,7 +332,7 @@ func backupToDirectory(target string) error {
/* /*
Backup acquis 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) bac := fmt.Sprintf("%s/acquis.yaml", target)
if err = copyFile(yamlAcquisFile, bac); err != nil { if err = copyFile(yamlAcquisFile, bac); err != nil {
return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err)
@ -341,7 +341,7 @@ func backupToDirectory(target string) error {
/* /*
Backup default.yaml 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) bac = fmt.Sprintf("%s/default.yaml", target)
if err = copyFile(defyaml, bac); err != nil { if err = copyFile(defyaml, bac); err != nil {
return fmt.Errorf("failed copy %s to %s : %s", yamlAcquisFile, bac, err) 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") log.Fatalf("no API output context, won't save api credentials")
} }
outputCTX.API = &cwapi.ApiCtx{} outputCTX.API = &cwapi.ApiCtx{}
if err = outputCTX.API.LoadConfig(path.Join(config.InstallFolder, apiConfigFile)); err != nil { 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) return fmt.Errorf("unable to load api config %s : %s", path.Join(config.installFolder, apiConfigFile), err)
} }
credsYaml, err := json.Marshal(&outputCTX.API.Creds) credsYaml, err := json.Marshal(&outputCTX.API.Creds)
if err != nil { if err != nil {

View file

@ -190,7 +190,6 @@ You can add/delete/list or flush current bans in your local ban DB.`,
return nil 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.PersistentFlags().StringVar(&remediationType, "remediation", "ban", "Set specific remediation type : ban|slow|captcha")
cmdBan.Flags().SortFlags = false cmdBan.Flags().SortFlags = false
cmdBan.PersistentFlags().SortFlags = false cmdBan.PersistentFlags().SortFlags = false

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"path"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -12,12 +11,13 @@ import (
/*CliCfg is the cli configuration structure, might be unexported*/ /*CliCfg is the cli configuration structure, might be unexported*/
type cliConfig struct { type cliConfig struct {
configured bool configured bool
configFolder string `yaml:"cliconfig,omitempty"` /*overload ~/.cscli/*/ ConfigFilePath string `yaml:"config_file"`
output string /*output is human, json*/ configFolder string
hubFolder string output string
InstallFolder string `yaml:"installdir"` /*/etc/crowdsec/*/ HubFolder string `yaml:"hub_folder"`
BackendPluginFolder string `yaml:"backend"` installFolder string
dbPath string BackendPluginFolder string `yaml:"backend_folder"`
DataFolder string `yaml:"data_folder"`
} }
func NewConfigCmd() *cobra.Command { 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) { Run: func(cmd *cobra.Command, args []string) {
if config.output == "json" { if config.output == "json" {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"installdir": config.InstallFolder, "crowdsec_configuration_file": config.ConfigFilePath,
"cliconfig": path.Join(config.configFolder, "/config"), "backend_folder": config.BackendPluginFolder,
"data_folder": config.DataFolder,
}).Warning("Current config") }).Warning("Current config")
} else { } else {
x, err := yaml.Marshal(config) 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) log.Fatalf("failed to marshal current configuration : %v", err)
} }
fmt.Printf("%s", x) fmt.Printf("%s", x)
fmt.Printf("#cliconfig: %s", path.Join(config.configFolder, "/config"))
} }
}, },
} }

View file

@ -37,7 +37,7 @@ func initConfig() {
} }
csConfig := csconfig.NewCrowdSecConfig() csConfig := csconfig.NewCrowdSecConfig()
if err := csConfig.GetOPT(); err != nil { if err := csConfig.GetCliConfig(&config.ConfigFilePath); err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
config.configFolder = filepath.Join(filepath.Clean(csConfig.CsCliFolder)) config.configFolder = filepath.Join(filepath.Clean(csConfig.CsCliFolder))
@ -50,18 +50,16 @@ func initConfig() {
config.configFolder = usr.HomeDir + "/" + config.configFolder[2:] config.configFolder = usr.HomeDir + "/" + config.configFolder[2:]
} }
log.Infof("CONFIG : %+v\n", config)
log.Infof("CSCFONg : %+v \n", csConfig)
/*read config*/ /*read config*/
config.InstallFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder), "./config/") config.installFolder = filepath.Join(filepath.Clean(csConfig.ConfigFolder))
config.hubFolder = filepath.Clean(config.configFolder + "/hub/") config.HubFolder = filepath.Clean(config.configFolder + "/hub/")
config.BackendPluginFolder = filepath.Clean(csConfig.OutputConfig.BackendFolder) 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.Cfgdir = config.configFolder
cwhub.Hubdir = config.hubFolder cwhub.Hubdir = config.HubFolder
config.configured = true config.configured = true
} }
func main() { func main() {
@ -112,6 +110,8 @@ API interaction:
rootCmd.AddCommand(cmdVersion) 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().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().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(&dbg_lvl, "debug", false, "Set logging to debug.")
rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.") rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.")

View file

@ -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 // GetOPT return flags parsed from command line
func (c *CrowdSec) GetOPT() error { func (c *CrowdSec) GetOPT() error {