Przeglądaj źródła

don't load lapi creds when running only api (#608)

Co-authored-by: AlteredCoder <AlteredCoder>
AlteredCoder 4 lat temu
rodzic
commit
22c4962768

+ 1 - 1
cmd/crowdsec-cli/main.go

@@ -44,7 +44,7 @@ func initConfig() {
 	csConfig = csconfig.NewConfig()
 
 	log.Debugf("Using %s as configuration file", ConfigFilePath)
-	if err := csConfig.LoadConfigurationFile(ConfigFilePath); err != nil {
+	if err := csConfig.LoadConfigurationFile(ConfigFilePath, csConfig.DisableAPI, csConfig.DisableAgent); err != nil {
 		log.Fatalf(err.Error())
 	}
 	if csConfig.Cscli == nil {

+ 3 - 5
cmd/crowdsec/main.go

@@ -201,17 +201,15 @@ func (f *Flags) Parse() {
 
 // LoadConfig return configuration parsed from configuration file
 func LoadConfig(config *csconfig.GlobalConfig) error {
-
+	disableAPI = flags.DisableAPI
+	disableAgent = flags.DisableAgent
 	if flags.ConfigFile != "" {
-		if err := config.LoadConfigurationFile(flags.ConfigFile); err != nil {
+		if err := config.LoadConfigurationFile(flags.ConfigFile, disableAPI, disableAgent); err != nil {
 			return fmt.Errorf("while loading configuration : %s", err)
 		}
 	} else {
 		log.Warningf("no configuration file provided")
 	}
-	disableAPI = flags.DisableAPI
-	disableAgent = flags.DisableAgent
-
 	if !disableAPI && (cConfig.API == nil || cConfig.API.Server == nil) {
 		log.Errorf("no API server configuration found, will not start the local API")
 		disableAPI = true

+ 15 - 12
pkg/csconfig/config.go

@@ -16,14 +16,16 @@ import (
 /*top-level config : defaults,overriden by cfg file,overriden by cli*/
 type GlobalConfig struct {
 	//just a path to ourself :p
-	Self        *string             `yaml:"-"`
-	Common      *CommonCfg          `yaml:"common,omitempty"`
-	Prometheus  *PrometheusCfg      `yaml:"prometheus,omitempty"`
-	Crowdsec    *CrowdsecServiceCfg `yaml:"crowdsec_service,omitempty"`
-	Cscli       *CscliCfg           `yaml:"cscli,omitempty"`
-	DbConfig    *DatabaseCfg        `yaml:"db_config,omitempty"`
-	API         *APICfg             `yaml:"api,omitempty"`
-	ConfigPaths *ConfigurationPaths `yaml:"config_paths,omitempty"`
+	Self         *string             `yaml:"-"`
+	Common       *CommonCfg          `yaml:"common,omitempty"`
+	Prometheus   *PrometheusCfg      `yaml:"prometheus,omitempty"`
+	Crowdsec     *CrowdsecServiceCfg `yaml:"crowdsec_service,omitempty"`
+	Cscli        *CscliCfg           `yaml:"cscli,omitempty"`
+	DbConfig     *DatabaseCfg        `yaml:"db_config,omitempty"`
+	API          *APICfg             `yaml:"api,omitempty"`
+	ConfigPaths  *ConfigurationPaths `yaml:"config_paths,omitempty"`
+	DisableAPI   bool                `yaml:"-"`
+	DisableAgent bool                `yaml:"-"`
 }
 
 func (c *GlobalConfig) Dump() error {
@@ -35,8 +37,9 @@ func (c *GlobalConfig) Dump() error {
 	return nil
 }
 
-func (c *GlobalConfig) LoadConfigurationFile(path string) error {
-
+func (c *GlobalConfig) LoadConfigurationFile(path string, disableAPI bool, disableAgent bool) error {
+	c.DisableAPI = disableAPI
+	c.DisableAgent = disableAgent
 	fcontent, err := ioutil.ReadFile(path)
 	if err != nil {
 		return errors.Wrap(err, "failed to read config file")
@@ -112,7 +115,7 @@ func (c *GlobalConfig) LoadConfiguration() error {
 		c.Cscli.HubIndexFile = c.ConfigPaths.HubIndexFile
 	}
 
-	if c.API.Client != nil && c.API.Client.CredentialsFilePath != "" {
+	if c.API.Client != nil && c.API.Client.CredentialsFilePath != "" && !c.DisableAgent {
 		fcontent, err := ioutil.ReadFile(c.API.Client.CredentialsFilePath)
 		if err != nil {
 			return errors.Wrap(err, fmt.Sprintf("failed to read api client credential configuration file '%s'", c.API.Client.CredentialsFilePath))
@@ -133,7 +136,7 @@ func (c *GlobalConfig) LoadConfiguration() error {
 		}
 	}
 
-	if c.API.Server != nil {
+	if c.API.Server != nil && !c.DisableAPI {
 		c.API.Server.DbConfig = c.DbConfig
 		c.API.Server.LogDir = c.Common.LogDir
 		if err := c.API.Server.LoadProfiles(); err != nil {

+ 3 - 3
pkg/csconfig/config_test.go

@@ -17,19 +17,19 @@ func TestDefaultConfig(t *testing.T) {
 func TestNormalLoad(t *testing.T) {
 
 	x := NewConfig()
-	err := x.LoadConfigurationFile("./tests/config.yaml")
+	err := x.LoadConfigurationFile("./tests/config.yaml", false, false)
 	if err != nil {
 		t.Fatalf("unexpected error %s", err)
 	}
 
 	x = NewConfig()
-	err = x.LoadConfigurationFile("./tests/xxx.yaml")
+	err = x.LoadConfigurationFile("./tests/xxx.yaml", false, false)
 	if fmt.Sprintf("%s", err) != "failed to read config file: open ./tests/xxx.yaml: no such file or directory" {
 		t.Fatalf("unexpected error %s", err)
 	}
 
 	x = NewConfig()
-	err = x.LoadConfigurationFile("./tests/simulation.yaml")
+	err = x.LoadConfigurationFile("./tests/simulation.yaml", false, false)
 	if !strings.HasPrefix(fmt.Sprintf("%s", err), "failed unmarshaling config: yaml: unmarshal error") {
 		t.Fatalf("unexpected error %s", err)
 	}

+ 3 - 1
pkg/cwhub/loader.go

@@ -337,7 +337,9 @@ func LocalSync(cscli *csconfig.CscliCfg) error {
 }
 
 func GetHubIdx(cscli *csconfig.CscliCfg) error {
-
+	if cscli == nil {
+		return fmt.Errorf("no configuration found for cscli")
+	}
 	log.Debugf("loading hub idx %s", cscli.HubIndexFile)
 	bidx, err := ioutil.ReadFile(cscli.HubIndexFile)
 	if err != nil {