audit_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/config"
  12. "github.com/zricethezav/gitleaks/manager"
  13. "github.com/zricethezav/gitleaks/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. description: "test local repo one aws leak single commit",
  144. opts: options.Options{
  145. RepoPath: "../test_data/test_repos/test_repo_1",
  146. Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
  147. Commit: "6557c92612d3b35979bd426d429255b3bf9fab74",
  148. ReportFormat: "json",
  149. },
  150. wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
  151. },
  152. {
  153. description: "test local repo one aws leak AND leak on python files",
  154. opts: options.Options{
  155. RepoPath: "../test_data/test_repos/test_repo_1",
  156. Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
  157. Config: "../test_data/test_configs/aws_key_file_regex.toml",
  158. ReportFormat: "json",
  159. },
  160. wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
  161. },
  162. {
  163. description: "test owner path",
  164. opts: options.Options{
  165. OwnerPath: "../test_data/test_repos/",
  166. Report: "../test_data/test_local_owner_aws_leak.json.got",
  167. ReportFormat: "json",
  168. },
  169. wantPath: "../test_data/test_local_owner_aws_leak.json",
  170. },
  171. {
  172. description: "test entropy",
  173. opts: options.Options{
  174. RepoPath: "../test_data/test_repos/test_repo_1",
  175. Report: "../test_data/test_entropy.json.got",
  176. Config: "../test_data/test_configs/entropy.toml",
  177. ReportFormat: "json",
  178. },
  179. wantPath: "../test_data/test_entropy.json",
  180. },
  181. {
  182. description: "test entropy and regex",
  183. opts: options.Options{
  184. RepoPath: "../test_data/test_repos/test_repo_1",
  185. Report: "../test_data/test_regex_entropy.json.got",
  186. Config: "../test_data/test_configs/regex_entropy.toml",
  187. ReportFormat: "json",
  188. },
  189. wantPath: "../test_data/test_regex_entropy.json",
  190. },
  191. {
  192. description: "test local repo four entropy alternative config",
  193. opts: options.Options{
  194. RepoPath: "../test_data/test_repos/test_repo_4",
  195. Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  196. RepoConfig: true,
  197. ReportFormat: "json",
  198. },
  199. wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json",
  200. },
  201. {
  202. description: "test local repo four entropy alternative config",
  203. opts: options.Options{
  204. RepoPath: "../test_data/test_repos/test_repo_1",
  205. Report: "../test_data/test_regex_whitelist.json.got",
  206. Config: "../test_data/test_configs/aws_key_aws_whitelisted.toml",
  207. ReportFormat: "json",
  208. },
  209. wantEmpty: true,
  210. },
  211. {
  212. description: "test local repo one aws leak timeout",
  213. opts: options.Options{
  214. RepoPath: "../test_data/test_repos/test_repo_1",
  215. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  216. ReportFormat: "json",
  217. Timeout: "10ns",
  218. },
  219. wantEmpty: true,
  220. },
  221. {
  222. description: "test local repo one aws leak long timeout",
  223. opts: options.Options{
  224. RepoPath: "../test_data/test_repos/test_repo_1",
  225. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  226. ReportFormat: "json",
  227. Timeout: "2m",
  228. },
  229. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  230. },
  231. }
  232. for _, test := range tests {
  233. fmt.Println(test.description)
  234. cfg, err := config.NewConfig(test.opts)
  235. if err != nil {
  236. t.Error(err)
  237. }
  238. m, err := manager.NewManager(test.opts, cfg)
  239. if err != nil {
  240. t.Error(err)
  241. }
  242. err = Run(m)
  243. if test.wantErr != nil {
  244. if err == nil {
  245. t.Errorf("did not receive wantErr: %v", test.wantErr)
  246. }
  247. if err.Error() != test.wantErr.Error() {
  248. t.Errorf("wantErr does not equal err received: %v", err.Error())
  249. }
  250. continue
  251. }
  252. err = m.Report()
  253. if test.wantEmpty {
  254. if len(m.GetLeaks()) != 0 {
  255. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  256. }
  257. continue
  258. }
  259. if test.wantPath != "" {
  260. err := fileCheck(test.wantPath, test.opts.Report)
  261. if err != nil {
  262. t.Error(err)
  263. }
  264. }
  265. }
  266. }
  267. func TestAuditUncommited(t *testing.T) {
  268. moveDotGit("dotGit", ".git")
  269. defer moveDotGit(".git", "dotGit")
  270. tests := []struct {
  271. description string
  272. opts options.Options
  273. wantPath string
  274. wantErr error
  275. emptyRepo bool
  276. wantEmpty bool
  277. fileToChange string
  278. addition string
  279. }{
  280. {
  281. description: "test audit local one leak",
  282. opts: options.Options{
  283. RepoPath: "../test_data/test_repos/test_repo_1",
  284. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  285. Uncommited: true,
  286. ReportFormat: "json",
  287. },
  288. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  289. fileToChange: "server.test.py",
  290. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  291. },
  292. {
  293. description: "test audit local no leak",
  294. opts: options.Options{
  295. RepoPath: "../test_data/test_repos/test_repo_1",
  296. Uncommited: true,
  297. ReportFormat: "json",
  298. },
  299. wantEmpty: true,
  300. fileToChange: "server.test.py",
  301. addition: "nothing bad",
  302. },
  303. }
  304. for _, test := range tests {
  305. fmt.Println(test.description)
  306. old, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  307. if err != nil {
  308. t.Error(err)
  309. }
  310. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  311. os.O_WRONLY|os.O_APPEND, 0644)
  312. if err != nil {
  313. t.Error(err)
  314. }
  315. _, err = altered.WriteString(test.addition)
  316. if err != nil {
  317. t.Error(err)
  318. }
  319. cfg, err := config.NewConfig(test.opts)
  320. if err != nil {
  321. t.Error(err)
  322. }
  323. m, err := manager.NewManager(test.opts, cfg)
  324. if err != nil {
  325. t.Error(err)
  326. }
  327. if err := Run(m); err != nil {
  328. t.Error(err)
  329. }
  330. if err := m.Report(); err != nil {
  331. t.Error(err)
  332. }
  333. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  334. if err != nil {
  335. t.Error(err)
  336. }
  337. if test.wantEmpty {
  338. continue
  339. }
  340. if test.wantPath != "" {
  341. err := fileCheck(test.wantPath, test.opts.Report)
  342. if err != nil {
  343. t.Error(err)
  344. }
  345. }
  346. }
  347. }
  348. func fileCheck(wantPath, gotPath string) error {
  349. var (
  350. gotLeaks []manager.Leak
  351. wantLeaks []manager.Leak
  352. )
  353. want, err := ioutil.ReadFile(wantPath)
  354. if err != nil {
  355. return err
  356. }
  357. got, err := ioutil.ReadFile(gotPath)
  358. if err != nil {
  359. return err
  360. }
  361. err = json.Unmarshal(got, &gotLeaks)
  362. if err != nil {
  363. return err
  364. }
  365. err = json.Unmarshal(want, &wantLeaks)
  366. if err != nil {
  367. return nil
  368. }
  369. sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit })
  370. sort.Slice(wantLeaks, func(i, j int) bool { return (wantLeaks)[i].Commit < (wantLeaks)[j].Commit })
  371. if !reflect.DeepEqual(gotLeaks, wantLeaks) {
  372. dmp := diffmatchpatch.New()
  373. diffs := dmp.DiffMain(string(want), string(got), false)
  374. return fmt.Errorf("does not equal: %s", dmp.DiffPrettyText(diffs))
  375. }
  376. if err := os.Remove(gotPath); err != nil {
  377. return err
  378. }
  379. return nil
  380. }
  381. func moveDotGit(from, to string) error {
  382. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  383. if err != nil {
  384. return err
  385. }
  386. for _, dir := range repoDirs {
  387. if !dir.IsDir() {
  388. continue
  389. }
  390. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  391. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  392. if err != nil {
  393. return err
  394. }
  395. }
  396. return nil
  397. }