audit_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package audit
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "reflect"
  8. "runtime"
  9. "sort"
  10. "testing"
  11. "github.com/zricethezav/gitleaks/v3/config"
  12. "github.com/zricethezav/gitleaks/v3/manager"
  13. "github.com/zricethezav/gitleaks/v3/options"
  14. "github.com/sergi/go-diff/diffmatchpatch"
  15. )
  16. const testRepoBase = "../test_data/test_repos/"
  17. func TestAudit(t *testing.T) {
  18. moveDotGit("dotGit", ".git")
  19. defer moveDotGit(".git", "dotGit")
  20. tests := []struct {
  21. description string
  22. opts options.Options
  23. wantPath string
  24. wantErr error
  25. emptyRepo bool
  26. wantEmpty bool
  27. }{
  28. {
  29. description: "test local repo one aws leak",
  30. opts: options.Options{
  31. RepoPath: "../test_data/test_repos/test_repo_1",
  32. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  33. ReportFormat: "json",
  34. },
  35. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  36. },
  37. {
  38. description: "test local repo one aws leak threaded",
  39. opts: options.Options{
  40. Threads: runtime.GOMAXPROCS(0),
  41. RepoPath: "../test_data/test_repos/test_repo_1",
  42. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  43. ReportFormat: "json",
  44. },
  45. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  46. },
  47. {
  48. description: "test non existent repo",
  49. opts: options.Options{
  50. RepoPath: "../test_data/test_repos/no_repo_here",
  51. ReportFormat: "json",
  52. },
  53. emptyRepo: true,
  54. },
  55. {
  56. description: "test local repo one aws leak whitelisted",
  57. opts: options.Options{
  58. RepoPath: "../test_data/test_repos/test_repo_1",
  59. ReportFormat: "json",
  60. Config: "../test_data/test_configs/aws_key_whitelist_python_files.toml",
  61. },
  62. wantEmpty: true,
  63. },
  64. {
  65. description: "test local repo two leaks",
  66. opts: options.Options{
  67. RepoPath: "../test_data/test_repos/test_repo_2",
  68. Report: "../test_data/test_local_repo_two_leaks.json.got",
  69. ReportFormat: "json",
  70. },
  71. wantPath: "../test_data/test_local_repo_two_leaks.json",
  72. },
  73. {
  74. description: "test local repo two leaks from commit",
  75. opts: options.Options{
  76. RepoPath: "../test_data/test_repos/test_repo_2",
  77. Report: "../test_data/test_local_repo_two_leaks_commit_from.json.got",
  78. ReportFormat: "json",
  79. CommitFrom: "996865bb912f3bc45898a370a13aadb315014b55",
  80. },
  81. wantPath: "../test_data/test_local_repo_two_leaks_commit_from.json",
  82. },
  83. {
  84. description: "test local repo two leaks to commit",
  85. opts: options.Options{
  86. RepoPath: "../test_data/test_repos/test_repo_2",
  87. Report: "../test_data/test_local_repo_two_leaks_commit_to.json.got",
  88. ReportFormat: "json",
  89. CommitTo: "996865bb912f3bc45898a370a13aadb315014b55",
  90. },
  91. wantPath: "../test_data/test_local_repo_two_leaks_commit_to.json",
  92. },
  93. {
  94. description: "test local repo two leaks range commit",
  95. opts: options.Options{
  96. RepoPath: "../test_data/test_repos/test_repo_2",
  97. Report: "../test_data/test_local_repo_two_leaks_commit_range.json.got",
  98. ReportFormat: "json",
  99. CommitFrom: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a",
  100. CommitTo: "51f6dcf6b89b93f4075ba92c400b075631a6cc93",
  101. },
  102. wantPath: "../test_data/test_local_repo_two_leaks_commit_range.json",
  103. },
  104. {
  105. description: "test local repo two leaks globally whitelisted",
  106. opts: options.Options{
  107. RepoPath: "../test_data/test_repos/test_repo_2",
  108. Config: "../test_data/test_configs/aws_key_global_whitelist_file.toml",
  109. ReportFormat: "json",
  110. },
  111. wantEmpty: true,
  112. },
  113. {
  114. description: "test local repo two leaks whitelisted",
  115. opts: options.Options{
  116. RepoPath: "../test_data/test_repos/test_repo_2",
  117. Config: "../test_data/test_configs/aws_key_whitelist_files.toml",
  118. ReportFormat: "json",
  119. },
  120. wantEmpty: true,
  121. },
  122. {
  123. description: "test local repo three leaks dev branch",
  124. opts: options.Options{
  125. RepoPath: "../test_data/test_repos/test_repo_3",
  126. Report: "../test_data/test_local_repo_three_leaks.json.got",
  127. Config: "../test_data/test_configs/aws_key.toml",
  128. Branch: "dev",
  129. ReportFormat: "json",
  130. },
  131. wantPath: "../test_data/test_local_repo_three_leaks.json",
  132. },
  133. {
  134. description: "test local repo branch does not exist",
  135. opts: options.Options{
  136. RepoPath: "../test_data/test_repos/test_repo_3",
  137. Branch: "nobranch",
  138. ReportFormat: "json",
  139. },
  140. wantEmpty: true,
  141. },
  142. {
  143. // TODO UPDATE TESTS
  144. description: "test local repo one aws leak single commit",
  145. opts: options.Options{
  146. RepoPath: "../test_data/test_repos/test_repo_1",
  147. Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
  148. FilesAtCommit: "6557c92612d3b35979bd426d429255b3bf9fab74",
  149. ReportFormat: "json",
  150. },
  151. wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
  152. },
  153. {
  154. description: "test local repo one aws leak AND leak on python files",
  155. opts: options.Options{
  156. RepoPath: "../test_data/test_repos/test_repo_1",
  157. Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
  158. Config: "../test_data/test_configs/aws_key_file_regex.toml",
  159. ReportFormat: "json",
  160. },
  161. wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
  162. },
  163. {
  164. description: "test owner path",
  165. opts: options.Options{
  166. OwnerPath: "../test_data/test_repos/",
  167. Report: "../test_data/test_local_owner_aws_leak.json.got",
  168. ReportFormat: "json",
  169. },
  170. wantPath: "../test_data/test_local_owner_aws_leak.json",
  171. },
  172. {
  173. description: "test entropy",
  174. opts: options.Options{
  175. RepoPath: "../test_data/test_repos/test_repo_1",
  176. Report: "../test_data/test_entropy.json.got",
  177. Config: "../test_data/test_configs/entropy.toml",
  178. ReportFormat: "json",
  179. },
  180. wantPath: "../test_data/test_entropy.json",
  181. },
  182. {
  183. description: "test entropy and regex",
  184. opts: options.Options{
  185. RepoPath: "../test_data/test_repos/test_repo_1",
  186. Report: "../test_data/test_regex_entropy.json.got",
  187. Config: "../test_data/test_configs/regex_entropy.toml",
  188. ReportFormat: "json",
  189. },
  190. wantPath: "../test_data/test_regex_entropy.json",
  191. },
  192. {
  193. description: "test local repo four entropy alternative config",
  194. opts: options.Options{
  195. RepoPath: "../test_data/test_repos/test_repo_4",
  196. Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  197. RepoConfig: true,
  198. ReportFormat: "json",
  199. },
  200. wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json",
  201. },
  202. {
  203. description: "test local repo four entropy alternative config",
  204. opts: options.Options{
  205. RepoPath: "../test_data/test_repos/test_repo_1",
  206. Report: "../test_data/test_regex_whitelist.json.got",
  207. Config: "../test_data/test_configs/aws_key_aws_whitelisted.toml",
  208. ReportFormat: "json",
  209. },
  210. wantEmpty: true,
  211. },
  212. {
  213. description: "test local repo one aws leak timeout",
  214. opts: options.Options{
  215. RepoPath: "../test_data/test_repos/test_repo_1",
  216. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  217. ReportFormat: "json",
  218. Timeout: "10ns",
  219. },
  220. wantEmpty: true,
  221. },
  222. {
  223. description: "test local repo one aws leak long timeout",
  224. opts: options.Options{
  225. RepoPath: "../test_data/test_repos/test_repo_1",
  226. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  227. ReportFormat: "json",
  228. Timeout: "2m",
  229. },
  230. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  231. },
  232. {
  233. description: "test owner path depth=2",
  234. opts: options.Options{
  235. OwnerPath: "../test_data/test_repos/",
  236. Report: "../test_data/test_local_owner_aws_leak_depth_2.json.got",
  237. ReportFormat: "json",
  238. Depth: 2,
  239. },
  240. wantPath: "../test_data/test_local_owner_aws_leak_depth_2.json",
  241. },
  242. }
  243. for _, test := range tests {
  244. fmt.Println(test.description)
  245. cfg, err := config.NewConfig(test.opts)
  246. if err != nil {
  247. t.Error(err)
  248. }
  249. m, err := manager.NewManager(test.opts, cfg)
  250. if err != nil {
  251. t.Error(err)
  252. }
  253. err = Run(m)
  254. if test.wantErr != nil {
  255. if err == nil {
  256. t.Errorf("did not receive wantErr: %v", test.wantErr)
  257. }
  258. if err.Error() != test.wantErr.Error() {
  259. t.Errorf("wantErr does not equal err received: %v", err.Error())
  260. }
  261. continue
  262. }
  263. err = m.Report()
  264. if test.wantEmpty {
  265. if len(m.GetLeaks()) != 0 {
  266. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  267. }
  268. continue
  269. }
  270. if test.wantPath != "" {
  271. err := fileCheck(test.wantPath, test.opts.Report)
  272. if err != nil {
  273. t.Error(err)
  274. }
  275. }
  276. }
  277. }
  278. func TestAuditUncommited(t *testing.T) {
  279. moveDotGit("dotGit", ".git")
  280. defer moveDotGit(".git", "dotGit")
  281. tests := []struct {
  282. description string
  283. opts options.Options
  284. wantPath string
  285. wantErr error
  286. emptyRepo bool
  287. wantEmpty bool
  288. fileToChange string
  289. addition string
  290. }{
  291. {
  292. description: "test audit local one leak",
  293. opts: options.Options{
  294. RepoPath: "../test_data/test_repos/test_repo_1",
  295. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  296. Uncommited: true,
  297. ReportFormat: "json",
  298. },
  299. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  300. fileToChange: "server.test.py",
  301. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  302. },
  303. {
  304. description: "test audit local no leak",
  305. opts: options.Options{
  306. RepoPath: "../test_data/test_repos/test_repo_1",
  307. Uncommited: true,
  308. ReportFormat: "json",
  309. },
  310. wantEmpty: true,
  311. fileToChange: "server.test.py",
  312. addition: "nothing bad",
  313. },
  314. }
  315. for _, test := range tests {
  316. fmt.Println(test.description)
  317. old, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  318. if err != nil {
  319. t.Error(err)
  320. }
  321. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  322. os.O_WRONLY|os.O_APPEND, 0644)
  323. if err != nil {
  324. t.Error(err)
  325. }
  326. _, err = altered.WriteString(test.addition)
  327. if err != nil {
  328. t.Error(err)
  329. }
  330. cfg, err := config.NewConfig(test.opts)
  331. if err != nil {
  332. t.Error(err)
  333. }
  334. m, err := manager.NewManager(test.opts, cfg)
  335. if err != nil {
  336. t.Error(err)
  337. }
  338. if err := Run(m); err != nil {
  339. t.Error(err)
  340. }
  341. if err := m.Report(); err != nil {
  342. t.Error(err)
  343. }
  344. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  345. if err != nil {
  346. t.Error(err)
  347. }
  348. if test.wantEmpty {
  349. continue
  350. }
  351. if test.wantPath != "" {
  352. err := fileCheck(test.wantPath, test.opts.Report)
  353. if err != nil {
  354. t.Error(err)
  355. }
  356. }
  357. }
  358. }
  359. func fileCheck(wantPath, gotPath string) error {
  360. var (
  361. gotLeaks []manager.Leak
  362. wantLeaks []manager.Leak
  363. )
  364. want, err := ioutil.ReadFile(wantPath)
  365. if err != nil {
  366. return err
  367. }
  368. got, err := ioutil.ReadFile(gotPath)
  369. if err != nil {
  370. return err
  371. }
  372. err = json.Unmarshal(got, &gotLeaks)
  373. if err != nil {
  374. return err
  375. }
  376. err = json.Unmarshal(want, &wantLeaks)
  377. if err != nil {
  378. return nil
  379. }
  380. sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit })
  381. sort.Slice(wantLeaks, func(i, j int) bool { return (wantLeaks)[i].Commit < (wantLeaks)[j].Commit })
  382. if !reflect.DeepEqual(gotLeaks, wantLeaks) {
  383. dmp := diffmatchpatch.New()
  384. diffs := dmp.DiffMain(string(want), string(got), false)
  385. return fmt.Errorf("does not equal: %s", dmp.DiffPrettyText(diffs))
  386. }
  387. if err := os.Remove(gotPath); err != nil {
  388. return err
  389. }
  390. return nil
  391. }
  392. func moveDotGit(from, to string) error {
  393. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  394. if err != nil {
  395. return err
  396. }
  397. for _, dir := range repoDirs {
  398. if !dir.IsDir() {
  399. continue
  400. }
  401. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  402. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  403. if err != nil {
  404. return err
  405. }
  406. }
  407. return nil
  408. }