my, things have changed...

This commit is contained in:
zricethezav committed 2018-02-28 19:36:21 -06:00
1 parent faca0e14d9
commit 7919e15a65
6 files changed
+415 -307

No files matched your search

+69 -47
View File
@@ -1,25 +1,24 @@
package main
import (
"path/filepath"
"os"
"fmt"
"os/exec"
"sync"
"bytes"
"encoding/json"
"log"
"strings"
"fmt"
"go.uber.org/zap"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"path"
)
type Repo struct {
name string
url string
path string
name string
url string
path string
status string // TODO
leaks []Leak
owner *Owner
}
type Leak struct {
@@ -41,13 +40,33 @@ type Commit struct {
Msg string
}
func newRepo(owner *Owner, name string, url string) *Repo {
// running gitleaks on local repo
func newLocalRepo(repoPath string) *Repo {
_, name := path.Split(repoPath)
repo := &Repo{
name: name,
url: url,
path: owner.path + "/" + name,
path: repoPath,
}
return repo
}
func newRepo(name string, url string) *Repo {
repo := &Repo{
name: name,
url: url,
// TODO handle existing one
path: opts.ClonePath + "/" + name,
}
return repo
}
func (repo *Repo) rLog(msg string) {
// logger should have these infos: msg, repo, owner, time
logger.Debug("Beginning audit",
zap.String("repo", repo.name),
zap.String("repo_path", repo.path),
)
}
// Audit operates on a single repo and searches the full or partial history of the repo.
@@ -56,7 +75,7 @@ func newRepo(owner *Owner, name string, url string) *Repo {
// commands so that users could opt for doing all clones/diffs in memory.
// Audit also declares two WaitGroups, one for distributing regex/entropy checks, and one for receiving
// the leaks if there are any. This could be done a little more elegantly in the future.
func (repo *Repo) audit(owner *Owner, opts *Options) error {
func (repo *Repo) audit(owner *Owner) (bool, error) {
var (
out []byte
err error
@@ -65,6 +84,7 @@ func (repo *Repo) audit(owner *Owner, opts *Options) error {
gitLeaksChan = make(chan Leak)
leaks []Leak
semaphoreChan = make(chan struct{}, opts.Concurrency)
leaksPst bool
)
dotGitPath := filepath.Join(repo.path, ".git")
@@ -72,22 +92,26 @@ func (repo *Repo) audit(owner *Owner, opts *Options) error {
// Navigate to proper location to being audit. Clone repo
// if not present, otherwise fetch for new changes.
if _, err := os.Stat(dotGitPath); os.IsNotExist(err) {
if opts.LocalMode {
return false, fmt.Errorf("%s does not exist", repo.path)
}
// no repo present, clone it
fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m into %s...\n", repo.url, repo.path)
err = exec.Command("git", "clone", repo.url, repo.path).Run()
if err != nil{
fmt.Println("can run clonse")
if err != nil {
return false, fmt.Errorf("cannot clone %s into %s", repo.url, repo.path)
}
} else {
fmt.Printf("Checking \x1b[37;1m%s\x1b[0m from %s...\n", repo.url, repo.path)
err = exec.Command("git", "fetch").Run()
if err != nil{
fmt.Println("can run fetch")
if err != nil {
return false, fmt.Errorf("cannot fetch %s from %s", repo.url, repo.path)
}
}
if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
fmt.Println("cant chdir")
err = os.Chdir(fmt.Sprintf(repo.path))
if err != nil {
return false, fmt.Errorf("cannot navigate to %s", repo.path)
}
gitFormat := "--format=%H%n%an%n%s%n%ci"
@@ -95,7 +119,7 @@ func (repo *Repo) audit(owner *Owner, opts *Options) error {
"--remotes", "--topo-order", gitFormat).Output()
if err != nil {
fmt.Println("problem with rev list")
return false, fmt.Errorf("could not retreive rev-list from %s", repo.name)
}
revListLines := bytes.Split(out, []byte("\n"))
@@ -107,7 +131,7 @@ func (repo *Repo) audit(owner *Owner, opts *Options) error {
}
commitWG.Add(1)
go auditDiff(commit, repo, &commitWG, &gitLeakReceiverWG, opts,
go auditDiff(commit, repo, &commitWG, &gitLeakReceiverWG,
semaphoreChan, gitLeaksChan)
if commit.Hash == opts.SinceCommit {
@@ -117,37 +141,37 @@ func (repo *Repo) audit(owner *Owner, opts *Options) error {
go reportAggregator(&gitLeakReceiverWG, gitLeaksChan, &leaks)
commitWG.Wait()
gitLeakReceiverWG.Wait()
// repo audit has finished
repo.leaks = leaks
if opts.EnableJSON && len(leaks) != 0 {
repo.writeReport(owner)
if len(leaks) != 0{
leaksPst = true
}
return nil
}
func (repo *Repo) log() {
if opts.ReportPath != "" && len(leaks) != 0 {
err = repo.writeReport()
if err != nil {
return leaksPst, fmt.Errorf("could not write report to %s", opts.ReportPath)
}
}
return leaksPst, nil
}
// Used by audit, writeReport will generate a report and write it out to
// $GITLEAKS_HOME/report/<owner>/<repo>. No report will be generated if
// no leaks have been found
func (repo *Repo) writeReport(owner *Owner) {
func (repo *Repo) writeReport() error {
reportJSON, _ := json.MarshalIndent(repo.leaks, "", "\t")
if _, err := os.Stat(owner.reportPath); os.IsNotExist(err) {
os.Mkdir(repo.owner.reportPath, os.ModePerm)
if _, err := os.Stat(opts.ReportPath); os.IsNotExist(err) {
os.Mkdir(opts.ReportPath, os.ModePerm)
}
reportFileName := fmt.Sprintf("%s_leaks.json", repo.name)
reportFile := filepath.Join(owner.reportPath, reportFileName)
reportFile := filepath.Join(opts.ReportPath, reportFileName)
err := ioutil.WriteFile(reportFile, reportJSON, 0644)
if err != nil {
fmt.Println("cant write report")
return err
}
fmt.Printf("Report written to %s\n", reportFile)
return nil
}
// parseRevList is responsible for parsing the output of
@@ -179,6 +203,7 @@ func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, lea
for gitLeak := range gitLeaks {
b, err := json.MarshalIndent(gitLeak, "", " ")
if err != nil {
// make waitgroup errArray
fmt.Println("failed to output leak:", err)
}
fmt.Println(string(b))
@@ -191,13 +216,14 @@ func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, lea
// Three channels are input here: 1. a semaphore to bind gitleaks, 2. a leak stream, 3. error handling (TODO)
// This func performs a diff and runs regexes checks on each line of the diff.
func auditDiff(currCommit Commit, repo *Repo, commitWG *sync.WaitGroup,
gitLeakReceiverWG *sync.WaitGroup, opts *Options, semaphoreChan chan struct{},
gitLeakReceiverWG *sync.WaitGroup, semaphoreChan chan struct{},
gitLeaks chan Leak) {
// signal to WG this diff is done being audited
defer commitWG.Done()
if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
log.Fatal(err)
// TODO handle this better
os.Exit(EXIT_FAILURE)
}
commitCmp := fmt.Sprintf("%s^!", currCommit.Hash)
@@ -206,13 +232,10 @@ func auditDiff(currCommit Commit, repo *Repo, commitWG *sync.WaitGroup,
<-semaphoreChan
if err != nil {
// TODO
if strings.Contains(err.Error(), "too many files open") {
log.Printf("error retrieving diff for commit %s. Try turning concurrency down. %v\n", currCommit, err)
}
os.Exit(EXIT_FAILURE)
}
leaks := doChecks(string(out), currCommit, opts, repo)
leaks := doChecks(string(out), currCommit, repo)
if len(leaks) == 0 {
return
}
@@ -221,4 +244,3 @@ func auditDiff(currCommit Commit, repo *Repo, commitWG *sync.WaitGroup,
gitLeaks <- leak
}
}