gitleaks_test.go 18 KB

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