parent.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package scan
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "github.com/zricethezav/gitleaks/v7/options"
  6. "github.com/zricethezav/gitleaks/v7/config"
  7. "github.com/go-git/go-git/v5"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. // ParentScanner is a parent directory scanner
  11. type ParentScanner struct {
  12. cfg config.Config
  13. opts options.Options
  14. }
  15. // NewParentScanner creates and returns a directory scanner
  16. func NewParentScanner(opts options.Options, cfg config.Config) *ParentScanner {
  17. ds := &ParentScanner{
  18. opts: opts,
  19. cfg: cfg,
  20. }
  21. return ds
  22. }
  23. // Scan kicks off a ParentScanner scan. This uses the directory from --path to discovery repos
  24. func (ds *ParentScanner) Scan() (Report, error) {
  25. var scannerReport Report
  26. log.Debugf("scanning repos in %s\n", ds.opts.Path)
  27. files, err := ioutil.ReadDir(ds.opts.Path)
  28. if err != nil {
  29. return scannerReport, err
  30. }
  31. for _, f := range files {
  32. if !f.IsDir() {
  33. continue
  34. }
  35. repo, err := git.PlainOpen(filepath.Join(ds.opts.Path, f.Name()))
  36. if err != nil {
  37. if err.Error() == "repository does not exist" {
  38. log.Debugf("%s is not a git repository", f.Name())
  39. continue
  40. }
  41. return scannerReport, err
  42. }
  43. if ds.cfg.Allowlist.RepoAllowed(f.Name()) {
  44. continue
  45. }
  46. if ds.opts.RepoConfigPath != "" {
  47. cfg, err := config.LoadRepoConfig(repo, ds.opts.RepoConfigPath)
  48. if err != nil {
  49. log.Warn(err)
  50. } else {
  51. ds.cfg = cfg
  52. }
  53. }
  54. rs := NewRepoScanner(ds.opts, ds.cfg, repo)
  55. rs.repoName = f.Name()
  56. repoReport, err := rs.Scan()
  57. if err != nil {
  58. return scannerReport, err
  59. }
  60. scannerReport.Leaks = append(scannerReport.Leaks, repoReport.Leaks...)
  61. scannerReport.Commits += repoReport.Commits
  62. }
  63. return scannerReport, nil
  64. }