Skip to content

Commit

Permalink
Merge pull request americanexpress#13 from grinish21/dev-work
Browse files Browse the repository at this point in the history
fix: read config file when flag provided americanexpress#8
  • Loading branch information
asishrs authored Dec 8, 2020
2 parents eaf1ac3 + f8086d4 commit 4f365f1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/earlybird.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 1 addition & 12 deletions pkg/config/cfgReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 13 additions & 5 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net/http"
"os"
"path"
"time"

"github.com/americanexpress/earlybird/pkg/api"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/scan/ruleCfgReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package scan
import (
"fmt"
"os"
"path"
"regexp"

cfgreader "github.com/americanexpress/earlybird/pkg/config"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package configupdate
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"path"

cfgreader "github.com/americanexpress/earlybird/pkg/config"
)
Expand All @@ -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 {
Expand All @@ -38,7 +40,7 @@ func UpdateConfigFiles(configDir string, appConfigPath string, appConfigURL stri
}
}

fmt.Println("Updating ", appConfigPath)
log.Println("Updating ", appConfigPath)
return downloadFile(moduleFilePath, appConfigURL)
}

Expand Down

0 comments on commit 4f365f1

Please sign in to comment.