main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package main
  2. import (
  3. _ "fmt"
  4. "github.com/mitchellh/go-homedir"
  5. "log"
  6. _"io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "go.uber.org/zap"
  11. _"time"
  12. "go.uber.org/zap/zapcore"
  13. )
  14. const EXIT_CLEAN = 0
  15. const EXIT_FAILURE = 1
  16. const EXIT_LEAKS = 2
  17. var (
  18. regexes map[string]*regexp.Regexp
  19. stopWords []string
  20. base64Chars string
  21. hexChars string
  22. assignRegex *regexp.Regexp
  23. fileDiffRegex *regexp.Regexp
  24. gitLeaksPath string
  25. gitLeaksClonePath string
  26. gitLeaksReportPath string
  27. logger *zap.Logger
  28. )
  29. func init() {
  30. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  31. hexChars = "1234567890abcdefABCDEF"
  32. stopWords = []string{"setting", "info", "env", "environment"}
  33. regexes = map[string]*regexp.Regexp{
  34. "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"),
  35. "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
  36. "SSH": regexp.MustCompile("-----BEGIN OPENSSH PRIVATE KEY-----"),
  37. "Facebook": regexp.MustCompile("(?i)facebook.*['|\"][0-9a-f]{32}['|\"]"),
  38. "Twitter": regexp.MustCompile("(?i)twitter.*['|\"][0-9a-zA-Z]{35,44}['|\"]"),
  39. "Github": regexp.MustCompile("(?i)github.*[['|\"]0-9a-zA-Z]{35,40}['|\"]"),
  40. "AWS": regexp.MustCompile("AKIA[0-9A-Z]{16}"),
  41. "Reddit": regexp.MustCompile("(?i)reddit.*['|\"][0-9a-zA-Z]{14}['|\"]"),
  42. "Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"),
  43. // "Custom": regexp.MustCompile(".*")
  44. }
  45. fileDiffRegex = regexp.MustCompile("diff --git a.+b/")
  46. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  47. // gitleaks dir defaults to $HOME/.gitleaks if no env var GITLEAKS_HOME is present.
  48. gitLeaksPath = os.Getenv("GITLEAKS_HOME")
  49. if gitLeaksPath == "" {
  50. homeDir, err := homedir.Dir()
  51. if err != nil {
  52. log.Fatal("Cant find home dir")
  53. }
  54. gitLeaksPath = filepath.Join(homeDir, ".gitleaks")
  55. }
  56. if _, err := os.Stat(gitLeaksPath); os.IsNotExist(err) {
  57. os.Mkdir(gitLeaksPath, os.ModePerm)
  58. }
  59. gitLeaksClonePath = filepath.Join(gitLeaksPath, "clones")
  60. if _, err := os.Stat(gitLeaksClonePath); os.IsNotExist(err) {
  61. os.Mkdir(gitLeaksClonePath, os.ModePerm)
  62. }
  63. gitLeaksReportPath = filepath.Join(gitLeaksPath, "report")
  64. if _, err := os.Stat(gitLeaksReportPath); os.IsNotExist(err) {
  65. os.Mkdir(gitLeaksReportPath, os.ModePerm)
  66. }
  67. }
  68. func main() {
  69. // TODO abstract logging
  70. atom := zap.NewAtomicLevel()
  71. encoderCfg := zap.NewProductionEncoderConfig()
  72. encoderCfg.TimeKey = ""
  73. logger = zap.New(zapcore.NewCore(
  74. zapcore.NewJSONEncoder(encoderCfg),
  75. zapcore.Lock(os.Stdout),
  76. atom,
  77. ))
  78. logger.Info("HEY")
  79. atom.SetLevel(zap.InfoLevel)
  80. logger.Info("HEY")
  81. args := os.Args[1:]
  82. opts := parseOptions(args)
  83. owner := newOwner(opts)
  84. owner.auditRepos(opts)
  85. // repos := getRepos(opts, owner)
  86. // start(repos, owner, opts)
  87. }