gitleaks_test.go 20 KB

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