protect.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package cmd
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. "github.com/rs/zerolog"
  7. "github.com/rs/zerolog/log"
  8. "github.com/spf13/cobra"
  9. "github.com/spf13/viper"
  10. "github.com/zricethezav/gitleaks/v8/config"
  11. "github.com/zricethezav/gitleaks/v8/detect"
  12. "github.com/zricethezav/gitleaks/v8/report"
  13. )
  14. func init() {
  15. protectCmd.Flags().Bool("staged", false, "detect secrets in a --staged state")
  16. protectCmd.Flags().String("log-opts", "", "git log options")
  17. protectCmd.Flags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
  18. rootCmd.AddCommand(protectCmd)
  19. }
  20. var protectCmd = &cobra.Command{
  21. Use: "protect",
  22. Short: "protect secrets in code",
  23. Run: runProtect,
  24. }
  25. func runProtect(cmd *cobra.Command, args []string) {
  26. initConfig()
  27. var vc config.ViperConfig
  28. if err := viper.Unmarshal(&vc); err != nil {
  29. log.Fatal().Err(err).Msg("Failed to load config")
  30. }
  31. cfg, err := vc.Translate()
  32. if err != nil {
  33. log.Fatal().Err(err).Msg("Failed to load config")
  34. }
  35. cfg.Path, _ = cmd.Flags().GetString("config")
  36. exitCode, _ := cmd.Flags().GetInt("exit-code")
  37. staged, _ := cmd.Flags().GetBool("staged")
  38. start := time.Now()
  39. // Setup detector
  40. detector := detect.NewDetector(cfg)
  41. // set color flag at first
  42. if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil {
  43. log.Fatal().Err(err).Msg("")
  44. }
  45. // also init logger again without color
  46. if detector.NoColor {
  47. log.Logger = log.Output(zerolog.ConsoleWriter{
  48. Out: os.Stderr,
  49. NoColor: detector.NoColor,
  50. })
  51. }
  52. detector.Config.Path, err = cmd.Flags().GetString("config")
  53. if err != nil {
  54. log.Fatal().Err(err).Msg("")
  55. }
  56. source, err := cmd.Flags().GetString("source")
  57. if err != nil {
  58. log.Fatal().Err(err).Msg("")
  59. }
  60. // if config path is not set, then use the {source}/.gitleaks.toml path.
  61. // note that there may not be a `{source}/.gitleaks.toml` file, this is ok.
  62. if detector.Config.Path == "" {
  63. detector.Config.Path = filepath.Join(source, ".gitleaks.toml")
  64. }
  65. // set verbose flag
  66. if detector.Verbose, err = cmd.Flags().GetBool("verbose"); err != nil {
  67. log.Fatal().Err(err).Msg("")
  68. }
  69. // set redact flag
  70. if detector.Redact, err = cmd.Flags().GetUint("redact"); err != nil {
  71. log.Fatal().Err(err).Msg("")
  72. }
  73. if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil {
  74. log.Fatal().Err(err).Msg("")
  75. }
  76. // set ignore gitleaks:allow flag
  77. if detector.IgnoreGitleaksAllow, err = cmd.Flags().GetBool("ignore-gitleaks-allow"); err != nil {
  78. log.Fatal().Err(err).Msg("")
  79. }
  80. gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path")
  81. if err != nil {
  82. log.Fatal().Err(err).Msg("could not get .gitleaksignore path")
  83. }
  84. if fileExists(gitleaksIgnorePath) {
  85. if err = detector.AddGitleaksIgnore(gitleaksIgnorePath); err != nil {
  86. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  87. }
  88. }
  89. if fileExists(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")) {
  90. if err = detector.AddGitleaksIgnore(filepath.Join(gitleaksIgnorePath, ".gitleaksignore")); err != nil {
  91. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  92. }
  93. }
  94. if fileExists(filepath.Join(source, ".gitleaksignore")) {
  95. if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {
  96. log.Fatal().Err(err).Msg("could not call AddGitleaksIgnore")
  97. }
  98. }
  99. // get log options for git scan
  100. logOpts, err := cmd.Flags().GetString("log-opts")
  101. if err != nil {
  102. log.Fatal().Err(err).Msg("")
  103. }
  104. // start git scan
  105. var findings []report.Finding
  106. if staged {
  107. findings, err = detector.DetectGit(source, logOpts, detect.ProtectStagedType)
  108. } else {
  109. findings, err = detector.DetectGit(source, logOpts, detect.ProtectType)
  110. }
  111. if err != nil {
  112. // don't exit on error, just log it
  113. log.Error().Err(err).Msg("")
  114. }
  115. // log info about the scan
  116. log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start)))
  117. if len(findings) != 0 {
  118. log.Warn().Msgf("leaks found: %d", len(findings))
  119. } else {
  120. log.Info().Msg("no leaks found")
  121. }
  122. reportPath, _ := cmd.Flags().GetString("report-path")
  123. ext, _ := cmd.Flags().GetString("report-format")
  124. if reportPath != "" {
  125. if err = report.Write(findings, cfg, ext, reportPath); err != nil {
  126. log.Fatal().Err(err).Msg("")
  127. }
  128. }
  129. if len(findings) != 0 {
  130. os.Exit(exitCode)
  131. }
  132. }