root.go 9.9 KB

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