root.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. env var GITLEAKS_CONFIG
  23. 3. (--source/-s)/.gitleaks.toml
  24. If none of the three options are used, then gitleaks will use the default config`
  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")
  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", "json", "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 from `--config`", cfgPath)
  71. } else if os.Getenv("GITLEAKS_CONFIG") != "" {
  72. envPath := os.Getenv("GITLEAKS_CONFIG")
  73. viper.SetConfigFile(envPath)
  74. log.Debug().Msgf("Using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
  75. } else {
  76. source, err := rootCmd.Flags().GetString("source")
  77. if err != nil {
  78. log.Fatal().Msg(err.Error())
  79. }
  80. fileInfo, err := os.Stat(source)
  81. if err != nil {
  82. log.Fatal().Msg(err.Error())
  83. }
  84. if !fileInfo.IsDir() {
  85. log.Debug().Msgf("Unable to load gitleaks config from %s since --source=%s is a file, using default config",
  86. filepath.Join(source, ".gitleaks.toml"), source)
  87. viper.SetConfigType("toml")
  88. viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  89. return
  90. }
  91. if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
  92. log.Debug().Msgf("No gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
  93. viper.SetConfigType("toml")
  94. viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  95. return
  96. } else {
  97. log.Debug().Msgf("Using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
  98. }
  99. viper.AddConfigPath(source)
  100. viper.SetConfigName(".gitleaks")
  101. viper.SetConfigType("toml")
  102. }
  103. if err := viper.ReadInConfig(); err != nil {
  104. log.Fatal().Msgf("Unable to load gitleaks config, err: %s", err)
  105. }
  106. }
  107. func Execute() {
  108. if err := rootCmd.Execute(); err != nil {
  109. if strings.Contains(err.Error(), "unknown flag") {
  110. // exit code 126: Command invoked cannot execute
  111. os.Exit(126)
  112. }
  113. log.Fatal().Msg(err.Error())
  114. }
  115. }