audit.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package audit
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path"
  6. "github.com/zricethezav/gitleaks/v4/manager"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. // Run accepts a manager and begins an audit based on the options/configs set in the manager.
  10. func Run(m *manager.Manager) error {
  11. if m.Opts.OwnerPath != "" {
  12. files, err := ioutil.ReadDir(m.Opts.OwnerPath)
  13. if err != nil {
  14. return err
  15. }
  16. for _, f := range files {
  17. if !f.IsDir() {
  18. continue
  19. }
  20. m.Opts.RepoPath = fmt.Sprintf("%s/%s", m.Opts.OwnerPath, f.Name())
  21. if err := runHelper(NewRepo(m)); err != nil {
  22. log.Warnf("%s is not a git repo, skipping", f.Name())
  23. }
  24. }
  25. return nil
  26. }
  27. return runHelper(NewRepo(m))
  28. }
  29. func runHelper(r *Repo) error {
  30. // Ignore whitelisted repos
  31. for _, wlRepo := range r.Manager.Config.Whitelist.Repos {
  32. if RegexMatched(r.Manager.Opts.RepoPath, wlRepo) {
  33. return nil
  34. }
  35. if RegexMatched(r.Manager.Opts.Repo, wlRepo) {
  36. return nil
  37. }
  38. }
  39. if r.Manager.Opts.OpenLocal() {
  40. r.Name = path.Base(r.Manager.Opts.RepoPath)
  41. if err := r.Open(); err != nil {
  42. return err
  43. }
  44. // Check if we are checking uncommitted files. This is the default behavior
  45. // for a "$ gitleaks" command with no options set
  46. if r.Manager.Opts.CheckUncommitted() {
  47. if err := r.AuditUncommitted(); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. } else {
  53. if err := r.Clone(nil); err != nil {
  54. return err
  55. }
  56. }
  57. return r.Audit()
  58. }