root.go 10 KB

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