gitleaks_test.go 19 KB

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