From 432c76a9d980f6db6d803dad306f89dee593752f Mon Sep 17 00:00:00 2001 From: Adam Kobi Date: Sat, 29 Dec 2018 13:10:39 +0200 Subject: [PATCH 1/2] added support for scanning only single remote branch --- github.go | 4 ++++ main.go | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/github.go b/github.go index b3b7c5e3..84edb582 100644 --- a/github.go +++ b/github.go @@ -159,6 +159,10 @@ func auditGithubRepos() ([]Leak, error) { continue } leaksFromRepo, err := auditGitRepo(repo) + if err != nil { + log.Warn(err) + continue + } if opts.Disk { os.RemoveAll(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name)) } diff --git a/main.go b/main.go index 0a834b75..47d56761 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,8 @@ import ( "time" "gopkg.in/src-d/go-git.v4/plumbing" - "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/transport/ssh" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -69,8 +69,9 @@ type Options struct { GitLabUser string `long:"gitlab-user" description:"GitLab user ID to audit"` GitLabOrg string `long:"gitlab-org" description:"GitLab group ID to audit"` - Commit string `short:"c" long:"commit" description:"sha of commit to stop at"` - Depth int `long:"depth" description:"maximum commit depth"` + Commit string `short:"c" long:"commit" description:"sha of commit to stop at"` + Depth int `long:"depth" description:"maximum commit depth"` + Branch plumbing.ReferenceName `long:"branch" description:"scan remote branch only (default is all)"` // local target option RepoPath string `long:"repo-path" description:"Path to repo"` @@ -429,6 +430,7 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { var ( err error leaks []Leak + refs storer.ReferenceIter ) for _, re := range whiteListRepos { if re.FindString(repo.name) != "" { @@ -439,7 +441,21 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { // clear commit cache commitMap = make(map[string]bool) - refs, err := repo.repository.Storer.IterReferences() + if opts.Branch != "" { + // var branchRef plumbing.ReferenceName + branchRef := "refs/remotes/origin/" + opts.Branch + log.Debugf("Auditing ref: %v", branchRef) + ref, err := repo.repository.Storer.Reference(branchRef) + if err != nil { + return leaks, err + } + branchLeaks := auditGitReference(repo, ref) + for _, leak := range branchLeaks { + leaks = append(leaks, leak) + } + return leaks, nil + } + refs, err = repo.repository.Storer.IterReferences() if err != nil { return leaks, err } From 6b3bd46fa89d1b6bf46bb41b561887c7841127d3 Mon Sep 17 00:00:00 2001 From: Adam Kobi Date: Sun, 13 Jan 2019 13:55:01 +0200 Subject: [PATCH 2/2] added tests for scanning single branch and non-existing branch --- gitleaks_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gitleaks_test.go b/gitleaks_test.go index d9088dea..2de13b2b 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -320,6 +320,23 @@ func TestRun(t *testing.T) { expectedErrMsg: "", commitPerPage: 1, }, + { + testOpts: Options{ + Repo: "https://github.com/gitleakstest/gronit.git", + Branch: "master", + }, + description: "test github leaks on single branch - master", + numLeaks: 2, + }, + { + testOpts: Options{ + Repo: "https://github.com/gitleakstest/gronit.git", + Branch: "nonExistingBranch", + }, + description: "test github leaks on single branch which doesn't exist", + numLeaks: 0, + expectedErrMsg: "reference not found", + }, } g := goblin.Goblin(t) for _, test := range tests {