main.go 2.5 KB

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