root.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. rootCmd.PersistentFlags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
  52. err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
  53. if err != nil {
  54. log.Fatal().Msgf("err binding config %s", err.Error())
  55. }
  56. }
  57. func initLog() {
  58. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  59. ll, err := rootCmd.Flags().GetString("log-level")
  60. if err != nil {
  61. log.Fatal().Msg(err.Error())
  62. }
  63. switch strings.ToLower(ll) {
  64. case "trace":
  65. zerolog.SetGlobalLevel(zerolog.TraceLevel)
  66. case "debug":
  67. zerolog.SetGlobalLevel(zerolog.DebugLevel)
  68. case "info":
  69. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  70. case "warn":
  71. zerolog.SetGlobalLevel(zerolog.WarnLevel)
  72. case "err", "error":
  73. zerolog.SetGlobalLevel(zerolog.ErrorLevel)
  74. case "fatal":
  75. zerolog.SetGlobalLevel(zerolog.FatalLevel)
  76. default:
  77. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  78. }
  79. }
  80. func initConfig() {
  81. hideBanner, err := rootCmd.Flags().GetBool("no-banner")
  82. if err != nil {
  83. log.Fatal().Msg(err.Error())
  84. }
  85. if !hideBanner {
  86. _, _ = fmt.Fprint(os.Stderr, banner)
  87. }
  88. cfgPath, err := rootCmd.Flags().GetString("config")
  89. if err != nil {
  90. log.Fatal().Msg(err.Error())
  91. }
  92. if cfgPath != "" {
  93. viper.SetConfigFile(cfgPath)
  94. log.Debug().Msgf("using gitleaks config %s from `--config`", cfgPath)
  95. } else if os.Getenv("GITLEAKS_CONFIG") != "" {
  96. envPath := os.Getenv("GITLEAKS_CONFIG")
  97. viper.SetConfigFile(envPath)
  98. log.Debug().Msgf("using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
  99. } else {
  100. source, err := rootCmd.Flags().GetString("source")
  101. if err != nil {
  102. log.Fatal().Msg(err.Error())
  103. }
  104. fileInfo, err := os.Stat(source)
  105. if err != nil {
  106. log.Fatal().Msg(err.Error())
  107. }
  108. if !fileInfo.IsDir() {
  109. log.Debug().Msgf("unable to load gitleaks config from %s since --source=%s is a file, using default config",
  110. filepath.Join(source, ".gitleaks.toml"), source)
  111. viper.SetConfigType("toml")
  112. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  113. log.Fatal().Msgf("err reading toml %s", err.Error())
  114. }
  115. return
  116. }
  117. if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
  118. log.Debug().Msgf("no gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
  119. viper.SetConfigType("toml")
  120. if err = viper.ReadConfig(strings.NewReader(config.DefaultConfig)); err != nil {
  121. log.Fatal().Msgf("err reading default config toml %s", err.Error())
  122. }
  123. return
  124. } else {
  125. log.Debug().Msgf("using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
  126. }
  127. viper.AddConfigPath(source)
  128. viper.SetConfigName(".gitleaks")
  129. viper.SetConfigType("toml")
  130. }
  131. if err := viper.ReadInConfig(); err != nil {
  132. log.Fatal().Msgf("unable to load gitleaks config, err: %s", err)
  133. }
  134. }
  135. func Execute() {
  136. if err := rootCmd.Execute(); err != nil {
  137. if strings.Contains(err.Error(), "unknown flag") {
  138. // exit code 126: Command invoked cannot execute
  139. os.Exit(126)
  140. }
  141. log.Fatal().Msg(err.Error())
  142. }
  143. }
  144. func Config(cmd *cobra.Command) config.Config {
  145. var vc config.ViperConfig
  146. if err := viper.Unmarshal(&vc); err != nil {
  147. log.Fatal().Err(err).Msg("Failed to load config")
  148. }
  149. cfg, err := vc.Translate()
  150. if err != nil {
  151. log.Fatal().Err(err).Msg("Failed to load config")
  152. }
  153. cfg.Path, _ = cmd.Flags().GetString("config")
  154. return cfg
  155. }
  156. func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Detector {
  157. var err error
  158. // Setup common detector
  159. detector := detect.NewDetector(cfg)
  160. // set color flag at first
  161. if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil {
  162. log.Fatal().Err(err).Msg("")
  163. }
  164. // also init logger again without color
  165. if detector.NoColor {
  166. log.Logger = log.Output(zerolog.ConsoleWriter{
  167. Out: os.Stderr,
  168. NoColor: detector.NoColor,
  169. })
  170. }
  171. detector.Config.Path, err = cmd.Flags().GetString("config")
  172. if err != nil {
  173. log.Fatal().Err(err).Msg("")
  174. }
  175. // if config path is not set, then use the {source}/.gitleaks.toml path.
  176. // note that there may not be a `{source}/.gitleaks.toml` file, this is ok.
  177. if detector.Config.Path == "" {
  178. detector.Config.Path = filepath.Join(source, ".gitleaks.toml")
  179. }
  180. // set verbose flag
  181. if detector.Verbose, err = cmd.Flags().GetBool("verbose"); err != nil {
  182. log.Fatal().Err(err).Msg("")
  183. }
  184. // set redact flag
  185. if detector.Redact, err = cmd.Flags().GetUint("redact"); err != nil {
  186. log.Fatal().Err(err).Msg("")
  187. }
  188. if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil {
  189. log.Fatal().Err(err).Msg("")
  190. }
  191. // set ignore gitleaks:allow flag
  192. if detector.IgnoreGitleaksAllow, err = cmd.Flags().GetBool("ignore-gitleaks-allow"); err != nil {
  193. log.Fatal().Err(err).Msg("")
  194. }
  195. gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path")
  196. if err != nil {
  197. log.Fatal().Err(err).Msg("could not get .gitleaksignore path")
  198. }
  199. if fileExists(gitleaksIgnorePath) {
  200. if err = detector.AddGitleaksIgnore(gitleaksIgnorePath); err != nil {
  201. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  202. }
  203. }
  204. if fileExists(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")) {
  205. if err = detector.AddGitleaksIgnore(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")); err != nil {
  206. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  207. }
  208. }
  209. if fileExists(filepath.Join(source, ".gitleaksignore")) {
  210. if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {
  211. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  212. }
  213. }
  214. // ignore findings from the baseline (an existing report in json format generated earlier)
  215. baselinePath, _ := cmd.Flags().GetString("baseline-path")
  216. if baselinePath != "" {
  217. err = detector.AddBaseline(baselinePath, source)
  218. if err != nil {
  219. log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
  220. }
  221. }
  222. // If set, only apply rules that are defined in the flag
  223. rules, _ := cmd.Flags().GetStringSlice("enable-rule")
  224. if len(rules) > 0 {
  225. log.Info().Msg("Overriding enabled rules: " + strings.Join(rules, ", "))
  226. ruleOverride := make(map[string]config.Rule)
  227. for _, ruleName := range rules {
  228. if rule, ok := cfg.Rules[ruleName]; ok {
  229. ruleOverride[ruleName] = rule
  230. } else {
  231. log.Fatal().Msgf("Requested rule %s not found in rules", ruleName)
  232. }
  233. }
  234. detector.Config.Rules = ruleOverride
  235. }
  236. // set follow symlinks flag
  237. if detector.FollowSymlinks, err = cmd.Flags().GetBool("follow-symlinks"); err != nil {
  238. log.Fatal().Err(err).Msg("")
  239. }
  240. return detector
  241. }
  242. func findingSummaryAndExit(findings []report.Finding, cmd *cobra.Command, cfg config.Config, exitCode int, start time.Time, err error) {
  243. if err == nil {
  244. log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start)))
  245. if len(findings) != 0 {
  246. log.Warn().Msgf("leaks found: %d", len(findings))
  247. } else {
  248. log.Info().Msg("no leaks found")
  249. }
  250. } else {
  251. log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start)))
  252. if len(findings) != 0 {
  253. log.Warn().Msgf("%d leaks found in partial scan", len(findings))
  254. } else {
  255. log.Warn().Msg("no leaks found in partial scan")
  256. }
  257. }
  258. // write report if desired
  259. reportPath, _ := cmd.Flags().GetString("report-path")
  260. ext, _ := cmd.Flags().GetString("report-format")
  261. if reportPath != "" {
  262. if err := report.Write(findings, cfg, ext, reportPath); err != nil {
  263. log.Fatal().Err(err).Msg("could not write")
  264. }
  265. }
  266. if err != nil {
  267. os.Exit(1)
  268. }
  269. if len(findings) != 0 {
  270. os.Exit(exitCode)
  271. }
  272. }
  273. func fileExists(fileName string) bool {
  274. // check for a .gitleaksignore file
  275. info, err := os.Stat(fileName)
  276. if err != nil && !os.IsNotExist(err) {
  277. return false
  278. }
  279. if info != nil && err == nil {
  280. if !info.IsDir() {
  281. return true
  282. }
  283. }
  284. return false
  285. }
  286. func FormatDuration(d time.Duration) string {
  287. scale := 100 * time.Second
  288. // look for the max scale that is smaller than d
  289. for scale > d {
  290. scale = scale / 10
  291. }
  292. return d.Round(scale / 100).String()
  293. }