audit_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package audit
  2. import (
  3. "fmt"
  4. "github.com/sergi/go-diff/diffmatchpatch"
  5. "github.com/zricethezav/gitleaks/config"
  6. "github.com/zricethezav/gitleaks/manager"
  7. "github.com/zricethezav/gitleaks/options"
  8. "io/ioutil"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. )
  14. const testRepoBase = "../test_data/test_repos/"
  15. func TestAudit(t *testing.T) {
  16. moveDotGit("dotGit", ".git")
  17. defer moveDotGit(".git", "dotGit")
  18. tests := []struct {
  19. description string
  20. opts options.Options
  21. wantPath string
  22. wantErr error
  23. emptyRepo bool
  24. wantEmpty bool
  25. }{
  26. {
  27. description: "test local repo one aws leak",
  28. opts: options.Options{
  29. RepoPath: "../test_data/test_repos/test_repo_1",
  30. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  31. },
  32. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  33. },
  34. {
  35. description: "test local repo one aws leak threaded",
  36. opts: options.Options{
  37. Threads: runtime.GOMAXPROCS(0),
  38. RepoPath: "../test_data/test_repos/test_repo_1",
  39. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  40. },
  41. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  42. },
  43. {
  44. description: "test non existent repo",
  45. opts: options.Options{
  46. RepoPath: "../test_data/test_repos/no_repo_here",
  47. },
  48. emptyRepo: true,
  49. },
  50. {
  51. description: "test local repo one aws leak whitelisted",
  52. opts: options.Options{
  53. RepoPath: "../test_data/test_repos/test_repo_1",
  54. Config: "../test_data/test_configs/aws_key_whitelist_python_files.toml",
  55. },
  56. wantEmpty: true,
  57. },
  58. {
  59. description: "test local repo two leaks",
  60. opts: options.Options{
  61. RepoPath: "../test_data/test_repos/test_repo_2",
  62. Report: "../test_data/test_local_repo_two_leaks.json.got",
  63. },
  64. wantPath: "../test_data/test_local_repo_two_leaks.json",
  65. },
  66. {
  67. description: "test local repo two leaks globally whitelisted",
  68. opts: options.Options{
  69. RepoPath: "../test_data/test_repos/test_repo_2",
  70. Config: "../test_data/test_configs/aws_key_global_whitelist_file.toml",
  71. },
  72. wantEmpty: true,
  73. },
  74. {
  75. description: "test local repo two leaks whitelisted",
  76. opts: options.Options{
  77. RepoPath: "../test_data/test_repos/test_repo_2",
  78. Config: "../test_data/test_configs/aws_key_whitelist_files.toml",
  79. },
  80. wantEmpty: true,
  81. },
  82. {
  83. description: "test local repo three leaks dev branch",
  84. opts: options.Options{
  85. RepoPath: "../test_data/test_repos/test_repo_3",
  86. Report: "../test_data/test_local_repo_three_leaks.json.got",
  87. Config: "../test_data/test_configs/aws_key.toml",
  88. Branch: "dev",
  89. },
  90. wantPath: "../test_data/test_local_repo_three_leaks.json",
  91. },
  92. {
  93. description: "test local repo branch does not exist",
  94. opts: options.Options{
  95. RepoPath: "../test_data/test_repos/test_repo_3",
  96. Branch: "nobranch",
  97. },
  98. wantEmpty: true,
  99. },
  100. {
  101. description: "test local repo one aws leak single commit",
  102. opts: options.Options{
  103. RepoPath: "../test_data/test_repos/test_repo_1",
  104. Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
  105. Commit: "6557c92612d3b35979bd426d429255b3bf9fab74",
  106. },
  107. wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
  108. },
  109. {
  110. description: "test local repo one aws leak AND leak on python files",
  111. opts: options.Options{
  112. RepoPath: "../test_data/test_repos/test_repo_1",
  113. Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
  114. Config: "../test_data/test_configs/aws_key_file_regex.toml",
  115. },
  116. wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
  117. },
  118. {
  119. description: "test owner path",
  120. opts: options.Options{
  121. OwnerPath: "../test_data/test_repos/",
  122. Report: "../test_data/test_local_owner_aws_leak.json.got",
  123. },
  124. wantPath: "../test_data/test_local_owner_aws_leak.json",
  125. },
  126. {
  127. description: "test entropy",
  128. opts: options.Options{
  129. RepoPath: "../test_data/test_repos/test_repo_1",
  130. Report: "../test_data/test_entropy.json.got",
  131. Config: "../test_data/test_configs/entropy.toml",
  132. },
  133. wantPath: "../test_data/test_entropy.json",
  134. },
  135. }
  136. for _, test := range tests {
  137. fmt.Println(test.description)
  138. cfg, err := config.NewConfig(test.opts)
  139. if err != nil {
  140. t.Error(err)
  141. }
  142. m, err := manager.NewManager(test.opts, cfg)
  143. if err != nil {
  144. t.Error(err)
  145. }
  146. err = Run(m)
  147. if test.wantErr != nil {
  148. if err == nil {
  149. t.Errorf("did not receive wantErr: %v", test.wantErr)
  150. }
  151. if err.Error() != test.wantErr.Error() {
  152. t.Errorf("wantErr does not equal err received: %v", err.Error())
  153. }
  154. continue
  155. }
  156. // time.Sleep(time.Millisecond * 50)
  157. err = m.Report()
  158. if test.wantEmpty {
  159. if len(m.GetLeaks()) != 0 {
  160. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  161. }
  162. continue
  163. }
  164. if test.wantPath != "" {
  165. err := fileCheck(test.wantPath, test.opts.Report)
  166. if err != nil {
  167. t.Error(err)
  168. }
  169. }
  170. }
  171. }
  172. func TestAuditUncommited(t *testing.T) {
  173. moveDotGit("dotGit", ".git")
  174. defer moveDotGit(".git", "dotGit")
  175. tests := []struct {
  176. description string
  177. opts options.Options
  178. wantPath string
  179. wantErr error
  180. emptyRepo bool
  181. wantEmpty bool
  182. fileToChange string
  183. addition string
  184. }{
  185. {
  186. description: "test audit local one leak",
  187. opts: options.Options{
  188. RepoPath: "../test_data/test_repos/test_repo_1",
  189. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  190. Uncommited: true,
  191. },
  192. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  193. fileToChange: "server.test.py",
  194. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  195. },
  196. {
  197. description: "test audit local no leak",
  198. opts: options.Options{
  199. RepoPath: "../test_data/test_repos/test_repo_1",
  200. Uncommited: true,
  201. },
  202. wantEmpty: true,
  203. fileToChange: "server.test.py",
  204. addition: "nothing bad",
  205. },
  206. }
  207. for _, test := range tests {
  208. fmt.Println(test.description)
  209. old, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  210. if err != nil {
  211. t.Error(err)
  212. }
  213. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  214. os.O_WRONLY|os.O_APPEND, 0644)
  215. if err != nil {
  216. t.Error(err)
  217. }
  218. _, err = altered.WriteString(test.addition)
  219. if err != nil {
  220. t.Error(err)
  221. }
  222. cfg, err := config.NewConfig(test.opts)
  223. if err != nil {
  224. t.Error(err)
  225. }
  226. m, err := manager.NewManager(test.opts, cfg)
  227. if err != nil {
  228. t.Error(err)
  229. }
  230. if err := Run(m); err != nil {
  231. t.Error(err)
  232. }
  233. if err := m.Report(); err != nil {
  234. t.Error(err)
  235. }
  236. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. if test.wantEmpty {
  241. continue
  242. }
  243. if test.wantPath != "" {
  244. err := fileCheck(test.wantPath, test.opts.Report)
  245. if err != nil {
  246. t.Error(err)
  247. }
  248. }
  249. }
  250. }
  251. func fileCheck(wantPath, gotPath string) error {
  252. want, err := ioutil.ReadFile(wantPath)
  253. if err != nil {
  254. return err
  255. }
  256. got, err := ioutil.ReadFile(gotPath)
  257. if err != nil {
  258. return err
  259. }
  260. if strings.Trim(string(want), "\n") != strings.Trim(string(got), "\n") {
  261. dmp := diffmatchpatch.New()
  262. diffs := dmp.DiffMain(string(want), string(got), false)
  263. return fmt.Errorf("does not equal: %s\n", dmp.DiffPrettyText(diffs))
  264. } else {
  265. if err := os.Remove(gotPath); err != nil {
  266. return err
  267. }
  268. }
  269. return nil
  270. }
  271. func moveDotGit(from, to string) error {
  272. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  273. if err != nil {
  274. return err
  275. }
  276. for _, dir := range repoDirs {
  277. if !dir.IsDir() {
  278. continue
  279. }
  280. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  281. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  282. if err != nil {
  283. return err
  284. }
  285. }
  286. return nil
  287. }