fixing all golint and adding some more tests

This commit is contained in:
zach rice committed 2019-11-11 21:50:50 -05:00
1 parent 2ccd40677d
commit e446ba0738
19 files changed
+161 -91

No files matched your search

+3 -3
View File
@@ -7,6 +7,7 @@ import (
"path"
)
// Run accepts a manager and begins an audit based on the options/configs set in the manager.
func Run(m *manager.Manager) error {
if m.Opts.OwnerPath != "" {
files, err := ioutil.ReadDir(m.Opts.OwnerPath)
@@ -17,7 +18,7 @@ func Run(m *manager.Manager) error {
if !f.IsDir() {
continue
}
m.Opts.RepoPath = fmt.Sprintf("%s/%s",m.Opts.OwnerPath, f.Name())
m.Opts.RepoPath = fmt.Sprintf("%s/%s", m.Opts.OwnerPath, f.Name())
if err := runHelper(NewRepo(m)); err != nil {
// TODO or send to errchan?
return err
@@ -46,10 +47,9 @@ func runHelper(r *Repo) error {
return nil
}
} else {
if err := r.Clone(); err != nil {
if err := r.Clone(nil); err != nil {
return err
}
}
return r.Audit()
}
+16 -13
View File
@@ -31,7 +31,6 @@ func TestAudit(t *testing.T) {
opts: options.Options{
RepoPath: "../test_data/test_repos/test_repo_1",
Report: "../test_data/test_local_repo_one_aws_leak.json.got",
},
wantPath: "../test_data/test_local_repo_one_aws_leak.json",
},
@@ -123,8 +122,7 @@ func TestAudit(t *testing.T) {
description: "test owner path",
opts: options.Options{
OwnerPath: "../test_data/test_repos/",
Report: "../test_data/test_local_owner_aws_leak.json.got",
Report: "../test_data/test_local_owner_aws_leak.json.got",
},
wantPath: "../test_data/test_local_owner_aws_leak.json",
},
@@ -137,13 +135,21 @@ func TestAudit(t *testing.T) {
},
wantPath: "../test_data/test_entropy.json",
},
{
description: "test entropy and regex",
opts: options.Options{
RepoPath: "../test_data/test_repos/test_repo_1",
Report: "../test_data/test_regex_entropy.json.got",
Config: "../test_data/test_configs/regex_entropy.toml",
},
wantPath: "../test_data/test_regex_entropy.json",
},
{
description: "test local repo four entropy alternative config",
opts: options.Options{
RepoPath: "../test_data/test_repos/test_repo_4",
Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
RepoPath: "../test_data/test_repos/test_repo_4",
Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
RepoConfig: true,
},
wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
},
@@ -277,7 +283,6 @@ func TestAuditUncommited(t *testing.T) {
}
}
}
}
func fileCheck(wantPath, gotPath string) error {
@@ -294,11 +299,10 @@ func fileCheck(wantPath, gotPath string) error {
if strings.Trim(string(want), "\n") != strings.Trim(string(got), "\n") {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(want), string(got), false)
return fmt.Errorf("does not equal: %s\n", dmp.DiffPrettyText(diffs))
} else {
if err := os.Remove(gotPath); err != nil {
return err
}
return fmt.Errorf("does not equal: %s", dmp.DiffPrettyText(diffs))
}
if err := os.Remove(gotPath); err != nil {
return err
}
return nil
}
@@ -320,4 +324,3 @@ func moveDotGit(from, to string) error {
}
return nil
}
+6 -12
View File
@@ -27,13 +27,12 @@ import (
type Repo struct {
*git.Repository
// AlternativeConfig is used when the --repo-config option is set.
// config is used when the --repo-config option is set.
// This allows users to load up configs specific to their repos.
// Imagine the scenario where you are doing an audit of a large organization
// and you want certain repos to look for specific rules. If those specific repos
// have a gitleaks.toml or .gitleaks.toml config then those configs will be used specifically
// for those repo audits.
AlternativeConfig config.Config
config config.Config
Name string
@@ -44,21 +43,18 @@ type Repo struct {
func NewRepo(m *manager.Manager) *Repo {
return &Repo{
Manager: m,
config: m.Config,
config: m.Config,
}
}
// Clone will clone a repo and return a Repo struct which contains a go-git repo. The clone method
// is determined by the clone options set in Manager.metadata.cloneOptions
func (repo *Repo) Clone(cloneOptions ...*git.CloneOptions) error {
func (repo *Repo) Clone(cloneOption *git.CloneOptions) error {
var (
repository *git.Repository
err error
cloneOption *git.CloneOptions
repository *git.Repository
err error
)
if len(cloneOptions) != 0 {
cloneOption = cloneOptions[0]
} else {
if cloneOption == nil {
cloneOption = repo.Manager.CloneOptions
}
@@ -228,7 +224,6 @@ func (repo *Repo) Audit() error {
return nil
}
// TODO check whitelist Commit
if isCommitWhiteListed(c.Hash.String(), repo.config.Whitelist.Commits) {
return nil
}
@@ -304,4 +299,3 @@ func (repo *Repo) loadRepoConfig() (config.Config, error) {
_, err = toml.DecodeReader(f, &tomlLoader)
return tomlLoader.Parse()
}
+36 -2
View File
@@ -99,6 +99,16 @@ func trippedEntropy(line string, rule config.Rule) bool {
return false
}
func ruleContainRegex(rule config.Rule) bool {
if rule.Regex == nil {
return false
}
if rule.Regex.String() == "" {
return false
}
return true
}
// InspectString accepts a string, commit object, repo, and filename. This function iterates over
// all the rules set by the gitleaks config. If the rule contains entropy checks then entropy will be checked first.
// Next, if the rule contains a regular expression then that will be checked.
@@ -106,11 +116,11 @@ func InspectString(content string, c *object.Commit, repo *Repo, filename string
for _, rule := range repo.config.Rules {
// check entropy
if len(rule.Entropy) != 0 {
// TODO
// an optimization would be to switch the regex from FindAllIndex to FindString
// since we are iterating on the lines if entropy rules exist...
for _, line := range strings.Split(content, "\n") {
if trippedEntropy(line, rule) {
entropyTripped := trippedEntropy(line, rule)
if entropyTripped && !ruleContainRegex(rule) {
_line := line
if len(_line) > maxLineLen {
_line = line[0 : maxLineLen-1]
@@ -128,8 +138,32 @@ func InspectString(content string, c *object.Commit, repo *Repo, filename string
Tags: strings.Join(rule.Tags, ", "),
File: filename,
})
} else if entropyTripped {
// entropy has been tripped which means if there is a regex specified in the same
// rule, we need to inspect the line for a regex match. In otherwords, the current rule has
// both entropy and regex set which work in combination. This helps narrow down false positives
// on searches for generic passwords in code.
match := rule.Regex.FindString(line)
if match != "" {
// both the regex and entropy in this rule have been tripped which means this line
// contains a leak
repo.Manager.SendLeaks(manager.Leak{
Line: line,
Offender: match,
Commit: c.Hash.String(),
Message: c.Message,
Repo: repo.Name,
Rule: rule.Description,
Author: c.Author.Name,
Email: c.Author.Email,
Date: c.Author.When,
Tags: strings.Join(rule.Tags, ", "),
File: filename,
})
}
}
}
return
}
if rule.Regex.String() == "" {
continue