detect.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // The `detect` and `protect` command is now deprecated. Here are some equivalent commands
  2. // to help guide you.
  3. // OLD CMD: gitleaks detect --source={repo}
  4. // NEW CMD: gitleaks git {repo}
  5. // OLD CMD: gitleaks protect --source={repo}
  6. // NEW CMD: gitleaks git --pre-commit {repo}
  7. // OLD CMD: gitleaks protect --staged --source={repo}
  8. // NEW CMD: gitleaks git --pre-commit --staged {repo}
  9. // OLD CMD: gitleaks detect --no-git --source={repo}
  10. // NEW CMD: gitleaks directory {directory/file}
  11. // OLD CMD: gitleaks detect --no-git --pipe
  12. // NEW CMD: gitleaks stdin
  13. package cmd
  14. import (
  15. "os"
  16. "time"
  17. "github.com/spf13/cobra"
  18. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  19. "github.com/zricethezav/gitleaks/v8/detect"
  20. "github.com/zricethezav/gitleaks/v8/logging"
  21. "github.com/zricethezav/gitleaks/v8/report"
  22. "github.com/zricethezav/gitleaks/v8/sources"
  23. )
  24. func init() {
  25. rootCmd.AddCommand(detectCmd)
  26. 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")
  27. detectCmd.Flags().Bool("pipe", false, "scan input from stdin, ex: `cat some_file | gitleaks detect --pipe`")
  28. detectCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
  29. detectCmd.Flags().StringP("source", "s", ".", "path to source")
  30. detectCmd.Flags().String("log-opts", "", "git log options")
  31. detectCmd.Flags().String("platform", "", "the target platform used to generate links (github, gitlab)")
  32. }
  33. var detectCmd = &cobra.Command{
  34. Use: "detect",
  35. Short: "detect secrets in code",
  36. Run: runDetect,
  37. Hidden: true,
  38. }
  39. func runDetect(cmd *cobra.Command, args []string) {
  40. // start timer
  41. start := time.Now()
  42. source := mustGetStringFlag(cmd, "source")
  43. // setup config (aka, the thing that defines rules)
  44. initConfig(source)
  45. cfg := Config(cmd)
  46. // create detector
  47. detector := Detector(cmd, cfg, source)
  48. // parse flags
  49. detector.FollowSymlinks = mustGetBoolFlag(cmd, "follow-symlinks")
  50. exitCode := mustGetIntFlag(cmd, "exit-code")
  51. noGit := mustGetBoolFlag(cmd, "no-git")
  52. fromPipe := mustGetBoolFlag(cmd, "pipe")
  53. // determine what type of scan:
  54. // - git: scan the history of the repo
  55. // - no-git: scan files by treating the repo as a plain directory
  56. var (
  57. findings []report.Finding
  58. err error
  59. )
  60. if noGit {
  61. paths, err := sources.DirectoryTargets(
  62. source,
  63. detector.Sema,
  64. detector.FollowSymlinks,
  65. detector.Config.Allowlist.PathAllowed,
  66. )
  67. if err != nil {
  68. logging.Fatal().Err(err).Send()
  69. }
  70. if findings, err = detector.DetectFiles(paths); err != nil {
  71. // don't exit on error, just log it
  72. logging.Error().Err(err).Msg("failed scan directory")
  73. }
  74. } else if fromPipe {
  75. if findings, err = detector.DetectReader(os.Stdin, 10); err != nil {
  76. // log fatal to exit, no need to continue since a report
  77. // will not be generated when scanning from a pipe...for now
  78. logging.Fatal().Err(err).Msg("failed scan input from stdin")
  79. }
  80. } else {
  81. var (
  82. logOpts = mustGetStringFlag(cmd, "log-opts")
  83. gitCmd *sources.GitCmd
  84. scmPlatform scm.Platform
  85. remote *detect.RemoteInfo
  86. )
  87. if gitCmd, err = sources.NewGitLogCmd(source, logOpts); err != nil {
  88. logging.Fatal().Err(err).Msg("could not create Git cmd")
  89. }
  90. if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
  91. logging.Fatal().Err(err).Send()
  92. }
  93. remote = detect.NewRemoteInfo(scmPlatform, source)
  94. if findings, err = detector.DetectGit(gitCmd, remote); err != nil {
  95. // don't exit on error, just log it
  96. logging.Error().Err(err).Msg("failed to scan Git repository")
  97. }
  98. }
  99. findingSummaryAndExit(detector, findings, exitCode, start, err)
  100. }