4
0

leaks.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // LeakElem contains the line and commit of a leak
  16. type LeakElem struct {
  17. Line string `json:"line"`
  18. Commit string `json:"commit"`
  19. Offender string `json:"string"`
  20. Reason string `json:"reason"`
  21. }
  22. // start clones and determines if there are any leaks
  23. func start(opts *Options) {
  24. c := make(chan os.Signal, 2)
  25. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  26. fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
  27. err := exec.Command("git", "clone", opts.RepoURL).Run()
  28. if err != nil {
  29. log.Printf("failed to clone repo %v", err)
  30. return
  31. }
  32. fmt.Printf("Evaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
  33. repoName := getLocalRepoName(opts.RepoURL)
  34. if err = os.Chdir(repoName); err != nil {
  35. log.Fatal(err)
  36. }
  37. go func() {
  38. <-c
  39. cleanup(repoName)
  40. os.Exit(1)
  41. }()
  42. report := getLeaks(repoName, opts)
  43. if len(report) == 0 {
  44. fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL)
  45. }
  46. cleanup(repoName)
  47. reportJSON, _ := json.MarshalIndent(report, "", "\t")
  48. err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJSON, 0644)
  49. if err != nil {
  50. log.Fatalf("Can't write to file: %s", err)
  51. }
  52. }
  53. // getLocalRepoName generates the name of the local clone folder based on the given URL
  54. func getLocalRepoName(url string) string {
  55. splitSlashes := strings.Split(url, "/")
  56. name := splitSlashes[len(splitSlashes)-1]
  57. name = strings.TrimSuffix(name, ".git")
  58. splitColons := strings.Split(name, ":")
  59. name = splitColons[len(splitColons)-1]
  60. return name
  61. }
  62. // cleanup deletes the repo
  63. func cleanup(repoName string) {
  64. if err := os.Chdir(appRoot); err != nil {
  65. log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
  66. }
  67. err := exec.Command("rm", "-rf", repoName).Run()
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. }
  72. // getLeaks will attempt to find gitleaks
  73. func getLeaks(repoName string, opts *Options) []LeakElem {
  74. var (
  75. out []byte
  76. err error
  77. commitWG sync.WaitGroup
  78. gitLeakReceiverWG sync.WaitGroup
  79. gitLeaks = make(chan LeakElem)
  80. report []LeakElem
  81. )
  82. semaphoreChan := make(chan struct{}, opts.Concurrency)
  83. go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  84. for gitLeak := range gitLeaks {
  85. b, err := json.MarshalIndent(gitLeak, "", " ")
  86. if err != nil {
  87. fmt.Println("failed to output leak:", err)
  88. }
  89. fmt.Println(string(b))
  90. report = append(report, gitLeak)
  91. gitLeakReceiverWG.Done()
  92. }
  93. }(&commitWG, &gitLeakReceiverWG)
  94. out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output()
  95. if err != nil {
  96. log.Fatalf("error retrieving commits%v\n", err)
  97. }
  98. commits := bytes.Split(out, []byte("\n"))
  99. for _, currCommitB := range commits {
  100. currCommit := string(currCommitB)
  101. if currCommit == "" {
  102. continue
  103. }
  104. if currCommit == opts.SinceCommit {
  105. break
  106. }
  107. commitWG.Add(1)
  108. go func(currCommit string, repoName string, commitWG *sync.WaitGroup,
  109. gitLeakReceiverWG *sync.WaitGroup) {
  110. defer commitWG.Done()
  111. if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
  112. log.Fatal(err)
  113. }
  114. commitCmp := fmt.Sprintf("%s^!", currCommit)
  115. semaphoreChan <- struct{}{}
  116. out, err := exec.Command("git", "diff", commitCmp).Output()
  117. <-semaphoreChan
  118. if err != nil {
  119. fmt.Printf("error retrieving diff for commit %s try turning concurrency factor down %v\n", currCommit, err)
  120. cleanup(repoName)
  121. return
  122. }
  123. leaks := doChecks(string(out), currCommit)
  124. if len(leaks) == 0 {
  125. return
  126. }
  127. for _, leak := range leaks {
  128. gitLeakReceiverWG.Add(1)
  129. gitLeaks <- leak
  130. }
  131. }(currCommit, repoName, &commitWG, &gitLeakReceiverWG)
  132. }
  133. commitWG.Wait()
  134. gitLeakReceiverWG.Wait()
  135. return report
  136. }