more refactoring

This commit is contained in:
zach rice committed 2019-03-28 10:42:43 -05:00
1 parent c2143c5843
commit ac14d347de
6 files changed
+153 -194

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
package gitleaks package gitleaks
const version = "1.24.0" const version = "1.25.0"
const defaultGithubURL = "https://api.github.com/" const defaultGithubURL = "https://api.github.com/"
const defaultThreadNum = 1 const defaultThreadNum = 1
+7 -34
View File
@@ -8,37 +8,10 @@ import (
"sync" "sync"
"time" "time"
"github.com/google/go-github/github"
"github.com/hako/durafmt" "github.com/hako/durafmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/src-d/go-git.v4/plumbing/object"
) )
// Leak represents a leaked secret or regex match.
type Leak struct {
Line string `json:"line"`
Commit string `json:"commit"`
Offender string `json:"offender"`
Type string `json:"reason"`
Message string `json:"commitMsg"`
Author string `json:"author"`
File string `json:"file"`
Repo string `json:"repo"`
Date time.Time `json:"date"`
}
type gitDiff struct {
content string
commit *object.Commit
filePath string
repoName string
githubCommit *github.RepositoryCommit
sha string
message string
author string
date time.Time
}
var ( var (
opts *Options opts *Options
config *Config config *Config
@@ -81,29 +54,29 @@ func Run(optsL *Options) {
// start audits // start audits
if opts.Repo != "" || opts.RepoPath != "" { if opts.Repo != "" || opts.RepoPath != "" {
repoD, err := newRepoInfo() repoInfo, err := newRepoInfo()
if err != nil { if err != nil {
goto postAudit goto postAudit
} }
err = repoD.clone() err = repoInfo.clone()
if err != nil { if err != nil {
goto postAudit goto postAudit
} }
leaks, err = repoD.audit() leaks, err = repoInfo.audit()
} else if opts.OwnerPath != "" { } else if opts.OwnerPath != "" {
repoDs, err := discoverRepos(opts.OwnerPath) repoDs, err := discoverRepos(opts.OwnerPath)
if err != nil { if err != nil {
goto postAudit goto postAudit
} }
for _, repoD := range repoDs { for _, repoInfo := range repoDs {
err = repoD.clone() err = repoInfo.clone()
if err != nil { if err != nil {
continue continue
} }
leaksFromRepo, err := repoD.audit() leaksFromRepo, err := repoInfo.audit()
if err != nil { if err != nil {
log.Warnf("error occured auditing repo: %s, continuing", repoD.name) log.Warnf("error occured auditing repo: %s, continuing", repoInfo.name)
} }
leaks = append(leaksFromRepo, leaks...) leaks = append(leaksFromRepo, leaks...)
} }
+10 -10
View File
@@ -43,13 +43,13 @@ func auditGithubPR() ([]Leak, error) {
return leaks, err return leaks, err
} }
for _, commit := range commits { for _, c := range commits {
totalCommits = totalCommits + 1 totalCommits = totalCommits + 1
commit, _, err := githubClient.Repositories.GetCommit(ctx, owner, repo, *commit.SHA) c, _, err := githubClient.Repositories.GetCommit(ctx, owner, repo, *c.SHA)
if err != nil { if err != nil {
continue continue
} }
files := commit.Files files := c.Files
for _, f := range files { for _, f := range files {
skipFile := false skipFile := false
if f.Patch == nil || f.Filename == nil { if f.Patch == nil || f.Filename == nil {
@@ -66,17 +66,17 @@ func auditGithubPR() ([]Leak, error) {
continue continue
} }
diff := gitDiff{ commit := commitInfo{
sha: commit.GetSHA(), sha: c.GetSHA(),
content: *f.Patch, content: *f.Patch,
filePath: *f.Filename, filePath: *f.Filename,
repoName: repo, repoName: repo,
githubCommit: commit, githubCommit: c,
author: commit.GetCommitter().GetLogin(), author: c.GetCommitter().GetLogin(),
message: *commit.Commit.Message, message: *c.Commit.Message,
date: *commit.Commit.Committer.Date, date: *c.Commit.Committer.Date,
} }
leaks = append(leaks, inspect(diff)...) leaks = append(leaks, inspect(commit)...)
} }
} }
page = resp.NextPage page = resp.NextPage
+2 -2
View File
@@ -78,13 +78,13 @@ func auditGitlabRepos() ([]Leak, error) {
} }
for _, p := range repos { for _, p := range repos {
repoD, err := cloneGitlabRepo(tempDir, p) repoInfo, err := cloneGitlabRepo(tempDir, p)
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)
continue continue
} }
leaksFromRepo, err := repoD.audit() leaksFromRepo, err := repoInfo.audit()
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)
} }
+122 -135
View File
@@ -7,16 +7,30 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
git "gopkg.in/src-d/go-git.v4" git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
diffType "gopkg.in/src-d/go-git.v4/plumbing/format/diff" diffType "gopkg.in/src-d/go-git.v4/plumbing/format/diff"
"gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/memory" "gopkg.in/src-d/go-git.v4/storage/memory"
) )
// Leak represents a leaked secret or regex match.
type Leak struct {
Line string `json:"line"`
Commit string `json:"commit"`
Offender string `json:"offender"`
Type string `json:"reason"`
Message string `json:"commitMsg"`
Author string `json:"author"`
File string `json:"file"`
Repo string `json:"repo"`
Date time.Time `json:"date"`
}
// RepoInfo contains a src-d git repository and other data about the repo // RepoInfo contains a src-d git repository and other data about the repo
type RepoInfo struct { type RepoInfo struct {
path string path string
@@ -26,6 +40,18 @@ type RepoInfo struct {
err error err error
} }
type commitInfo struct {
content string
commit *object.Commit
filePath string
repoName string
githubCommit *github.RepositoryCommit
sha string
message string
author string
date time.Time
}
func newRepoInfo() (*RepoInfo, error) { func newRepoInfo() (*RepoInfo, error) {
for _, re := range config.WhiteList.repos { for _, re := range config.WhiteList.repos {
if re.FindString(opts.Repo) != "" { if re.FindString(opts.Repo) != "" {
@@ -39,7 +65,8 @@ func newRepoInfo() (*RepoInfo, error) {
}, nil }, nil
} }
func (repoD *RepoInfo) clone() error { // clone will clone a repo
func (repoInfo *RepoInfo) clone() error {
var ( var (
err error err error
repo *git.Repository repo *git.Repository
@@ -63,9 +90,9 @@ func (repoD *RepoInfo) clone() error {
Progress: os.Stdout, Progress: os.Stdout,
}) })
} }
} else if repoD.path != "" { } else if repoInfo.path != "" {
log.Infof("opening %s", opts.RepoPath) log.Infof("opening %s", opts.RepoPath)
repo, err = git.PlainOpen(repoD.path) repo, err = git.PlainOpen(repoInfo.path)
} else { } else {
// cloning to memory // cloning to memory
log.Infof("cloning %s", opts.Repo) log.Infof("cloning %s", opts.Repo)
@@ -82,66 +109,43 @@ func (repoD *RepoInfo) clone() error {
}) })
} }
} }
repoD.repository = repo repoInfo.repository = repo
repoD.err = err repoInfo.err = err
return err return err
} }
func (repoD *RepoInfo) audit() ([]Leak, error) { // audit performs an audit
var ( func (repoInfo *RepoInfo) audit() ([]Leak, error) {
err error
leaks []Leak
)
for _, re := range config.WhiteList.repos {
if re.FindString(repoD.name) != "" {
return leaks, fmt.Errorf("skipping %s, whitelisted", repoD.name)
}
}
// check if target contains an external gitleaks toml
if opts.RepoConfig {
err := config.updateFromRepo(repoD)
if err != nil {
return leaks, nil
}
}
// clear commit cache
commitMap = make(map[string]bool)
refs, err := repoD.repository.Storer.IterReferences()
if err != nil {
return leaks, err
}
err = refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().IsTag() {
return nil
}
branchLeaks := repoD.auditRef(ref)
for _, leak := range branchLeaks {
leaks = append(leaks, leak)
}
return nil
})
return leaks, err
}
// auditGitReference beings the audit for a git reference. This function will
// traverse the git reference and audit each line of each diff.
func (repoD *RepoInfo) auditRef(ref *plumbing.Reference) []Leak {
var ( var (
err error err error
repoName string
leaks []Leak leaks []Leak
commitCount int64 commitCount int64
commitWg sync.WaitGroup commitWg sync.WaitGroup
mutex = &sync.Mutex{} mutex = &sync.Mutex{}
semaphore chan bool semaphore chan bool
) )
if auditDone { for _, re := range config.WhiteList.repos {
return nil if re.FindString(repoInfo.name) != "" {
return leaks, fmt.Errorf("skipping %s, whitelisted", repoInfo.name)
}
} }
repoName = repoD.name
// check if target contains an external gitleaks toml
if opts.RepoConfig {
err := config.updateFromRepo(repoInfo)
if err != nil {
return leaks, nil
}
}
// iterate all through commits
cIter, err := repoInfo.repository.Log(&git.LogOptions{
All: true,
})
if err != nil {
return leaks, nil
}
if opts.Threads != 0 { if opts.Threads != 0 {
threads = opts.Threads threads = opts.Threads
} }
@@ -150,95 +154,27 @@ func (repoD *RepoInfo) auditRef(ref *plumbing.Reference) []Leak {
} }
semaphore = make(chan bool, threads) semaphore = make(chan bool, threads)
cIter, err := repoD.repository.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil
}
err = cIter.ForEach(func(c *object.Commit) error { err = cIter.ForEach(func(c *object.Commit) error {
if c == nil || (opts.Depth != 0 && commitCount == opts.Depth) || auditDone { if c == nil || (opts.Depth != 0 && commitCount == opts.Depth) {
if commitCount == opts.Depth {
auditDone = true
}
return storer.ErrStop return storer.ErrStop
} }
commitCount = commitCount + 1
if config.WhiteList.commits[c.Hash.String()] { if config.WhiteList.commits[c.Hash.String()] {
log.Infof("skipping commit: %s\n", c.Hash.String()) log.Infof("skipping commit: %s\n", c.Hash.String())
return nil return nil
} }
commitCount = commitCount + 1
totalCommits = totalCommits + 1
// commits w/o parent (root of git the git ref) or option for single commit is not empty str // commits w/o parent (root of git the git ref) or option for single commit is not empty str
if len(c.ParentHashes) == 0 || opts.Commit == c.Hash.String() { if len(c.ParentHashes) == 0 || opts.Commit == c.Hash.String() {
if commitMap[c.Hash.String()] { leaks = append(repoInfo.auditSingleCommit(c, mutex), leaks...)
return nil
}
if opts.Commit == c.Hash.String() {
auditDone = true
}
cMutex.Lock()
commitMap[c.Hash.String()] = true
cMutex.Unlock()
totalCommits = totalCommits + 1
fIter, err := c.Files()
if err != nil {
return nil
}
err = fIter.ForEach(func(f *object.File) error {
bin, err := f.IsBinary()
if bin || err != nil {
return nil
}
for _, re := range config.WhiteList.files {
if re.FindString(f.Name) != "" {
log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name)
return nil
}
}
content, err := f.Contents()
if err != nil {
return nil
}
diff := gitDiff{
repoName: repoName,
filePath: f.Name,
content: content,
sha: c.Hash.String(),
author: c.Author.String(),
message: strings.Replace(c.Message, "\n", " ", -1),
date: c.Author.When,
}
fileLeaks := inspect(diff)
mutex.Lock()
leaks = append(leaks, fileLeaks...)
mutex.Unlock()
return nil
})
return nil return nil
} }
// single commit // regular commit audit
if opts.Commit != "" {
return nil
}
skipCount := false
err = c.Parents().ForEach(func(parent *object.Commit) error { err = c.Parents().ForEach(func(parent *object.Commit) error {
// check if we've seen this diff before
if commitMap[c.Hash.String()+parent.Hash.String()] {
return nil
}
cMutex.Lock()
commitMap[c.Hash.String()+parent.Hash.String()] = true
cMutex.Unlock()
if !skipCount {
totalCommits = totalCommits + 1
skipCount = true
}
commitWg.Add(1) commitWg.Add(1)
semaphore <- true semaphore <- true
go func(c *object.Commit, parent *object.Commit) { go func(c *object.Commit, parent *object.Commit) {
@@ -283,8 +219,8 @@ func (repoD *RepoInfo) auditRef(ref *plumbing.Reference) []Leak {
chunks := f.Chunks() chunks := f.Chunks()
for _, chunk := range chunks { for _, chunk := range chunks {
if chunk.Type() == diffType.Add || chunk.Type() == diffType.Delete { if chunk.Type() == diffType.Add || chunk.Type() == diffType.Delete {
diff := gitDiff{ diff := commitInfo{
repoName: repoName, repoName: repoInfo.name,
filePath: filePath, filePath: filePath,
content: chunk.Content(), content: chunk.Content(),
sha: c.Hash.String(), sha: c.Hash.String(),
@@ -306,14 +242,65 @@ func (repoD *RepoInfo) auditRef(ref *plumbing.Reference) []Leak {
return nil return nil
}) })
// stop audit if we are at commitStop
if c.Hash.String() == opts.CommitStop {
auditDone = true
return storer.ErrStop
}
return nil return nil
}) })
commitWg.Wait() commitWg.Wait()
return leaks, nil
// // clear commit cache
// commitMap = make(map[string]bool)
// refs, err := repoInfo.repository.Storer.IterReferences()
// if err != nil {
// return leaks, err
// }
// err = refs.ForEach(func(ref *plumbing.Reference) error {
// if ref.Name().IsTag() {
// return nil
// }
// branchLeaks := repoInfo.auditRef(ref)
// for _, leak := range branchLeaks {
// leaks = append(leaks, leak)
// }
// return nil
// })
}
func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit, mutex *sync.Mutex) []Leak {
var leaks []Leak
fIter, err := c.Files()
if err != nil {
return nil
}
err = fIter.ForEach(func(f *object.File) error {
bin, err := f.IsBinary()
if bin || err != nil {
return nil
}
for _, re := range config.WhiteList.files {
if re.FindString(f.Name) != "" {
log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name)
return nil
}
}
content, err := f.Contents()
if err != nil {
return nil
}
diff := commitInfo{
repoName: repoInfo.name,
filePath: f.Name,
content: content,
sha: c.Hash.String(),
author: c.Author.String(),
message: strings.Replace(c.Message, "\n", " ", -1),
date: c.Author.When,
}
fileLeaks := inspect(diff)
mutex.Lock()
leaks = append(leaks, fileLeaks...)
mutex.Unlock()
return nil
})
return leaks return leaks
} }
+11 -12
View File
@@ -73,13 +73,13 @@ func writeReport(leaks []Leak) error {
// a set of regexes set by the config (see gitleaks.toml for example). This function // a set of regexes set by the config (see gitleaks.toml for example). This function
// will skip lines that include a whitelisted regex. A list of leaks is returned. // will skip lines that include a whitelisted regex. A list of leaks is returned.
// If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered. // If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
func inspect(diff gitDiff) []Leak { func inspect(commit commitInfo) []Leak {
var ( var (
leaks []Leak leaks []Leak
skipLine bool skipLine bool
) )
lines := strings.Split(diff.content, "\n") lines := strings.Split(commit.content, "\n")
for _, line := range lines { for _, line := range lines {
skipLine = false skipLine = false
@@ -91,7 +91,7 @@ func inspect(diff gitDiff) []Leak {
if skipLine = isLineWhitelisted(line); skipLine { if skipLine = isLineWhitelisted(line); skipLine {
break break
} }
leaks = addLeak(leaks, line, match, re.description, diff) leaks = addLeak(leaks, line, match, re.description, commit)
} }
if !skipLine && (opts.Entropy > 0 || len(config.Entropy.entropyRanges) != 0) { if !skipLine && (opts.Entropy > 0 || len(config.Entropy.entropyRanges) != 0) {
@@ -108,7 +108,7 @@ func inspect(diff gitDiff) []Leak {
if skipLine = !highEntropyLineIsALeak(line) || isLineWhitelisted(line); skipLine { if skipLine = !highEntropyLineIsALeak(line) || isLineWhitelisted(line); skipLine {
break break
} }
leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff) leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), commit)
} }
} }
} }
@@ -127,17 +127,17 @@ func isLineWhitelisted(line string) bool {
} }
// addLeak is helper for func inspect() to append leaks if found during a diff check. // addLeak is helper for func inspect() to append leaks if found during a diff check.
func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak { func addLeak(leaks []Leak, line string, offender string, leakType string, commit commitInfo) []Leak {
leak := Leak{ leak := Leak{
Line: line, Line: line,
Commit: diff.sha, Commit: commit.sha,
Offender: offender, Offender: offender,
Type: leakType, Type: leakType,
Author: diff.author, Author: commit.author,
File: diff.filePath, File: commit.filePath,
Repo: diff.repoName, Repo: commit.repoName,
Message: diff.message, Message: commit.message,
Date: diff.date, Date: commit.date,
} }
if opts.Redact { if opts.Redact {
leak.Offender = "REDACTED" leak.Offender = "REDACTED"
@@ -182,7 +182,6 @@ func (leak Leak) log() {
func containsGit(repoPath string) bool { func containsGit(repoPath string) bool {
if _, err := os.Stat(repoPath); os.IsNotExist(err) { if _, err := os.Stat(repoPath); os.IsNotExist(err) {
// path/to/whatever does not exist
return false return false
} }
return true return true