address review comments

This commit is contained in:
Grant Murphy 2017-12-14 10:04:22 +10:00
parent af25ac1f6e
commit 25d74c6b20
4 changed files with 41 additions and 5 deletions

View file

@ -101,7 +101,7 @@ func (gas *Analyzer) Process(packagePath string) error {
} }
packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments} packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments}
packageFiles := make([]string, 0) var packageFiles []string
for _, filename := range basePackage.GoFiles { for _, filename := range basePackage.GoFiles {
packageFiles = append(packageFiles, path.Join(packagePath, filename)) packageFiles = append(packageFiles, path.Join(packagePath, filename))
} }

View file

@ -52,6 +52,27 @@ var _ = Describe("Analyzer", func() {
}) })
It("should be able to analyze mulitple Go files", func() {
analyzer.LoadRules(rules.Generate().Builders()...)
pkg := testutils.NewTestPackage()
defer pkg.Close()
pkg.AddFile("foo.go", `
package main
func main(){
bar()
}`)
pkg.AddFile("bar.go", `
package main
func bar(){
println("package has two files!")
}`)
pkg.Build()
err := analyzer.Process(pkg.Path)
Expect(err).ShouldNot(HaveOccurred())
_, metrics := analyzer.Report()
Expect(metrics.NumFiles).To(Equal(2))
})
It("should find errors when nosec is not in use", func() { It("should find errors when nosec is not in use", func() {
// Rule for MD5 weak crypto usage // Rule for MD5 weak crypto usage

View file

@ -91,7 +91,7 @@ func usage() {
flag.PrintDefaults() flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\n\nRULES:\n\n") fmt.Fprint(os.Stderr, "\n\nRULES:\n\n")
// sorted rule list for eas of reading // sorted rule list for ease of reading
rl := rules.Generate() rl := rules.Generate()
keys := make([]string, 0, len(rl)) keys := make([]string, 0, len(rl))
for key := range rl { for key := range rl {
@ -126,13 +126,19 @@ func loadConfig(configFile string) (gas.Config, error) {
func loadRules(include, exclude string) rules.RuleList { func loadRules(include, exclude string) rules.RuleList {
var filters []rules.RuleFilter var filters []rules.RuleFilter
if include != "" { if include != "" {
log.Printf("including rules: %s", include)
including := strings.Split(include, ",") including := strings.Split(include, ",")
filters = append(filters, rules.NewRuleFilter(false, including...)) filters = append(filters, rules.NewRuleFilter(false, including...))
} else {
log.Println("including rules: default")
} }
if exclude != "" { if exclude != "" {
log.Printf("excluding rules: %s", exclude)
excluding := strings.Split(exclude, ",") excluding := strings.Split(exclude, ",")
filters = append(filters, rules.NewRuleFilter(true, excluding...)) filters = append(filters, rules.NewRuleFilter(true, excluding...))
} else {
log.Println("excluding rules: default")
} }
return rules.Generate(filters...) return rules.Generate(filters...)
} }
@ -186,6 +192,9 @@ func main() {
// Load enabled rule definitions // Load enabled rule definitions
ruleDefinitions := loadRules(*flagRulesInclude, *flagRulesExclude) ruleDefinitions := loadRules(*flagRulesInclude, *flagRulesExclude)
if len(ruleDefinitions) <= 0 {
log.Fatal("cannot continue: no rules are configured.")
}
// Create the analyzer // Create the analyzer
analyzer := gas.NewAnalyzer(config, logger) analyzer := gas.NewAnalyzer(config, logger)

View file

@ -8,6 +8,12 @@ import (
"io/ioutil" "io/ioutil"
) )
const (
// Globals are applicable to all rules and used for general
// configuration settings for gas.
Globals = "global"
)
// Config is used to provide configuration and customization to each of the rules. // Config is used to provide configuration and customization to each of the rules.
type Config map[string]interface{} type Config map[string]interface{}
@ -16,7 +22,7 @@ type Config map[string]interface{}
// or from a *os.File. // or from a *os.File.
func NewConfig() Config { func NewConfig() Config {
cfg := make(Config) cfg := make(Config)
cfg["global"] = make(map[string]string) cfg[Globals] = make(map[string]string)
return cfg return cfg
} }
@ -60,7 +66,7 @@ func (c Config) Set(section string, value interface{}) {
// GetGlobal returns value associated with global configuration option // GetGlobal returns value associated with global configuration option
func (c Config) GetGlobal(option string) (string, error) { func (c Config) GetGlobal(option string) (string, error) {
if globals, ok := c["global"]; ok { if globals, ok := c[Globals]; ok {
if settings, ok := globals.(map[string]string); ok { if settings, ok := globals.(map[string]string); ok {
if value, ok := settings[option]; ok { if value, ok := settings[option]; ok {
return value, nil return value, nil
@ -74,7 +80,7 @@ func (c Config) GetGlobal(option string) (string, error) {
// SetGlobal associates a value with a global configuration ooption // SetGlobal associates a value with a global configuration ooption
func (c Config) SetGlobal(option, value string) { func (c Config) SetGlobal(option, value string) {
if globals, ok := c["global"]; ok { if globals, ok := c[Globals]; ok {
if settings, ok := globals.(map[string]string); ok { if settings, ok := globals.(map[string]string); ok {
settings[option] = value settings[option] = value
} }