main.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "github.com/hako/durafmt"
  4. log "github.com/sirupsen/logrus"
  5. "github.com/zricethezav/gitleaks/audit"
  6. "github.com/zricethezav/gitleaks/config"
  7. "github.com/zricethezav/gitleaks/hosts"
  8. "github.com/zricethezav/gitleaks/manager"
  9. "github.com/zricethezav/gitleaks/options"
  10. "io/ioutil"
  11. "os"
  12. "time"
  13. )
  14. func main() {
  15. opts, err := options.ParseOptions()
  16. if err != nil {
  17. log.Error(err)
  18. os.Exit(options.ErrorEncountered)
  19. }
  20. err = opts.Guard()
  21. if err != nil {
  22. log.Error(err)
  23. os.Exit(options.ErrorEncountered)
  24. }
  25. cfg, err := config.NewConfig(opts)
  26. if err != nil {
  27. log.Error(err)
  28. os.Exit(options.ErrorEncountered)
  29. }
  30. m, err := manager.NewManager(opts, cfg)
  31. if err != nil {
  32. log.Error(err)
  33. os.Exit(options.ErrorEncountered)
  34. }
  35. err = Run(m)
  36. if err != nil {
  37. log.Error(err)
  38. os.Exit(options.ErrorEncountered)
  39. }
  40. leaks := m.GetLeaks()
  41. metadata := m.GetMetadata()
  42. if len(m.GetLeaks()) != 0 {
  43. if m.Opts.CheckUncommitted() {
  44. log.Warnf("%d leaks detected in staged changes", len(leaks))
  45. } else {
  46. log.Warnf("%d leaks detected. %d commits audited in %s", len(leaks),
  47. metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond))
  48. }
  49. os.Exit(options.LeaksPresent)
  50. } else {
  51. if m.Opts.CheckUncommitted() {
  52. log.Infof("No leaks detected in staged changes")
  53. } else {
  54. log.Infof("No leaks detected. %d commits audited in %s",
  55. metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond))
  56. }
  57. os.Exit(options.Success)
  58. }
  59. }
  60. // Run begins the program and contains some basic logic on how to continue with the audit. If any external git host
  61. // options are set (like auditing a gitlab or github user) then a specific host client will be created and
  62. // then Audit() and Report() will be called. Otherwise, gitleaks will create a new repo and an audit will proceed.
  63. // If no options or the uncommitted option is set then a pre-commit audit will
  64. // take place -- this is similar to running `git diff` on all the tracked files.
  65. func Run(m *manager.Manager) error {
  66. if m.Opts.Disk {
  67. dir, err := ioutil.TempDir("", "gitleaks")
  68. defer os.RemoveAll(dir)
  69. if err != nil {
  70. return err
  71. }
  72. m.CloneDir = dir
  73. }
  74. var err error
  75. if m.Opts.Host != "" {
  76. err = hosts.Run(m)
  77. } else {
  78. err = audit.Run(m)
  79. }
  80. if err != nil {
  81. return err
  82. }
  83. return m.Report()
  84. }