audit_test.go 9.2 KB

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