git_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package detect
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/git"
  11. "github.com/zricethezav/gitleaks/v8/report"
  12. )
  13. const repoBasePath = "../testdata/repos/"
  14. const expectPath = "../testdata/expected/"
  15. const configPath = "../testdata/config/"
  16. // TestFromGit tests the FromGit function
  17. func TestFromGit(t *testing.T) {
  18. tests := []struct {
  19. cfgName string
  20. opts Options
  21. source string
  22. logOpts string
  23. expected string
  24. expectedFindings []*report.Finding
  25. }{
  26. {
  27. source: filepath.Join(repoBasePath, "small"),
  28. expected: filepath.Join(expectPath, "git", "small.txt"),
  29. cfgName: "simple",
  30. expectedFindings: []*report.Finding{
  31. {
  32. Description: "AWS Access Key",
  33. StartLine: 20,
  34. EndLine: 20,
  35. StartColumn: 19,
  36. EndColumn: 38,
  37. Secret: "AKIALALEMEL33243OLIA",
  38. File: "main.go",
  39. // Line: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
  40. Date: "2021-11-02T23:37:53Z",
  41. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  42. Author: "Zachary Rice",
  43. Email: "zricer@protonmail.com",
  44. Message: "Accidentally add a secret",
  45. RuleID: "aws-access-key",
  46. Tags: []string{"key", "AWS"},
  47. },
  48. {
  49. Description: "AWS Access Key",
  50. StartLine: 9,
  51. EndLine: 9,
  52. StartColumn: 17,
  53. EndColumn: 36,
  54. Secret: "AKIALALEMEL33243OLIA",
  55. File: "foo/foo.go",
  56. // Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
  57. Date: "2021-11-02T23:48:06Z",
  58. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  59. Author: "Zach Rice",
  60. Email: "zricer@protonmail.com",
  61. Message: "adding foo package with secret",
  62. RuleID: "aws-access-key",
  63. Tags: []string{"key", "AWS"},
  64. },
  65. },
  66. },
  67. {
  68. source: filepath.Join(repoBasePath, "small"),
  69. expected: filepath.Join(expectPath, "git", "small-branch-foo.txt"),
  70. logOpts: "--all foo...",
  71. cfgName: "simple",
  72. expectedFindings: []*report.Finding{
  73. {
  74. Description: "AWS Access Key",
  75. StartLine: 9,
  76. EndLine: 9,
  77. StartColumn: 17,
  78. EndColumn: 36,
  79. Secret: "AKIALALEMEL33243OLIA",
  80. // Line: "\taws_token := \"AKIALALEMEL33243OLIA\"",
  81. Date: "2021-11-02T23:48:06Z",
  82. File: "foo/foo.go",
  83. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  84. Author: "Zach Rice",
  85. Email: "zricer@protonmail.com",
  86. Message: "adding foo package with secret",
  87. RuleID: "aws-access-key",
  88. Tags: []string{"key", "AWS"},
  89. },
  90. },
  91. },
  92. }
  93. err := moveDotGit("dotGit", ".git")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. defer moveDotGit(".git", "dotGit")
  98. for _, tt := range tests {
  99. files, err := git.GitLog(tt.source, tt.logOpts)
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. viper.AddConfigPath(configPath)
  104. viper.SetConfigName("simple")
  105. viper.SetConfigType("toml")
  106. err = viper.ReadInConfig()
  107. if err != nil {
  108. t.Error(err)
  109. }
  110. var vc config.ViperConfig
  111. viper.Unmarshal(&vc)
  112. cfg, _ := vc.Translate()
  113. findings := FromGit(files, cfg, tt.opts)
  114. for _, f := range findings {
  115. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  116. }
  117. assert.ElementsMatch(t, tt.expectedFindings, findings)
  118. }
  119. }
  120. func moveDotGit(from, to string) error {
  121. repoDirs, err := os.ReadDir("../testdata/repos")
  122. if err != nil {
  123. return err
  124. }
  125. for _, dir := range repoDirs {
  126. if to == ".git" {
  127. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  128. if os.IsNotExist(err) {
  129. // dont want to delete the only copy of .git accidentally
  130. continue
  131. }
  132. os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  133. }
  134. if !dir.IsDir() {
  135. continue
  136. }
  137. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  138. if os.IsNotExist(err) {
  139. continue
  140. }
  141. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  142. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  143. if err != nil {
  144. return err
  145. }
  146. }
  147. return nil
  148. }