gitleaks_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "github.com/franela/goblin"
  11. git "gopkg.in/src-d/go-git.v4"
  12. "gopkg.in/src-d/go-git.v4/storage/memory"
  13. )
  14. const testWhitelistCommit = `
  15. [[regexes]]
  16. description = "AWS"
  17. regex = '''AKIA[0-9A-Z]{16}'''
  18. [whitelist]
  19. commits = [
  20. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973",
  21. ]
  22. `
  23. const testWhitelistFile = `
  24. [[regexes]]
  25. description = "AWS"
  26. regex = '''AKIA[0-9A-Z]{16}'''
  27. [whitelist]
  28. files = [
  29. ".go",
  30. ]
  31. `
  32. const testWhitelistBranch = `
  33. [[regexes]]
  34. description = "AWS"
  35. regex = '''AKIA[0-9A-Z]{16}'''
  36. [whitelist]
  37. branches = [
  38. "origin/master",
  39. ]
  40. `
  41. const testWhitelistRegex = `
  42. [[regexes]]
  43. description = "AWS"
  44. regex = '''AKIA[0-9A-Z]{16}'''
  45. [whitelist]
  46. regexes= [
  47. "AKIA",
  48. ]
  49. `
  50. const testWhitelistRepo = `
  51. [[regexes]]
  52. description = "AWS"
  53. regex = '''AKIA[0-9A-Z]{16}'''
  54. [whitelist]
  55. repos = [
  56. "gronit",
  57. ]
  58. `
  59. var benchmarkRepo *RepoDescriptor
  60. var benchmarkLeaksRepo *RepoDescriptor
  61. func getBenchmarkLeaksRepo() *RepoDescriptor {
  62. if benchmarkLeaksRepo != nil {
  63. return benchmarkLeaksRepo
  64. }
  65. leaksR, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  66. URL: "https://github.com/gitleakstest/gronit.git",
  67. })
  68. benchmarkLeaksRepo = &RepoDescriptor{
  69. repository: leaksR,
  70. }
  71. return benchmarkLeaksRepo
  72. }
  73. func getBenchmarkRepo() *RepoDescriptor {
  74. if benchmarkRepo != nil {
  75. return benchmarkRepo
  76. }
  77. bmRepo, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  78. URL: "https://github.com/apple/swift-package-manager.git",
  79. })
  80. benchmarkRepo = &RepoDescriptor{
  81. repository: bmRepo,
  82. }
  83. return benchmarkRepo
  84. }
  85. func TestGetRepo(t *testing.T) {
  86. var err error
  87. dir, err = ioutil.TempDir("", "gitleaksTestRepo")
  88. defer os.RemoveAll(dir)
  89. if err != nil {
  90. panic(err)
  91. }
  92. _, err = git.PlainClone(dir, false, &git.CloneOptions{
  93. URL: "https://github.com/gitleakstest/gronit",
  94. })
  95. if err != nil {
  96. panic(err)
  97. }
  98. var tests = []struct {
  99. testOpts Options
  100. description string
  101. expectedErrMsg string
  102. }{
  103. {
  104. testOpts: Options{
  105. Repo: "https://github.com/gitleakstest/gronit",
  106. },
  107. description: "test plain clone remote repo",
  108. expectedErrMsg: "",
  109. },
  110. {
  111. testOpts: Options{
  112. Repo: "https://github.com/gitleakstest/gronit",
  113. Disk: true,
  114. },
  115. description: "test on disk clone remote repo",
  116. expectedErrMsg: "",
  117. },
  118. {
  119. testOpts: Options{
  120. RepoPath: dir,
  121. },
  122. description: "test local clone repo",
  123. expectedErrMsg: "",
  124. },
  125. {
  126. testOpts: Options{
  127. Repo: "https://github.com/gitleakstest/nope",
  128. },
  129. description: "test no repo",
  130. expectedErrMsg: "authentication required",
  131. },
  132. {
  133. testOpts: Options{
  134. Repo: "https://github.com/gitleakstest/private",
  135. IncludePrivate: true,
  136. },
  137. description: "test private repo",
  138. expectedErrMsg: "invalid auth method",
  139. },
  140. {
  141. testOpts: Options{
  142. Repo: "https://github.com/gitleakstest/private",
  143. IncludePrivate: true,
  144. Disk: true,
  145. },
  146. description: "test private repo",
  147. expectedErrMsg: "invalid auth method",
  148. },
  149. }
  150. g := goblin.Goblin(t)
  151. for _, test := range tests {
  152. g.Describe("TestGetRepo", func() {
  153. g.It(test.description, func() {
  154. opts = test.testOpts
  155. _, err := cloneRepo()
  156. if err != nil {
  157. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  158. }
  159. })
  160. })
  161. }
  162. }
  163. func TestRun(t *testing.T) {
  164. var err error
  165. configsDir := testTomlLoader()
  166. dir, err = ioutil.TempDir("", "gitleaksTestOwner")
  167. defer os.RemoveAll(dir)
  168. if err != nil {
  169. panic(err)
  170. }
  171. git.PlainClone(dir+"/gronit", false, &git.CloneOptions{
  172. URL: "https://github.com/gitleakstest/gronit",
  173. })
  174. git.PlainClone(dir+"/h1domains", false, &git.CloneOptions{
  175. URL: "https://github.com/gitleakstest/h1domains",
  176. })
  177. var tests = []struct {
  178. testOpts Options
  179. description string
  180. expectedErrMsg string
  181. whiteListRepos []string
  182. numLeaks int
  183. configPath string
  184. }{
  185. {
  186. testOpts: Options{
  187. GithubUser: "gitleakstest",
  188. },
  189. description: "test github user",
  190. numLeaks: 2,
  191. expectedErrMsg: "",
  192. },
  193. {
  194. testOpts: Options{
  195. GithubUser: "gitleakstest",
  196. Disk: true,
  197. },
  198. description: "test github user on disk ",
  199. numLeaks: 2,
  200. expectedErrMsg: "",
  201. },
  202. {
  203. testOpts: Options{
  204. GithubOrg: "gitleakstestorg",
  205. },
  206. description: "test github org",
  207. numLeaks: 2,
  208. expectedErrMsg: "",
  209. },
  210. {
  211. testOpts: Options{
  212. GithubOrg: "gitleakstestorg",
  213. Disk: true,
  214. },
  215. description: "test org on disk",
  216. numLeaks: 2,
  217. expectedErrMsg: "",
  218. },
  219. {
  220. testOpts: Options{
  221. OwnerPath: dir,
  222. },
  223. description: "test owner path",
  224. numLeaks: 2,
  225. expectedErrMsg: "",
  226. },
  227. {
  228. testOpts: Options{
  229. GithubOrg: "gitleakstestorg",
  230. IncludePrivate: true,
  231. SSHKey: "reallyreallyreallyreallywrongpath",
  232. },
  233. description: "test private org no ssh",
  234. numLeaks: 0,
  235. expectedErrMsg: "unable to generate ssh key: open reallyreallyreallyreallywrongpath: no such file or directory",
  236. },
  237. {
  238. testOpts: Options{
  239. Repo: "https://github.com/gitleakstest/gronit.git",
  240. },
  241. description: "test leak",
  242. numLeaks: 2,
  243. expectedErrMsg: "",
  244. },
  245. {
  246. testOpts: Options{
  247. Repo: "https://github.com/gitleakstest/h1domains.git",
  248. },
  249. description: "test clean",
  250. numLeaks: 0,
  251. expectedErrMsg: "",
  252. },
  253. {
  254. testOpts: Options{
  255. Repo: "https://github.com/gitleakstest/empty.git",
  256. },
  257. description: "test empty",
  258. numLeaks: 0,
  259. expectedErrMsg: "reference not found",
  260. },
  261. {
  262. testOpts: Options{
  263. GithubOrg: "gitleakstestorg",
  264. },
  265. description: "test github org, whitelist repo",
  266. numLeaks: 0,
  267. expectedErrMsg: "",
  268. configPath: path.Join(configsDir, "repo"),
  269. },
  270. {
  271. testOpts: Options{
  272. GithubOrg: "gitleakstestorg",
  273. ExcludeForks: true,
  274. },
  275. description: "test github org, exclude forks",
  276. numLeaks: 0,
  277. expectedErrMsg: "",
  278. },
  279. }
  280. g := goblin.Goblin(t)
  281. for _, test := range tests {
  282. g.Describe("TestRun", func() {
  283. g.It(test.description, func() {
  284. if test.configPath != "" {
  285. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  286. }
  287. opts = test.testOpts
  288. leaks, err := run()
  289. if err != nil {
  290. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  291. }
  292. g.Assert(len(leaks)).Equal(test.numLeaks)
  293. })
  294. })
  295. }
  296. }
  297. func TestWriteReport(t *testing.T) {
  298. tmpDir, _ := ioutil.TempDir("", "reportDir")
  299. reportJSON := path.Join(tmpDir, "report.json")
  300. reportJASON := path.Join(tmpDir, "report.jason")
  301. reportVOID := path.Join("thereIsNoWay", "thisReportWillGetWritten.json")
  302. reportCSV := path.Join(tmpDir, "report.csv")
  303. defer os.RemoveAll(tmpDir)
  304. leaks := []Leak{
  305. {
  306. Line: "eat",
  307. Commit: "your",
  308. Offender: "veggies",
  309. Type: "and",
  310. Message: "get",
  311. Author: "some",
  312. File: "sleep",
  313. Branch: "thxu",
  314. },
  315. }
  316. var tests = []struct {
  317. leaks []Leak
  318. reportFile string
  319. fileName string
  320. description string
  321. testOpts Options
  322. expectedErrMsg string
  323. }{
  324. {
  325. leaks: leaks,
  326. reportFile: reportJSON,
  327. fileName: "report.json",
  328. description: "can we write a json file",
  329. testOpts: Options{
  330. Report: reportJSON,
  331. },
  332. },
  333. {
  334. leaks: leaks,
  335. reportFile: reportCSV,
  336. fileName: "report.csv",
  337. description: "can we write a csv file",
  338. testOpts: Options{
  339. Report: reportCSV,
  340. },
  341. },
  342. {
  343. leaks: leaks,
  344. reportFile: reportJASON,
  345. fileName: "report.jason",
  346. description: "bad file",
  347. expectedErrMsg: "Report should be a .json or .csv file",
  348. testOpts: Options{
  349. Report: reportJASON,
  350. },
  351. },
  352. {
  353. leaks: leaks,
  354. reportFile: reportVOID,
  355. fileName: "report.jason",
  356. description: "bad dir",
  357. expectedErrMsg: "thereIsNoWay does not exist",
  358. testOpts: Options{
  359. Report: reportVOID,
  360. },
  361. },
  362. }
  363. g := goblin.Goblin(t)
  364. for _, test := range tests {
  365. g.Describe("TestWriteReport", func() {
  366. g.It(test.description, func() {
  367. opts = test.testOpts
  368. err := optsGuard()
  369. if err != nil {
  370. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  371. } else {
  372. writeReport(test.leaks)
  373. f, _ := os.Stat(test.reportFile)
  374. g.Assert(f.Name()).Equal(test.fileName)
  375. }
  376. })
  377. })
  378. }
  379. }
  380. func testTomlLoader() string {
  381. tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
  382. ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
  383. ioutil.WriteFile(path.Join(tmpDir, "branch"), []byte(testWhitelistBranch), 0644)
  384. ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
  385. ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
  386. ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
  387. return tmpDir
  388. }
  389. func TestAuditRepo(t *testing.T) {
  390. var leaks []Leak
  391. err := loadToml()
  392. configsDir := testTomlLoader()
  393. defer os.RemoveAll(configsDir)
  394. if err != nil {
  395. panic(err)
  396. }
  397. leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  398. URL: "https://github.com/gitleakstest/gronit.git",
  399. })
  400. if err != nil {
  401. panic(err)
  402. }
  403. leaksRepo := &RepoDescriptor{
  404. repository: leaksR,
  405. name: "gronit",
  406. }
  407. cleanR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  408. URL: "https://github.com/gitleakstest/h1domains.git",
  409. })
  410. if err != nil {
  411. panic(err)
  412. }
  413. cleanRepo := &RepoDescriptor{
  414. repository: cleanR,
  415. name: "h1domains",
  416. }
  417. var tests = []struct {
  418. testOpts Options
  419. description string
  420. expectedErrMsg string
  421. numLeaks int
  422. repo *RepoDescriptor
  423. whiteListFiles []*regexp.Regexp
  424. whiteListCommits map[string]bool
  425. whiteListBranches []string
  426. whiteListRepos []string
  427. whiteListRegexes []*regexp.Regexp
  428. configPath string
  429. }{
  430. {
  431. repo: leaksRepo,
  432. description: "two leaks present",
  433. numLeaks: 2,
  434. },
  435. {
  436. repo: leaksRepo,
  437. description: "two leaks present limit goroutines",
  438. numLeaks: 2,
  439. testOpts: Options{
  440. MaxGoRoutines: 4,
  441. },
  442. },
  443. {
  444. repo: leaksRepo,
  445. description: "audit specific bad branch",
  446. numLeaks: 2,
  447. testOpts: Options{
  448. Branch: "master",
  449. },
  450. },
  451. {
  452. repo: leaksRepo,
  453. description: "audit specific good branch",
  454. numLeaks: 0,
  455. testOpts: Options{
  456. Branch: "dev",
  457. },
  458. },
  459. {
  460. repo: leaksRepo,
  461. description: "audit all branch",
  462. numLeaks: 6,
  463. testOpts: Options{
  464. AuditAllRefs: true,
  465. },
  466. },
  467. {
  468. repo: leaksRepo,
  469. description: "audit all branch whitelist 1",
  470. numLeaks: 4,
  471. testOpts: Options{
  472. AuditAllRefs: true,
  473. },
  474. whiteListBranches: []string{
  475. "origin/master",
  476. },
  477. },
  478. {
  479. repo: leaksRepo,
  480. description: "two leaks present whitelist AWS.. no leaks",
  481. whiteListRegexes: []*regexp.Regexp{
  482. regexp.MustCompile("AKIA"),
  483. },
  484. numLeaks: 0,
  485. },
  486. {
  487. repo: leaksRepo,
  488. description: "two leaks present limit goroutines",
  489. numLeaks: 2,
  490. },
  491. {
  492. repo: cleanRepo,
  493. description: "no leaks present",
  494. numLeaks: 0,
  495. },
  496. {
  497. repo: leaksRepo,
  498. description: "two leaks present whitelist go files",
  499. whiteListFiles: []*regexp.Regexp{
  500. regexp.MustCompile(".go"),
  501. },
  502. numLeaks: 0,
  503. },
  504. {
  505. repo: leaksRepo,
  506. description: "two leaks present whitelist bad commit",
  507. whiteListCommits: map[string]bool{
  508. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
  509. },
  510. numLeaks: 1,
  511. },
  512. {
  513. repo: leaksRepo,
  514. description: "redact",
  515. testOpts: Options{
  516. Redact: true,
  517. },
  518. numLeaks: 2,
  519. },
  520. {
  521. repo: leaksRepo,
  522. description: "toml whitelist regex",
  523. configPath: path.Join(configsDir, "regex"),
  524. numLeaks: 0,
  525. },
  526. {
  527. repo: leaksRepo,
  528. description: "toml whitelist branch",
  529. configPath: path.Join(configsDir, "branch"),
  530. testOpts: Options{
  531. AuditAllRefs: true,
  532. },
  533. numLeaks: 4,
  534. },
  535. {
  536. repo: leaksRepo,
  537. description: "toml whitelist file",
  538. configPath: path.Join(configsDir, "file"),
  539. numLeaks: 0,
  540. },
  541. {
  542. repo: leaksRepo,
  543. description: "toml whitelist commit",
  544. configPath: path.Join(configsDir, "commit"),
  545. numLeaks: 1,
  546. },
  547. {
  548. repo: leaksRepo,
  549. description: "audit whitelist repo",
  550. numLeaks: 0,
  551. whiteListRepos: []string{
  552. "gronit",
  553. },
  554. },
  555. {
  556. repo: leaksRepo,
  557. description: "toml whitelist repo",
  558. numLeaks: 0,
  559. configPath: path.Join(configsDir, "repo"),
  560. },
  561. {
  562. repo: leaksRepo,
  563. description: "leaks present with entropy",
  564. testOpts: Options{
  565. Entropy: 4.7,
  566. },
  567. numLeaks: 7,
  568. },
  569. {
  570. repo: leaksRepo,
  571. description: "Audit until specific commit",
  572. numLeaks: 1,
  573. testOpts: Options{
  574. Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
  575. },
  576. },
  577. {
  578. repo: leaksRepo,
  579. description: "commit depth = 1, no leaks",
  580. numLeaks: 0,
  581. testOpts: Options{
  582. Depth: 1,
  583. },
  584. },
  585. {
  586. repo: leaksRepo,
  587. description: "commit depth = 2, one leak",
  588. numLeaks: 1,
  589. testOpts: Options{
  590. Depth: 2,
  591. },
  592. },
  593. }
  594. whiteListCommits = make(map[string]bool)
  595. g := goblin.Goblin(t)
  596. for _, test := range tests {
  597. g.Describe("TestAuditRepo", func() {
  598. g.It(test.description, func() {
  599. opts = test.testOpts
  600. // settin da globs
  601. if test.whiteListFiles != nil {
  602. whiteListFiles = test.whiteListFiles
  603. } else {
  604. whiteListFiles = nil
  605. }
  606. if test.whiteListCommits != nil {
  607. whiteListCommits = test.whiteListCommits
  608. } else {
  609. whiteListCommits = nil
  610. }
  611. if test.whiteListBranches != nil {
  612. whiteListBranches = test.whiteListBranches
  613. } else {
  614. whiteListBranches = nil
  615. }
  616. if test.whiteListRegexes != nil {
  617. whiteListRegexes = test.whiteListRegexes
  618. } else {
  619. whiteListRegexes = nil
  620. }
  621. if test.whiteListRepos != nil {
  622. whiteListRepos = test.whiteListRepos
  623. } else {
  624. whiteListRepos = nil
  625. }
  626. // config paths
  627. if test.configPath != "" {
  628. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  629. loadToml()
  630. }
  631. leaks, err = auditGitRepo(test.repo)
  632. if opts.Redact {
  633. g.Assert(leaks[0].Offender).Equal("REDACTED")
  634. }
  635. g.Assert(len(leaks)).Equal(test.numLeaks)
  636. })
  637. })
  638. }
  639. }
  640. func TestOptionGuard(t *testing.T) {
  641. var tests = []struct {
  642. testOpts Options
  643. githubToken bool
  644. description string
  645. expectedErrMsg string
  646. expectedErrMsgFuzzy string
  647. }{
  648. {
  649. testOpts: Options{},
  650. description: "default no opts",
  651. expectedErrMsg: "",
  652. },
  653. {
  654. testOpts: Options{
  655. IncludePrivate: true,
  656. GithubOrg: "fakeOrg",
  657. },
  658. description: "private org no githubtoken",
  659. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  660. githubToken: false,
  661. },
  662. {
  663. testOpts: Options{
  664. IncludePrivate: true,
  665. GithubUser: "fakeUser",
  666. },
  667. description: "private user no githubtoken",
  668. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  669. githubToken: false,
  670. },
  671. {
  672. testOpts: Options{
  673. IncludePrivate: true,
  674. GithubUser: "fakeUser",
  675. GithubOrg: "fakeOrg",
  676. },
  677. description: "double owner",
  678. expectedErrMsg: "github user and organization set",
  679. },
  680. {
  681. testOpts: Options{
  682. IncludePrivate: true,
  683. GithubOrg: "fakeOrg",
  684. OwnerPath: "/dev/null",
  685. },
  686. description: "local and remote target",
  687. expectedErrMsg: "github organization set and local owner path",
  688. },
  689. {
  690. testOpts: Options{
  691. IncludePrivate: true,
  692. GithubUser: "fakeUser",
  693. OwnerPath: "/dev/null",
  694. },
  695. description: "local and remote target",
  696. expectedErrMsg: "github user set and local owner path",
  697. },
  698. {
  699. testOpts: Options{
  700. GithubUser: "fakeUser",
  701. SingleSearch: "*/./....",
  702. },
  703. description: "single search invalid regex gaurd",
  704. expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
  705. },
  706. {
  707. testOpts: Options{
  708. GithubUser: "fakeUser",
  709. SingleSearch: "mystring",
  710. },
  711. description: "single search regex gaurd",
  712. expectedErrMsg: "",
  713. },
  714. {
  715. testOpts: Options{
  716. GithubOrg: "fakeOrg",
  717. Entropy: 9,
  718. },
  719. description: "Invalid entropy level guard",
  720. expectedErrMsg: "The maximum level of entropy is 8",
  721. },
  722. }
  723. g := goblin.Goblin(t)
  724. for _, test := range tests {
  725. g.Describe("Test Option Gaurd", func() {
  726. g.It(test.description, func() {
  727. os.Clearenv()
  728. opts = test.testOpts
  729. if test.githubToken {
  730. os.Setenv("GITHUB_TOKEN", "fakeToken")
  731. }
  732. err := optsGuard()
  733. if err != nil {
  734. if test.expectedErrMsgFuzzy != "" {
  735. g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
  736. } else {
  737. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  738. }
  739. } else {
  740. g.Assert("").Equal(test.expectedErrMsg)
  741. }
  742. })
  743. })
  744. }
  745. }
  746. func TestLoadToml(t *testing.T) {
  747. tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
  748. defer os.RemoveAll(tmpDir)
  749. err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
  750. if err != nil {
  751. panic(err)
  752. }
  753. configPath := path.Join(tmpDir, "gitleaksConfig")
  754. noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
  755. var tests = []struct {
  756. testOpts Options
  757. description string
  758. configPath string
  759. expectedErrMsg string
  760. singleSearch bool
  761. }{
  762. {
  763. testOpts: Options{
  764. ConfigPath: configPath,
  765. },
  766. description: "path to config",
  767. },
  768. {
  769. testOpts: Options{},
  770. description: "env var path to no config",
  771. singleSearch: true,
  772. },
  773. {
  774. testOpts: Options{
  775. ConfigPath: noConfigPath,
  776. },
  777. description: "no path to config",
  778. expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
  779. },
  780. {
  781. testOpts: Options{},
  782. description: "env var path to config",
  783. configPath: configPath,
  784. expectedErrMsg: "",
  785. },
  786. {
  787. testOpts: Options{},
  788. description: "env var path to no config",
  789. configPath: noConfigPath,
  790. expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
  791. },
  792. }
  793. g := goblin.Goblin(t)
  794. for _, test := range tests {
  795. g.Describe("TestLoadToml", func() {
  796. g.It(test.description, func() {
  797. opts = test.testOpts
  798. if test.singleSearch {
  799. singleSearchRegex = regexp.MustCompile("test")
  800. } else {
  801. singleSearchRegex = nil
  802. }
  803. if test.configPath != "" {
  804. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  805. } else {
  806. os.Clearenv()
  807. }
  808. err := loadToml()
  809. if err != nil {
  810. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  811. } else {
  812. g.Assert("").Equal(test.expectedErrMsg)
  813. }
  814. })
  815. })
  816. }
  817. }
  818. func BenchmarkAuditRepo1Proc(b *testing.B) {
  819. loadToml()
  820. opts.MaxGoRoutines = 1
  821. benchmarkRepo = getBenchmarkRepo()
  822. for n := 0; n < b.N; n++ {
  823. auditGitRepo(benchmarkRepo)
  824. }
  825. }
  826. func BenchmarkAuditRepo2Proc(b *testing.B) {
  827. loadToml()
  828. opts.MaxGoRoutines = 2
  829. benchmarkRepo = getBenchmarkRepo()
  830. for n := 0; n < b.N; n++ {
  831. auditGitRepo(benchmarkRepo)
  832. }
  833. }
  834. func BenchmarkAuditRepo4Proc(b *testing.B) {
  835. loadToml()
  836. opts.MaxGoRoutines = 4
  837. benchmarkRepo = getBenchmarkRepo()
  838. for n := 0; n < b.N; n++ {
  839. auditGitRepo(benchmarkRepo)
  840. }
  841. }
  842. func BenchmarkAuditRepo8Proc(b *testing.B) {
  843. loadToml()
  844. opts.MaxGoRoutines = 8
  845. benchmarkRepo = getBenchmarkRepo()
  846. for n := 0; n < b.N; n++ {
  847. auditGitRepo(benchmarkRepo)
  848. }
  849. }
  850. func BenchmarkAuditRepo10Proc(b *testing.B) {
  851. loadToml()
  852. opts.MaxGoRoutines = 10
  853. benchmarkRepo = getBenchmarkRepo()
  854. for n := 0; n < b.N; n++ {
  855. auditGitRepo(benchmarkRepo)
  856. }
  857. }
  858. func BenchmarkAuditRepo100Proc(b *testing.B) {
  859. loadToml()
  860. opts.MaxGoRoutines = 100
  861. benchmarkRepo = getBenchmarkRepo()
  862. for n := 0; n < b.N; n++ {
  863. auditGitRepo(benchmarkRepo)
  864. }
  865. }
  866. func BenchmarkAuditRepo1000Proc(b *testing.B) {
  867. loadToml()
  868. opts.MaxGoRoutines = 1000
  869. benchmarkRepo = getBenchmarkRepo()
  870. for n := 0; n < b.N; n++ {
  871. auditGitRepo(benchmarkRepo)
  872. }
  873. }
  874. func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
  875. loadToml()
  876. opts.MaxGoRoutines = 1
  877. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  878. for n := 0; n < b.N; n++ {
  879. auditGitRepo(benchmarkRepo)
  880. }
  881. }
  882. func BenchmarkAuditLeakRepo2Proc(b *testing.B) {
  883. loadToml()
  884. opts.MaxGoRoutines = 2
  885. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  886. for n := 0; n < b.N; n++ {
  887. auditGitRepo(benchmarkRepo)
  888. }
  889. }
  890. func BenchmarkAuditLeakRepo4Proc(b *testing.B) {
  891. loadToml()
  892. opts.MaxGoRoutines = 4
  893. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  894. for n := 0; n < b.N; n++ {
  895. auditGitRepo(benchmarkRepo)
  896. }
  897. }
  898. func BenchmarkAuditLeakRepo8Proc(b *testing.B) {
  899. loadToml()
  900. opts.MaxGoRoutines = 8
  901. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  902. for n := 0; n < b.N; n++ {
  903. auditGitRepo(benchmarkRepo)
  904. }
  905. }
  906. func BenchmarkAuditLeakRepo10Proc(b *testing.B) {
  907. loadToml()
  908. opts.MaxGoRoutines = 10
  909. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  910. for n := 0; n < b.N; n++ {
  911. auditGitRepo(benchmarkRepo)
  912. }
  913. }
  914. func BenchmarkAuditLeakRepo100Proc(b *testing.B) {
  915. loadToml()
  916. opts.MaxGoRoutines = 100
  917. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  918. for n := 0; n < b.N; n++ {
  919. auditGitRepo(benchmarkRepo)
  920. }
  921. }
  922. func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
  923. loadToml()
  924. opts.MaxGoRoutines = 1000
  925. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  926. for n := 0; n < b.N; n++ {
  927. auditGitRepo(benchmarkRepo)
  928. }
  929. }