detect.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package cmd
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. "github.com/rs/zerolog/log"
  7. "github.com/spf13/cobra"
  8. "github.com/spf13/viper"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/detect"
  11. "github.com/zricethezav/gitleaks/v8/report"
  12. )
  13. func init() {
  14. rootCmd.AddCommand(detectCmd)
  15. detectCmd.Flags().String("log-opts", "", "git log options")
  16. detectCmd.Flags().Bool("no-git", false, "treat git repo as a regular directory and scan those files, --log-opts has no effect on the scan when --no-git is set")
  17. detectCmd.Flags().Bool("pipe", false, "scan input from stdin, ex: `cat some_file | gitleaks detect --pipe`")
  18. detectCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
  19. detectCmd.Flags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
  20. }
  21. var detectCmd = &cobra.Command{
  22. Use: "detect",
  23. Short: "detect secrets in code",
  24. Run: runDetect,
  25. }
  26. func runDetect(cmd *cobra.Command, args []string) {
  27. initConfig()
  28. var (
  29. vc config.ViperConfig
  30. findings []report.Finding
  31. err error
  32. )
  33. // Load config
  34. if err = viper.Unmarshal(&vc); err != nil {
  35. log.Fatal().Err(err).Msg("Failed to load config")
  36. }
  37. cfg, err := vc.Translate()
  38. if err != nil {
  39. log.Fatal().Err(err).Msg("Failed to load config")
  40. }
  41. cfg.Path, _ = cmd.Flags().GetString("config")
  42. // start timer
  43. start := time.Now()
  44. // Setup detector
  45. detector := detect.NewDetector(cfg)
  46. detector.Config.Path, err = cmd.Flags().GetString("config")
  47. if err != nil {
  48. log.Fatal().Err(err).Msg("")
  49. }
  50. source, err := cmd.Flags().GetString("source")
  51. if err != nil {
  52. log.Fatal().Err(err).Msg("")
  53. }
  54. // if config path is not set, then use the {source}/.gitleaks.toml path.
  55. // note that there may not be a `{source}/.gitleaks.toml` file, this is ok.
  56. if detector.Config.Path == "" {
  57. detector.Config.Path = filepath.Join(source, ".gitleaks.toml")
  58. }
  59. // set verbose flag
  60. if detector.Verbose, err = cmd.Flags().GetBool("verbose"); err != nil {
  61. log.Fatal().Err(err).Msg("")
  62. }
  63. // set redact flag
  64. if detector.Redact, err = cmd.Flags().GetBool("redact"); err != nil {
  65. log.Fatal().Err(err).Msg("")
  66. }
  67. if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil {
  68. log.Fatal().Err(err).Msg("")
  69. }
  70. // set color flag
  71. if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil {
  72. log.Fatal().Err(err).Msg("")
  73. }
  74. gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path")
  75. if err != nil {
  76. log.Fatal().Err(err).Msg("could not get .gitleaksignore path")
  77. }
  78. if fileExists(gitleaksIgnorePath) {
  79. if err = detector.AddGitleaksIgnore(gitleaksIgnorePath); err != nil {
  80. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  81. }
  82. }
  83. if fileExists(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")) {
  84. if err = detector.AddGitleaksIgnore(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")); err != nil {
  85. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  86. }
  87. }
  88. if fileExists(filepath.Join(source, ".gitleaksignore")) {
  89. if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {
  90. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  91. }
  92. }
  93. // ignore findings from the baseline (an existing report in json format generated earlier)
  94. baselinePath, _ := cmd.Flags().GetString("baseline-path")
  95. if baselinePath != "" {
  96. err = detector.AddBaseline(baselinePath, source)
  97. if err != nil {
  98. log.Error().Msgf("Could not load baseline. The path must point of a gitleaks report generated using the default format: %s", err)
  99. }
  100. }
  101. // set follow symlinks flag
  102. if detector.FollowSymlinks, err = cmd.Flags().GetBool("follow-symlinks"); err != nil {
  103. log.Fatal().Err(err).Msg("")
  104. }
  105. // set exit code
  106. exitCode, err := cmd.Flags().GetInt("exit-code")
  107. if err != nil {
  108. log.Fatal().Err(err).Msg("could not get exit code")
  109. }
  110. // determine what type of scan:
  111. // - git: scan the history of the repo
  112. // - no-git: scan files by treating the repo as a plain directory
  113. noGit, err := cmd.Flags().GetBool("no-git")
  114. if err != nil {
  115. log.Fatal().Err(err).Msg("could not call GetBool() for no-git")
  116. }
  117. fromPipe, err := cmd.Flags().GetBool("pipe")
  118. if err != nil {
  119. log.Fatal().Err(err)
  120. }
  121. // start the detector scan
  122. if noGit {
  123. findings, err = detector.DetectFiles(source)
  124. if err != nil {
  125. // don't exit on error, just log it
  126. log.Error().Err(err).Msg("")
  127. }
  128. } else if fromPipe {
  129. findings, err = detector.DetectReader(os.Stdin, 10)
  130. if err != nil {
  131. // log fatal to exit, no need to continue since a report
  132. // will not be generated when scanning from a pipe...for now
  133. log.Fatal().Err(err).Msg("")
  134. }
  135. } else {
  136. var logOpts string
  137. logOpts, err = cmd.Flags().GetString("log-opts")
  138. if err != nil {
  139. log.Fatal().Err(err).Msg("")
  140. }
  141. findings, err = detector.DetectGit(source, logOpts, detect.DetectType)
  142. if err != nil {
  143. // don't exit on error, just log it
  144. log.Error().Err(err).Msg("")
  145. }
  146. }
  147. // log info about the scan
  148. if err == nil {
  149. log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start)))
  150. if len(findings) != 0 {
  151. log.Warn().Msgf("leaks found: %d", len(findings))
  152. } else {
  153. log.Info().Msg("no leaks found")
  154. }
  155. } else {
  156. log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start)))
  157. if len(findings) != 0 {
  158. log.Warn().Msgf("%d leaks found in partial scan", len(findings))
  159. } else {
  160. log.Warn().Msg("no leaks found in partial scan")
  161. }
  162. }
  163. // write report if desired
  164. reportPath, _ := cmd.Flags().GetString("report-path")
  165. ext, _ := cmd.Flags().GetString("report-format")
  166. if reportPath != "" {
  167. if err := report.Write(findings, cfg, ext, reportPath); err != nil {
  168. log.Fatal().Err(err).Msg("could not write")
  169. }
  170. }
  171. if err != nil {
  172. os.Exit(1)
  173. }
  174. if len(findings) != 0 {
  175. os.Exit(exitCode)
  176. }
  177. }
  178. func fileExists(fileName string) bool {
  179. // check for a .gitleaksignore file
  180. info, err := os.Stat(fileName)
  181. if err != nil && !os.IsNotExist(err) {
  182. return false
  183. }
  184. if info != nil && err == nil {
  185. if !info.IsDir() {
  186. return true
  187. }
  188. }
  189. return false
  190. }
  191. func FormatDuration(d time.Duration) string {
  192. scale := 100 * time.Second
  193. // look for the max scale that is smaller than d
  194. for scale > d {
  195. scale = scale / 10
  196. }
  197. return d.Round(scale / 100).String()
  198. }