Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Eduardo Argollo committed 2019-06-06 11:13:51 -07:00
commit 5309811a09
4 files changed
+52 -29

No files matched your search

+6 -1
View File
@@ -2,14 +2,19 @@ package main
import ( import (
"os" "os"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/src" gitleaks "github.com/zricethezav/gitleaks/src"
) )
func main() { func main() {
report, err := gitleaks.Run(gitleaks.ParseOpts()) report, err := gitleaks.Run(gitleaks.ParseOpts())
if err != nil { if err != nil {
if strings.Contains(err.Error(), "whitelisted") {
log.Info(err.Error())
os.Exit(0)
}
log.Error(err) log.Error(err)
os.Exit(gitleaks.ErrExit) os.Exit(gitleaks.ErrExit)
} }
+5 -9
View File
@@ -3,7 +3,6 @@ package gitleaks
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
@@ -60,7 +59,8 @@ func Run(optsL *Options) (*Report, error) {
// start audits // start audits
if opts.Repo != "" || opts.RepoPath != "" { if opts.Repo != "" || opts.RepoPath != "" {
repoInfo, err := newRepoInfo() var repoInfo *RepoInfo
repoInfo, err = newRepoInfo()
if err != nil { if err != nil {
goto postAudit goto postAudit
} }
@@ -70,7 +70,8 @@ func Run(optsL *Options) (*Report, error) {
} }
leaks, err = repoInfo.audit() leaks, err = repoInfo.audit()
} else if opts.OwnerPath != "" { } else if opts.OwnerPath != "" {
repoDs, err := discoverRepos(opts.OwnerPath) var repoDs []*RepoInfo
repoDs, err = discoverRepos(opts.OwnerPath)
if err != nil { if err != nil {
goto postAudit goto postAudit
} }
@@ -96,12 +97,7 @@ func Run(optsL *Options) (*Report, error) {
postAudit: postAudit:
if err != nil { if err != nil {
if strings.Contains(err.Error(), "whitelisted") { return &Report{}, err
log.Info(err.Error())
os.Exit(0)
}
log.Error(err)
os.Exit(ErrExit)
} }
if opts.Report != "" { if opts.Report != "" {
+5 -5
View File
@@ -64,22 +64,22 @@ func TestGetRepo(t *testing.T) {
Repo: "https://github.com/gitleakstest/nope", Repo: "https://github.com/gitleakstest/nope",
}, },
description: "test no repo", description: "test no repo",
expectedErrMsg: "authentication required", expectedErrMsg: "repository not found",
}, },
{ {
testOpts: &Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
}, },
description: "test private repo", description: "test private repo",
expectedErrMsg: "authentication required", expectedErrMsg: "repository not found",
}, },
{ {
testOpts: &Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
Disk: true, Disk: true,
}, },
description: "test private repo", description: "test private repo disk",
expectedErrMsg: "authentication required", expectedErrMsg: "repository not found",
}, },
} }
g := goblin.Goblin(t) g := goblin.Goblin(t)
@@ -207,7 +207,7 @@ func TestRun(t *testing.T) {
}, },
description: "test empty", description: "test empty",
numLeaks: 0, numLeaks: 0,
expectedErrMsg: "reference not found", expectedErrMsg: "repository not found",
}, },
{ {
testOpts: &Options{ testOpts: &Options{
+36 -14
View File
@@ -15,6 +15,7 @@ import (
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"
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/storage/memory" "gopkg.in/src-d/go-git.v4/storage/memory"
) )
@@ -77,10 +78,17 @@ func (repoInfo *RepoInfo) clone() error {
}) })
} else { } else {
// public // public
repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ options := &git.CloneOptions{
URL: opts.Repo, URL: opts.Repo,
Progress: os.Stdout, Progress: os.Stdout,
}) }
if os.Getenv("GITHUB_TOKEN") != "" {
options.Auth = &gitHttp.BasicAuth{
Username: "fakeUsername", // yes, this can be anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
}
}
repo, err = git.PlainClone(cloneTarget, false, options)
} }
} else if repoInfo.path != "" { } else if repoInfo.path != "" {
log.Infof("opening %s", repoInfo.path) log.Infof("opening %s", repoInfo.path)
@@ -98,10 +106,17 @@ func (repoInfo *RepoInfo) clone() error {
Auth: config.sshAuth, Auth: config.sshAuth,
}) })
} else { } else {
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ options := &git.CloneOptions{
URL: opts.Repo, URL: opts.Repo,
Progress: os.Stdout, Progress: os.Stdout,
}) }
if os.Getenv("GITHUB_TOKEN") != "" {
options.Auth = &gitHttp.BasicAuth{
Username: "fakeUsername", // yes, this can be anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
}
}
repo, err = git.Clone(memory.NewStorage(), nil, options)
} }
} }
repoInfo.repository = repo repoInfo.repository = repo
@@ -133,7 +148,21 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
} }
} }
if opts.Branch != "" { if opts.Commit != "" {
h := plumbing.NewHash(opts.Commit)
c, err := repoInfo.repository.CommitObject(h)
if err != nil {
return leaks, nil
}
commitCount = commitCount + 1
totalCommits = totalCommits + 1
leaksFromSingleCommit := repoInfo.auditSingleCommit(c)
mutex.Lock()
leaks = append(leaksFromSingleCommit, leaks...)
mutex.Unlock()
return leaks, err
} else if opts.Branch != "" {
refs, err := repoInfo.repository.Storer.IterReferences() refs, err := repoInfo.repository.Storer.IterReferences()
if err != nil { if err != nil {
return leaks, err return leaks, err
@@ -187,21 +216,14 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
return nil return nil
} }
// 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)
if (len(c.ParentHashes) == 0 && opts.Commit == "") || (len(c.ParentHashes) == 0 && opts.Commit == c.Hash.String()) { if len(c.ParentHashes) == 0 {
commitCount = commitCount + 1 commitCount = commitCount + 1
totalCommits = totalCommits + 1 totalCommits = totalCommits + 1
leaksFromSingleCommit := repoInfo.auditSingleCommit(c) leaksFromSingleCommit := repoInfo.auditSingleCommit(c)
mutex.Lock() mutex.Lock()
leaks = append(leaksFromSingleCommit, leaks...) leaks = append(leaksFromSingleCommit, leaks...)
mutex.Unlock() mutex.Unlock()
if opts.Commit == c.Hash.String() {
return storer.ErrStop
}
return nil
}
if opts.Commit != "" && opts.Commit != c.Hash.String() {
return nil return nil
} }