root.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. "github.com/rs/zerolog"
  9. "github.com/rs/zerolog/log"
  10. "github.com/spf13/cobra"
  11. "github.com/spf13/viper"
  12. "github.com/zricethezav/gitleaks/v8/config"
  13. "github.com/zricethezav/gitleaks/v8/detect"
  14. "github.com/zricethezav/gitleaks/v8/report"
  15. )
  16. const banner = `
  17. │╲
  18. │ ○
  19. ○ ░
  20. ░ gitleaks
  21. `
  22. const configDescription = `config file path
  23. order of precedence:
  24. 1. --config/-c
  25. 2. env var GITLEAKS_CONFIG
  26. 3. (target path)/.gitleaks.toml
  27. If none of the three options are used, then gitleaks will use the default config`
  28. var rootCmd = &cobra.Command{
  29. Use: "gitleaks",
  30. Short: "Gitleaks scans code, past or present, for secrets",
  31. Version: Version,
  32. }
  33. func init() {
  34. cobra.OnInitialize(initLog)
  35. rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
  36. rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered")
  37. rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
  38. rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, jsonextra, csv, junit, sarif)")
  39. rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored")
  40. rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)")
  41. rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
  42. rootCmd.PersistentFlags().BoolP("no-color", "", false, "turn off color for verbose output")
  43. rootCmd.PersistentFlags().Int("max-target-megabytes", 0, "files larger than this will be skipped")
  44. rootCmd.PersistentFlags().BoolP("ignore-gitleaks-allow", "", false, "ignore gitleaks:allow comments")
  45. rootCmd.PersistentFlags().Uint("redact", 0, "redact secrets from logs and stdout. To redact only parts of the secret just apply a percent value from 0..100. For example --redact=20 (default 100%)")
  46. rootCmd.Flag("redact").NoOptDefVal = "100"
  47. rootCmd.PersistentFlags().Bool("no-banner", false, "suppress banner")
  48. rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id")
  49. rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
  50. rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)")
  51. err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
  52. if err != nil {
  53. log.Fatal().Msgf("err binding config %s", err.Error())
  54. }
  55. }
  56. func initLog() {
  57. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  58. ll, err := rootCmd.Flags().GetString("log-level")
  59. if err != nil {
  60. log.Fatal().Msg(err.Error())
  61. }
  62. switch strings.ToLower(ll) {
  63. case "trace":
  64. zerolog.SetGlobalLevel(zerolog.TraceLevel)
  65. case "debug":
  66. zerolog.SetGlobalLevel(zerolog.DebugLevel)
  67. case "info":
  68. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  69. case "warn":
  70. zerolog.SetGlobalLevel(zerolog.WarnLevel)
  71. case "err", "error":
  72. zerolog.SetGlobalLevel(zerolog.ErrorLevel)
  73. case "fatal":
  74. zerolog.SetGlobalLevel(zerolog.FatalLevel)
  75. default:
  76. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  77. }
  78. }
  79. func initConfig(source string) {
  80. hideBanner, err := rootCmd.Flags().GetBool("no-banner")
  81. if err != nil {
  82. log.Fatal().Msg(err.Error())
  83. }
  84. if !hideBanner {
  85. _, _ = fmt.Fprint(os.Stderr, banner)
  86. }
  87. cfgPath, err := rootCmd.Flags().GetString("config")
  88. if err != nil {
  89. log.Fatal().Msg(err.Error())
  90. }
  91. if cfgPath != "" {
  92. viper.SetConfigFile(cfgPath)
  93. log.Debug().Msgf("using gitleaks config %s from `--config`", cfgPath)
  94. } else if os.Getenv("GITLEAKS_CONFIG") != "" {
  95. envPath := os.Getenv("GITLEAKS_CONFIG")
  96. viper.SetConfigFile(envPath)
  97. log.Debug().Msgf("using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
  98. } else {
  99. fileInfo, err := os.Stat(source)
  100. if err != nil {
  101. log.Fatal().Msg(err.Error())
  102. }
  103. if !fileInfo.IsDir() {
  104. log.Debug().Msgf("unable to load gitleaks config from %s since --source=%s is a file, using default config",
  105. filepath.Join(source, ".gitleaks.toml"), source)
  106. viper.SetConfigType("toml")
  107. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  108. log.Fatal().Msgf("err reading toml %s", err.Error())
  109. }
  110. return
  111. }
  112. if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
  113. log.Debug().Msgf("no gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
  114. viper.SetConfigType("toml")
  115. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  116. log.Fatal().Msgf("err reading default config toml %s", err.Error())
  117. }
  118. return
  119. } else {
  120. log.Debug().Msgf("using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
  121. }
  122. viper.AddConfigPath(source)
  123. viper.SetConfigName(".gitleaks")
  124. viper.SetConfigType("toml")
  125. }
  126. if err := viper.ReadInConfig(); err != nil {
  127. log.Fatal().Msgf("unable to load gitleaks config, err: %s", err)
  128. }
  129. }
  130. func Execute() {
  131. if err := rootCmd.Execute(); err != nil {
  132. if strings.Contains(err.Error(), "unknown flag") {
  133. // exit code 126: Command invoked cannot execute
  134. os.Exit(126)
  135. }
  136. log.Fatal().Msg(err.Error())
  137. }
  138. }
  139. func Config(cmd *cobra.Command) config.Config {
  140. var vc config.ViperConfig
  141. if err := viper.Unmarshal(&vc); err != nil {
  142. log.Fatal().Err(err).Msg("Failed to load config")
  143. }
  144. cfg, err := vc.Translate()
  145. if err != nil {
  146. log.Fatal().Err(err).Msg("Failed to load config")
  147. }
  148. cfg.Path, _ = cmd.Flags().GetString("config")
  149. return cfg
  150. }
  151. func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Detector {
  152. var err error
  153. // Setup common detector
  154. detector := detect.NewDetector(cfg)
  155. if detector.MaxDecodeDepth, err = cmd.Flags().GetInt("max-decode-depth"); err != nil {
  156. log.Fatal().Err(err).Msg("")
  157. }
  158. // set color flag at first
  159. if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil {
  160. log.Fatal().Err(err).Msg("")
  161. }
  162. // also init logger again without color
  163. if detector.NoColor {
  164. log.Logger = log.Output(zerolog.ConsoleWriter{
  165. Out: os.Stderr,
  166. NoColor: detector.NoColor,
  167. })
  168. }
  169. detector.Config.Path, err = cmd.Flags().GetString("config")
  170. if err != nil {
  171. log.Fatal().Err(err).Msg("")
  172. }
  173. // if config path is not set, then use the {source}/.gitleaks.toml path.
  174. // note that there may not be a `{source}/.gitleaks.toml` file, this is ok.
  175. if detector.Config.Path == "" {
  176. detector.Config.Path = filepath.Join(source, ".gitleaks.toml")
  177. }
  178. // set verbose flag
  179. if detector.Verbose, err = cmd.Flags().GetBool("verbose"); err != nil {
  180. log.Fatal().Err(err).Msg("")
  181. }
  182. // set redact flag
  183. if detector.Redact, err = cmd.Flags().GetUint("redact"); err != nil {
  184. log.Fatal().Err(err).Msg("")
  185. }
  186. if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil {
  187. log.Fatal().Err(err).Msg("")
  188. }
  189. // set ignore gitleaks:allow flag
  190. if detector.IgnoreGitleaksAllow, err = cmd.Flags().GetBool("ignore-gitleaks-allow"); err != nil {
  191. log.Fatal().Err(err).Msg("")
  192. }
  193. gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path")
  194. if err != nil {
  195. log.Fatal().Err(err).Msg("could not get .gitleaksignore path")
  196. }
  197. if fileExists(gitleaksIgnorePath) {
  198. if err = detector.AddGitleaksIgnore(gitleaksIgnorePath); err != nil {
  199. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  200. }
  201. }
  202. if fileExists(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")) {
  203. if err = detector.AddGitleaksIgnore(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")); err != nil {
  204. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  205. }
  206. }
  207. if fileExists(filepath.Join(source, ".gitleaksignore")) {
  208. if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {
  209. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  210. }
  211. }
  212. // ignore findings from the baseline (an existing report in json format generated earlier)
  213. baselinePath, _ := cmd.Flags().GetString("baseline-path")
  214. if baselinePath != "" {
  215. err = detector.AddBaseline(baselinePath, source)
  216. if err != nil {
  217. log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
  218. }
  219. }
  220. // If set, only apply rules that are defined in the flag
  221. rules, _ := cmd.Flags().GetStringSlice("enable-rule")
  222. if len(rules) > 0 {
  223. log.Info().Msg("Overriding enabled rules: " + strings.Join(rules, ", "))
  224. ruleOverride := make(map[string]config.Rule)
  225. for _, ruleName := range rules {
  226. if rule, ok := cfg.Rules[ruleName]; ok {
  227. ruleOverride[ruleName] = rule
  228. } else {
  229. log.Fatal().Msgf("Requested rule %s not found in rules", ruleName)
  230. }
  231. }
  232. detector.Config.Rules = ruleOverride
  233. }
  234. return detector
  235. }
  236. func findingSummaryAndExit(findings []report.Finding, cmd *cobra.Command, cfg config.Config, exitCode int, start time.Time, err error) {
  237. if err == nil {
  238. log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start)))
  239. if len(findings) != 0 {
  240. log.Warn().Msgf("leaks found: %d", len(findings))
  241. } else {
  242. log.Info().Msg("no leaks found")
  243. }
  244. } else {
  245. log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start)))
  246. if len(findings) != 0 {
  247. log.Warn().Msgf("%d leaks found in partial scan", len(findings))
  248. } else {
  249. log.Warn().Msg("no leaks found in partial scan")
  250. }
  251. }
  252. // write report if desired
  253. reportPath, _ := cmd.Flags().GetString("report-path")
  254. ext, _ := cmd.Flags().GetString("report-format")
  255. if reportPath != "" {
  256. reportWriter := os.Stdout
  257. if reportPath != "-" {
  258. reportWriter, err = os.Create(reportPath)
  259. if err != nil {
  260. log.Fatal().Err(err).Msg("could not create report file")
  261. }
  262. }
  263. if err = report.Write(findings, cfg, ext, reportWriter); err != nil {
  264. log.Fatal().Err(err).Msg("could not write")
  265. }
  266. }
  267. if err != nil {
  268. os.Exit(1)
  269. }
  270. if len(findings) != 0 {
  271. os.Exit(exitCode)
  272. }
  273. }
  274. func fileExists(fileName string) bool {
  275. // check for a .gitleaksignore file
  276. info, err := os.Stat(fileName)
  277. if err != nil && !os.IsNotExist(err) {
  278. return false
  279. }
  280. if info != nil && err == nil {
  281. if !info.IsDir() {
  282. return true
  283. }
  284. }
  285. return false
  286. }
  287. func FormatDuration(d time.Duration) string {
  288. scale := 100 * time.Second
  289. // look for the max scale that is smaller than d
  290. for scale > d {
  291. scale = scale / 10
  292. }
  293. return d.Round(scale / 100).String()
  294. }