leaks.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "os/signal"
  11. "strings"
  12. "sync"
  13. "syscall"
  14. )
  15. type ReportElem struct {
  16. Lines []string `json:"lines"`
  17. Commit string `json:"commit"`
  18. }
  19. type GitLeak struct {
  20. leaks []string
  21. commit string
  22. }
  23. func start(opts *Options, repoUrl string) {
  24. c := make(chan os.Signal, 2)
  25. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  26. err := exec.Command("git", "clone", repoUrl).Run()
  27. if err != nil {
  28. log.Fatalf("failed to clone repo %v", err)
  29. }
  30. repoName := strings.Split(repoUrl, "/")[4]
  31. if err := os.Chdir(repoName); err != nil {
  32. log.Fatal(err)
  33. }
  34. go func() {
  35. <-c
  36. cleanup(repoName)
  37. os.Exit(1)
  38. }()
  39. report := getLeaks(repoName)
  40. cleanup(repoName)
  41. reportJson, _ := json.MarshalIndent(report, "", "\t")
  42. err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644)
  43. }
  44. // cleanup changes to app root and recursive rms target repo
  45. func cleanup(repoName string) {
  46. if err := os.Chdir(appRoot); err != nil {
  47. log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
  48. }
  49. err := exec.Command("rm", "-rf", repoName).Run()
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. }
  54. // audit parses git branch --all
  55. func getLeaks(repoName string) []ReportElem {
  56. var (
  57. out []byte
  58. err error
  59. wg sync.WaitGroup
  60. concurrent = 100
  61. semaphoreChan = make(chan struct{}, concurrent)
  62. gitLeaks = make(chan GitLeak)
  63. )
  64. out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output()
  65. if err != nil {
  66. log.Fatalf("error retrieving commits%v\n", err)
  67. }
  68. commits := bytes.Split(out, []byte("\n"))
  69. for j, currCommitB := range commits {
  70. currCommit := string(currCommitB)
  71. if j == len(commits)-2 {
  72. break
  73. }
  74. wg.Add(1)
  75. go func(currCommit string, repoName string) {
  76. defer wg.Done()
  77. var leakPrs bool
  78. var leaks []string
  79. if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
  80. log.Fatal(err)
  81. }
  82. commitCmp := fmt.Sprintf("%s^!", currCommit)
  83. semaphoreChan <- struct{}{}
  84. out, err := exec.Command("git", "diff", commitCmp).Output()
  85. <-semaphoreChan
  86. if err != nil {
  87. return
  88. }
  89. lines := checkRegex(string(out))
  90. if len(lines) == 0 {
  91. return
  92. }
  93. for _, line := range lines {
  94. leakPrs = checkEntropy(line)
  95. if leakPrs {
  96. leaks = append(leaks, line)
  97. }
  98. }
  99. gitLeaks <- GitLeak{leaks, currCommit}
  100. }(currCommit, repoName)
  101. }
  102. go func() {
  103. for gitLeak := range gitLeaks {
  104. if len(gitLeak.leaks) != 0 {
  105. fmt.Println(gitLeak.leaks)
  106. report = append(report, ReportElem{gitLeak.leaks, gitLeak.commit})
  107. }
  108. }
  109. }()
  110. wg.Wait()
  111. return report
  112. }