root.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. (--source/-s)/.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. }
  32. func init() {
  33. cobra.OnInitialize(initLog)
  34. rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
  35. rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered")
  36. rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source")
  37. rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
  38. rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, 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().String("log-opts", "", "git log options")
  49. rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id, ex: `gitleaks detect --enable-rule=atlassian-api-token --enable-rule=slack-access-token`")
  50. rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
  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() {
  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. source, err := rootCmd.Flags().GetString("source")
  100. if err != nil {
  101. log.Fatal().Msg(err.Error())
  102. }
  103. fileInfo, err := os.Stat(source)
  104. if err != nil {
  105. log.Fatal().Msg(err.Error())
  106. }
  107. if !fileInfo.IsDir() {
  108. log.Debug().Msgf("unable to load gitleaks config from %s since --source=%s is a file, using default config",
  109. filepath.Join(source, ".gitleaks.toml"), source)
  110. viper.SetConfigType("toml")
  111. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  112. log.Fatal().Msgf("err reading toml %s", err.Error())
  113. }
  114. return
  115. }
  116. if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
  117. log.Debug().Msgf("no gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
  118. viper.SetConfigType("toml")
  119. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  120. log.Fatal().Msgf("err reading default config toml %s", err.Error())
  121. }
  122. return
  123. } else {
  124. log.Debug().Msgf("using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
  125. }
  126. viper.AddConfigPath(source)
  127. viper.SetConfigName(".gitleaks")
  128. viper.SetConfigType("toml")
  129. }
  130. if err := viper.ReadInConfig(); err != nil {
  131. log.Fatal().Msgf("unable to load gitleaks config, err: %s", err)
  132. }
  133. }
  134. func Execute() {
  135. if err := rootCmd.Execute(); err != nil {
  136. if strings.Contains(err.Error(), "unknown flag") {
  137. // exit code 126: Command invoked cannot execute
  138. os.Exit(126)
  139. }
  140. log.Fatal().Msg(err.Error())
  141. }
  142. }
  143. func Config(cmd *cobra.Command) config.Config {
  144. var vc config.ViperConfig
  145. if err := viper.Unmarshal(&vc); err != nil {
  146. log.Fatal().Err(err).Msg("Failed to load config")
  147. }
  148. cfg, err := vc.Translate()
  149. if err != nil {
  150. log.Fatal().Err(err).Msg("Failed to load config")
  151. }
  152. cfg.Path, _ = cmd.Flags().GetString("config")
  153. return cfg
  154. }
  155. func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Detector {
  156. var err error
  157. // Setup common detector
  158. detector := detect.NewDetector(cfg)
  159. // set color flag at first
  160. if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil {
  161. log.Fatal().Err(err).Msg("")
  162. }
  163. // also init logger again without color
  164. if detector.NoColor {
  165. log.Logger = log.Output(zerolog.ConsoleWriter{
  166. Out: os.Stderr,
  167. NoColor: detector.NoColor,
  168. })
  169. }
  170. detector.Config.Path, err = cmd.Flags().GetString("config")
  171. if err != nil {
  172. log.Fatal().Err(err).Msg("")
  173. }
  174. // if config path is not set, then use the {source}/.gitleaks.toml path.
  175. // note that there may not be a `{source}/.gitleaks.toml` file, this is ok.
  176. if detector.Config.Path == "" {
  177. detector.Config.Path = filepath.Join(source, ".gitleaks.toml")
  178. }
  179. // set verbose flag
  180. if detector.Verbose, err = cmd.Flags().GetBool("verbose"); err != nil {
  181. log.Fatal().Err(err).Msg("")
  182. }
  183. // set redact flag
  184. if detector.Redact, err = cmd.Flags().GetUint("redact"); err != nil {
  185. log.Fatal().Err(err).Msg("")
  186. }
  187. if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil {
  188. log.Fatal().Err(err).Msg("")
  189. }
  190. // set ignore gitleaks:allow flag
  191. if detector.IgnoreGitleaksAllow, err = cmd.Flags().GetBool("ignore-gitleaks-allow"); err != nil {
  192. log.Fatal().Err(err).Msg("")
  193. }
  194. gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path")
  195. if err != nil {
  196. log.Fatal().Err(err).Msg("could not get .gitleaksignore path")
  197. }
  198. if fileExists(gitleaksIgnorePath) {
  199. if err = detector.AddGitleaksIgnore(gitleaksIgnorePath); err != nil {
  200. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  201. }
  202. }
  203. if fileExists(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")) {
  204. if err = detector.AddGitleaksIgnore(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")); err != nil {
  205. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  206. }
  207. }
  208. if fileExists(filepath.Join(source, ".gitleaksignore")) {
  209. if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {
  210. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  211. }
  212. }
  213. // ignore findings from the baseline (an existing report in json format generated earlier)
  214. baselinePath, _ := cmd.Flags().GetString("baseline-path")
  215. if baselinePath != "" {
  216. err = detector.AddBaseline(baselinePath, source)
  217. if err != nil {
  218. log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
  219. }
  220. }
  221. // If set, only apply rules that are defined in the flag
  222. rules, _ := cmd.Flags().GetStringSlice("enable-rule")
  223. if len(rules) > 0 {
  224. log.Info().Msg("Overriding enabled rules: " + strings.Join(rules, ", "))
  225. ruleOverride := make(map[string]config.Rule)
  226. for _, ruleName := range rules {
  227. if rule, ok := cfg.Rules[ruleName]; ok {
  228. ruleOverride[ruleName] = rule
  229. } else {
  230. log.Fatal().Msgf("Requested rule %s not found in rules", ruleName)
  231. }
  232. }
  233. detector.Config.Rules = ruleOverride
  234. }
  235. // set follow symlinks flag
  236. if detector.FollowSymlinks, err = cmd.Flags().GetBool("follow-symlinks"); err != nil {
  237. log.Fatal().Err(err).Msg("")
  238. }
  239. return detector
  240. }
  241. func findingSummaryAndExit(findings []report.Finding, cmd *cobra.Command, cfg config.Config, exitCode int, start time.Time, err error) {
  242. if err == nil {
  243. log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start)))
  244. if len(findings) != 0 {
  245. log.Warn().Msgf("leaks found: %d", len(findings))
  246. } else {
  247. log.Info().Msg("no leaks found")
  248. }
  249. } else {
  250. log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start)))
  251. if len(findings) != 0 {
  252. log.Warn().Msgf("%d leaks found in partial scan", len(findings))
  253. } else {
  254. log.Warn().Msg("no leaks found in partial scan")
  255. }
  256. }
  257. // write report if desired
  258. reportPath, _ := cmd.Flags().GetString("report-path")
  259. ext, _ := cmd.Flags().GetString("report-format")
  260. if reportPath != "" {
  261. if err := report.Write(findings, cfg, ext, reportPath); err != nil {
  262. log.Fatal().Err(err).Msg("could not write")
  263. }
  264. }
  265. if err != nil {
  266. os.Exit(1)
  267. }
  268. if len(findings) != 0 {
  269. os.Exit(exitCode)
  270. }
  271. }
  272. func fileExists(fileName string) bool {
  273. // check for a .gitleaksignore file
  274. info, err := os.Stat(fileName)
  275. if err != nil && !os.IsNotExist(err) {
  276. return false
  277. }
  278. if info != nil && err == nil {
  279. if !info.IsDir() {
  280. return true
  281. }
  282. }
  283. return false
  284. }
  285. func FormatDuration(d time.Duration) string {
  286. scale := 100 * time.Second
  287. // look for the max scale that is smaller than d
  288. for scale > d {
  289. scale = scale / 10
  290. }
  291. return d.Round(scale / 100).String()
  292. }