detect_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package detect
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/spf13/viper"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. "github.com/zricethezav/gitleaks/v8/report"
  11. )
  12. const configPath = "../testdata/config/"
  13. const repoBasePath = "../testdata/repos/"
  14. func TestDetect(t *testing.T) {
  15. tests := []struct {
  16. cfgName string
  17. baselinePath string
  18. fragment Fragment
  19. // NOTE: for expected findings, all line numbers will be 0
  20. // because line deltas are added _after_ the finding is created.
  21. // I.e, if the finding is from a --no-git file, the line number will be
  22. // increase by 1 in DetectFromFiles(). If the finding is from git,
  23. // the line number will be increased by the patch delta.
  24. expectedFindings []report.Finding
  25. wantError error
  26. }{
  27. {
  28. cfgName: "simple",
  29. fragment: Fragment{
  30. Raw: `awsToken := \"AKIALALEMEL33243OKIA\ // gitleaks:allow"`,
  31. FilePath: "tmp.go",
  32. },
  33. expectedFindings: []report.Finding{},
  34. },
  35. {
  36. cfgName: "simple",
  37. fragment: Fragment{
  38. Raw: `awsToken := \
  39. \"AKIALALEMEL33243OKIA\ // gitleaks:allow"
  40. `,
  41. FilePath: "tmp.go",
  42. },
  43. expectedFindings: []report.Finding{},
  44. },
  45. {
  46. cfgName: "simple",
  47. fragment: Fragment{
  48. Raw: `awsToken := \"AKIALALEMEL33243OKIA\"
  49. // gitleaks:allow"
  50. `,
  51. FilePath: "tmp.go",
  52. },
  53. expectedFindings: []report.Finding{
  54. {
  55. Description: "AWS Access Key",
  56. Secret: "AKIALALEMEL33243OKIA",
  57. Match: "AKIALALEMEL33243OKIA",
  58. File: "tmp.go",
  59. Line: `awsToken := \"AKIALALEMEL33243OKIA\"`,
  60. RuleID: "aws-access-key",
  61. Tags: []string{"key", "AWS"},
  62. StartLine: 0,
  63. EndLine: 0,
  64. StartColumn: 15,
  65. EndColumn: 34,
  66. Entropy: 3.1464393,
  67. },
  68. },
  69. },
  70. {
  71. cfgName: "escaped_character_group",
  72. fragment: Fragment{
  73. Raw: `pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`,
  74. FilePath: "tmp.go",
  75. },
  76. expectedFindings: []report.Finding{
  77. {
  78. Description: "PyPI upload token",
  79. Secret: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
  80. Match: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB",
  81. Line: `pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`,
  82. File: "tmp.go",
  83. RuleID: "pypi-upload-token",
  84. Tags: []string{"key", "pypi"},
  85. StartLine: 0,
  86. EndLine: 0,
  87. StartColumn: 1,
  88. EndColumn: 86,
  89. Entropy: 1.9606875,
  90. },
  91. },
  92. },
  93. {
  94. cfgName: "simple",
  95. fragment: Fragment{
  96. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  97. FilePath: "tmp.go",
  98. },
  99. expectedFindings: []report.Finding{
  100. {
  101. Description: "AWS Access Key",
  102. Secret: "AKIALALEMEL33243OLIA",
  103. Match: "AKIALALEMEL33243OLIA",
  104. Line: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  105. File: "tmp.go",
  106. RuleID: "aws-access-key",
  107. Tags: []string{"key", "AWS"},
  108. StartLine: 0,
  109. EndLine: 0,
  110. StartColumn: 15,
  111. EndColumn: 34,
  112. Entropy: 3.0841837,
  113. },
  114. },
  115. },
  116. {
  117. cfgName: "simple",
  118. fragment: Fragment{
  119. Raw: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  120. FilePath: "tmp.sh",
  121. },
  122. expectedFindings: []report.Finding{
  123. {
  124. Description: "Sidekiq Secret",
  125. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;",
  126. Secret: "cafebabe:deadbeef",
  127. Line: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  128. File: "tmp.sh",
  129. RuleID: "sidekiq-secret",
  130. Tags: []string{},
  131. Entropy: 2.6098502,
  132. StartLine: 0,
  133. EndLine: 0,
  134. StartColumn: 8,
  135. EndColumn: 60,
  136. },
  137. },
  138. },
  139. {
  140. cfgName: "simple",
  141. fragment: Fragment{
  142. Raw: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  143. FilePath: "tmp.sh",
  144. },
  145. expectedFindings: []report.Finding{
  146. {
  147. Description: "Sidekiq Secret",
  148. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=\"cafebabe:deadbeef\"",
  149. Secret: "cafebabe:deadbeef",
  150. File: "tmp.sh",
  151. Line: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  152. RuleID: "sidekiq-secret",
  153. Tags: []string{},
  154. Entropy: 2.6098502,
  155. StartLine: 0,
  156. EndLine: 0,
  157. StartColumn: 21,
  158. EndColumn: 74,
  159. },
  160. },
  161. },
  162. {
  163. cfgName: "simple",
  164. fragment: Fragment{
  165. Raw: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  166. FilePath: "tmp.sh",
  167. },
  168. expectedFindings: []report.Finding{
  169. {
  170. Description: "Sidekiq Sensitive URL",
  171. Match: "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:",
  172. Secret: "cafeb4b3:d3adb33f",
  173. File: "tmp.sh",
  174. Line: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  175. RuleID: "sidekiq-sensitive-url",
  176. Tags: []string{},
  177. Entropy: 2.984234,
  178. StartLine: 0,
  179. EndLine: 0,
  180. StartColumn: 8,
  181. EndColumn: 58,
  182. },
  183. },
  184. },
  185. {
  186. cfgName: "allow_aws_re",
  187. fragment: Fragment{
  188. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  189. FilePath: "tmp.go",
  190. },
  191. expectedFindings: []report.Finding{},
  192. },
  193. {
  194. cfgName: "allow_path",
  195. fragment: Fragment{
  196. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  197. FilePath: "tmp.go",
  198. },
  199. expectedFindings: []report.Finding{},
  200. },
  201. {
  202. cfgName: "allow_commit",
  203. fragment: Fragment{
  204. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  205. FilePath: "tmp.go",
  206. CommitSHA: "allowthiscommit",
  207. },
  208. expectedFindings: []report.Finding{},
  209. },
  210. {
  211. cfgName: "entropy_group",
  212. fragment: Fragment{
  213. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  214. FilePath: "tmp.go",
  215. },
  216. expectedFindings: []report.Finding{
  217. {
  218. Description: "Discord API key",
  219. Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  220. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  221. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  222. File: "tmp.go",
  223. RuleID: "discord-api-key",
  224. Tags: []string{},
  225. Entropy: 3.7906237,
  226. StartLine: 0,
  227. EndLine: 0,
  228. StartColumn: 7,
  229. EndColumn: 93,
  230. },
  231. },
  232. },
  233. {
  234. cfgName: "generic_with_py_path",
  235. fragment: Fragment{
  236. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  237. FilePath: "tmp.go",
  238. },
  239. expectedFindings: []report.Finding{},
  240. },
  241. {
  242. cfgName: "generic_with_py_path",
  243. fragment: Fragment{
  244. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  245. FilePath: "tmp.py",
  246. },
  247. expectedFindings: []report.Finding{
  248. {
  249. Description: "Generic API Key",
  250. Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  251. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  252. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  253. File: "tmp.py",
  254. RuleID: "generic-api-key",
  255. Tags: []string{},
  256. Entropy: 3.7906237,
  257. StartLine: 0,
  258. EndLine: 0,
  259. StartColumn: 22,
  260. EndColumn: 93,
  261. },
  262. },
  263. },
  264. {
  265. cfgName: "path_only",
  266. fragment: Fragment{
  267. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  268. FilePath: "tmp.py",
  269. },
  270. expectedFindings: []report.Finding{
  271. {
  272. Description: "Python Files",
  273. Match: "file detected: tmp.py",
  274. File: "tmp.py",
  275. RuleID: "python-files-only",
  276. Tags: []string{},
  277. },
  278. },
  279. },
  280. {
  281. cfgName: "bad_entropy_group",
  282. fragment: Fragment{
  283. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  284. FilePath: "tmp.go",
  285. },
  286. expectedFindings: []report.Finding{},
  287. wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
  288. },
  289. {
  290. cfgName: "simple",
  291. fragment: Fragment{
  292. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  293. FilePath: filepath.Join(configPath, "simple.toml"),
  294. },
  295. expectedFindings: []report.Finding{},
  296. },
  297. {
  298. cfgName: "allow_global_aws_re",
  299. fragment: Fragment{
  300. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  301. FilePath: "tmp.go",
  302. },
  303. expectedFindings: []report.Finding{},
  304. },
  305. {
  306. cfgName: "generic_with_py_path",
  307. fragment: Fragment{
  308. Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  309. FilePath: "tmp.py",
  310. },
  311. expectedFindings: []report.Finding{},
  312. },
  313. {
  314. cfgName: "path_only",
  315. baselinePath: ".baseline.json",
  316. fragment: Fragment{
  317. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  318. FilePath: ".baseline.json",
  319. },
  320. expectedFindings: []report.Finding{},
  321. },
  322. }
  323. for _, tt := range tests {
  324. viper.Reset()
  325. viper.AddConfigPath(configPath)
  326. viper.SetConfigName(tt.cfgName)
  327. viper.SetConfigType("toml")
  328. err := viper.ReadInConfig()
  329. if err != nil {
  330. t.Error(err)
  331. }
  332. var vc config.ViperConfig
  333. err = viper.Unmarshal(&vc)
  334. if err != nil {
  335. t.Error(err)
  336. }
  337. cfg, err := vc.Translate()
  338. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  339. if tt.wantError != nil {
  340. if err == nil {
  341. t.Errorf("expected error")
  342. }
  343. assert.Equal(t, tt.wantError, err)
  344. }
  345. d := NewDetector(cfg)
  346. d.baselinePath = tt.baselinePath
  347. findings := d.Detect(tt.fragment)
  348. assert.ElementsMatch(t, tt.expectedFindings, findings)
  349. }
  350. }
  351. // TestFromGit tests the FromGit function
  352. func TestFromGit(t *testing.T) {
  353. tests := []struct {
  354. cfgName string
  355. source string
  356. logOpts string
  357. expectedFindings []report.Finding
  358. }{
  359. {
  360. source: filepath.Join(repoBasePath, "small"),
  361. cfgName: "simple",
  362. expectedFindings: []report.Finding{
  363. {
  364. Description: "AWS Access Key",
  365. StartLine: 20,
  366. EndLine: 20,
  367. StartColumn: 19,
  368. EndColumn: 38,
  369. Line: "\n awsToken := \"AKIALALEMEL33243OLIA\"",
  370. Secret: "AKIALALEMEL33243OLIA",
  371. Match: "AKIALALEMEL33243OLIA",
  372. File: "main.go",
  373. Date: "2021-11-02T23:37:53Z",
  374. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  375. Author: "Zachary Rice",
  376. Email: "zricer@protonmail.com",
  377. Message: "Accidentally add a secret",
  378. RuleID: "aws-access-key",
  379. Tags: []string{"key", "AWS"},
  380. Entropy: 3.0841837,
  381. Fingerprint: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587:main.go:aws-access-key:20",
  382. },
  383. {
  384. Description: "AWS Access Key",
  385. StartLine: 9,
  386. EndLine: 9,
  387. StartColumn: 17,
  388. EndColumn: 36,
  389. Secret: "AKIALALEMEL33243OLIA",
  390. Match: "AKIALALEMEL33243OLIA",
  391. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  392. File: "foo/foo.go",
  393. Date: "2021-11-02T23:48:06Z",
  394. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  395. Author: "Zach Rice",
  396. Email: "zricer@protonmail.com",
  397. Message: "adding foo package with secret",
  398. RuleID: "aws-access-key",
  399. Tags: []string{"key", "AWS"},
  400. Entropy: 3.0841837,
  401. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  402. },
  403. },
  404. },
  405. {
  406. source: filepath.Join(repoBasePath, "small"),
  407. logOpts: "--all foo...",
  408. cfgName: "simple",
  409. expectedFindings: []report.Finding{
  410. {
  411. Description: "AWS Access Key",
  412. StartLine: 9,
  413. EndLine: 9,
  414. StartColumn: 17,
  415. EndColumn: 36,
  416. Secret: "AKIALALEMEL33243OLIA",
  417. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  418. Match: "AKIALALEMEL33243OLIA",
  419. Date: "2021-11-02T23:48:06Z",
  420. File: "foo/foo.go",
  421. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  422. Author: "Zach Rice",
  423. Email: "zricer@protonmail.com",
  424. Message: "adding foo package with secret",
  425. RuleID: "aws-access-key",
  426. Tags: []string{"key", "AWS"},
  427. Entropy: 3.0841837,
  428. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  429. },
  430. },
  431. },
  432. }
  433. err := moveDotGit("dotGit", ".git")
  434. if err != nil {
  435. t.Fatal(err)
  436. }
  437. defer func() {
  438. if err := moveDotGit(".git", "dotGit"); err != nil {
  439. t.Error(err)
  440. }
  441. }()
  442. for _, tt := range tests {
  443. viper.AddConfigPath(configPath)
  444. viper.SetConfigName("simple")
  445. viper.SetConfigType("toml")
  446. err = viper.ReadInConfig()
  447. if err != nil {
  448. t.Error(err)
  449. }
  450. var vc config.ViperConfig
  451. err = viper.Unmarshal(&vc)
  452. if err != nil {
  453. t.Error(err)
  454. }
  455. cfg, err := vc.Translate()
  456. if err != nil {
  457. t.Error(err)
  458. }
  459. detector := NewDetector(cfg)
  460. findings, err := detector.DetectGit(tt.source, tt.logOpts, DetectType)
  461. if err != nil {
  462. t.Error(err)
  463. }
  464. for _, f := range findings {
  465. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  466. }
  467. assert.ElementsMatch(t, tt.expectedFindings, findings)
  468. }
  469. }
  470. // TestFromFiles tests the FromFiles function
  471. func TestFromFiles(t *testing.T) {
  472. tests := []struct {
  473. cfgName string
  474. source string
  475. expectedFindings []report.Finding
  476. }{
  477. {
  478. source: filepath.Join(repoBasePath, "nogit"),
  479. cfgName: "simple",
  480. expectedFindings: []report.Finding{
  481. {
  482. Description: "AWS Access Key",
  483. StartLine: 20,
  484. EndLine: 20,
  485. StartColumn: 16,
  486. EndColumn: 35,
  487. Match: "AKIALALEMEL33243OLIA",
  488. Secret: "AKIALALEMEL33243OLIA",
  489. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  490. File: "../testdata/repos/nogit/main.go",
  491. SymlinkFile: "",
  492. RuleID: "aws-access-key",
  493. Tags: []string{"key", "AWS"},
  494. Entropy: 3.0841837,
  495. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  496. },
  497. },
  498. },
  499. {
  500. source: filepath.Join(repoBasePath, "nogit", "main.go"),
  501. cfgName: "simple",
  502. expectedFindings: []report.Finding{
  503. {
  504. Description: "AWS Access Key",
  505. StartLine: 20,
  506. EndLine: 20,
  507. StartColumn: 16,
  508. EndColumn: 35,
  509. Match: "AKIALALEMEL33243OLIA",
  510. Secret: "AKIALALEMEL33243OLIA",
  511. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  512. File: "../testdata/repos/nogit/main.go",
  513. RuleID: "aws-access-key",
  514. Tags: []string{"key", "AWS"},
  515. Entropy: 3.0841837,
  516. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  517. },
  518. },
  519. },
  520. }
  521. for _, tt := range tests {
  522. viper.AddConfigPath(configPath)
  523. viper.SetConfigName("simple")
  524. viper.SetConfigType("toml")
  525. err := viper.ReadInConfig()
  526. if err != nil {
  527. t.Error(err)
  528. }
  529. var vc config.ViperConfig
  530. err = viper.Unmarshal(&vc)
  531. if err != nil {
  532. t.Error(err)
  533. }
  534. cfg, _ := vc.Translate()
  535. detector := NewDetector(cfg)
  536. detector.FollowSymlinks = true
  537. findings, err := detector.DetectFiles(tt.source)
  538. if err != nil {
  539. t.Error(err)
  540. }
  541. assert.ElementsMatch(t, tt.expectedFindings, findings)
  542. }
  543. }
  544. func TestDetectWithSymlinks(t *testing.T) {
  545. tests := []struct {
  546. cfgName string
  547. source string
  548. expectedFindings []report.Finding
  549. }{
  550. {
  551. source: filepath.Join(repoBasePath, "symlinks/file_symlink"),
  552. cfgName: "simple",
  553. expectedFindings: []report.Finding{
  554. {
  555. Description: "Asymmetric Private Key",
  556. StartLine: 1,
  557. EndLine: 1,
  558. StartColumn: 1,
  559. EndColumn: 35,
  560. Match: "-----BEGIN OPENSSH PRIVATE KEY-----",
  561. Secret: "-----BEGIN OPENSSH PRIVATE KEY-----",
  562. Line: "-----BEGIN OPENSSH PRIVATE KEY-----",
  563. File: "../testdata/repos/symlinks/source_file/id_ed25519",
  564. SymlinkFile: "../testdata/repos/symlinks/file_symlink/symlinked_id_ed25519",
  565. RuleID: "apkey",
  566. Tags: []string{"key", "AsymmetricPrivateKey"},
  567. Entropy: 3.587164,
  568. Fingerprint: "../testdata/repos/symlinks/source_file/id_ed25519:apkey:1",
  569. },
  570. },
  571. },
  572. }
  573. for _, tt := range tests {
  574. viper.AddConfigPath(configPath)
  575. viper.SetConfigName("simple")
  576. viper.SetConfigType("toml")
  577. err := viper.ReadInConfig()
  578. if err != nil {
  579. t.Error(err)
  580. }
  581. var vc config.ViperConfig
  582. err = viper.Unmarshal(&vc)
  583. if err != nil {
  584. t.Error(err)
  585. }
  586. cfg, _ := vc.Translate()
  587. detector := NewDetector(cfg)
  588. detector.FollowSymlinks = true
  589. findings, err := detector.DetectFiles(tt.source)
  590. if err != nil {
  591. t.Error(err)
  592. }
  593. assert.ElementsMatch(t, tt.expectedFindings, findings)
  594. }
  595. }
  596. func moveDotGit(from, to string) error {
  597. repoDirs, err := os.ReadDir("../testdata/repos")
  598. if err != nil {
  599. return err
  600. }
  601. for _, dir := range repoDirs {
  602. if to == ".git" {
  603. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  604. if os.IsNotExist(err) {
  605. // dont want to delete the only copy of .git accidentally
  606. continue
  607. }
  608. os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  609. }
  610. if !dir.IsDir() {
  611. continue
  612. }
  613. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  614. if os.IsNotExist(err) {
  615. continue
  616. }
  617. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  618. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  619. if err != nil {
  620. return err
  621. }
  622. }
  623. return nil
  624. }