root.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/rs/zerolog"
  8. "github.com/rs/zerolog/log"
  9. "github.com/spf13/cobra"
  10. "github.com/spf13/viper"
  11. "github.com/zricethezav/gitleaks/v8/config"
  12. )
  13. const banner = `
  14. │╲
  15. │ ○
  16. ○ ░
  17. ░ gitleaks
  18. `
  19. const configDescription = `config file path
  20. order of precedence:
  21. 1. --config/-c
  22. 2. (--source/-s)/.gitleaks.toml
  23. if --config/-c is not set and no (--source/s)/.gitleaks.toml is present
  24. then .gitleaks.toml will be written to (--source/-s)/.gitleaks.toml for future use`
  25. var rootCmd = &cobra.Command{
  26. Use: "gitleaks",
  27. Short: "Gitleaks scans code, past or present, for secrets",
  28. }
  29. func init() {
  30. cobra.OnInitialize(initLog)
  31. rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
  32. rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered (default: 1)")
  33. rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source (default: $PWD)")
  34. rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
  35. rootCmd.PersistentFlags().StringP("report-format", "f", "", "output format (json, csv, sarif)")
  36. rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (debug, info, warn, error, fatal)")
  37. rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
  38. rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
  39. viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
  40. }
  41. func initLog() {
  42. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  43. ll, err := rootCmd.Flags().GetString("log-level")
  44. if err != nil {
  45. log.Fatal().Msg(err.Error())
  46. }
  47. switch strings.ToLower(ll) {
  48. case "debug":
  49. zerolog.SetGlobalLevel(zerolog.DebugLevel)
  50. case "info":
  51. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  52. case "warn":
  53. zerolog.SetGlobalLevel(zerolog.WarnLevel)
  54. case "err", "error":
  55. zerolog.SetGlobalLevel(zerolog.ErrorLevel)
  56. case "fatal":
  57. zerolog.SetGlobalLevel(zerolog.FatalLevel)
  58. default:
  59. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  60. }
  61. }
  62. func initConfig() {
  63. fmt.Fprintf(os.Stderr, banner)
  64. cfgPath, err := rootCmd.Flags().GetString("config")
  65. if err != nil {
  66. log.Fatal().Msg(err.Error())
  67. }
  68. if cfgPath != "" {
  69. viper.SetConfigFile(cfgPath)
  70. log.Debug().Msgf("Using gitleaks config %s", cfgPath)
  71. } else {
  72. source, err := rootCmd.Flags().GetString("source")
  73. if err != nil {
  74. log.Fatal().Msg(err.Error())
  75. }
  76. fileInfo, err := os.Stat(source)
  77. if err != nil {
  78. log.Fatal().Msg(err.Error())
  79. }
  80. if !fileInfo.IsDir() {
  81. log.Debug().Msgf("Unable to write default gitleaks config to %s since --source=%s is a file, using default config",
  82. filepath.Join(source, ".gitleaks.toml"), source)
  83. viper.SetConfigType("toml")
  84. viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  85. return
  86. }
  87. if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
  88. log.Debug().Msgf("No gitleaks config found, writing default gitleaks config to %s", filepath.Join(source, ".gitleaks.toml"))
  89. if err := os.WriteFile(filepath.Join(source, ".gitleaks.toml"), []byte(config.DefaultConfig), os.ModePerm); err != nil {
  90. log.Debug().Msgf("Unable to write default gitleaks config to %s, using default config", filepath.Join(source, ".gitleaks.toml"))
  91. viper.SetConfigType("toml")
  92. viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  93. return
  94. }
  95. } else {
  96. log.Debug().Msgf("Using existing gitleaks config %s", filepath.Join(source, ".gitleaks.toml"))
  97. }
  98. viper.AddConfigPath(source)
  99. viper.SetConfigName(".gitleaks")
  100. viper.SetConfigType("toml")
  101. }
  102. if err := viper.ReadInConfig(); err != nil {
  103. log.Fatal().Msgf("Unable to load gitleaks config, err: %s", err)
  104. }
  105. }
  106. func Execute() {
  107. if err := rootCmd.Execute(); err != nil {
  108. if strings.Contains(err.Error(), "unknown flag") {
  109. // exit code 126: Command invoked cannot execute
  110. os.Exit(126)
  111. }
  112. log.Fatal().Msg(err.Error())
  113. }
  114. }