mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 04:04:25 +01:00
Removing base scanner (#477)
This commit is contained in:
1 parent
7db08bfd90
commit
65a91ae976
8 files changed
+86
-78
No files matched your search
+12
-8
@@ -5,6 +5,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
fdiff "github.com/go-git/go-git/v5/plumbing/format/diff"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
@@ -12,7 +15,8 @@ import (
|
||||
|
||||
// CommitScanner is a commit scanner
|
||||
type CommitScanner struct {
|
||||
BaseScanner
|
||||
cfg config.Config
|
||||
opts options.Options
|
||||
repo *git.Repository
|
||||
repoName string
|
||||
commit *object.Commit
|
||||
@@ -20,14 +24,14 @@ type CommitScanner struct {
|
||||
}
|
||||
|
||||
// NewCommitScanner creates and returns a commit scanner
|
||||
func NewCommitScanner(base BaseScanner, repo *git.Repository, commit *object.Commit) *CommitScanner {
|
||||
func NewCommitScanner(opts options.Options, cfg config.Config, repo *git.Repository, commit *object.Commit) *CommitScanner {
|
||||
cs := &CommitScanner{
|
||||
BaseScanner: base,
|
||||
repo: repo,
|
||||
commit: commit,
|
||||
repoName: getRepoName(base.opts),
|
||||
cfg: cfg,
|
||||
opts: opts,
|
||||
repo: repo,
|
||||
commit: commit,
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
cs.scannerType = typeCommitScanner
|
||||
return cs
|
||||
}
|
||||
|
||||
@@ -46,7 +50,7 @@ func (cs *CommitScanner) SetPatch(patch *object.Patch) {
|
||||
func (cs *CommitScanner) Scan() (Report, error) {
|
||||
var scannerReport Report
|
||||
if len(cs.commit.ParentHashes) == 0 {
|
||||
facScanner := NewFilesAtCommitScanner(cs.BaseScanner, cs.repo, cs.commit)
|
||||
facScanner := NewFilesAtCommitScanner(cs.opts, cs.cfg, cs.repo, cs.commit)
|
||||
return facScanner.Scan()
|
||||
}
|
||||
|
||||
|
||||
+11
-7
@@ -2,11 +2,14 @@ package scan
|
||||
|
||||
import (
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
)
|
||||
|
||||
// CommitsScanner is a commit scanner
|
||||
type CommitsScanner struct {
|
||||
BaseScanner
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
|
||||
repo *git.Repository
|
||||
repoName string
|
||||
@@ -14,12 +17,13 @@ type CommitsScanner struct {
|
||||
}
|
||||
|
||||
// NewCommitsScanner creates and returns a commits scanner, notice the 's' in commits
|
||||
func NewCommitsScanner(base BaseScanner, repo *git.Repository, commits []string) *CommitsScanner {
|
||||
func NewCommitsScanner(opts options.Options, cfg config.Config, repo *git.Repository, commits []string) *CommitsScanner {
|
||||
return &CommitsScanner{
|
||||
BaseScanner: base,
|
||||
repo: repo,
|
||||
commits: commits,
|
||||
repoName: getRepoName(base.opts),
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
commits: commits,
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +35,7 @@ func (css *CommitsScanner) Scan() (Report, error) {
|
||||
if err != nil {
|
||||
return scannerReport, nil
|
||||
}
|
||||
cs := NewCommitScanner(css.BaseScanner, css.repo, c)
|
||||
cs := NewCommitScanner(css.opts, css.cfg, css.repo, c)
|
||||
commitReport, err := cs.Scan()
|
||||
if err != nil {
|
||||
return scannerReport, err
|
||||
|
||||
+11
-8
@@ -4,6 +4,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
@@ -13,22 +16,22 @@ import (
|
||||
// files available at a commit's worktree and scans the entire content of said files.
|
||||
// Apologies for the awful struct name...
|
||||
type FilesAtCommitScanner struct {
|
||||
BaseScanner
|
||||
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
repo *git.Repository
|
||||
commit *object.Commit
|
||||
repoName string
|
||||
}
|
||||
|
||||
// NewFilesAtCommitScanner creates and returns a files at commit scanner
|
||||
func NewFilesAtCommitScanner(base BaseScanner, repo *git.Repository, commit *object.Commit) *FilesAtCommitScanner {
|
||||
func NewFilesAtCommitScanner(opts options.Options, cfg config.Config, repo *git.Repository, commit *object.Commit) *FilesAtCommitScanner {
|
||||
fs := &FilesAtCommitScanner{
|
||||
BaseScanner: base,
|
||||
repo: repo,
|
||||
commit: commit,
|
||||
repoName: getRepoName(base.opts),
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
commit: commit,
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
fs.scannerType = typeFilesAtCommitScanner
|
||||
return fs
|
||||
}
|
||||
|
||||
|
||||
+8
-5
@@ -7,6 +7,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -14,17 +17,17 @@ import (
|
||||
|
||||
// NoGitScanner is a scanner that absolutely despises git
|
||||
type NoGitScanner struct {
|
||||
BaseScanner
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
}
|
||||
|
||||
// NewNoGitScanner creates and returns a nogit scanner. This is used for scanning files and directories
|
||||
func NewNoGitScanner(base BaseScanner) *NoGitScanner {
|
||||
func NewNoGitScanner(opts options.Options, cfg config.Config) *NoGitScanner {
|
||||
ngs := &NoGitScanner{
|
||||
BaseScanner: base,
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
ngs.scannerType = typeNoGitScanner
|
||||
|
||||
// no-git scans should ignore .git folders by default
|
||||
// issue: https://github.com/zricethezav/gitleaks/issues/474
|
||||
// ngs.cfg.Allowlist
|
||||
|
||||
+9
-6
@@ -4,6 +4,8 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
@@ -12,15 +14,16 @@ import (
|
||||
|
||||
// ParentScanner is a parent directory scanner
|
||||
type ParentScanner struct {
|
||||
BaseScanner
|
||||
cfg config.Config
|
||||
opts options.Options
|
||||
}
|
||||
|
||||
// NewParentScanner creates and returns a directory scanner
|
||||
func NewParentScanner(base BaseScanner) *ParentScanner {
|
||||
func NewParentScanner(opts options.Options, cfg config.Config) *ParentScanner {
|
||||
ds := &ParentScanner{
|
||||
BaseScanner: base,
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
}
|
||||
ds.scannerType = typeDirScanner
|
||||
return ds
|
||||
}
|
||||
|
||||
@@ -55,11 +58,11 @@ func (ds *ParentScanner) Scan() (Report, error) {
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
} else {
|
||||
ds.BaseScanner.cfg = cfg
|
||||
ds.cfg = cfg
|
||||
}
|
||||
}
|
||||
|
||||
rs := NewRepoScanner(ds.BaseScanner, repo)
|
||||
rs := NewRepoScanner(ds.opts, ds.cfg, repo)
|
||||
rs.repoName = f.Name()
|
||||
repoReport, err := rs.Scan()
|
||||
if err != nil {
|
||||
|
||||
+15
-12
@@ -4,6 +4,9 @@ import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
@@ -13,7 +16,8 @@ import (
|
||||
|
||||
// RepoScanner is a repo scanner
|
||||
type RepoScanner struct {
|
||||
BaseScanner
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
repo *git.Repository
|
||||
repoName string
|
||||
|
||||
@@ -25,18 +29,17 @@ type RepoScanner struct {
|
||||
|
||||
// NewRepoScanner returns a new repo scanner (go figure). This function also
|
||||
// sets up the leak listener for multi-threaded awesomeness.
|
||||
func NewRepoScanner(base BaseScanner, repo *git.Repository) *RepoScanner {
|
||||
func NewRepoScanner(opts options.Options, cfg config.Config, repo *git.Repository) *RepoScanner {
|
||||
rs := &RepoScanner{
|
||||
BaseScanner: base,
|
||||
repo: repo,
|
||||
leakChan: make(chan Leak),
|
||||
leakWG: &sync.WaitGroup{},
|
||||
leakCache: make(map[string]bool),
|
||||
repoName: getRepoName(base.opts),
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
leakChan: make(chan Leak),
|
||||
leakWG: &sync.WaitGroup{},
|
||||
leakCache: make(map[string]bool),
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
|
||||
rs.scannerType = typeRepoScanner
|
||||
|
||||
go rs.receiveLeaks()
|
||||
|
||||
return rs
|
||||
@@ -68,7 +71,7 @@ func (rs *RepoScanner) Scan() (Report, error) {
|
||||
// Check if at root
|
||||
if len(c.ParentHashes) == 0 {
|
||||
scannerReport.Commits++
|
||||
facScanner := NewFilesAtCommitScanner(rs.BaseScanner, rs.repo, c)
|
||||
facScanner := NewFilesAtCommitScanner(rs.opts, rs.cfg, rs.repo, c)
|
||||
facScanner.repoName = rs.repoName
|
||||
facReport, err := facScanner.Scan()
|
||||
if err != nil {
|
||||
@@ -99,7 +102,7 @@ func (rs *RepoScanner) Scan() (Report, error) {
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
commitScanner := NewCommitScanner(rs.BaseScanner, rs.repo, c)
|
||||
commitScanner := NewCommitScanner(rs.opts, rs.cfg, rs.repo, c)
|
||||
commitScanner.SetRepoName(rs.repoName)
|
||||
commitScanner.SetPatch(patch)
|
||||
report, err := commitScanner.Scan()
|
||||
|
||||
+10
-26
@@ -16,14 +16,6 @@ type Scanner interface {
|
||||
Scan() (Report, error)
|
||||
}
|
||||
|
||||
// BaseScanner is a container for common data each scanner needs.
|
||||
type BaseScanner struct {
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
stopChan chan os.Signal
|
||||
scannerType ScannerType
|
||||
}
|
||||
|
||||
// ScannerType is the scanner type which is determined based on program arguments
|
||||
type ScannerType int
|
||||
|
||||
@@ -46,11 +38,6 @@ func NewScanner(opts options.Options, cfg config.Config) (Scanner, error) {
|
||||
err error
|
||||
)
|
||||
|
||||
base := BaseScanner{
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
// We want to return a dir scanner immediately since if the scan type is a directory scan
|
||||
// we don't want to clone/open a repo until inside ParentScanner.Scan
|
||||
st, err := scanType(opts)
|
||||
@@ -58,12 +45,12 @@ func NewScanner(opts options.Options, cfg config.Config) (Scanner, error) {
|
||||
return nil, err
|
||||
}
|
||||
if st == typeDirScanner {
|
||||
return NewParentScanner(base), nil
|
||||
return NewParentScanner(opts, cfg), nil
|
||||
}
|
||||
|
||||
// Clone or open a repo if we need it
|
||||
if needsRepo(st) {
|
||||
repo, err = getRepo(base.opts)
|
||||
repo, err = getRepo(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -71,7 +58,7 @@ func NewScanner(opts options.Options, cfg config.Config) (Scanner, error) {
|
||||
|
||||
// load up alternative config if possible, if not use manager's config
|
||||
if opts.RepoConfigPath != "" && !opts.NoGit {
|
||||
base.cfg, err = config.LoadRepoConfig(repo, opts.RepoConfigPath)
|
||||
cfg, err = config.LoadRepoConfig(repo, opts.RepoConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -83,34 +70,31 @@ func NewScanner(opts options.Options, cfg config.Config) (Scanner, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewCommitScanner(base, repo, c), nil
|
||||
return NewCommitScanner(opts, cfg, repo, c), nil
|
||||
case typeCommitsScanner:
|
||||
commits, err := optsToCommits(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewCommitsScanner(base, repo, commits), nil
|
||||
return NewCommitsScanner(opts, cfg, repo, commits), nil
|
||||
case typeFilesAtCommitScanner:
|
||||
c, err := obtainCommit(repo, opts.FilesAtCommit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewFilesAtCommitScanner(base, repo, c), nil
|
||||
return NewFilesAtCommitScanner(opts, cfg, repo, c), nil
|
||||
case typeUnstagedScanner:
|
||||
return NewUnstagedScanner(base, repo), nil
|
||||
return NewUnstagedScanner(opts, cfg, repo), nil
|
||||
case typeDirScanner:
|
||||
return NewParentScanner(base), nil
|
||||
return NewParentScanner(opts, cfg), nil
|
||||
case typeNoGitScanner:
|
||||
return NewNoGitScanner(base), nil
|
||||
return NewNoGitScanner(opts, cfg), nil
|
||||
default:
|
||||
return NewRepoScanner(base, repo), nil
|
||||
return NewRepoScanner(opts, cfg, repo), nil
|
||||
}
|
||||
}
|
||||
|
||||
func scanType(opts options.Options) (ScannerType, error) {
|
||||
//if opts.OwnerPath != "" {
|
||||
// return typeDirScanner
|
||||
//}
|
||||
if opts.Commit != "" {
|
||||
return typeCommitScanner, nil
|
||||
}
|
||||
|
||||
+10
-6
@@ -9,6 +9,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v7/config"
|
||||
"github.com/zricethezav/gitleaks/v7/options"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
@@ -17,19 +20,20 @@ import (
|
||||
// UnstagedScanner is an unstaged scanner. This is the scanner used when you don't provide program arguments
|
||||
// which will then scan your PWD. This scans unstaged changes in your repo.
|
||||
type UnstagedScanner struct {
|
||||
BaseScanner
|
||||
opts options.Options
|
||||
cfg config.Config
|
||||
repo *git.Repository
|
||||
repoName string
|
||||
}
|
||||
|
||||
// NewUnstagedScanner returns an unstaged scanner
|
||||
func NewUnstagedScanner(base BaseScanner, repo *git.Repository) *UnstagedScanner {
|
||||
func NewUnstagedScanner(opts options.Options, cfg config.Config, repo *git.Repository) *UnstagedScanner {
|
||||
us := &UnstagedScanner{
|
||||
BaseScanner: base,
|
||||
repo: repo,
|
||||
repoName: getRepoName(base.opts),
|
||||
opts: opts,
|
||||
cfg: cfg,
|
||||
repo: repo,
|
||||
repoName: getRepoName(opts),
|
||||
}
|
||||
us.scannerType = typeUnstagedScanner
|
||||
return us
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user