Răsfoiți Sursa

[update] seems to be working with concurrency upgrades... do some timing

zricethezav 8 ani în urmă
părinte
comite
61c4987cd2
2 a modificat fișierele cu 109 adăugiri și 53 ștergeri
  1. 30 21
      checks.go
  2. 79 32
      repo.go

+ 30 - 21
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{}
+	// }
 
-	out, err := exec.Command("git", "diff", commit1, commit2).Output()
-	if err != nil {
-		return []string{}
+	if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
+		log.Fatal(err)
 	}
 
-	cache[commit1+commit2] = true
-	lines := checkRegex(string(out))
-	if len(lines) == 0 {
+	cmd := exec.Command("git", "diff", commit1, commit2)
+	_, err := cmd.Output()
+	// fmt.Println(string(out))
+	if err != nil {
 		return []string{}
 	}
+	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

+ 79 - 32
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 <branch>
 	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
 }