Add line number to report plus some other things (#409)

* Init refactor

* think i got it working

* we got em

* Updating tests

* fixing tests

* Better comments, more test fixing

* Bumping to v5

* Fixing golint messages

* Replacing usage of audit with scan in comments

* bumping go-git

* Updating to v5 in some other spots
This commit is contained in:
Zachary Rice authored and GitHub committed 2020-07-24 10:49:03 -04:00
1 parent cfbb600fcb
commit 6ca7a11d88
50 files changed
+1483 -1167

No files matched your search

+19 -15
View File
@@ -6,9 +6,9 @@ import (
"strings"
"sync"
"github.com/zricethezav/gitleaks/v4/audit"
"github.com/zricethezav/gitleaks/v4/manager"
"github.com/zricethezav/gitleaks/v4/options"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v5/options"
"github.com/zricethezav/gitleaks/v5/scan"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
@@ -27,7 +27,7 @@ type Github struct {
}
// NewGithubClient accepts a manager struct and returns a Github host pointer which will be used to
// perform a github audit on an organization, user, or PR.
// perform a github scan on an organization, user, or PR.
func NewGithubClient(m *manager.Manager) (*Github, error) {
var err error
ctx := context.Background()
@@ -50,8 +50,8 @@ func NewGithubClient(m *manager.Manager) (*Github, error) {
}, err
}
// Audit will audit a github user or organization's repos.
func (g *Github) Audit() {
// Scan will scan a github user or organization's repos.
func (g *Github) Scan() {
ctx := context.Background()
listOptions := github.ListOptions{
PerPage: 100,
@@ -105,7 +105,7 @@ func (g *Github) Audit() {
}
for _, repo := range githubRepos {
r := audit.NewRepo(g.manager)
r := scan.NewRepo(g.manager)
if g.manager.CloneOptions != nil {
auth = g.manager.CloneOptions.Auth
@@ -119,7 +119,7 @@ func (g *Github) Audit() {
log.Warn("unable to clone via https and access token, attempting with ssh now")
auth, err := options.SSHAuth(g.manager.Opts)
if err != nil {
log.Warnf("unable to get ssh auth, skipping clone and audit for repo %s: %+v\n", *repo.CloneURL, err)
log.Warnf("unable to get ssh auth, skipping clone and scan for repo %s: %+v\n", *repo.CloneURL, err)
continue
}
err = r.Clone(&git.CloneOptions{
@@ -127,26 +127,26 @@ func (g *Github) Audit() {
Auth: auth,
})
if err != nil {
log.Warnf("err cloning %s, skipping clone and audit: %+v\n", *repo.SSHURL, err)
log.Warnf("err cloning %s, skipping clone and scan: %+v\n", *repo.SSHURL, err)
continue
}
}
if err = r.Audit(); err != nil {
if err = r.Scan(); err != nil {
log.Warn(err)
}
}
}
// AuditPR audits a single github PR
func (g *Github) AuditPR() {
// ScanPR scan a single github PR
func (g *Github) ScanPR() {
ctx := context.Background()
splits := strings.Split(g.manager.Opts.PullRequest, "/")
owner := splits[len(splits)-4]
repoName := splits[len(splits)-3]
prNum, err := strconv.Atoi(splits[len(splits)-1])
repo := audit.NewRepo(g.manager)
repo := scan.NewRepo(g.manager)
repo.Name = repoName
log.Infof("auditing pr %s\n", g.manager.Opts.PullRequest)
log.Infof("scanning pr %s\n", g.manager.Opts.PullRequest)
if err != nil {
return
@@ -175,7 +175,11 @@ func (g *Github) AuditPR() {
if f.Patch == nil {
continue
}
audit.InspectFile(*f.Patch, *f.Filename, &commitObj, repo)
repo.CheckRules(&scan.Bundle{
Content: *f.Patch,
FilePath: *f.Filename,
Commit: &commitObj,
})
}
}
page = resp.NextPage
+10 -10
View File
@@ -4,9 +4,9 @@ import (
"context"
"sync"
"github.com/zricethezav/gitleaks/v4/audit"
"github.com/zricethezav/gitleaks/v4/manager"
"github.com/zricethezav/gitleaks/v4/options"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v5/options"
"github.com/zricethezav/gitleaks/v5/scan"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
@@ -21,7 +21,7 @@ type Gitlab struct {
}
// NewGitlabClient accepts a manager struct and returns a Gitlab host pointer which will be used to
// perform a gitlab audit on an group or user.
// perform a gitlab scan on an group or user.
func NewGitlabClient(m *manager.Manager) (*Gitlab, error) {
var err error
@@ -38,8 +38,8 @@ func NewGitlabClient(m *manager.Manager) (*Gitlab, error) {
return gitlabClient, err
}
// Audit will audit a github user or organization's repos.
func (g *Gitlab) Audit() {
// Scan will scan a github user or organization's repos.
func (g *Gitlab) Scan() {
var (
projects []*gitlab.Project
resp *gitlab.Response
@@ -89,20 +89,20 @@ func (g *Gitlab) Audit() {
// iterate of gitlab projects
for _, p := range projects {
r := audit.NewRepo(g.manager)
r := scan.NewRepo(g.manager)
cloneOpts := g.manager.CloneOptions
cloneOpts.URL = p.HTTPURLToRepo
err := r.Clone(cloneOpts)
// TODO handle clone retry with ssh like github host
r.Name = p.Name
if err = r.Audit(); err != nil {
if err = r.Scan(); err != nil {
log.Error(err)
}
}
}
// AuditPR TODO not implemented
func (g *Gitlab) AuditPR() {
// ScanPR TODO not implemented
func (g *Gitlab) ScanPR() {
log.Error("AuditPR is not implemented in Gitlab host yet...")
}
+11 -6
View File
@@ -1,8 +1,9 @@
package hosts
import (
"github.com/zricethezav/gitleaks/v4/manager"
"strings"
"github.com/zricethezav/gitleaks/v5/manager"
)
const (
@@ -13,11 +14,11 @@ const (
// Host is an interface used for defining external git hosting providers like github and gitlab.
// TODO add bitbucket
type Host interface {
Audit()
AuditPR()
Scan()
ScanPR()
}
// Run kicks off a host audit. This function accepts a manager and determines what host it should audit
// Run kicks off a host scan. This function accepts a manager and determines what host it should scan
func Run(m *manager.Manager) error {
var host Host
var err error
@@ -30,10 +31,14 @@ func Run(m *manager.Manager) error {
return nil
}
if err != nil {
return err
}
if m.Opts.PullRequest != "" {
host.AuditPR()
host.ScanPR()
} else {
host.Audit()
host.Scan()
}
return err
}
+4 -3
View File
@@ -3,11 +3,12 @@ package hosts
import (
"flag"
"fmt"
"github.com/zricethezav/gitleaks/v4/config"
"github.com/zricethezav/gitleaks/v4/manager"
"github.com/zricethezav/gitleaks/v4/options"
"os"
"testing"
"github.com/zricethezav/gitleaks/v5/config"
"github.com/zricethezav/gitleaks/v5/manager"
"github.com/zricethezav/gitleaks/v5/options"
)
var (