gitleaks_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. testOpts: Options{
  302. GithubPR: "https://github.com/gitleakstest/gronit/pull/2",
  303. },
  304. description: "test github pr with commits without patch info",
  305. numLeaks: 0,
  306. expectedErrMsg: "",
  307. commitPerPage: 1,
  308. },
  309. {
  310. testOpts: Options{
  311. Repo: "https://github.com/gitleakstest/gronit.git",
  312. Branch: "master",
  313. },
  314. description: "test github leaks on single branch - master",
  315. numLeaks: 2,
  316. },
  317. {
  318. testOpts: Options{
  319. Repo: "https://github.com/gitleakstest/gronit.git",
  320. Branch: "nonExistingBranch",
  321. },
  322. description: "test github leaks on single branch which doesn't exist",
  323. numLeaks: 0,
  324. expectedErrMsg: "reference not found",
  325. },
  326. }
  327. g := goblin.Goblin(t)
  328. for _, test := range tests {
  329. g.Describe("TestRun", func() {
  330. g.It(test.description, func() {
  331. if test.configPath != "" {
  332. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  333. }
  334. if test.commitPerPage != 0 {
  335. githubPages = test.commitPerPage
  336. }
  337. if test.whiteListFiles != nil {
  338. whiteListFiles = test.whiteListFiles
  339. } else {
  340. whiteListFiles = nil
  341. }
  342. opts = test.testOpts
  343. leaks, err := run()
  344. if err != nil {
  345. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  346. }
  347. g.Assert(len(leaks)).Equal(test.numLeaks)
  348. githubPages = 100
  349. })
  350. })
  351. }
  352. }
  353. func TestWriteReport(t *testing.T) {
  354. tmpDir, _ := ioutil.TempDir("", "reportDir")
  355. reportJSON := path.Join(tmpDir, "report.json")
  356. reportJASON := path.Join(tmpDir, "report.jason")
  357. reportVOID := path.Join("thereIsNoWay", "thisReportWillGetWritten.json")
  358. reportCSV := path.Join(tmpDir, "report.csv")
  359. defer os.RemoveAll(tmpDir)
  360. leaks := []Leak{
  361. {
  362. Line: "eat",
  363. Commit: "your",
  364. Offender: "veggies",
  365. Type: "and",
  366. Message: "get",
  367. Author: "some",
  368. File: "sleep",
  369. Date: time.Now(),
  370. },
  371. }
  372. var tests = []struct {
  373. leaks []Leak
  374. reportFile string
  375. fileName string
  376. description string
  377. testOpts Options
  378. expectedErrMsg string
  379. }{
  380. {
  381. leaks: leaks,
  382. reportFile: reportJSON,
  383. fileName: "report.json",
  384. description: "can we write a json file",
  385. testOpts: Options{
  386. Report: reportJSON,
  387. },
  388. },
  389. {
  390. leaks: leaks,
  391. reportFile: reportCSV,
  392. fileName: "report.csv",
  393. description: "can we write a csv file",
  394. testOpts: Options{
  395. Report: reportCSV,
  396. },
  397. },
  398. {
  399. leaks: leaks,
  400. reportFile: reportJASON,
  401. fileName: "report.jason",
  402. description: "bad file",
  403. expectedErrMsg: "Report should be a .json or .csv file",
  404. testOpts: Options{
  405. Report: reportJASON,
  406. },
  407. },
  408. {
  409. leaks: leaks,
  410. reportFile: reportVOID,
  411. fileName: "report.jason",
  412. description: "bad dir",
  413. expectedErrMsg: "thereIsNoWay does not exist",
  414. testOpts: Options{
  415. Report: reportVOID,
  416. },
  417. },
  418. }
  419. g := goblin.Goblin(t)
  420. for _, test := range tests {
  421. g.Describe("TestWriteReport", func() {
  422. g.It(test.description, func() {
  423. opts = test.testOpts
  424. err := optsGuard()
  425. if err != nil {
  426. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  427. } else {
  428. writeReport(test.leaks)
  429. f, _ := os.Stat(test.reportFile)
  430. g.Assert(f.Name()).Equal(test.fileName)
  431. }
  432. })
  433. })
  434. }
  435. }
  436. func testTomlLoader() string {
  437. tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
  438. ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
  439. ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
  440. ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
  441. ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
  442. ioutil.WriteFile(path.Join(tmpDir, "entropy"), []byte(testEntropyRange), 0644)
  443. ioutil.WriteFile(path.Join(tmpDir, "badEntropy"), []byte(testBadEntropyRange), 0644)
  444. ioutil.WriteFile(path.Join(tmpDir, "badEntropy2"), []byte(testBadEntropyRange2), 0644)
  445. return tmpDir
  446. }
  447. func TestAuditRepo(t *testing.T) {
  448. var leaks []Leak
  449. err := loadToml()
  450. configsDir := testTomlLoader()
  451. defer os.RemoveAll(configsDir)
  452. if err != nil {
  453. panic(err)
  454. }
  455. leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  456. URL: "https://github.com/gitleakstest/gronit.git",
  457. })
  458. if err != nil {
  459. panic(err)
  460. }
  461. leaksRepo := &RepoDescriptor{
  462. repository: leaksR,
  463. name: "gronit",
  464. }
  465. cleanR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  466. URL: "https://github.com/gitleakstest/h1domains.git",
  467. })
  468. if err != nil {
  469. panic(err)
  470. }
  471. cleanRepo := &RepoDescriptor{
  472. repository: cleanR,
  473. name: "h1domains",
  474. }
  475. var tests = []struct {
  476. testOpts Options
  477. description string
  478. expectedErrMsg string
  479. numLeaks int
  480. repo *RepoDescriptor
  481. whiteListFiles []*regexp.Regexp
  482. whiteListCommits map[string]bool
  483. whiteListRepos []*regexp.Regexp
  484. whiteListRegexes []*regexp.Regexp
  485. configPath string
  486. }{
  487. {
  488. repo: leaksRepo,
  489. description: "pinned config",
  490. numLeaks: 0,
  491. testOpts: Options{
  492. RepoConfig: true,
  493. },
  494. },
  495. {
  496. repo: leaksRepo,
  497. description: "commit depth = 1, one leak",
  498. numLeaks: 1,
  499. testOpts: Options{
  500. Depth: 1,
  501. },
  502. },
  503. {
  504. repo: leaksRepo,
  505. description: "two leaks present",
  506. numLeaks: 2,
  507. },
  508. {
  509. repo: leaksRepo,
  510. description: "two leaks present limit goroutines",
  511. numLeaks: 2,
  512. testOpts: Options{
  513. Threads: 4,
  514. },
  515. },
  516. {
  517. repo: leaksRepo,
  518. description: "two leaks present whitelist AWS.. no leaks",
  519. whiteListRegexes: []*regexp.Regexp{
  520. regexp.MustCompile("AKIA"),
  521. },
  522. numLeaks: 0,
  523. },
  524. {
  525. repo: leaksRepo,
  526. description: "two leaks present limit goroutines",
  527. numLeaks: 2,
  528. },
  529. {
  530. repo: cleanRepo,
  531. description: "no leaks present",
  532. numLeaks: 0,
  533. },
  534. {
  535. repo: leaksRepo,
  536. description: "two leaks present whitelist go files",
  537. whiteListFiles: []*regexp.Regexp{
  538. regexp.MustCompile(".go"),
  539. },
  540. numLeaks: 0,
  541. },
  542. {
  543. repo: leaksRepo,
  544. description: "two leaks present whitelist bad commit",
  545. whiteListCommits: map[string]bool{
  546. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
  547. },
  548. numLeaks: 1,
  549. },
  550. {
  551. repo: leaksRepo,
  552. description: "redact",
  553. testOpts: Options{
  554. Redact: true,
  555. },
  556. numLeaks: 2,
  557. },
  558. {
  559. repo: leaksRepo,
  560. description: "toml whitelist regex",
  561. configPath: path.Join(configsDir, "regex"),
  562. numLeaks: 0,
  563. },
  564. {
  565. repo: leaksRepo,
  566. description: "toml whitelist file",
  567. configPath: path.Join(configsDir, "file"),
  568. numLeaks: 0,
  569. },
  570. {
  571. repo: leaksRepo,
  572. description: "toml whitelist commit",
  573. configPath: path.Join(configsDir, "commit"),
  574. numLeaks: 1,
  575. },
  576. {
  577. repo: leaksRepo,
  578. description: "audit whitelist repo",
  579. numLeaks: 0,
  580. whiteListRepos: []*regexp.Regexp{
  581. regexp.MustCompile("gronit"),
  582. },
  583. },
  584. {
  585. repo: leaksRepo,
  586. description: "toml whitelist repo",
  587. numLeaks: 0,
  588. configPath: path.Join(configsDir, "repo"),
  589. },
  590. {
  591. repo: leaksRepo,
  592. description: "leaks present with entropy",
  593. testOpts: Options{
  594. Entropy: 4.7,
  595. },
  596. numLeaks: 6,
  597. },
  598. {
  599. repo: leaksRepo,
  600. description: "leaks present with entropy",
  601. testOpts: Options{
  602. Entropy: 4.7,
  603. NoiseReduction: true,
  604. },
  605. numLeaks: 2,
  606. },
  607. {
  608. repo: leaksRepo,
  609. description: "Audit until specific commit",
  610. numLeaks: 2,
  611. testOpts: Options{
  612. Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
  613. },
  614. },
  615. {
  616. repo: leaksRepo,
  617. description: "commit depth = 2, two leaks",
  618. numLeaks: 2,
  619. testOpts: Options{
  620. Depth: 2,
  621. },
  622. },
  623. {
  624. repo: leaksRepo,
  625. description: "toml entropy range",
  626. numLeaks: 298,
  627. configPath: path.Join(configsDir, "entropy"),
  628. },
  629. {
  630. repo: leaksRepo,
  631. testOpts: Options{
  632. NoiseReduction: true,
  633. },
  634. description: "toml entropy range",
  635. numLeaks: 58,
  636. configPath: path.Join(configsDir, "entropy"),
  637. },
  638. {
  639. repo: leaksRepo,
  640. description: "toml bad entropy range",
  641. numLeaks: 0,
  642. configPath: path.Join(configsDir, "badEntropy"),
  643. expectedErrMsg: "entropy range must be ascending",
  644. },
  645. {
  646. repo: leaksRepo,
  647. description: "toml bad entropy2 range",
  648. numLeaks: 0,
  649. configPath: path.Join(configsDir, "badEntropy2"),
  650. expectedErrMsg: "invalid entropy ranges, must be within 0.0-8.0",
  651. },
  652. }
  653. whiteListCommits = make(map[string]bool)
  654. g := goblin.Goblin(t)
  655. for _, test := range tests {
  656. g.Describe("TestAuditRepo", func() {
  657. g.It(test.description, func() {
  658. opts = test.testOpts
  659. // settin da globs
  660. if test.whiteListFiles != nil {
  661. whiteListFiles = test.whiteListFiles
  662. } else {
  663. whiteListFiles = nil
  664. }
  665. if test.whiteListCommits != nil {
  666. whiteListCommits = test.whiteListCommits
  667. } else {
  668. whiteListCommits = nil
  669. }
  670. if test.whiteListRegexes != nil {
  671. whiteListRegexes = test.whiteListRegexes
  672. } else {
  673. whiteListRegexes = nil
  674. }
  675. if test.whiteListRepos != nil {
  676. whiteListRepos = test.whiteListRepos
  677. } else {
  678. whiteListRepos = nil
  679. }
  680. skip := false
  681. // config paths
  682. if test.configPath != "" {
  683. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  684. err := loadToml()
  685. if err != nil {
  686. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  687. skip = true
  688. }
  689. }
  690. if !skip {
  691. leaks, err = auditGitRepo(test.repo)
  692. if opts.Redact {
  693. g.Assert(leaks[0].Offender).Equal("REDACTED")
  694. }
  695. g.Assert(len(leaks)).Equal(test.numLeaks)
  696. }
  697. })
  698. })
  699. }
  700. }
  701. func TestOptionGuard(t *testing.T) {
  702. var tests = []struct {
  703. testOpts Options
  704. githubToken bool
  705. description string
  706. expectedErrMsg string
  707. expectedErrMsgFuzzy string
  708. }{
  709. {
  710. testOpts: Options{},
  711. description: "default no opts",
  712. expectedErrMsg: "",
  713. },
  714. {
  715. testOpts: Options{
  716. GithubUser: "fakeUser",
  717. GithubOrg: "fakeOrg",
  718. },
  719. description: "double owner",
  720. expectedErrMsg: "github user and organization set",
  721. },
  722. {
  723. testOpts: Options{
  724. GithubOrg: "fakeOrg",
  725. OwnerPath: "/dev/null",
  726. },
  727. description: "local and remote target",
  728. expectedErrMsg: "github organization set and local owner path",
  729. },
  730. {
  731. testOpts: Options{
  732. GithubUser: "fakeUser",
  733. OwnerPath: "/dev/null",
  734. },
  735. description: "local and remote target",
  736. expectedErrMsg: "github user set and local owner path",
  737. },
  738. {
  739. testOpts: Options{
  740. GithubUser: "fakeUser",
  741. SingleSearch: "*/./....",
  742. },
  743. description: "single search invalid regex gaurd",
  744. expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
  745. },
  746. {
  747. testOpts: Options{
  748. GithubUser: "fakeUser",
  749. SingleSearch: "mystring",
  750. },
  751. description: "single search regex gaurd",
  752. expectedErrMsg: "",
  753. },
  754. {
  755. testOpts: Options{
  756. GithubOrg: "fakeOrg",
  757. Entropy: 9,
  758. },
  759. description: "Invalid entropy level guard",
  760. expectedErrMsg: "The maximum level of entropy is 8",
  761. },
  762. }
  763. g := goblin.Goblin(t)
  764. for _, test := range tests {
  765. g.Describe("Test Option Gaurd", func() {
  766. g.It(test.description, func() {
  767. os.Clearenv()
  768. opts = test.testOpts
  769. if test.githubToken {
  770. os.Setenv("GITHUB_TOKEN", "fakeToken")
  771. }
  772. err := optsGuard()
  773. if err != nil {
  774. if test.expectedErrMsgFuzzy != "" {
  775. g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
  776. } else {
  777. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  778. }
  779. } else {
  780. g.Assert("").Equal(test.expectedErrMsg)
  781. }
  782. })
  783. })
  784. }
  785. }
  786. func TestLoadToml(t *testing.T) {
  787. tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
  788. defer os.RemoveAll(tmpDir)
  789. err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
  790. if err != nil {
  791. panic(err)
  792. }
  793. configPath := path.Join(tmpDir, "gitleaksConfig")
  794. noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
  795. var tests = []struct {
  796. testOpts Options
  797. description string
  798. configPath string
  799. expectedErrMsg string
  800. singleSearch bool
  801. }{
  802. {
  803. testOpts: Options{
  804. ConfigPath: configPath,
  805. },
  806. description: "path to config",
  807. },
  808. {
  809. testOpts: Options{},
  810. description: "env var path to no config",
  811. singleSearch: true,
  812. },
  813. {
  814. testOpts: Options{
  815. ConfigPath: noConfigPath,
  816. },
  817. description: "no path to config",
  818. expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
  819. },
  820. {
  821. testOpts: Options{},
  822. description: "env var path to config",
  823. configPath: configPath,
  824. expectedErrMsg: "",
  825. },
  826. {
  827. testOpts: Options{},
  828. description: "env var path to no config",
  829. configPath: noConfigPath,
  830. expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
  831. },
  832. }
  833. g := goblin.Goblin(t)
  834. for _, test := range tests {
  835. g.Describe("TestLoadToml", func() {
  836. g.It(test.description, func() {
  837. opts = test.testOpts
  838. if test.singleSearch {
  839. singleSearchRegex = regexp.MustCompile("test")
  840. } else {
  841. singleSearchRegex = nil
  842. }
  843. if test.configPath != "" {
  844. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  845. } else {
  846. os.Clearenv()
  847. }
  848. err := loadToml()
  849. if err != nil {
  850. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  851. } else {
  852. g.Assert("").Equal(test.expectedErrMsg)
  853. }
  854. })
  855. })
  856. }
  857. }