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
}
err = outputCTX.LoadAPIConfig(path.Join(config.InstallFolder, apiConfigFile))
err = outputCTX.LoadAPIConfig(path.Join(config.installFolder, apiConfigFile))
if err != nil {
return err
}

View file

@ -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 {

View file

@ -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

View file

@ -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"))
}
},
}

View file

@ -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.")

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
func (c *CrowdSec) GetOPT() error {