From 16765c19cfad89901e7d59eb8c635c399eefdaa7 Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 09:38:36 -0500 Subject: [PATCH 1/7] adding go routines does not help performance much.... posting benchmarks soon --- main.go | 143 ++++++++++++++++++-------------------------------------- 1 file changed, 45 insertions(+), 98 deletions(-) diff --git a/main.go b/main.go index b166a87b..f0b02ec4 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ import ( "runtime" "strconv" "strings" - "sync" "time" "gopkg.in/src-d/go-git.v4/plumbing" @@ -478,12 +477,8 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { var ( err error - prevCommit *object.Commit - semaphore chan bool repoName string leaks []Leak - commitWg sync.WaitGroup - mutex = &sync.Mutex{} commitCount int ) repoName = repo.name @@ -491,126 +486,78 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { maxGo = opts.MaxGoRoutines } - semaphore = make(chan bool, maxGo) cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()}) if err != nil { return nil } err = cIter.ForEach(func(c *object.Commit) error { - if c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) { + defer func() { + if r := recover(); r != nil { + log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String()) + } + }() + if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) { cIter.Close() return errors.New("ErrStop") } commitCount = commitCount + 1 totalCommits = totalCommits + 1 if whiteListCommits[c.Hash.String()] { - prevCommit = c log.Infof("skipping commit: %s\n", c.Hash.String()) return nil } - if prevCommit != nil { - if whiteListCommits[prevCommit.Hash.String()] { - prevCommit = c - log.Infof("skipping commit: %s\n", c.Hash.String()) + + var ( + filePath string + skipFile bool + ) + err = c.Parents().ForEach(func(parent *object.Commit) error { + patch, err := c.Patch(parent) + if err != nil { + log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) return nil } - } - - commitWg.Add(1) - semaphore <- true - go func(c *object.Commit, prevCommit *object.Commit) { - var ( - filePath string - skipFile bool - ) - defer func() { - commitWg.Done() - <-semaphore - if r := recover(); r != nil { - log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String()) + for _, f := range patch.FilePatches() { + skipFile = false + from, to := f.Files() + filePath = "???" + if from != nil { + filePath = from.Path() + } else if to != nil { + filePath = to.Path() } - }() - - if prevCommit == nil { - t, _ := c.Tree() - files := t.Files() - err := files.ForEach(func(file *object.File) error { - content, err := file.Contents() - if err != nil { - return err + for _, re := range whiteListFiles { + if re.FindString(filePath) != "" { + skipFile = true + break } - diff := gitDiff{ - branchName: string(ref.Name()), - repoName: repoName, - filePath: file.Name, - content: content, - sha: c.Hash.String(), - author: c.Author.String(), - message: c.Message, - } - chunkLeaks := inspect(diff) - for _, leak := range chunkLeaks { - mutex.Lock() - leaks = append(leaks, leak) - mutex.Unlock() - } - return nil - }) - if err != nil { - log.Warnf("problem generating diff for commit: %s\n", c.Hash.String()) - return } - } else { - patch, err := c.Patch(prevCommit) - if err != nil { - log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) - return + if skipFile { + continue } - for _, f := range patch.FilePatches() { - skipFile = false - from, to := f.Files() - filePath = "???" - if from != nil { - filePath = from.Path() - } else if to != nil { - filePath = to.Path() - } - for _, re := range whiteListFiles { - if re.FindString(filePath) != "" { - skipFile = true - break + chunks := f.Chunks() + for _, chunk := range chunks { + if chunk.Type() == 1 || chunk.Type() == 2 { + diff := gitDiff{ + branchName: string(ref.Name()), + repoName: repoName, + filePath: filePath, + content: chunk.Content(), + sha: c.Hash.String(), + author: c.Author.String(), + message: c.Message, } - } - if skipFile { - continue - } - chunks := f.Chunks() - for _, chunk := range chunks { - if chunk.Type() == 1 || chunk.Type() == 2 { - diff := gitDiff{ - branchName: string(ref.Name()), - repoName: repoName, - filePath: filePath, - content: chunk.Content(), - sha: prevCommit.Hash.String(), - author: prevCommit.Author.String(), - message: prevCommit.Message, - } - chunkLeaks := inspect(diff) - for _, leak := range chunkLeaks { - mutex.Lock() - leaks = append(leaks, leak) - mutex.Unlock() - } + chunkLeaks := inspect(diff) + for _, leak := range chunkLeaks { + leaks = append(leaks, leak) } } } } - }(c, prevCommit) - prevCommit = c + return nil + }) return nil }) - commitWg.Wait() return leaks } From 64e7afea98894f00bb67f1bbadea47f7b3100045 Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 09:58:39 -0500 Subject: [PATCH 2/7] updating version --- main.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index f0b02ec4..ae123c38 100644 --- a/main.go +++ b/main.go @@ -81,14 +81,13 @@ type Options struct { OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"` // Process options - MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"` - Disk bool `long:"disk" description:"Clones repo(s) to disk"` - AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` - SingleSearch string `long:"single-search" description:"single regular expression to search for"` - ConfigPath string `long:"config" description:"path to gitleaks config"` - SSHKey string `long:"ssh-key" description:"path to ssh key"` - ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"` - Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"` + Disk bool `long:"disk" description:"Clones repo(s) to disk"` + AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` + SingleSearch string `long:"single-search" description:"single regular expression to search for"` + ConfigPath string `long:"config" description:"path to gitleaks config"` + SSHKey string `long:"ssh-key" description:"path to ssh key"` + ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"` + Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"` // TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"` // Output options @@ -136,7 +135,7 @@ type entropyRange struct { } const defaultGithubURL = "https://api.github.com/" -const version = "1.15.0" +const version = "1.16.0" const errExit = 2 const leakExit = 1 const defaultConfig = ` @@ -472,8 +471,7 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { } // auditGitReference beings the audit for a git reference. This function will -// traverse the git reference and audit each line of each diff. Set maximum concurrency with -// the --max-go option (default is set to the number of cores on your cpu). +// traverse the git reference and audit each line of each diff. func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { var ( err error @@ -482,9 +480,6 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { commitCount int ) repoName = repo.name - if opts.MaxGoRoutines != 0 { - maxGo = opts.MaxGoRoutines - } cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()}) if err != nil { From 98aeee9b1d84174ce07611e7e1e64a3649ca1e60 Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 12:28:54 -0500 Subject: [PATCH 3/7] fixing everything --- main.go | 127 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 53 deletions(-) diff --git a/main.go b/main.go index ae123c38..de88325f 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "runtime" "strconv" "strings" + "sync" "time" "gopkg.in/src-d/go-git.v4/plumbing" @@ -81,13 +82,14 @@ type Options struct { OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"` // Process options - Disk bool `long:"disk" description:"Clones repo(s) to disk"` - AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` - SingleSearch string `long:"single-search" description:"single regular expression to search for"` - ConfigPath string `long:"config" description:"path to gitleaks config"` - SSHKey string `long:"ssh-key" description:"path to ssh key"` - ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"` - Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"` + MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"` + Disk bool `long:"disk" description:"Clones repo(s) to disk"` + AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` + SingleSearch string `long:"single-search" description:"single regular expression to search for"` + ConfigPath string `long:"config" description:"path to gitleaks config"` + SSHKey string `long:"ssh-key" description:"path to ssh key"` + ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"` + Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"` // TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"` // Output options @@ -383,7 +385,7 @@ func cloneRepo() (*RepoDescriptor, error) { }) } } else if opts.RepoPath != "" { - log.Infof("opening %s", opts.Repo) + log.Infof("opening %s", opts.RepoPath) repo, err = git.PlainOpen(opts.RepoPath) } else { log.Infof("cloning %s", opts.Repo) @@ -478,19 +480,24 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { repoName string leaks []Leak commitCount int + commitWg sync.WaitGroup + mutex = &sync.Mutex{} + semaphore chan bool ) repoName = repo.name + if opts.MaxGoRoutines != 0 { + maxGo = opts.MaxGoRoutines + } + if opts.RepoPath != "" { + maxGo = 1 + } + semaphore = make(chan bool, maxGo) cIter, err := repo.repository.Log(&git.LogOptions{From: ref.Hash()}) if err != nil { return nil } err = cIter.ForEach(func(c *object.Commit) error { - defer func() { - if r := recover(); r != nil { - log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String()) - } - }() if c == nil || c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) { cIter.Close() return errors.New("ErrStop") @@ -502,57 +509,71 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { return nil } - var ( - filePath string - skipFile bool - ) err = c.Parents().ForEach(func(parent *object.Commit) error { - patch, err := c.Patch(parent) - if err != nil { - log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) - return nil - } - for _, f := range patch.FilePatches() { - skipFile = false - from, to := f.Files() - filePath = "???" - if from != nil { - filePath = from.Path() - } else if to != nil { - filePath = to.Path() - } - for _, re := range whiteListFiles { - if re.FindString(filePath) != "" { - skipFile = true - break + commitWg.Add(1) + semaphore <- true + go func(c *object.Commit, parent *object.Commit) { + var ( + filePath string + skipFile bool + ) + defer func() { + commitWg.Done() + <-semaphore + if r := recover(); r != nil { + log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String()) } + }() + patch, err := c.Patch(parent) + if err != nil { + log.Warnf("problem generating patch for commit: %s\n", c.Hash.String()) + return } - if skipFile { - continue - } - chunks := f.Chunks() - for _, chunk := range chunks { - if chunk.Type() == 1 || chunk.Type() == 2 { - diff := gitDiff{ - branchName: string(ref.Name()), - repoName: repoName, - filePath: filePath, - content: chunk.Content(), - sha: c.Hash.String(), - author: c.Author.String(), - message: c.Message, + for _, f := range patch.FilePatches() { + skipFile = false + from, to := f.Files() + filePath = "???" + if from != nil { + filePath = from.Path() + } else if to != nil { + filePath = to.Path() + } + for _, re := range whiteListFiles { + if re.FindString(filePath) != "" { + skipFile = true + break } - chunkLeaks := inspect(diff) - for _, leak := range chunkLeaks { - leaks = append(leaks, leak) + } + if skipFile { + continue + } + chunks := f.Chunks() + for _, chunk := range chunks { + if chunk.Type() == 1 || chunk.Type() == 2 { + diff := gitDiff{ + branchName: string(ref.Name()), + repoName: repoName, + filePath: filePath, + content: chunk.Content(), + sha: c.Hash.String(), + author: c.Author.String(), + message: c.Message, + } + chunkLeaks := inspect(diff) + for _, leak := range chunkLeaks { + mutex.Lock() + leaks = append(leaks, leak) + mutex.Unlock() + } } } } - } + }(c, parent) return nil }) return nil }) + commitWg.Wait() return leaks } From e288db29a8944fce57ef675a3d8b2a941b6addea Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 12:40:05 -0500 Subject: [PATCH 4/7] fixing tests --- gitleaks_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gitleaks_test.go b/gitleaks_test.go index 862ee9d8..88c308f4 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -634,28 +634,28 @@ func TestAuditRepo(t *testing.T) { testOpts: Options{ Entropy: 4.7, }, - numLeaks: 7, + numLeaks: 6, }, { repo: leaksRepo, description: "Audit until specific commit", - numLeaks: 1, + numLeaks: 2, testOpts: Options{ Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c", }, }, { repo: leaksRepo, - description: "commit depth = 1, no leaks", - numLeaks: 0, + description: "commit depth = 1, one leak", + numLeaks: 1, testOpts: Options{ Depth: 1, }, }, { repo: leaksRepo, - description: "commit depth = 2, one leak", - numLeaks: 1, + description: "commit depth = 2, two leaks", + numLeaks: 2, testOpts: Options{ Depth: 2, }, @@ -663,7 +663,7 @@ func TestAuditRepo(t *testing.T) { { repo: leaksRepo, description: "toml entropy range", - numLeaks: 422, + numLeaks: 283, configPath: path.Join(configsDir, "entropy"), }, { From 98e6ba93785990f4947d652a14cd0b3d7e7dbfed Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 15:47:18 -0500 Subject: [PATCH 5/7] better ssh handling --- github.go | 20 +++++---------- gitleaks_test.go | 67 ++++++++++++++++++++++-------------------------- main.go | 43 +++++++++++++++---------------- 3 files changed, 57 insertions(+), 73 deletions(-) diff --git a/github.go b/github.go index 1066d331..ee3494a9 100644 --- a/github.go +++ b/github.go @@ -117,11 +117,11 @@ func auditGithubRepos() ([]Leak, error) { break } if opts.GithubUser != "" { - if opts.IncludePrivate { - pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, "", githubOptions) - } else { - pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, opts.GithubUser, githubOptions) - } + // if opts.IncludePrivate { + // pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, "", githubOptions) + // } else { + pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, opts.GithubUser, githubOptions) + // } if err != nil { done = true } @@ -199,10 +199,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) { if err != nil { return nil, fmt.Errorf("unable to generater owner temp dir: %v", err) } - if opts.IncludePrivate { - if sshAuth == nil { - return nil, fmt.Errorf("no ssh auth available") - } + if sshAuth != nil { repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{ URL: *githubRepo.SSHURL, Auth: sshAuth, @@ -213,10 +210,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) { }) } } else { - if opts.IncludePrivate { - if sshAuth == nil { - return nil, fmt.Errorf("no ssh auth available") - } + if sshAuth != nil { repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ URL: *githubRepo.SSHURL, Auth: sshAuth, diff --git a/gitleaks_test.go b/gitleaks_test.go index 88c308f4..cac4f1cf 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -167,17 +167,15 @@ func TestGetRepo(t *testing.T) { }, { testOpts: Options{ - Repo: "https://github.com/gitleakstest/private", - IncludePrivate: true, + Repo: "https://github.com/gitleakstest/private", }, description: "test private repo", expectedErrMsg: "invalid auth method", }, { testOpts: Options{ - Repo: "https://github.com/gitleakstest/private", - IncludePrivate: true, - Disk: true, + Repo: "https://github.com/gitleakstest/private", + Disk: true, }, description: "test private repo", expectedErrMsg: "invalid auth method", @@ -264,13 +262,29 @@ func TestRun(t *testing.T) { }, { testOpts: Options{ - GithubOrg: "gitleakstestorg", - IncludePrivate: true, - SSHKey: "reallyreallyreallyreallywrongpath", + Repo: "git@github.com:gitleakstest/gronit.git", + SSHKey: "trash", }, - description: "test private org no ssh", + description: "test leak", numLeaks: 0, - expectedErrMsg: "unable to generate ssh key: open reallyreallyreallyreallywrongpath: no such file or directory", + expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory", + }, + { + testOpts: Options{ + Repo: "git@github.com:gitleakstest/gronit.git", + }, + description: "test leak", + numLeaks: 2, + expectedErrMsg: "", + }, + { + testOpts: Options{ + Repo: "git@github.com:gitleakstest/gronit.git", + Disk: true, + }, + description: "test leak", + numLeaks: 2, + expectedErrMsg: "", }, { testOpts: Options{ @@ -751,45 +765,24 @@ func TestOptionGuard(t *testing.T) { }, { testOpts: Options{ - IncludePrivate: true, - GithubOrg: "fakeOrg", - }, - description: "private org no githubtoken", - expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set", - githubToken: false, - }, - { - testOpts: Options{ - IncludePrivate: true, - GithubUser: "fakeUser", - }, - description: "private user no githubtoken", - expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set", - githubToken: false, - }, - { - testOpts: Options{ - IncludePrivate: true, - GithubUser: "fakeUser", - GithubOrg: "fakeOrg", + GithubUser: "fakeUser", + GithubOrg: "fakeOrg", }, description: "double owner", expectedErrMsg: "github user and organization set", }, { testOpts: Options{ - IncludePrivate: true, - GithubOrg: "fakeOrg", - OwnerPath: "/dev/null", + GithubOrg: "fakeOrg", + OwnerPath: "/dev/null", }, description: "local and remote target", expectedErrMsg: "github organization set and local owner path", }, { testOpts: Options{ - IncludePrivate: true, - GithubUser: "fakeUser", - OwnerPath: "/dev/null", + GithubUser: "fakeUser", + OwnerPath: "/dev/null", }, description: "local and remote target", expectedErrMsg: "github user set and local owner path", diff --git a/main.go b/main.go index de88325f..92ba028c 100644 --- a/main.go +++ b/main.go @@ -60,12 +60,11 @@ type RepoDescriptor struct { // Options for gitleaks type Options struct { // remote target options - Repo string `short:"r" long:"repo" description:"Repo url to audit"` - GithubUser string `long:"github-user" description:"Github user to audit"` - GithubOrg string `long:"github-org" description:"Github organization to audit"` - GithubURL string `long:"github-url" default:"https://api.github.com/" description:"GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/"` - GithubPR string `long:"github-pr" description:"Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set"` - IncludePrivate bool `short:"p" long:"private" description:"Include private repos in audit"` + Repo string `short:"r" long:"repo" description:"Repo url to audit"` + GithubUser string `long:"github-user" description:"Github user to audit"` + GithubOrg string `long:"github-org" description:"Github organization to audit"` + GithubURL string `long:"github-url" default:"https://api.github.com/" description:"GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/"` + GithubPR string `long:"github-pr" description:"Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set"` /* TODO: @@ -280,13 +279,11 @@ func run() ([]Leak, error) { if err != nil { return nil, err } - if opts.IncludePrivate { - // if including private repos use ssh as authentication - sshAuth, err = getSSHAuth() - if err != nil { - return nil, err - } + sshAuth, err = getSSHAuth() + if err != nil { + return leaks, err } + if opts.Disk { // temporary directory where all the gitleaks plain clones will reside dir, err = ioutil.TempDir("", "gitleaks") @@ -319,8 +316,7 @@ func run() ([]Leak, error) { leaks = append(leaksFromRepo, leaks...) } } else if opts.GithubOrg != "" || opts.GithubUser != "" { - // Audit a github owner -- a user or organization. If you want to include - // private repos you must pass a --private/-p option and have your ssh keys set + // Audit a github owner -- a user or organization. leaks, err = auditGithubRepos() if err != nil { return leaks, err @@ -355,9 +351,7 @@ func writeReport(leaks []Leak) error { return err } -// cloneRepo clones a repo to memory(default) or to disk if the --disk option is set. If you want to -// clone a private repo you must set the --private/-p option, use a ssh target, and have your ssh keys -// configured. If you want to audit a local repo, getRepo will load up a repo located at --repo-path +// cloneRepo clones a repo to memory(default) or to disk if the --disk option is set. func cloneRepo() (*RepoDescriptor, error) { var ( err error @@ -372,12 +366,13 @@ func cloneRepo() (*RepoDescriptor, error) { if opts.Disk { log.Infof("cloning %s", opts.Repo) cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo)))) - if opts.IncludePrivate { + if strings.HasPrefix(opts.Repo, "git") { repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ URL: opts.Repo, Progress: os.Stdout, Auth: sshAuth, }) + } else { repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ URL: opts.Repo, @@ -389,7 +384,7 @@ func cloneRepo() (*RepoDescriptor, error) { repo, err = git.PlainOpen(opts.RepoPath) } else { log.Infof("cloning %s", opts.Repo) - if opts.IncludePrivate { + if strings.HasPrefix(opts.Repo, "git") { repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ URL: opts.Repo, Progress: os.Stdout, @@ -736,8 +731,6 @@ func optsGuard() error { return fmt.Errorf("github organization set and local owner path") } else if opts.GithubUser != "" && opts.OwnerPath != "" { return fmt.Errorf("github user set and local owner path") - } else if opts.IncludePrivate && os.Getenv("GITHUB_TOKEN") == "" && (opts.GithubOrg != "" || opts.GithubUser != "") { - return fmt.Errorf("user/organization private repos require env var GITHUB_TOKEN to be set") } // do the URL Parse and error checking here, so we can skip it later @@ -889,9 +882,13 @@ func getSSHAuth() (*ssh.PublicKeys, error) { } sshAuth, err := ssh.NewPublicKeysFromFile("git", sshKeyPath, "") if err != nil { - return nil, fmt.Errorf("unable to generate ssh key: %v", err) + if strings.HasPrefix(opts.Repo, "git") { + // if you are attempting to clone a git repo via ssh and supply a bad ssh key, + // the clone will fail. + return nil, fmt.Errorf("unable to generate ssh key: %v", err) + } } - return sshAuth, err + return sshAuth, nil } func (leak Leak) log() { From 94389137e899518337849e2ab9aea8b2be3a451d Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 16:00:15 -0500 Subject: [PATCH 6/7] removing private opts --- README.md | 1 - main.go | 1 - 2 files changed, 2 deletions(-) diff --git a/README.md b/README.md index cb3eebda..d09df559 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,6 @@ Application Options: --github-org= Github organization to audit --github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/) --github-pr= Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set - -p, --private Include private repos in audit -b, --branch= branch name to audit (defaults to HEAD) -c, --commit= sha of commit to stop at --depth= maximum commit depth diff --git a/main.go b/main.go index 92ba028c..c142db23 100644 --- a/main.go +++ b/main.go @@ -372,7 +372,6 @@ func cloneRepo() (*RepoDescriptor, error) { Progress: os.Stdout, Auth: sshAuth, }) - } else { repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ URL: opts.Repo, From 847687a637463f5f631e90ddd7eb79968b582d44 Mon Sep 17 00:00:00 2001 From: zach rice Date: Wed, 24 Oct 2018 16:48:34 -0500 Subject: [PATCH 7/7] fixing test. travis doesnt have rsa id --- github.go | 4 ---- gitleaks_test.go | 17 ----------------- 2 files changed, 21 deletions(-) diff --git a/github.go b/github.go index ee3494a9..3f63a8e2 100644 --- a/github.go +++ b/github.go @@ -117,11 +117,7 @@ func auditGithubRepos() ([]Leak, error) { break } if opts.GithubUser != "" { - // if opts.IncludePrivate { - // pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, "", githubOptions) - // } else { pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, opts.GithubUser, githubOptions) - // } if err != nil { done = true } diff --git a/gitleaks_test.go b/gitleaks_test.go index cac4f1cf..51833bb1 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -269,23 +269,6 @@ func TestRun(t *testing.T) { numLeaks: 0, expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory", }, - { - testOpts: Options{ - Repo: "git@github.com:gitleakstest/gronit.git", - }, - description: "test leak", - numLeaks: 2, - expectedErrMsg: "", - }, - { - testOpts: Options{ - Repo: "git@github.com:gitleakstest/gronit.git", - Disk: true, - }, - description: "test leak", - numLeaks: 2, - expectedErrMsg: "", - }, { testOpts: Options{ Repo: "https://github.com/gitleakstest/gronit.git",