audit_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. description: "test local repo four entropy alternative config",
  137. opts: options.Options{
  138. RepoPath: "../test_data/test_repos/test_repo_4",
  139. Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  140. RepoConfig: true,
  141. },
  142. wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  143. },
  144. }
  145. for _, test := range tests {
  146. fmt.Println(test.description)
  147. cfg, err := config.NewConfig(test.opts)
  148. if err != nil {
  149. t.Error(err)
  150. }
  151. m, err := manager.NewManager(test.opts, cfg)
  152. if err != nil {
  153. t.Error(err)
  154. }
  155. err = Run(m)
  156. if test.wantErr != nil {
  157. if err == nil {
  158. t.Errorf("did not receive wantErr: %v", test.wantErr)
  159. }
  160. if err.Error() != test.wantErr.Error() {
  161. t.Errorf("wantErr does not equal err received: %v", err.Error())
  162. }
  163. continue
  164. }
  165. // time.Sleep(time.Millisecond * 50)
  166. err = m.Report()
  167. if test.wantEmpty {
  168. if len(m.GetLeaks()) != 0 {
  169. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  170. }
  171. continue
  172. }
  173. if test.wantPath != "" {
  174. err := fileCheck(test.wantPath, test.opts.Report)
  175. if err != nil {
  176. t.Error(err)
  177. }
  178. }
  179. }
  180. }
  181. func TestAuditUncommited(t *testing.T) {
  182. moveDotGit("dotGit", ".git")
  183. defer moveDotGit(".git", "dotGit")
  184. tests := []struct {
  185. description string
  186. opts options.Options
  187. wantPath string
  188. wantErr error
  189. emptyRepo bool
  190. wantEmpty bool
  191. fileToChange string
  192. addition string
  193. }{
  194. {
  195. description: "test audit local one leak",
  196. opts: options.Options{
  197. RepoPath: "../test_data/test_repos/test_repo_1",
  198. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  199. Uncommited: true,
  200. },
  201. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  202. fileToChange: "server.test.py",
  203. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  204. },
  205. {
  206. description: "test audit local no leak",
  207. opts: options.Options{
  208. RepoPath: "../test_data/test_repos/test_repo_1",
  209. Uncommited: true,
  210. },
  211. wantEmpty: true,
  212. fileToChange: "server.test.py",
  213. addition: "nothing bad",
  214. },
  215. }
  216. for _, test := range tests {
  217. fmt.Println(test.description)
  218. old, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  219. if err != nil {
  220. t.Error(err)
  221. }
  222. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  223. os.O_WRONLY|os.O_APPEND, 0644)
  224. if err != nil {
  225. t.Error(err)
  226. }
  227. _, err = altered.WriteString(test.addition)
  228. if err != nil {
  229. t.Error(err)
  230. }
  231. cfg, err := config.NewConfig(test.opts)
  232. if err != nil {
  233. t.Error(err)
  234. }
  235. m, err := manager.NewManager(test.opts, cfg)
  236. if err != nil {
  237. t.Error(err)
  238. }
  239. if err := Run(m); err != nil {
  240. t.Error(err)
  241. }
  242. if err := m.Report(); err != nil {
  243. t.Error(err)
  244. }
  245. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  246. if err != nil {
  247. t.Error(err)
  248. }
  249. if test.wantEmpty {
  250. continue
  251. }
  252. if test.wantPath != "" {
  253. err := fileCheck(test.wantPath, test.opts.Report)
  254. if err != nil {
  255. t.Error(err)
  256. }
  257. }
  258. }
  259. }
  260. func fileCheck(wantPath, gotPath string) error {
  261. want, err := ioutil.ReadFile(wantPath)
  262. if err != nil {
  263. return err
  264. }
  265. got, err := ioutil.ReadFile(gotPath)
  266. if err != nil {
  267. return err
  268. }
  269. if strings.Trim(string(want), "\n") != strings.Trim(string(got), "\n") {
  270. dmp := diffmatchpatch.New()
  271. diffs := dmp.DiffMain(string(want), string(got), false)
  272. return fmt.Errorf("does not equal: %s\n", dmp.DiffPrettyText(diffs))
  273. } else {
  274. if err := os.Remove(gotPath); err != nil {
  275. return err
  276. }
  277. }
  278. return nil
  279. }
  280. func moveDotGit(from, to string) error {
  281. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  282. if err != nil {
  283. return err
  284. }
  285. for _, dir := range repoDirs {
  286. if !dir.IsDir() {
  287. continue
  288. }
  289. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  290. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  291. if err != nil {
  292. return err
  293. }
  294. }
  295. return nil
  296. }