From f8086d4abee869df3c43a334574b8b156242bfd9 Mon Sep 17 00:00:00 2001 From: grinish21 Date: Sun, 6 Dec 2020 19:17:20 -0700 Subject: [PATCH] fix: read config file when flag provided #8 --- config/earlybird.json | 2 +- pkg/config/cfgReader.go | 13 +------------ pkg/core/core.go | 18 +++++++++++++----- pkg/scan/ruleCfgReader.go | 7 ++++--- pkg/update/update.go | 8 +++++--- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/config/earlybird.json b/config/earlybird.json index 7f3db92..b8a22a7 100644 --- a/config/earlybird.json +++ b/config/earlybird.json @@ -50,5 +50,5 @@ "display_threshold_level": 3, "display_confidence_threshold_level": 2, "earlybird_config_url": "https://raw.githubusercontent.com/americanexpress/earlybird/master/config/earlybird.json", - "version": "1.24.6" + "version": "1.25.0" } \ No newline at end of file diff --git a/pkg/config/cfgReader.go b/pkg/config/cfgReader.go index 7de9410..0abdcd3 100755 --- a/pkg/config/cfgReader.go +++ b/pkg/config/cfgReader.go @@ -19,26 +19,15 @@ package cfgreader import ( "encoding/json" "io/ioutil" - "log" - "os" - - "github.com/americanexpress/earlybird/pkg/utils" ) //Settings contains imported earlybird.json configuration var Settings Configs -func init() { - configPath := utils.GetConfigDir() + "earlybird.json" - err := LoadConfig(&Settings, configPath) - if err != nil { - log.Fatal("Failed to load Earlybird config", err) - } -} - //LoadConfig parses json configuration file into structure func LoadConfig(cfg interface{}, path string) (err error) { + jsonFile, err := os.Open(path) // if we os.Open returns an error then handle it if err != nil { diff --git a/pkg/core/core.go b/pkg/core/core.go index 6b2cd72..634d3ec 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -23,6 +23,7 @@ import ( "log" "net/http" "os" + "path" "time" "github.com/americanexpress/earlybird/pkg/api" @@ -134,14 +135,23 @@ func (eb *EarlybirdCfg) StartHTTP(ptr PTRHTTPConfig) { //ConfigInit loads in the earlybird configuration and CLI flags func (eb *EarlybirdCfg) ConfigInit() { - eb.Config.ConfigDir = utils.GetConfigDir() + flag.Parse() + + //Load Earlybird config + eb.Config.ConfigDir = *ptrConfigDir + configPath := path.Join(eb.Config.ConfigDir, "earlybird.json") + log.Printf("Earlybird config path- %s ...", configPath) + err := cfgreader.LoadConfig(&cfgreader.Settings, configPath) + if err != nil { + log.Fatal("Failed to load Earlybird config ", err) + } + eb.Config.LevelMap = cfgreader.Settings.GetLevelMap() // Build the string to display available modules for the CLI flags availableModules := cfgreader.Settings.GetAvailableModules() - //Load CLI arguments and parse + //Load CLI arguments flag.Var(&enableFlags, "enable", "Enable individual scanning modules "+utils.GetDisplayList(availableModules)) - flag.Parse() //Assign CLI arguments to our global configuration eb.Config.WorkerCount = *ptrWorkerCount @@ -153,7 +163,6 @@ func (eb *EarlybirdCfg) ConfigInit() { eb.Config.OutputFormat = *ptrOutputFormat eb.Config.OutputFile = *ptrOutputFile eb.Config.SearchDir = *ptrPath - eb.Config.ConfigDir = *ptrConfigDir eb.Config.IgnoreFile = *ptrIgnoreFile eb.Config.GitStream = *ptrGitStreamInput eb.Config.RulesOnly = *ptrRulesOnly @@ -169,7 +178,6 @@ func (eb *EarlybirdCfg) ConfigInit() { // Check to see if the user opted to update. If they choose this option // the configuration files will be updated and the program will exit. if *ptrUpdateFlag { - configPath := utils.GetConfigDir() + "earlybird.json" doUpdate(eb.Config.ConfigDir, configPath, cfgreader.Settings.ConfigFileURL) } diff --git a/pkg/scan/ruleCfgReader.go b/pkg/scan/ruleCfgReader.go index 863a163..74514b6 100755 --- a/pkg/scan/ruleCfgReader.go +++ b/pkg/scan/ruleCfgReader.go @@ -19,6 +19,7 @@ package scan import ( "fmt" "os" + "path" "regexp" cfgreader "github.com/americanexpress/earlybird/pkg/config" @@ -47,14 +48,14 @@ func Init(cfg cfgreader.EarlybirdConfig) { //Load solutions for the rules if cfg.ShowSolutions { - SolutionConfigs = loadSolutions(cfg.ConfigDir + "solutions.json") + SolutionConfigs = loadSolutions(path.Join(cfg.ConfigDir , "solutions.json")) } // Init label configs - Labels = loadLabelConfigs(cfg.ConfigDir + "labels.json") + Labels = loadLabelConfigs(path.Join(cfg.ConfigDir , "labels.json")) //Load false positive rules - FalsePositiveRules = loadFalsePositives(cfg.ConfigDir + "false-positives.json") + FalsePositiveRules = loadFalsePositives(path.Join(cfg.ConfigDir, "false-positives.json")) // If we're only displaying the rules to be run, filter out anything that we wouldn't fail on and exit to skip the scan. // Only one output option since we exit before any interaction with Writers diff --git a/pkg/update/update.go b/pkg/update/update.go index beb98e0..eacb364 100755 --- a/pkg/update/update.go +++ b/pkg/update/update.go @@ -19,7 +19,9 @@ package configupdate import ( "fmt" "io/ioutil" + "log" "net/http" + "path" cfgreader "github.com/americanexpress/earlybird/pkg/config" ) @@ -28,8 +30,8 @@ import ( func UpdateConfigFiles(configDir string, appConfigPath string, appConfigURL string) error { var moduleFilePath string for _, module := range cfgreader.Settings.ModuleConfigs { - moduleFilePath = configDir + module.Name + ".json" - fmt.Println("Updating ", moduleFilePath) + moduleFilePath = path.Join(configDir , module.Name + ".json") + log.Println("Updating ", moduleFilePath) if module.ConfigURL != "" { err := downloadFile(moduleFilePath, module.ConfigURL) if err != nil { @@ -38,7 +40,7 @@ func UpdateConfigFiles(configDir string, appConfigPath string, appConfigURL stri } } - fmt.Println("Updating ", appConfigPath) + log.Println("Updating ", appConfigPath) return downloadFile(moduleFilePath, appConfigURL) }