main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // TODO documentation for
  15. // 1. ./gitleaks --repo=https://github.com/gitleakstest/gronit -v | jq -R 'fromjson?'
  16. // 2. Dockerfile
  17. // 3. need to add tests for --repo-config
  18. // 4. look over comments and code
  19. // 5. prepare release
  20. func main() {
  21. opts, err := options.ParseOptions()
  22. if err != nil {
  23. log.Error(err)
  24. os.Exit(options.ErrorEncountered)
  25. }
  26. err = opts.Guard()
  27. if err != nil {
  28. log.Error(err)
  29. os.Exit(options.ErrorEncountered)
  30. }
  31. cfg, err := config.NewConfig(opts)
  32. if err != nil {
  33. log.Error(err)
  34. os.Exit(options.ErrorEncountered)
  35. }
  36. m, err := manager.NewManager(opts, cfg)
  37. if err != nil {
  38. log.Error(err)
  39. os.Exit(options.ErrorEncountered)
  40. }
  41. err = Run(m)
  42. if err != nil {
  43. log.Error(err)
  44. os.Exit(options.ErrorEncountered)
  45. }
  46. leaks := m.GetLeaks()
  47. metadata := m.GetMetadata()
  48. if len(m.GetLeaks()) != 0 {
  49. log.Warnf("%d leaks detected. %d commits audited in %s", len(leaks),
  50. metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond))
  51. os.Exit(options.LeaksPresent)
  52. } else {
  53. log.Infof("No leaks detected. %d commits audited in %s",
  54. metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond))
  55. }
  56. os.Exit(options.Success)
  57. }
  58. // Run begins the program and contains some basic logic on how to continue with the audit. If any external git host
  59. // options are set (like auditing a gitlab or github user) then a specific host client will be created and
  60. // then Audit() and Report() will be called. Otherwise, gitleaks will create a new repo and an audit will proceed.
  61. // If no options or the uncommitted option is set then a pre-commit audit will
  62. // take place -- this is similar to running `git diff` on all the tracked files.
  63. // TODO handle errors from errChan
  64. func Run(m *manager.Manager) error {
  65. if m.Opts.Disk {
  66. dir, err := ioutil.TempDir("", "gitleaks")
  67. defer os.RemoveAll(dir)
  68. if err != nil {
  69. return err
  70. }
  71. m.CloneDir = dir
  72. }
  73. var err error
  74. if m.Opts.Host != "" {
  75. err = hosts.Run(m)
  76. } else {
  77. err = audit.Run(m)
  78. }
  79. if err != nil {
  80. return err
  81. }
  82. return m.Report()
  83. }