From 4654bc8ea5d54c8a92496f2df37ce55ed0480d20 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Mon, 29 Jan 2018 22:06:34 -0500 Subject: [PATCH 1/6] trying to split out work into goroutines --- repo.go | 56 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/repo.go b/repo.go index 93318039..a5e45db2 100644 --- a/repo.go +++ b/repo.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "strings" + "sync" ) type ReportElem struct { @@ -61,6 +62,7 @@ func (repo Repo) audit() []ReportElem { branch string commits [][]byte leaks []string + wg sync.WaitGroup ) out, err = exec.Command("git", "branch", "--all").Output() @@ -70,31 +72,51 @@ func (repo Repo) audit() []ReportElem { // iterate through branches, git rev-list branches := bytes.Split(out, []byte("\n")) + + messages := make(chan string) + wg.Add(len(branches) - 3) + for i, branchB := range branches { if i < 2 || i == len(branches)-1 { continue } - // if err := os.Chdir(repo.name); err != nil { - // log.Fatal(err) - // } branch = string(bytes.Trim(branchB, " ")) - out, err = exec.Command("git", "rev-list", branch).Output() - if err != nil { - continue - } - // iterate through commits - commits = bytes.Split(out, []byte("\n")) - for j, commitB := range commits { - if j == len(commits)-2 { - break + go func(branch string) { + defer wg.Done() + cmd := exec.Command("git", "rev-list", branch) + + if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil { + log.Fatal(err) } - leaks = checkDiff(string(commitB), string(commits[j+1])) - if len(leaks) != 0 { - report = append(report, ReportElem{leaks, branch, - string(commitB), string(commits[j+1])}) + out, err := cmd.Output() + if err != nil { + log.Fatal(err) } - } + // iterate through commits + commits = bytes.Split(out, []byte("\n")) + for j, commitB := range commits { + fmt.Println(branch, string(commitB), j) + if j == len(commits)-2 { + break + } + + leaks = checkDiff(string(commitB), string(commits[j+1])) + if len(leaks) != 0 { + report = append(report, ReportElem{leaks, branch, + string(commitB), string(commits[j+1])}) + } + } + messages <- branch + }(branch) + go func() { + for i := range messages { + fmt.Println(i) + } + }() + } + wg.Wait() + return report } From 06b3a9b4e75c3eae4d7209d3de37b8e31b737a33 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Mon, 29 Jan 2018 22:20:08 -0500 Subject: [PATCH 2/6] update todo --- checks.go | 3 ++- repo.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/checks.go b/checks.go index 2d17ca83..debc5e3a 100644 --- a/checks.go +++ b/checks.go @@ -1,7 +1,7 @@ package main import ( - _ "fmt" + "fmt" "github.com/nbutton23/zxcvbn-go" "os/exec" "strings" @@ -13,6 +13,7 @@ func checkDiff(commit1 string, commit2 string) []string { var leaks []string _, seen := cache[commit1+commit2] if seen { + fmt.Println("WE HAVE SEEN THIS") return []string{} } diff --git a/repo.go b/repo.go index a5e45db2..2571f3cd 100644 --- a/repo.go +++ b/repo.go @@ -73,6 +73,9 @@ func (repo Repo) audit() []ReportElem { // iterate through branches, git rev-list branches := bytes.Split(out, []byte("\n")) + // TODO instead of making branches concurrent use commits + // make the wait group update the cache. Use channel to communicate + // cache misses and leaks messages := make(chan string) wg.Add(len(branches) - 3) @@ -96,7 +99,7 @@ func (repo Repo) audit() []ReportElem { // iterate through commits commits = bytes.Split(out, []byte("\n")) for j, commitB := range commits { - fmt.Println(branch, string(commitB), j) + // fmt.Println(branch, string(commitB), j) if j == len(commits)-2 { break } From 61c4987cd21f05d674a6fd2cd33fab3d2193ed65 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Tue, 30 Jan 2018 18:12:31 -0600 Subject: [PATCH 3/6] [update] seems to be working with concurrency upgrades... do some timing --- checks.go | 51 ++++++++++++++----------- repo.go | 111 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 109 insertions(+), 53 deletions(-) diff --git a/checks.go b/checks.go index debc5e3a..edc999bd 100644 --- a/checks.go +++ b/checks.go @@ -3,38 +3,47 @@ package main import ( "fmt" "github.com/nbutton23/zxcvbn-go" + "log" + "os" "os/exec" "strings" ) // checkDiff operates on a single diff between to chronological commits -func checkDiff(commit1 string, commit2 string) []string { - var leakPrs bool - var leaks []string - _, seen := cache[commit1+commit2] - if seen { - fmt.Println("WE HAVE SEEN THIS") - return []string{} +func checkDiff(commit1 string, commit2 string, repoName string) []string { + // var leakPrs bool + // var leaks []string + // _, seen := cache[commit1+commit2] + // if seen { + // fmt.Println("WE HAVE SEEN THIS") + // return []string{} + // } + + if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { + log.Fatal(err) } - out, err := exec.Command("git", "diff", commit1, commit2).Output() + cmd := exec.Command("git", "diff", commit1, commit2) + _, err := cmd.Output() + // fmt.Println(string(out)) if err != nil { return []string{} } + return []string{} - cache[commit1+commit2] = true - lines := checkRegex(string(out)) - if len(lines) == 0 { - return []string{} - } - - for _, line := range lines { - leakPrs = checkEntropy(line) - if leakPrs { - leaks = append(leaks, line) - } - } - return leaks + // cache[commit1+commit2] = true + // lines := checkRegex(string(out)) + // if len(lines) == 0 { + // return []string{} + // } + // + // for _, line := range lines { + // leakPrs = checkEntropy(line) + // if leakPrs { + // leaks = append(leaks, line) + // } + // } + // return leaks } // check each line of a diff and see if there are any potential secrets diff --git a/repo.go b/repo.go index 2571f3cd..04824cac 100644 --- a/repo.go +++ b/repo.go @@ -25,6 +25,16 @@ type Repo struct { path string } +type MemoMessage struct { + hash string + leaks []string + commitA string + commitB string + branch string +} + +var lock = sync.RWMutex{} + func repoStart(repoUrl string) { err := exec.Command("git", "clone", repoUrl).Run() if err != nil { @@ -61,8 +71,10 @@ func (repo Repo) audit() []ReportElem { err error branch string commits [][]byte - leaks []string + // leaks []string wg sync.WaitGroup + commitA string + commitB string ) out, err = exec.Command("git", "branch", "--all").Output() @@ -73,53 +85,88 @@ func (repo Repo) audit() []ReportElem { // iterate through branches, git rev-list branches := bytes.Split(out, []byte("\n")) - // TODO instead of making branches concurrent use commits - // make the wait group update the cache. Use channel to communicate - // cache misses and leaks - messages := make(chan string) - wg.Add(len(branches) - 3) + messages := make(chan MemoMessage) for i, branchB := range branches { if i < 2 || i == len(branches)-1 { continue } branch = string(bytes.Trim(branchB, " ")) - go func(branch string) { - defer wg.Done() - cmd := exec.Command("git", "rev-list", branch) + cmd := exec.Command("git", "rev-list", branch) - if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil { - log.Fatal(err) - } + out, err := cmd.Output() + if err != nil { + fmt.Println("skipping branch", branch) + continue + } - out, err := cmd.Output() - if err != nil { - log.Fatal(err) + // iterate through commits + commits = bytes.Split(out, []byte("\n")) + wg.Add(len(commits) - 2) + for j, currCommit := range commits { + if j == len(commits)-2 { + break } - // iterate through commits - commits = bytes.Split(out, []byte("\n")) - for j, commitB := range commits { - // fmt.Println(branch, string(commitB), j) - if j == len(commits)-2 { - break + commitA = string(commits[j+1]) + commitB = string(currCommit) + + go func(commitA string, commitB string, + j int) { + defer wg.Done() + var leakPrs bool + var leaks []string + fmt.Println(j, branch) + + lock.RLock() + _, seen := cache[commitA+commitB] + lock.RUnlock() + if seen { + fmt.Println("WE HAVE SEEN THIS") + return + } + if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil { + log.Fatal(err) + } + cmd := exec.Command("git", "diff", commitA, commitB) + out, err := cmd.Output() + if err != nil { + return + } + lines := checkRegex(string(out)) + if len(lines) == 0 { + return } - leaks = checkDiff(string(commitB), string(commits[j+1])) - if len(leaks) != 0 { - report = append(report, ReportElem{leaks, branch, - string(commitB), string(commits[j+1])}) + for _, line := range lines { + leakPrs = checkEntropy(line) + if leakPrs { + leaks = append(leaks, line) + } } - } - messages <- branch - }(branch) + + // if len(leaks) != 0 { + // report = append(report, ReportElem{leaks, branch, + // string(commitB), string(commits[j+1])}) + // } + messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch} + + }(commitA, commitB, j) + } + go func() { - for i := range messages { - fmt.Println(i) + for memoMsg := range messages { + fmt.Println(memoMsg) + lock.Lock() + cache[memoMsg.hash] = true + lock.Unlock() + if len(memoMsg.leaks) != 0 { + report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch, + memoMsg.commitA, memoMsg.commitB}) + } } }() - + wg.Wait() } - wg.Wait() return report } From a430e46a8ea6d0b00508f3e4e6073c377339928a Mon Sep 17 00:00:00 2001 From: zricethezav Date: Wed, 31 Jan 2018 19:12:25 -0600 Subject: [PATCH 4/6] good ol semaphores throttling git diff --- repo.go | 68 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/repo.go b/repo.go index 04824cac..cb609df2 100644 --- a/repo.go +++ b/repo.go @@ -34,6 +34,7 @@ type MemoMessage struct { } var lock = sync.RWMutex{} +var cmdLock = sync.Mutex{} func repoStart(repoUrl string) { err := exec.Command("git", "clone", repoUrl).Run() @@ -72,9 +73,11 @@ func (repo Repo) audit() []ReportElem { branch string commits [][]byte // leaks []string - wg sync.WaitGroup - commitA string - commitB string + wg sync.WaitGroup + commitA string + commitB string + concurrent = 10 + semaphoreChan = make(chan struct{}, concurrent) ) out, err = exec.Command("git", "branch", "--all").Output() @@ -92,44 +95,51 @@ func (repo Repo) audit() []ReportElem { continue } branch = string(bytes.Trim(branchB, " ")) + cmdLock.Lock() cmd := exec.Command("git", "rev-list", branch) - out, err := cmd.Output() + cmdLock.Unlock() if err != nil { - fmt.Println("skipping branch", branch) + fmt.Println("skipping branch", branch, err) continue } // iterate through commits commits = bytes.Split(out, []byte("\n")) - wg.Add(len(commits) - 2) for j, currCommit := range commits { if j == len(commits)-2 { break } commitA = string(commits[j+1]) commitB = string(currCommit) + _, seen := cache[commitA+commitB] + if seen { + fmt.Println("WE HAVE SEEN THIS") + continue + } else { + cache[commitA+commitB] = true + } + wg.Add(1) go func(commitA string, commitB string, - j int) { + j int, branch string) { defer wg.Done() var leakPrs bool var leaks []string fmt.Println(j, branch) - lock.RLock() - _, seen := cache[commitA+commitB] - lock.RUnlock() - if seen { - fmt.Println("WE HAVE SEEN THIS") - return - } if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil { log.Fatal(err) } + + semaphoreChan <- struct{}{} + fmt.Println("diffing", branch, j) cmd := exec.Command("git", "diff", commitA, commitB) out, err := cmd.Output() + <-semaphoreChan + if err != nil { + fmt.Println("no diff: ", err) return } lines := checkRegex(string(out)) @@ -144,29 +154,21 @@ func (repo Repo) audit() []ReportElem { } } - // if len(leaks) != 0 { - // report = append(report, ReportElem{leaks, branch, - // string(commitB), string(commits[j+1])}) - // } messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch} - }(commitA, commitB, j) + }(commitA, commitB, j, branch) } - - go func() { - for memoMsg := range messages { - fmt.Println(memoMsg) - lock.Lock() - cache[memoMsg.hash] = true - lock.Unlock() - if len(memoMsg.leaks) != 0 { - report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch, - memoMsg.commitA, memoMsg.commitB}) - } - } - }() - wg.Wait() } + go func() { + for memoMsg := range messages { + fmt.Println(memoMsg) + if len(memoMsg.leaks) != 0 { + report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch, + memoMsg.commitA, memoMsg.commitB}) + } + } + }() + wg.Wait() return report } From 15d3d8e7dec0902c3179a5c175b6da6f14a0912d Mon Sep 17 00:00:00 2001 From: zricethezav Date: Fri, 2 Feb 2018 08:09:55 -0600 Subject: [PATCH 5/6] better naming --- checks.go | 44 +------------------------ repo.go => leaks.go | 79 +++++++++++++++++++++------------------------ main.go | 20 ++++-------- options.go | 39 ++++++++++++---------- 4 files changed, 65 insertions(+), 117 deletions(-) rename repo.go => leaks.go (63%) diff --git a/checks.go b/checks.go index edc999bd..338b8f11 100644 --- a/checks.go +++ b/checks.go @@ -1,51 +1,10 @@ package main import ( - "fmt" "github.com/nbutton23/zxcvbn-go" - "log" - "os" - "os/exec" "strings" ) -// checkDiff operates on a single diff between to chronological commits -func checkDiff(commit1 string, commit2 string, repoName string) []string { - // var leakPrs bool - // var leaks []string - // _, seen := cache[commit1+commit2] - // if seen { - // fmt.Println("WE HAVE SEEN THIS") - // return []string{} - // } - - if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { - log.Fatal(err) - } - - cmd := exec.Command("git", "diff", commit1, commit2) - _, err := cmd.Output() - // fmt.Println(string(out)) - if err != nil { - return []string{} - } - return []string{} - - // cache[commit1+commit2] = true - // lines := checkRegex(string(out)) - // if len(lines) == 0 { - // return []string{} - // } - // - // for _, line := range lines { - // leakPrs = checkEntropy(line) - // if leakPrs { - // leaks = append(leaks, line) - // } - // } - // return leaks -} - // check each line of a diff and see if there are any potential secrets // [1] https://people.eecs.berkeley.edu/~rohanpadhye/files/key_leaks-msr15.pdf func checkRegex(diff string) []string { @@ -54,7 +13,7 @@ func checkRegex(diff string) []string { lines := strings.Split(diff, "\n") for _, line := range lines { // doubtful a leak would be on a line > 120 characters - if len(line) == 0 || len(line) > 80 { + if len(line) == 0 || len(line) > 120 { continue } for _, re := range regexes { @@ -85,7 +44,6 @@ func checkEntropy(target string) bool { return false } - // entropy := shannonEntropy(target) entropy := zxcvbn.PasswordStrength(target, nil).Entropy // tune this/make option diff --git a/repo.go b/leaks.go similarity index 63% rename from repo.go rename to leaks.go index cb609df2..3964f991 100644 --- a/repo.go +++ b/leaks.go @@ -8,8 +8,10 @@ import ( "log" "os" "os/exec" + "os/signal" "strings" "sync" + "syscall" ) type ReportElem struct { @@ -19,13 +21,7 @@ type ReportElem struct { CommitB string `json:"commitB"` } -type Repo struct { - url string - name string - path string -} - -type MemoMessage struct { +type GitLeak struct { hash string leaks []string commitA string @@ -33,10 +29,10 @@ type MemoMessage struct { branch string } -var lock = sync.RWMutex{} -var cmdLock = sync.Mutex{} +func start(opts *Options, repoUrl string) { + c := make(chan os.Signal, 2) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) -func repoStart(repoUrl string) { err := exec.Command("git", "clone", repoUrl).Run() if err != nil { log.Fatalf("failed to clone repo %v", err) @@ -45,39 +41,45 @@ func repoStart(repoUrl string) { if err := os.Chdir(repoName); err != nil { log.Fatal(err) } + go func() { + <-c + cleanup(repoName) + os.Exit(1) + }() - repo := Repo{repoUrl, repoName, ""} - report := repo.audit() - repo.cleanup() + report := getLeaks(repoName) + cleanup(repoName) reportJson, _ := json.MarshalIndent(report, "", "\t") - err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repo.name), reportJson, 0644) + err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644) } // cleanup changes to app root and recursive rms target repo -func (repo Repo) cleanup() { +func cleanup(repoName string) { if err := os.Chdir(appRoot); err != nil { log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err) } - err := exec.Command("rm", "-rf", repo.name).Run() + err := exec.Command("rm", "-rf", repoName).Run() if err != nil { log.Fatal(err) } } // audit parses git branch --all -func (repo Repo) audit() []ReportElem { +func getLeaks(repoName string) []ReportElem { var ( - out []byte - err error - branch string - commits [][]byte - // leaks []string + out []byte + err error + branch string + commits [][]byte wg sync.WaitGroup commitA string commitB string - concurrent = 10 + concurrent = 100 semaphoreChan = make(chan struct{}, concurrent) + gitLeaks = make(chan GitLeak) + cmdLock = sync.Mutex{} + cache = make(map[string]bool) ) out, err = exec.Command("git", "branch", "--all").Output() @@ -88,16 +90,13 @@ func (repo Repo) audit() []ReportElem { // iterate through branches, git rev-list branches := bytes.Split(out, []byte("\n")) - messages := make(chan MemoMessage) - for i, branchB := range branches { if i < 2 || i == len(branches)-1 { continue } branch = string(bytes.Trim(branchB, " ")) cmdLock.Lock() - cmd := exec.Command("git", "rev-list", branch) - out, err := cmd.Output() + out, err := exec.Command("git", "rev-list", "--topo-order", branch).Output() cmdLock.Unlock() if err != nil { fmt.Println("skipping branch", branch, err) @@ -114,32 +113,26 @@ func (repo Repo) audit() []ReportElem { commitB = string(currCommit) _, seen := cache[commitA+commitB] if seen { - fmt.Println("WE HAVE SEEN THIS") continue } else { cache[commitA+commitB] = true } wg.Add(1) - go func(commitA string, commitB string, - j int, branch string) { + go func(commitA string, commitB string, branch string, repoName string) { defer wg.Done() var leakPrs bool var leaks []string - fmt.Println(j, branch) - if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil { + if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { log.Fatal(err) } semaphoreChan <- struct{}{} - fmt.Println("diffing", branch, j) - cmd := exec.Command("git", "diff", commitA, commitB) - out, err := cmd.Output() + out, err := exec.Command("git", "diff", commitA, commitB).Output() <-semaphoreChan if err != nil { - fmt.Println("no diff: ", err) return } lines := checkRegex(string(out)) @@ -154,17 +147,17 @@ func (repo Repo) audit() []ReportElem { } } - messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch} + gitLeaks <- GitLeak{commitA + commitB, leaks, commitA, commitB, branch} - }(commitA, commitB, j, branch) + }(commitA, commitB, branch, repoName) } } go func() { - for memoMsg := range messages { - fmt.Println(memoMsg) - if len(memoMsg.leaks) != 0 { - report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch, - memoMsg.commitA, memoMsg.commitB}) + for gitLeak := range gitLeaks { + if len(gitLeak.leaks) != 0 { + fmt.Println(gitLeak.branch) + report = append(report, ReportElem{gitLeak.leaks, gitLeak.branch, + gitLeak.commitA, gitLeak.commitB}) } } }() diff --git a/main.go b/main.go index 81692b51..56d18e9d 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,7 @@ import ( "regexp" ) - var ( - cache map[string]bool appRoot string regexes map[string]*regexp.Regexp assignRegex *regexp.Regexp @@ -16,9 +14,9 @@ var ( func init() { appRoot, _ = os.Getwd() - cache = make(map[string]bool) // TODO update regex to look for things like: - // client("fewafewakwafejwkaf", + // TODO ability to add/filter regex + // client("AKAI32fJ334...", regexes = map[string]*regexp.Regexp{ "github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].*(=|:|:=|<-).*\w+.*`), "aws": regexp.MustCompile(`[a|A][w|W][s|S].*(=|:=|:|<-).*\w+.*`), @@ -32,14 +30,8 @@ func init() { } func main() { - args := os.Args[1:] - opts := parseOptions(args) - start(opts) + args := os.Args[2:] + repoUrl := os.Args[1] + opts := parseOptions(args, repoUrl) + start(opts, repoUrl) } - -func start(opts *Options) { - if opts.Repo != "" { - repoStart(opts.Repo) - } -} - diff --git a/options.go b/options.go index 0567e650..441dabde 100644 --- a/options.go +++ b/options.go @@ -3,26 +3,21 @@ package main import ( "fmt" "os" + "strconv" ) // TODO regex on type.. user/organization can be treated as the same: // hittps://github.com/ // hittps://github.com//repo -const usage = `usage: gogethunt [options] +const usage = `usage: gitleaks [git link] [options] Options: - -u --user Target user - -r --repo Target repo - -o --org Target organization - -h --help Display this message - -e --entropy Enable entropy detection - -r --regex Enable regex detection + -c Concurrency factor (potential number of git files open) + -h --help Display this message ` type Options struct { - User string - Repo string - Org string + Concurrency int } func help() { @@ -30,6 +25,20 @@ func help() { os.Exit(1) } +func optionsNextInt(args []string, i *int) int { + if len(args) > *i+1 { + *i++ + } else { + help() + } + argInt, err := strconv.Atoi(args[*i]) + if err != nil { + fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i]) + help() + } + return argInt +} + func optionsNextString(args []string, i *int) string { if len(args) > *i+1 { *i++ @@ -39,17 +48,13 @@ func optionsNextString(args []string, i *int) string { return args[*i] } -func parseOptions(args []string) *Options { +func parseOptions(args []string, repoUrl string) *Options { opts := &Options{} for i := 0; i < len(args); i++ { arg := args[i] switch arg { - case "-o", "--org": - opts.Org = optionsNextString(args, &i) - case "-r", "--repo": - opts.Repo = optionsNextString(args, &i) - case "-u", "--user": - opts.User = optionsNextString(args, &i) + case "-c": + opts.Concurrency = optionsNextInt(args, &i) case "-h", "--help": help() return nil From e6fd11d600e7aca99e5384907a65d20752c77438 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sat, 3 Feb 2018 10:20:03 -0600 Subject: [PATCH 6/6] using git diff COMMIT^! for comparing all commits --- leaks.go | 116 +++++++++++++++++++------------------------------------ 1 file changed, 40 insertions(+), 76 deletions(-) diff --git a/leaks.go b/leaks.go index 3964f991..4cee0518 100644 --- a/leaks.go +++ b/leaks.go @@ -15,18 +15,13 @@ import ( ) type ReportElem struct { - Lines []string `json:"lines"` - Branch string `json:"branch"` - CommitA string `json:"commitA"` - CommitB string `json:"commitB"` + Lines []string `json:"lines"` + Commit string `json:"commit"` } type GitLeak struct { - hash string - leaks []string - commitA string - commitB string - branch string + leaks []string + commit string } func start(opts *Options, repoUrl string) { @@ -70,94 +65,63 @@ func getLeaks(repoName string) []ReportElem { var ( out []byte err error - branch string - commits [][]byte wg sync.WaitGroup - commitA string - commitB string concurrent = 100 semaphoreChan = make(chan struct{}, concurrent) gitLeaks = make(chan GitLeak) - cmdLock = sync.Mutex{} - cache = make(map[string]bool) ) - out, err = exec.Command("git", "branch", "--all").Output() + out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output() if err != nil { - log.Fatalf("error retrieving branches %v\n", err) + log.Fatalf("error retrieving commits%v\n", err) } - // iterate through branches, git rev-list - branches := bytes.Split(out, []byte("\n")) - - for i, branchB := range branches { - if i < 2 || i == len(branches)-1 { - continue - } - branch = string(bytes.Trim(branchB, " ")) - cmdLock.Lock() - out, err := exec.Command("git", "rev-list", "--topo-order", branch).Output() - cmdLock.Unlock() - if err != nil { - fmt.Println("skipping branch", branch, err) - continue + commits := bytes.Split(out, []byte("\n")) + for j, currCommitB := range commits { + currCommit := string(currCommitB) + if j == len(commits)-2 { + break } - // iterate through commits - commits = bytes.Split(out, []byte("\n")) - for j, currCommit := range commits { - if j == len(commits)-2 { - break - } - commitA = string(commits[j+1]) - commitB = string(currCommit) - _, seen := cache[commitA+commitB] - if seen { - continue - } else { - cache[commitA+commitB] = true + wg.Add(1) + go func(currCommit string, repoName string) { + defer wg.Done() + var leakPrs bool + var leaks []string + + if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { + log.Fatal(err) } - wg.Add(1) - go func(commitA string, commitB string, branch string, repoName string) { - defer wg.Done() - var leakPrs bool - var leaks []string + commitCmp := fmt.Sprintf("%s^!", currCommit) + semaphoreChan <- struct{}{} + out, err := exec.Command("git", "diff", commitCmp).Output() + <-semaphoreChan - if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil { - log.Fatal(err) + if err != nil { + return + } + lines := checkRegex(string(out)) + if len(lines) == 0 { + return + } + + for _, line := range lines { + leakPrs = checkEntropy(line) + if leakPrs { + leaks = append(leaks, line) } + } - semaphoreChan <- struct{}{} - out, err := exec.Command("git", "diff", commitA, commitB).Output() - <-semaphoreChan + gitLeaks <- GitLeak{leaks, currCommit} - if err != nil { - return - } - lines := checkRegex(string(out)) - if len(lines) == 0 { - return - } - - for _, line := range lines { - leakPrs = checkEntropy(line) - if leakPrs { - leaks = append(leaks, line) - } - } - - gitLeaks <- GitLeak{commitA + commitB, leaks, commitA, commitB, branch} - - }(commitA, commitB, branch, repoName) - } + }(currCommit, repoName) } go func() { for gitLeak := range gitLeaks { if len(gitLeak.leaks) != 0 { - fmt.Println(gitLeak.branch) - report = append(report, ReportElem{gitLeak.leaks, gitLeak.branch, - gitLeak.commitA, gitLeak.commitB}) + fmt.Println(gitLeak.leaks) + report = append(report, ReportElem{gitLeak.leaks, gitLeak.commit}) } } }()