detect.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. initDiagnostics()
  46. cfg := Config(cmd)
  47. // create detector
  48. detector := Detector(cmd, cfg, source)
  49. // parse flags
  50. detector.FollowSymlinks = mustGetBoolFlag(cmd, "follow-symlinks")
  51. exitCode := mustGetIntFlag(cmd, "exit-code")
  52. noGit := mustGetBoolFlag(cmd, "no-git")
  53. fromPipe := mustGetBoolFlag(cmd, "pipe")
  54. // determine what type of scan:
  55. // - git: scan the history of the repo
  56. // - no-git: scan files by treating the repo as a plain directory
  57. var (
  58. findings []report.Finding
  59. err error
  60. )
  61. if noGit {
  62. paths, err := sources.DirectoryTargets(
  63. source,
  64. detector.Sema,
  65. detector.FollowSymlinks,
  66. detector.Config.Allowlists,
  67. )
  68. if err != nil {
  69. logging.Fatal().Err(err).Send()
  70. }
  71. if findings, err = detector.DetectFiles(paths); err != nil {
  72. // don't exit on error, just log it
  73. logging.Error().Err(err).Msg("failed scan directory")
  74. }
  75. } else if fromPipe {
  76. if findings, err = detector.DetectReader(os.Stdin, 10); err != nil {
  77. // log fatal to exit, no need to continue since a report
  78. // will not be generated when scanning from a pipe...for now
  79. logging.Fatal().Err(err).Msg("failed scan input from stdin")
  80. }
  81. } else {
  82. var (
  83. logOpts = mustGetStringFlag(cmd, "log-opts")
  84. gitCmd *sources.GitCmd
  85. scmPlatform scm.Platform
  86. remote *detect.RemoteInfo
  87. )
  88. if gitCmd, err = sources.NewGitLogCmd(source, logOpts); err != nil {
  89. logging.Fatal().Err(err).Msg("could not create Git cmd")
  90. }
  91. if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
  92. logging.Fatal().Err(err).Send()
  93. }
  94. remote = detect.NewRemoteInfo(scmPlatform, source)
  95. if findings, err = detector.DetectGit(gitCmd, remote); err != nil {
  96. // don't exit on error, just log it
  97. logging.Error().Err(err).Msg("failed to scan Git repository")
  98. }
  99. }
  100. findingSummaryAndExit(detector, findings, exitCode, start, err)
  101. }