From faca0e14d91e974afec4d491e504a8eaebefbb31 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Tue, 27 Feb 2018 21:56:46 -0600 Subject: [PATCH 1/5] refactoring after actually learning some go patterns --- checks.go | 10 +- checks_test.go | 4 +- leaks.go | 209 ++++++---------------------------------- main.go | 201 ++++++++------------------------------- owner.go | 251 +++++++++++++++++++++++++++++++++++++++++++++++++ repo.go | 224 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 549 insertions(+), 350 deletions(-) create mode 100644 owner.go create mode 100644 repo.go diff --git a/checks.go b/checks.go index 86cfdcca..78993189 100644 --- a/checks.go +++ b/checks.go @@ -9,11 +9,11 @@ import ( // TODO LOCAL REPO!!!! // checks Regex and if enabled, entropy and stopwords -func doChecks(diff string, commit Commit, opts *Options, repo RepoDesc) []LeakElem { +func doChecks(diff string, commit Commit, opts *Options, repo *Repo) []Leak { var ( match string - leaks []LeakElem - leak LeakElem + leaks []Leak + leak Leak ) lines := strings.Split(diff, "\n") @@ -34,7 +34,7 @@ func doChecks(diff string, commit Commit, opts *Options, repo RepoDesc) []LeakEl continue } - leak = LeakElem{ + leak = Leak{ Line: line, Commit: commit.Hash, Offender: match, @@ -109,7 +109,7 @@ func checkShannonEntropy(target string, opts *Options) bool { func containsStopWords(target string) bool { // Convert to lowercase to reduce the number of loops needed. target = strings.ToLower(target) - + for _, stopWord := range stopWords { if strings.Contains(target, stopWord) { return true diff --git a/checks_test.go b/checks_test.go index 0d1e5905..89a377de 100644 --- a/checks_test.go +++ b/checks_test.go @@ -5,14 +5,14 @@ import ( ) func TestCheckRegex(t *testing.T) { - var results []LeakElem + var results []Leak opts := &Options{ Concurrency: 10, B64EntropyCutoff: 70, HexEntropyCutoff: 40, Entropy: false, } - repo := RepoDesc{ + repo := Repo{ url: "someurl", } commit := Commit{} diff --git a/leaks.go b/leaks.go index 3281f700..ec732b3a 100644 --- a/leaks.go +++ b/leaks.go @@ -1,66 +1,25 @@ package main -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "log" - "os" - "os/exec" - "os/signal" - "path/filepath" - "strings" - "sync" - "syscall" -) +// TODO https://medium.com/@sebdah/go-best-practices-error-handling-2d15e1f0c5ee +// implement better error handling -// LeakElem contains the line and commit of a leak -type LeakElem struct { - Line string `json:"line"` - Commit string `json:"commit"` - Offender string `json:"string"` - Reason string `json:"reason"` - Msg string `json:"commitMsg"` - Time string `json:"time"` - Author string `json:"author"` - File string `json:"file"` - RepoURL string `json:"repoURL"` -} +// Commit is so and so -type Commit struct { - Hash string - Author string - Time string - Msg string -} - -func rmTmp(owner *Owner) { - if _, err := os.Stat(owner.path); err == nil { - err := os.RemoveAll(owner.path) - log.Printf("\nCleaning up tmp repos in %s\n", owner.path) - if err != nil { - log.Printf("failed to properly remove tmp gitleaks dir: %v", err) - } - } - os.Exit(1) -} - -// start -func start(repos []RepoDesc, owner *Owner, opts *Options) { - var report []LeakElem - if opts.Tmp { - defer rmTmp(owner) - } +/* +// start kicks off the audit +func start(repos []Repo, owner *Owner, opts *Options) error { + var ( + report []Leak + err error + ) + defer rmTmpDirs(owner, opts) // interrupt handling c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c - if opts.Tmp { - rmTmp(owner) - } + rmTmpDirs(owner, opts) os.Exit(1) }() @@ -73,24 +32,23 @@ func start(repos []RepoDesc, owner *Owner, opts *Options) { } // use pre-cloned repo fmt.Printf("Checking \x1b[37;1m%s\x1b[0m...\n", repo.url) - err := exec.Command("git", "fetch").Run() - if err != nil { - log.Printf("failed to fetch repo %v", err) - return - } - report = getLeaks(repo, owner, opts) + err = exec.Command("git", "fetch").Run() } else { // no repo present, clone it if err := os.Chdir(fmt.Sprintf(owner.path)); err != nil { log.Fatal(err) } fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url) - err := exec.Command("git", "clone", repo.url).Run() - if err != nil { - fmt.Printf("failed to clone repo %v", err) - return - } - report = getLeaks(repo, owner, opts) + err = exec.Command("git", "clone", repo.url).Run() + } + if err != nil { + log.Printf("failed to fetch repo %v", err) + return nil + } + + report, err = audit(&repo, opts) + if err != nil { + return nil } if len(report) == 0 { @@ -98,123 +56,14 @@ func start(repos []RepoDesc, owner *Owner, opts *Options) { } if opts.EnableJSON && len(report) != 0 { - outputGitLeaksReport(report, repo, opts) + writeReport(report, repo) } } + return nil } +*/ -// outputGitLeaksReport -func outputGitLeaksReport(report []LeakElem, repo RepoDesc, opts *Options) { - reportJSON, _ := json.MarshalIndent(report, "", "\t") - if _, err := os.Stat(repo.owner.reportPath); os.IsNotExist(err) { - os.Mkdir(repo.owner.reportPath, os.ModePerm) - } +// Used by start, writeReport will generate a report and write it out to +// $GITLEAKS_HOME/report//. No report will be generated if +// no leaks have been found - reportFileName := fmt.Sprintf("%s_leaks.json", repo.name) - reportFile := filepath.Join(repo.owner.reportPath, reportFileName) - err := ioutil.WriteFile(reportFile, reportJSON, 0644) - if err != nil { - log.Fatalf("Can't write to file: %s", err) - } - fmt.Printf("Report written to %s\n", reportFile) -} - -// getLeaks will attempt to find gitleaks -func getLeaks(repo RepoDesc, owner *Owner, opts *Options) []LeakElem { - var ( - out []byte - err error - commitWG sync.WaitGroup - gitLeakReceiverWG sync.WaitGroup - gitLeaks = make(chan LeakElem) - report []LeakElem - ) - semaphoreChan := make(chan struct{}, opts.Concurrency) - - go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) { - for gitLeak := range gitLeaks { - b, err := json.MarshalIndent(gitLeak, "", " ") - if err != nil { - fmt.Println("failed to output leak:", err) - } - fmt.Println(string(b)) - report = append(report, gitLeak) - gitLeakReceiverWG.Done() - } - }(&commitWG, &gitLeakReceiverWG) - - if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil { - log.Fatal(err) - } - - gitFormat := "--format=%H%n%an%n%s%n%ci" - out, err = exec.Command("git", "rev-list", "--all", - "--remotes", "--topo-order", gitFormat).Output() - if err != nil { - log.Fatalf("error retrieving commits%v\n", err) - } - - revListLines := bytes.Split(out, []byte("\n")) - commits := parseFormattedRevList(revListLines) - - for _, commit := range commits { - if commit.Hash == "" { - continue - } - - commitWG.Add(1) - go func(currCommit Commit, repoName string, commitWG *sync.WaitGroup, - gitLeakReceiverWG *sync.WaitGroup, opts *Options) { - defer commitWG.Done() - if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil { - log.Fatal(err) - } - - commitCmp := fmt.Sprintf("%s^!", currCommit.Hash) - semaphoreChan <- struct{}{} - out, err := exec.Command("git", "diff", commitCmp).Output() - <-semaphoreChan - - if err != nil { - 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) - } - if opts.Tmp { - rmTmp(owner) - } - } - - leaks := doChecks(string(out), currCommit, opts, repo) - if len(leaks) == 0 { - return - } - for _, leak := range leaks { - gitLeakReceiverWG.Add(1) - gitLeaks <- leak - } - - }(commit, repo.name, &commitWG, &gitLeakReceiverWG, opts) - - if commit.Hash == opts.SinceCommit { - break - } - } - - commitWG.Wait() - gitLeakReceiverWG.Wait() - return report -} - -func parseFormattedRevList(revList [][]byte) []Commit { - var commits []Commit - for i := 0; i < len(revList)-1; i = i + 5 { - commit := Commit{ - Hash: string(revList[i+1]), - Author: string(revList[i+2]), - Msg: string(revList[i+3]), - Time: string(revList[i+4]), - } - commits = append(commits, commit) - } - return commits -} diff --git a/main.go b/main.go index fc5d82df..9af8bb28 100644 --- a/main.go +++ b/main.go @@ -1,20 +1,22 @@ package main import ( - "context" - "github.com/google/go-github/github" + _ "fmt" "github.com/mitchellh/go-homedir" - "golang.org/x/oauth2" - "io/ioutil" "log" - "net/http" + _"io/ioutil" "os" - "path" "path/filepath" "regexp" - "strings" + "go.uber.org/zap" + _"time" + "go.uber.org/zap/zapcore" ) +const EXIT_CLEAN = 0 +const EXIT_FAILURE = 1 +const EXIT_LEAKS = 2 + var ( regexes map[string]*regexp.Regexp stopWords []string @@ -25,29 +27,13 @@ var ( gitLeaksPath string gitLeaksClonePath string gitLeaksReportPath string + logger *zap.Logger ) -type RepoDesc struct { - name string - url string - path string - owner *Owner -} - -type Owner struct { - name string - url string - accountType string - path string - reportPath string -} - func init() { base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" hexChars = "1234567890abcdefABCDEF" - stopWords = []string{"setting", "info", "env", "environment"} - regexes = map[string]*regexp.Regexp{ "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"), "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"), @@ -60,14 +46,19 @@ func init() { "Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"), // "Custom": regexp.MustCompile(".*") } - assignRegex = regexp.MustCompile(`(=|:|:=|<-)`) fileDiffRegex = regexp.MustCompile("diff --git a.+b/") - homeDir, err := homedir.Dir() - if err != nil { - log.Fatal("Cant find home dir") + assignRegex = regexp.MustCompile(`(=|:|:=|<-)`) + + // gitleaks dir defaults to $HOME/.gitleaks if no env var GITLEAKS_HOME is present. + gitLeaksPath = os.Getenv("GITLEAKS_HOME") + if gitLeaksPath == "" { + homeDir, err := homedir.Dir() + if err != nil { + log.Fatal("Cant find home dir") + } + gitLeaksPath = filepath.Join(homeDir, ".gitleaks") } - gitLeaksPath = filepath.Join(homeDir, ".gitleaks") if _, err := os.Stat(gitLeaksPath); os.IsNotExist(err) { os.Mkdir(gitLeaksPath, os.ModePerm) } @@ -81,141 +72,25 @@ func init() { } } -// getOwner -func getOwner(opts *Options) *Owner { - var owner Owner - if opts.RepoURL != "" { - splitSlashes := strings.Split(opts.RepoURL, "/") - owner = Owner{ - name: splitSlashes[len(splitSlashes)-2], - url: opts.RepoURL, - accountType: "users", - } - - } else if opts.UserURL != "" { - _, ownerName := path.Split(opts.UserURL) - owner = Owner{ - name: ownerName, - url: opts.UserURL, - accountType: "user", - } - } else if opts.OrgURL != "" { - _, ownerName := path.Split(opts.OrgURL) - owner = Owner{ - name: ownerName, - url: opts.OrgURL, - accountType: "org", - } - } - - if opts.Tmp { - dir, err := ioutil.TempDir("", owner.name) - if err != nil { - log.Fatal("Cant make temp dir") - } - owner.path = dir - } else { - owner.path = filepath.Join(gitLeaksClonePath, owner.name) - if _, err := os.Stat(owner.path); os.IsNotExist(err) { - os.Mkdir(owner.path, os.ModePerm) - } - } - owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name) - return &owner -} - -// getRepos -func getRepos(opts *Options, owner *Owner) []RepoDesc { - var ( - allRepos []*github.Repository - repos []*github.Repository - repoDescs []RepoDesc - resp *github.Response - ctx = context.Background() - err error - ) - if opts.RepoURL != "" { - _, repoName := path.Split(opts.RepoURL) - if strings.HasSuffix(repoName, ".git") { - repoName = repoName[:len(repoName)-4] - } - ownerPath := filepath.Join(owner.path, repoName) - repo := RepoDesc{ - name: repoName, - url: opts.RepoURL, - owner: owner, - path: ownerPath} - repoDescs = append(repoDescs, repo) - return repoDescs - } - - tokenClient := getAccessToken(opts) - gitClient := github.NewClient(tokenClient) - - // TODO include fork check - orgOpt := &github.RepositoryListByOrgOptions{ - ListOptions: github.ListOptions{PerPage: 10}, - } - userOpt := &github.RepositoryListOptions{ - ListOptions: github.ListOptions{PerPage: 10}, - } - - for { - if opts.UserURL != "" { - repos, resp, err = gitClient.Repositories.List( - ctx, owner.name, userOpt) - } else if opts.OrgURL != "" { - repos, resp, err = gitClient.Repositories.ListByOrg( - ctx, owner.name, orgOpt) - } - allRepos = append(allRepos, repos...) - if resp.NextPage == 0 || err != nil { - break - } - - for _, repo := range repos { - repoPath := filepath.Join(owner.path, *repo.Name) - repoDescs = append(repoDescs, - RepoDesc{ - name: *repo.Name, - url: *repo.CloneURL, - owner: owner, - path: repoPath}) - } - - orgOpt.Page = resp.NextPage - userOpt.Page = resp.NextPage - } - - return repoDescs -} - -// getAccessToken checks -// 1. option -// 2. env var -// TODO. $HOME/.gitleaks/.creds -func getAccessToken(opts *Options) *http.Client { - var token string - if opts.Token != "" { - token = opts.Token - } else { - token = os.Getenv("GITHUB_TOKEN") - } - if token == "" { - return nil - } - - tokenService := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - tokenClient := oauth2.NewClient(context.Background(), tokenService) - return tokenClient -} - func main() { + // TODO abstract logging + atom := zap.NewAtomicLevel() + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "" + logger = zap.New(zapcore.NewCore( + zapcore.NewJSONEncoder(encoderCfg), + zapcore.Lock(os.Stdout), + atom, + )) + logger.Info("HEY") + atom.SetLevel(zap.InfoLevel) + logger.Info("HEY") + + args := os.Args[1:] opts := parseOptions(args) - owner := getOwner(opts) - repos := getRepos(opts, owner) - start(repos, owner, opts) + owner := newOwner(opts) + owner.auditRepos(opts) + // repos := getRepos(opts, owner) + // start(repos, owner, opts) } diff --git a/owner.go b/owner.go new file mode 100644 index 00000000..1af6cc28 --- /dev/null +++ b/owner.go @@ -0,0 +1,251 @@ +package main + +import ( + "path" + "io/ioutil" + "path/filepath" + "os" + "github.com/google/go-github/github" + "strings" + "context" + "golang.org/x/oauth2" + "net/http" + "log" + "os/signal" + _"fmt" +) + +type Owner struct { + name string + url string + accountType string + path string + reportPath string + repos []Repo +} + +// newOwner instantiates an owner and creates any necessary resources for said owner. +// newOwner returns a Owner struct pointer +func newOwner(opts *Options) *Owner { + name, err := ownerName(opts) + owner := &Owner{ + name: name, + url: opts.UserURL, + accountType: ownerType(opts), + } + + if err != nil { + owner.failf() + } + + // listen for ctrl-c + // NOTE: need some help on how to actually shut down gracefully. + // On interrupt a repo may still be trying to clone... This has no + // actual effect other than extraneous logging. + sigC := make(chan os.Signal, 1) + signal.Notify(sigC, os.Interrupt, os.Interrupt) + go func() { + <-sigC + owner.rmTmp() + }() + + owner.setupDir(opts) + owner.fetchRepos(opts) + return owner +} + +// fetchRepos is used by newOwner and is responsible for fetching one or more +// of the owner's repos. If opts.RepoURL is not the empty string then fetchRepos will +// only grab the repo specified in opts.RepoURL. Otherwise, fetchRepos will reach out to +// github's api and grab all repos associated with owner. +func (owner *Owner) fetchRepos(opts *Options) { + ctx := context.Background() + if owner.accountType == "" { + // single repo, ambiguous account type + _, repoName := path.Split(opts.RepoURL) + repo := newRepo(owner, repoName, opts.RepoURL) + owner.repos = append(owner.repos, *repo) + } else { + // org or user account type, would fail if not valid before + tokenClient := githubTokenClient(opts) + gitClient := github.NewClient(tokenClient) + + if owner.accountType == "org" { + // org account type + orgOpt := &github.RepositoryListByOrgOptions{ + ListOptions: github.ListOptions{PerPage: 10}, + } + owner.fetchOrgRepos(orgOpt, gitClient, ctx) + } else { + // user account type + userOpt := &github.RepositoryListOptions{ + ListOptions: github.ListOptions{PerPage: 10}, + } + owner.fetchUserRepos(userOpt, gitClient, ctx) + } + } +} + +// fetchOrgRepos used by fetchRepos is responsible for parsing github's org repo response. If no +// github token is available then fetchOrgRepos might run into a rate limit in which case owner will +// log an error and gitleaks will exit. The rate limit for no token is 50 req/hour... not much. +func (owner *Owner) fetchOrgRepos(orgOpts *github.RepositoryListByOrgOptions, gitClient *github.Client, + ctx context.Context) { + var ( + githubRepos []*github.Repository + resp *github.Response + err error + ) + + for { + githubRepos, resp, err = gitClient.Repositories.ListByOrg( + ctx, owner.name, orgOpts) + owner.addRepos(githubRepos) + if _, ok := err.(*github.RateLimitError); ok { + log.Println("hit rate limit") + break + } else if err != nil { + log.Println("other error") + break + } else if resp.NextPage == 0 { + break + } + orgOpts.Page = resp.NextPage + } +} + +// fetchUserRepos used by fetchRepos is responsible for parsing github's user repo response. If no +// github token is available then fetchUserRepos might run into a rate limit in which case owner will +// log an error and gitleaks will exit. The rate limit for no token is 50 req/hour... not much. +// sorry for the redundancy +func (owner *Owner) fetchUserRepos(userOpts *github.RepositoryListOptions, gitClient *github.Client, + ctx context.Context) { + var ( + githubRepos []*github.Repository + resp *github.Response + err error + ) + for { + githubRepos, resp, err = gitClient.Repositories.List( + ctx, owner.name, userOpts) + owner.addRepos(githubRepos) + if _, ok := err.(*github.RateLimitError); ok { + log.Println("hit rate limit") + break + } else if err != nil { + log.Println("other error") + break + } else if resp.NextPage == 0 { + break + } + userOpts.Page = resp.NextPage + } +} + +// addRepos used by fetchUserRepos and fetchOrgRepos appends new repos from +// github's org/user response. +func (owner *Owner) addRepos (githubRepos []*github.Repository) { + for _, repo := range githubRepos { + owner.repos = append(owner.repos, *newRepo(owner, *repo.Name, *repo.CloneURL)) + } +} + +// auditRepos +func (owner *Owner) auditRepos(opts *Options) { + for _, repo := range owner.repos { + err := repo.audit(owner, opts) + if err != nil { + owner.failf() + } + } +} + +// failf +func (owner *Owner) failf() { + // TODO +} + +// exitNow +func (owner *Owner) exitNow() { + +} + +// setupDir sets up the owner's directory for clones and reports. +// If the temporary option is set then a temporary directory will be +// used for the owner repo clones. +func (owner *Owner) setupDir(opts *Options) { + if opts.Tmp { + dir, err := ioutil.TempDir("", owner.name) + if err != nil { + owner.failf() + } + owner.path = dir + } else { + owner.path = filepath.Join(gitLeaksClonePath, owner.name) + if _, err := os.Stat(owner.path); os.IsNotExist(err) { + os.Mkdir(owner.path, os.ModePerm) + } + } + owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name) +} + +// rmTmp removes the temporary repo +func (owner *Owner) rmTmp() { + os.RemoveAll(owner.path) + os.Exit(EXIT_FAILURE) +} + +// ownerType returns the owner type extracted from opts. +// If no owner type is provided, gitleaks assumes the owner is ambiguous +// and the user is running gitleaks on a single repo +func ownerType(opts *Options) string { + if opts.OrgURL != "" { + return "org" + + } else if opts.UserURL != "" { + return "user" + } + return "" +} + +// ownerName returns the owner name extracted from the urls provided in opts. +// If no RepoURL, OrgURL, or UserURL is provided, then owner will log an error +// and gitleaks will exit. +func ownerName(opts *Options) (string, error) { + if opts.RepoURL != "" { + splitSlashes := strings.Split(opts.RepoURL, "/") + return splitSlashes[len(splitSlashes)-2], nil + } else if opts.UserURL != "" { + _, ownerName := path.Split(opts.UserURL) + return ownerName, nil + } else if opts.OrgURL != "" { + _, ownerName := path.Split(opts.OrgURL) + return ownerName, nil + } + + // TODO error + return "", nil +} + +// githubTokenClient creates an oauth client from your github access token. +// Gitleaks will attempt to retrieve your github access token from a cli argument +// or an env var - "GITHUB_TOKEN". +// Might be good to eventually parse the token from a config or creds file in +// $GITLEAKS_HOME +func githubTokenClient(opts *Options) *http.Client { + var token string + if opts.Token != "" { + token = opts.Token + } else { + token = os.Getenv("GITHUB_TOKEN") + } + if token == "" { + return nil + } + + tokenService := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tokenClient := oauth2.NewClient(context.Background(), tokenService) + return tokenClient +} diff --git a/repo.go b/repo.go new file mode 100644 index 00000000..8259e2a4 --- /dev/null +++ b/repo.go @@ -0,0 +1,224 @@ +package main + +import ( + "path/filepath" + "os" + "fmt" + "os/exec" + "sync" + "bytes" + "encoding/json" + "log" + "strings" + "io/ioutil" +) + +type Repo struct { + name string + url string + path string + status string // TODO + leaks []Leak + owner *Owner +} + +type Leak struct { + Line string `json:"line"` + Commit string `json:"commit"` + Offender string `json:"string"` + Reason string `json:"reason"` + Msg string `json:"commitMsg"` + Time string `json:"time"` + Author string `json:"author"` + File string `json:"file"` + RepoURL string `json:"repoURL"` +} + +type Commit struct { + Hash string + Author string + Time string + Msg string +} + +func newRepo(owner *Owner, name string, url string) *Repo { + repo := &Repo{ + name: name, + url: url, + path: owner.path + "/" + name, + } + return repo +} + +// Audit operates on a single repo and searches the full or partial history of the repo. +// A semaphore is declared for every repo to bind concurrency. If unbounded, the system will throw a +// `too many open files` error. Eventually, gitleaks should use src-d/go-git to avoid shelling out +// 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 { + var ( + out []byte + err error + commitWG sync.WaitGroup + gitLeakReceiverWG sync.WaitGroup + gitLeaksChan = make(chan Leak) + leaks []Leak + semaphoreChan = make(chan struct{}, opts.Concurrency) + ) + + dotGitPath := filepath.Join(repo.path, ".git") + + // 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) { + // 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") + } + } 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 := os.Chdir(fmt.Sprintf(repo.path)); err != nil { + fmt.Println("cant chdir") + } + + gitFormat := "--format=%H%n%an%n%s%n%ci" + out, err = exec.Command("git", "rev-list", "--all", + "--remotes", "--topo-order", gitFormat).Output() + + if err != nil { + fmt.Println("problem with rev list") + } + + revListLines := bytes.Split(out, []byte("\n")) + commits := parseRevList(revListLines) + + for _, commit := range commits { + if commit.Hash == "" { + continue + } + + commitWG.Add(1) + go auditDiff(commit, repo, &commitWG, &gitLeakReceiverWG, opts, + semaphoreChan, gitLeaksChan) + + if commit.Hash == opts.SinceCommit { + break + } + } + 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) + } + + return nil +} + +func (repo *Repo) log() { + +} + +// Used by audit, writeReport will generate a report and write it out to +// $GITLEAKS_HOME/report//. No report will be generated if +// no leaks have been found +func (repo *Repo) writeReport(owner *Owner) { + reportJSON, _ := json.MarshalIndent(repo.leaks, "", "\t") + if _, err := os.Stat(owner.reportPath); os.IsNotExist(err) { + os.Mkdir(repo.owner.reportPath, os.ModePerm) + } + + reportFileName := fmt.Sprintf("%s_leaks.json", repo.name) + reportFile := filepath.Join(owner.reportPath, reportFileName) + err := ioutil.WriteFile(reportFile, reportJSON, 0644) + if err != nil { + fmt.Println("cant write report") + } + fmt.Printf("Report written to %s\n", reportFile) +} + +// parseRevList is responsible for parsing the output of +// $ `git rev-list --all -remotes --topo-order --format=%H%n%an%n%s%n%ci` +// sample output from the above command looks like: +// ... +// SHA +// Author Name +// Commit Msg +// Commit Date +// ... +// Used by audit +func parseRevList(revList [][]byte) []Commit { + var commits []Commit + for i := 0; i < len(revList)-1; i = i + 5 { + commit := Commit{ + Hash: string(revList[i+1]), + Author: string(revList[i+2]), + Msg: string(revList[i+3]), + Time: string(revList[i+4]), + } + commits = append(commits, commit) + } + return commits +} + +// reportAggregator is a go func responsible for ... +func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) { + for gitLeak := range gitLeaks { + b, err := json.MarshalIndent(gitLeak, "", " ") + if err != nil { + fmt.Println("failed to output leak:", err) + } + fmt.Println(string(b)) + *leaks = append(*leaks, gitLeak) + gitLeakReceiverWG.Done() + } +} + +// Used by audit, auditDiff is a go func responsible for diffing and auditing a commit. +// 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{}, + 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) + } + + commitCmp := fmt.Sprintf("%s^!", currCommit.Hash) + semaphoreChan <- struct{}{} + out, err := exec.Command("git", "diff", commitCmp).Output() + <-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) + } + } + + leaks := doChecks(string(out), currCommit, opts, repo) + if len(leaks) == 0 { + return + } + for _, leak := range leaks { + gitLeakReceiverWG.Add(1) + gitLeaks <- leak + } +} + From 7919e15a653aecbf475054353f7143beeb28890b Mon Sep 17 00:00:00 2001 From: zricethezav Date: Wed, 28 Feb 2018 19:36:21 -0600 Subject: [PATCH 2/5] my, things have changed... --- checks.go | 4 +- leaks.go | 69 ------------- main.go | 90 ++++++----------- options.go | 278 +++++++++++++++++++++++++++++++++++++++++++---------- owner.go | 165 ++++++++++++++++--------------- repo.go | 116 +++++++++++++--------- 6 files changed, 415 insertions(+), 307 deletions(-) delete mode 100644 leaks.go diff --git a/checks.go b/checks.go index 78993189..0c287bb2 100644 --- a/checks.go +++ b/checks.go @@ -6,10 +6,8 @@ import ( "strings" ) -// TODO LOCAL REPO!!!! - // checks Regex and if enabled, entropy and stopwords -func doChecks(diff string, commit Commit, opts *Options, repo *Repo) []Leak { +func doChecks(diff string, commit Commit, repo *Repo) []Leak { var ( match string leaks []Leak diff --git a/leaks.go b/leaks.go deleted file mode 100644 index ec732b3a..00000000 --- a/leaks.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -// TODO https://medium.com/@sebdah/go-best-practices-error-handling-2d15e1f0c5ee -// implement better error handling - -// Commit is so and so - -/* -// start kicks off the audit -func start(repos []Repo, owner *Owner, opts *Options) error { - var ( - report []Leak - err error - ) - defer rmTmpDirs(owner, opts) - - // interrupt handling - c := make(chan os.Signal, 2) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - go func() { - <-c - rmTmpDirs(owner, opts) - os.Exit(1) - }() - - // run checks on repos - for _, repo := range repos { - dotGitPath := filepath.Join(repo.path, ".git") - if _, err := os.Stat(dotGitPath); err == nil { - if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil { - log.Fatal(err) - } - // use pre-cloned repo - fmt.Printf("Checking \x1b[37;1m%s\x1b[0m...\n", repo.url) - err = exec.Command("git", "fetch").Run() - } else { - // no repo present, clone it - if err := os.Chdir(fmt.Sprintf(owner.path)); err != nil { - log.Fatal(err) - } - fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url) - err = exec.Command("git", "clone", repo.url).Run() - } - if err != nil { - log.Printf("failed to fetch repo %v", err) - return nil - } - - report, err = audit(&repo, opts) - if err != nil { - return nil - } - - if len(report) == 0 { - fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n", repo.url) - } - - if opts.EnableJSON && len(report) != 0 { - writeReport(report, repo) - } - } - return nil -} -*/ - -// Used by start, writeReport will generate a report and write it out to -// $GITLEAKS_HOME/report//. No report will be generated if -// no leaks have been found - diff --git a/main.go b/main.go index 9af8bb28..ad8aa84f 100644 --- a/main.go +++ b/main.go @@ -2,38 +2,38 @@ package main import ( _ "fmt" - "github.com/mitchellh/go-homedir" - "log" - _"io/ioutil" - "os" - "path/filepath" - "regexp" "go.uber.org/zap" - _"time" - "go.uber.org/zap/zapcore" + _ "io/ioutil" + "os" + "regexp" + _ "time" + "fmt" ) const EXIT_CLEAN = 0 const EXIT_FAILURE = 1 const EXIT_LEAKS = 2 +// package globals var ( - regexes map[string]*regexp.Regexp - stopWords []string - base64Chars string - hexChars string - assignRegex *regexp.Regexp - fileDiffRegex *regexp.Regexp - gitLeaksPath string - gitLeaksClonePath string - gitLeaksReportPath string - logger *zap.Logger + regexes map[string]*regexp.Regexp + stopWords []string + base64Chars string + hexChars string + assignRegex *regexp.Regexp + fileDiffRegex *regexp.Regexp + logger *zap.Logger + opts *Options ) func init() { base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" hexChars = "1234567890abcdefABCDEF" stopWords = []string{"setting", "info", "env", "environment"} + fileDiffRegex = regexp.MustCompile("diff --git a.+b/") + assignRegex = regexp.MustCompile(`(=|:|:=|<-)`) + + // TODO Externalize regex... this is tricky making it yml compliant regexes = map[string]*regexp.Regexp{ "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"), "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"), @@ -44,53 +44,19 @@ func init() { "AWS": regexp.MustCompile("AKIA[0-9A-Z]{16}"), "Reddit": regexp.MustCompile("(?i)reddit.*['|\"][0-9a-zA-Z]{14}['|\"]"), "Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"), - // "Custom": regexp.MustCompile(".*") - } - fileDiffRegex = regexp.MustCompile("diff --git a.+b/") - assignRegex = regexp.MustCompile(`(=|:|:=|<-)`) - - // gitleaks dir defaults to $HOME/.gitleaks if no env var GITLEAKS_HOME is present. - gitLeaksPath = os.Getenv("GITLEAKS_HOME") - if gitLeaksPath == "" { - homeDir, err := homedir.Dir() - if err != nil { - log.Fatal("Cant find home dir") - } - gitLeaksPath = filepath.Join(homeDir, ".gitleaks") - } - - if _, err := os.Stat(gitLeaksPath); os.IsNotExist(err) { - os.Mkdir(gitLeaksPath, os.ModePerm) - } - gitLeaksClonePath = filepath.Join(gitLeaksPath, "clones") - if _, err := os.Stat(gitLeaksClonePath); os.IsNotExist(err) { - os.Mkdir(gitLeaksClonePath, os.ModePerm) - } - gitLeaksReportPath = filepath.Join(gitLeaksPath, "report") - if _, err := os.Stat(gitLeaksReportPath); os.IsNotExist(err) { - os.Mkdir(gitLeaksReportPath, os.ModePerm) } } func main() { - // TODO abstract logging - atom := zap.NewAtomicLevel() - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "" - logger = zap.New(zapcore.NewCore( - zapcore.NewJSONEncoder(encoderCfg), - zapcore.Lock(os.Stdout), - atom, - )) - logger.Info("HEY") - atom.SetLevel(zap.InfoLevel) - logger.Info("HEY") - - args := os.Args[1:] - opts := parseOptions(args) - owner := newOwner(opts) - owner.auditRepos(opts) - // repos := getRepos(opts, owner) - // start(repos, owner, opts) + opts = newOpts(args) + owner := newOwner() + os.Exit(owner.auditRepos()) } + +func failF(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, format, args...) + os.Exit(EXIT_FAILURE) +} + + diff --git a/options.go b/options.go index 7a4b7fa2..353d4630 100644 --- a/options.go +++ b/options.go @@ -4,52 +4,90 @@ import ( "fmt" "os" "strconv" + "strings" + "regexp" + "github.com/mitchellh/go-homedir" + "path/filepath" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) -const usage = `usage: gitleaks [options] +const DEBUG = 0 +const INFO = 1 +const ERROR = 2 + +const usage = `usage: gitleaks [options] / Options: - -c --concurrency Upper bound on concurrent diffs - -u --user Git user url - -r --repo Git repo url - -o --org Git organization url - -s --since Commit to stop at - -b --b64Entropy Base64 entropy cutoff (default is 70) - -x --hexEntropy Hex entropy cutoff (default is 40) +Modes + -u --user Git user mode + -r --repo Git repo mode + -o --org Git organization mode + -l --local Local mode, gitleaks will look for local repo in + +Logging + -ll --log= 0: Debug, 1: Info, 3: Error + -v --verbose Verbose mode, will output leaks as gitleaks finds them + +Locations + --report_path= Report output, default $GITLEAKS_HOME/report + --clone_path= Gitleaks will clone repos here, default $GITLEAKS_HOME/clones + +Other + -t --temp Clone to temporary directory + -c Upper bound on concurrent diffs + --since= Commit to stop at + --b64Entropy= Base64 entropy cutoff (default is 70) + --hexEntropy= Hex entropy cutoff (default is 40) -e --entropy Enable entropy - -j --json Output gitleaks report -h --help Display this message - --token Github API token - --strict Enables stopwords + --token= Github API token + --stopwords Enables stopwords + ` -// Options for gitleaks + +// Options for gitleaks. need to support remote repo/owner +// and local repo/owner mode type Options struct { + URL string + RepoPath string + + ClonePath string + ReportPath string + Concurrency int B64EntropyCutoff int HexEntropyCutoff int - UserURL string - OrgURL string - RepoURL string + + // MODES + UserMode bool + OrgMode bool + RepoMode bool + LocalMode bool + + // OPTS Strict bool Entropy bool SinceCommit string Persist bool IncludeForks bool Tmp bool - EnableJSON bool + ReportOut bool Token string + + // LOGS/REPORT Verbose bool + LogLevel int } // help prints the usage string and exits func help() { os.Stderr.WriteString(usage) - os.Exit(1) } // optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid -func optionsNextInt(args []string, i *int) int { +func (opts *Options) nextInt(args []string, i *int) int { if len(args) > *i+1 { *i++ } else { @@ -57,30 +95,90 @@ func optionsNextInt(args []string, i *int) int { } argInt, err := strconv.Atoi(args[*i]) if err != nil { - fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i]) - help() + opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i]) } return argInt } // optionsNextString is a parseOptions helper that returns the value (string) of an option if valid -func optionsNextString(args []string, i *int) string { +func (opts *Options) nextString(args []string, i *int) string { if len(args) > *i+1 { *i++ } else { - fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i]) - help() + opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i]) } return args[*i] } -// parseOptions -func parseOptions(args []string) *Options { - opts := &Options{ +// optInt grabs the string ... +func (opts *Options) optString(arg string, prefixes ...string) (bool, string) { + for _, prefix := range prefixes { + if strings.HasPrefix(arg, prefix) { + return true, arg[len(prefix):] + } + } + return false, "" +} + +// optInt grabs the int ... +func (opts *Options) optInt(arg string, prefixes ...string) (bool, int) { + for _, prefix := range prefixes { + if strings.HasPrefix(arg, prefix) { + i, err := strconv.Atoi(arg[len(prefix):]) + if err != nil { + opts.failF("Invalid %s int option\n", prefix) + } + return true, i + } + } + return false, 0 +} + +// newOpts generates opts and parses arguments +func newOpts(args []string) (*Options) { + opts, err := defaultOptions() + if err != nil{ + opts.failF("%v", err) + } + err = opts.parseOptions(args) + if err != nil{ + opts.failF("%v", err) + } + opts.setupLogger() + return opts +} + +// deafultOptions provides the default options +func defaultOptions() (*Options, error) { + // default GITLEAKS_HOME is $HOME/.gitleaks + // gitleaks will use this location for clones if + // no clone-path is provided + gitleaksHome := os.Getenv("GITLEAKS_HOME") + if gitleaksHome == "" { + homeDir, err := homedir.Dir() + if err != nil { + return nil, fmt.Errorf("could not find system home dir") + } + gitleaksHome = filepath.Join(homeDir, ".gitleaks") + } + + // make sure gitleaks home exists + if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) { + os.Mkdir(gitleaksHome, os.ModePerm) + } + + return &Options{ Concurrency: 10, B64EntropyCutoff: 70, HexEntropyCutoff: 40, - } + LogLevel: INFO, + ClonePath: filepath.Join(gitleaksHome, "clone"), + ReportPath: filepath.Join(gitleaksHome, "report"), + }, nil +} + +// parseOptions +func (opts *Options) parseOptions(args []string) error { if len(args) == 0 { help() @@ -89,48 +187,130 @@ func parseOptions(args []string) *Options { for i := 0; i < len(args); i++ { arg := args[i] switch arg { - case "-s", "--since": - opts.SinceCommit = optionsNextString(args, &i) + case "-s": + opts.SinceCommit = opts.nextString(args, &i) case "--strict": opts.Strict = true case "-b", "--b64Entropy": - opts.B64EntropyCutoff = optionsNextInt(args, &i) + opts.B64EntropyCutoff = opts.nextInt(args, &i) case "-x", "--hexEntropy": - opts.HexEntropyCutoff = optionsNextInt(args, &i) + opts.HexEntropyCutoff = opts.nextInt(args, &i) case "-e", "--entropy": opts.Entropy = true - case "-c", "--concurrency": - opts.Concurrency = optionsNextInt(args, &i) + case "-c": + opts.Concurrency = opts.nextInt(args, &i) + case "-o", "--org": - opts.OrgURL = optionsNextString(args, &i) + opts.OrgMode = true case "-u", "--user": - opts.UserURL = optionsNextString(args, &i) + opts.UserMode = true case "-r", "--repo": - opts.RepoURL = optionsNextString(args, &i) - case "-t", "--temporary": + opts.RepoMode = true + case "-l", "--local": + opts.LocalMode = true + + case "--report-out": + opts.ReportOut = true + + case "-t", "--temp": opts.Tmp = true - case "--token": - opts.Token = optionsNextString(args, &i) - case "-j", "--json": - opts.EnableJSON = true + case "-ll": + opts.LogLevel = opts.nextInt(args, &i) case "-h", "--help": help() - return nil + os.Exit(EXIT_CLEAN) default: - if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" && - opts.UserURL == "" { - opts.RepoURL = arg + // TARGETS + if i == len(args)-1 { + fmt.Println(arg[i]) + if opts.LocalMode { + opts.RepoPath = args[i] + } else { + opts.URL = args[i] + } + } else if match, value := opts.optString(arg, "--token="); match { + opts.Token = value + } else if match, value := opts.optString(arg, "--since="); match { + opts.SinceCommit = value + } else if match, value := opts.optString(arg, "--report-path="); match { + opts.ReportPath = value + } else if match, value := opts.optString(arg, "--clone-path="); match { + opts.ClonePath = value + } else if match, value := opts.optInt(arg, "--log="); match { + opts.LogLevel = value + } else if match, value := opts.optInt(arg, "--b64Entropy="); match { + opts.B64EntropyCutoff = value + } else if match, value := opts.optInt(arg, "--hexEntropy="); match { + opts.HexEntropyCutoff = value } else { fmt.Printf("Unknown option %s\n\n", arg) help() + return fmt.Errorf("Unknown option %s\n\n", arg) } } } + err := opts.guards() + if err != nil{ + fmt.Printf("%v", err) + } + return err +} - // "guards" - if opts.Tmp && opts.EnableJSON { - fmt.Println("Report generation with temporary clones not supported") +// failF prints a failure message out to stderr, displays help +// and exits with a exit code 2 +func (opts *Options) failF(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, format, args...) + help() + os.Exit(EXIT_FAILURE) +} + +// guards will prevent gitleaks from continuing if any invalid options +// are found. +func (opts *Options) guards() error { + if (opts.RepoMode || opts.OrgMode || opts.UserMode) && !isGithubTarget(opts.URL) { + return fmt.Errorf("Not valid github target %s\n", opts.URL) + } else if (opts.RepoMode || opts.OrgMode || opts.UserMode) && opts.LocalMode { + return fmt.Errorf("Cannot run Gitleaks on repo/user/org mode and local mode\n") + } else if (opts.RepoMode || opts.UserMode) && opts.OrgMode { + return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") + } else if (opts.OrgMode || opts.UserMode) && opts.RepoMode { + return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") + } else if (opts.OrgMode || opts.RepoMode) && opts.UserMode{ + return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") + } else if opts.LocalMode && opts.Tmp { + return fmt.Errorf("Cannot run Gitleaks with temp settings and local mode\n") + } else if opts.SinceCommit != "" && (opts.OrgMode || opts.UserMode) { + return fmt.Errorf("Cannot run Gitleaks with since commit flag and a owner mode\n") } - return opts + return nil +} + +// setupLogger initiates the logger and sets the logging level +// based on what is set in arguments. Default logging level is +// INFO +func (opts *Options) setupLogger() { + atom := zap.NewAtomicLevel() + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "" + logger = zap.New(zapcore.NewCore( + zapcore.NewJSONEncoder(encoderCfg), + zapcore.Lock(os.Stdout), + atom, + )) + + switch opts.LogLevel { + case DEBUG: + atom.SetLevel(zap.DebugLevel) + case INFO: + atom.SetLevel(zap.InfoLevel) + case ERROR: + atom.SetLevel(zap.ErrorLevel) + } +} + +// isGithubTarget checks if url is a valid github target +func isGithubTarget(url string) bool { + re := regexp.MustCompile("github\\.com\\/") + return re.MatchString(url) } diff --git a/owner.go b/owner.go index 1af6cc28..b7e80eab 100644 --- a/owner.go +++ b/owner.go @@ -1,18 +1,17 @@ package main import ( - "path" - "io/ioutil" - "path/filepath" - "os" - "github.com/google/go-github/github" - "strings" "context" + _ "fmt" + "github.com/google/go-github/github" "golang.org/x/oauth2" + "io/ioutil" "net/http" - "log" + "os" "os/signal" - _"fmt" + "path" + "strings" + "fmt" ) type Owner struct { @@ -21,21 +20,17 @@ type Owner struct { accountType string path string reportPath string - repos []Repo + repos []Repo } // newOwner instantiates an owner and creates any necessary resources for said owner. // newOwner returns a Owner struct pointer -func newOwner(opts *Options) *Owner { - name, err := ownerName(opts) +func newOwner() (*Owner) { + name := ownerName() owner := &Owner{ name: name, - url: opts.UserURL, - accountType: ownerType(opts), - } - - if err != nil { - owner.failf() + url: opts.URL, + accountType: ownerType(), } // listen for ctrl-c @@ -49,8 +44,23 @@ func newOwner(opts *Options) *Owner { owner.rmTmp() }() - owner.setupDir(opts) - owner.fetchRepos(opts) + + // if running on local repo, just go right to it. + if opts.LocalMode { + repo := newLocalRepo(opts.RepoPath) + owner.repos = append(owner.repos, *repo) + return owner + } + + err := owner.setupDir() + if err != nil { + owner.failF("%v", err) + } + + err = owner.fetchRepos() + if err != nil { + owner.failF("%v", err) + } return owner } @@ -58,16 +68,17 @@ func newOwner(opts *Options) *Owner { // of the owner's repos. If opts.RepoURL is not the empty string then fetchRepos will // only grab the repo specified in opts.RepoURL. Otherwise, fetchRepos will reach out to // github's api and grab all repos associated with owner. -func (owner *Owner) fetchRepos(opts *Options) { +func (owner *Owner) fetchRepos() error { + var err error ctx := context.Background() if owner.accountType == "" { // single repo, ambiguous account type - _, repoName := path.Split(opts.RepoURL) - repo := newRepo(owner, repoName, opts.RepoURL) + _, repoName := path.Split(opts.URL) + repo := newRepo(repoName, opts.URL) owner.repos = append(owner.repos, *repo) } else { // org or user account type, would fail if not valid before - tokenClient := githubTokenClient(opts) + tokenClient := githubTokenClient() gitClient := github.NewClient(tokenClient) if owner.accountType == "org" { @@ -75,26 +86,27 @@ func (owner *Owner) fetchRepos(opts *Options) { orgOpt := &github.RepositoryListByOrgOptions{ ListOptions: github.ListOptions{PerPage: 10}, } - owner.fetchOrgRepos(orgOpt, gitClient, ctx) + err = owner.fetchOrgRepos(orgOpt, gitClient, ctx) } else { // user account type userOpt := &github.RepositoryListOptions{ ListOptions: github.ListOptions{PerPage: 10}, } - owner.fetchUserRepos(userOpt, gitClient, ctx) + err = owner.fetchUserRepos(userOpt, gitClient, ctx) } } + return err } // fetchOrgRepos used by fetchRepos is responsible for parsing github's org repo response. If no // github token is available then fetchOrgRepos might run into a rate limit in which case owner will // log an error and gitleaks will exit. The rate limit for no token is 50 req/hour... not much. func (owner *Owner) fetchOrgRepos(orgOpts *github.RepositoryListByOrgOptions, gitClient *github.Client, - ctx context.Context) { + ctx context.Context) error { var ( - githubRepos []*github.Repository - resp *github.Response - err error + githubRepos []*github.Repository + resp *github.Response + err error ) for { @@ -102,16 +114,15 @@ func (owner *Owner) fetchOrgRepos(orgOpts *github.RepositoryListByOrgOptions, gi ctx, owner.name, orgOpts) owner.addRepos(githubRepos) if _, ok := err.(*github.RateLimitError); ok { - log.Println("hit rate limit") - break + logger.Info("hit rate limit") } else if err != nil { - log.Println("other error") - break + return fmt.Errorf("failed fetching org repos, bad request") } else if resp.NextPage == 0 { break } orgOpts.Page = resp.NextPage } + return nil } // fetchUserRepos used by fetchRepos is responsible for parsing github's user repo response. If no @@ -119,74 +130,79 @@ func (owner *Owner) fetchOrgRepos(orgOpts *github.RepositoryListByOrgOptions, gi // log an error and gitleaks will exit. The rate limit for no token is 50 req/hour... not much. // sorry for the redundancy func (owner *Owner) fetchUserRepos(userOpts *github.RepositoryListOptions, gitClient *github.Client, - ctx context.Context) { + ctx context.Context) error { var ( - githubRepos []*github.Repository - resp *github.Response - err error + githubRepos []*github.Repository + resp *github.Response + err error ) for { githubRepos, resp, err = gitClient.Repositories.List( ctx, owner.name, userOpts) owner.addRepos(githubRepos) if _, ok := err.(*github.RateLimitError); ok { - log.Println("hit rate limit") + logger.Info("hit rate limit") break } else if err != nil { - log.Println("other error") - break + return fmt.Errorf("failed fetching user repos, bad request") } else if resp.NextPage == 0 { break } userOpts.Page = resp.NextPage } + return nil } // addRepos used by fetchUserRepos and fetchOrgRepos appends new repos from // github's org/user response. -func (owner *Owner) addRepos (githubRepos []*github.Repository) { +func (owner *Owner) addRepos(githubRepos []*github.Repository) { for _, repo := range githubRepos { - owner.repos = append(owner.repos, *newRepo(owner, *repo.Name, *repo.CloneURL)) + owner.repos = append(owner.repos, *newRepo(*repo.Name, *repo.CloneURL)) } } // auditRepos -func (owner *Owner) auditRepos(opts *Options) { +func (owner *Owner) auditRepos() (int) { + exitCode := EXIT_CLEAN for _, repo := range owner.repos { - err := repo.audit(owner, opts) + leaksPst, err := repo.audit(owner) if err != nil { - owner.failf() + failF("%v\n", err) + } + if leaksPst{ + exitCode = EXIT_LEAKS } } + return exitCode } -// failf -func (owner *Owner) failf() { - // TODO -} - -// exitNow -func (owner *Owner) exitNow() { - +// failF prints a failure message out to stderr +// and exits with a exit code 2 +func (owner *Owner) failF(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, format, args...) + os.Exit(EXIT_FAILURE) } // setupDir sets up the owner's directory for clones and reports. // If the temporary option is set then a temporary directory will be // used for the owner repo clones. -func (owner *Owner) setupDir(opts *Options) { +func (owner *Owner) setupDir() error { if opts.Tmp { dir, err := ioutil.TempDir("", owner.name) if err != nil { - owner.failf() + return err + owner.failF("Unabled to create temp directories for cloning") } owner.path = dir } else { - owner.path = filepath.Join(gitLeaksClonePath, owner.name) - if _, err := os.Stat(owner.path); os.IsNotExist(err) { + if _, err := os.Stat(opts.ClonePath); os.IsNotExist(err) { os.Mkdir(owner.path, os.ModePerm) } } - owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name) + return nil + + // TODO could be handled via option + // owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name) } // rmTmp removes the temporary repo @@ -198,11 +214,10 @@ func (owner *Owner) rmTmp() { // ownerType returns the owner type extracted from opts. // If no owner type is provided, gitleaks assumes the owner is ambiguous // and the user is running gitleaks on a single repo -func ownerType(opts *Options) string { - if opts.OrgURL != "" { +func ownerType() string { + if opts.OrgMode { return "org" - - } else if opts.UserURL != "" { + } else if opts.UserMode { return "user" } return "" @@ -211,28 +226,24 @@ func ownerType(opts *Options) string { // ownerName returns the owner name extracted from the urls provided in opts. // If no RepoURL, OrgURL, or UserURL is provided, then owner will log an error // and gitleaks will exit. -func ownerName(opts *Options) (string, error) { - if opts.RepoURL != "" { - splitSlashes := strings.Split(opts.RepoURL, "/") - return splitSlashes[len(splitSlashes)-2], nil - } else if opts.UserURL != "" { - _, ownerName := path.Split(opts.UserURL) - return ownerName, nil - } else if opts.OrgURL != "" { - _, ownerName := path.Split(opts.OrgURL) - return ownerName, nil +func ownerName() (string) { + if opts.RepoMode { + splitSlashes := strings.Split(opts.URL, "/") + return splitSlashes[len(splitSlashes)-2] + } else if opts.UserMode|| opts.OrgMode { + _, ownerName := path.Split(opts.URL) + return ownerName } - - // TODO error - return "", nil + // local repo + return "" } // githubTokenClient creates an oauth client from your github access token. // Gitleaks will attempt to retrieve your github access token from a cli argument // or an env var - "GITHUB_TOKEN". -// Might be good to eventually parse the token from a config or creds file in +// Might be good to eventually parse the token from a Config or creds file in // $GITLEAKS_HOME -func githubTokenClient(opts *Options) *http.Client { +func githubTokenClient() *http.Client { var token string if opts.Token != "" { token = opts.Token diff --git a/repo.go b/repo.go index 8259e2a4..0e70f2e8 100644 --- a/repo.go +++ b/repo.go @@ -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//. 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 } } - From 0d75136c1c266710de798c99f95cd4225b4e4e25 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Wed, 28 Feb 2018 22:02:03 -0600 Subject: [PATCH 3/5] structured logs --- main.go | 6 ++--- options.go | 72 +++++++++++++++++++++++++++++------------------------- owner.go | 21 ++++++---------- repo.go | 55 ++++++++++++++++++++++++++++------------- 4 files changed, 87 insertions(+), 67 deletions(-) diff --git a/main.go b/main.go index ad8aa84f..02480510 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,13 @@ package main import ( + "fmt" _ "fmt" "go.uber.org/zap" _ "io/ioutil" "os" "regexp" _ "time" - "fmt" ) const EXIT_CLEAN = 0 @@ -23,7 +23,7 @@ var ( assignRegex *regexp.Regexp fileDiffRegex *regexp.Regexp logger *zap.Logger - opts *Options + opts *Options ) func init() { @@ -58,5 +58,3 @@ func failF(format string, args ...interface{}) { fmt.Fprintf(os.Stderr, format, args...) os.Exit(EXIT_FAILURE) } - - diff --git a/options.go b/options.go index 353d4630..b9d03be2 100644 --- a/options.go +++ b/options.go @@ -2,14 +2,14 @@ package main import ( "fmt" - "os" - "strconv" - "strings" - "regexp" "github.com/mitchellh/go-homedir" - "path/filepath" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" ) const DEBUG = 0 @@ -43,42 +43,42 @@ Other -h --help Display this message --token= Github API token --stopwords Enables stopwords + --pretty Enables pretty printing for humans, otherwise you'll get logs' ` - // Options for gitleaks. need to support remote repo/owner // and local repo/owner mode type Options struct { - URL string - RepoPath string + URL string + RepoPath string - ClonePath string - ReportPath string + ClonePath string + ReportPath string Concurrency int B64EntropyCutoff int HexEntropyCutoff int // MODES - UserMode bool - OrgMode bool - RepoMode bool - LocalMode bool + UserMode bool + OrgMode bool + RepoMode bool + LocalMode bool // OPTS - Strict bool - Entropy bool - SinceCommit string - Persist bool - IncludeForks bool - Tmp bool - ReportOut bool - Token string + Strict bool + Entropy bool + SinceCommit string + Persist bool + IncludeForks bool + Tmp bool + ReportOut bool + Token string // LOGS/REPORT - Verbose bool - LogLevel int + LogLevel int + PrettyPrint bool } // help prints the usage string and exits @@ -135,13 +135,13 @@ func (opts *Options) optInt(arg string, prefixes ...string) (bool, int) { } // newOpts generates opts and parses arguments -func newOpts(args []string) (*Options) { +func newOpts(args []string) *Options { opts, err := defaultOptions() - if err != nil{ + if err != nil { opts.failF("%v", err) } err = opts.parseOptions(args) - if err != nil{ + if err != nil { opts.failF("%v", err) } opts.setupLogger() @@ -171,9 +171,9 @@ func defaultOptions() (*Options, error) { Concurrency: 10, B64EntropyCutoff: 70, HexEntropyCutoff: 40, - LogLevel: INFO, - ClonePath: filepath.Join(gitleaksHome, "clone"), - ReportPath: filepath.Join(gitleaksHome, "report"), + LogLevel: INFO, + ClonePath: filepath.Join(gitleaksHome, "clone"), + ReportPath: filepath.Join(gitleaksHome, "report"), }, nil } @@ -211,6 +211,8 @@ func (opts *Options) parseOptions(args []string) error { case "--report-out": opts.ReportOut = true + case "--pretty": + opts.PrettyPrint = true case "-t", "--temp": opts.Tmp = true @@ -222,7 +224,6 @@ func (opts *Options) parseOptions(args []string) error { default: // TARGETS if i == len(args)-1 { - fmt.Println(arg[i]) if opts.LocalMode { opts.RepoPath = args[i] } else { @@ -250,7 +251,7 @@ func (opts *Options) parseOptions(args []string) error { } } err := opts.guards() - if err != nil{ + if err != nil { fmt.Printf("%v", err) } return err @@ -275,7 +276,7 @@ func (opts *Options) guards() error { return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") } else if (opts.OrgMode || opts.UserMode) && opts.RepoMode { return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") - } else if (opts.OrgMode || opts.RepoMode) && opts.UserMode{ + } else if (opts.OrgMode || opts.RepoMode) && opts.UserMode { return fmt.Errorf("Cannot run Gitleaks on more than one mode\n") } else if opts.LocalMode && opts.Tmp { return fmt.Errorf("Cannot run Gitleaks with temp settings and local mode\n") @@ -307,6 +308,11 @@ func (opts *Options) setupLogger() { case ERROR: atom.SetLevel(zap.ErrorLevel) } + + // set to ErrorLevel if pretty printing + if opts.PrettyPrint{ + atom.SetLevel(zap.ErrorLevel) + } } // isGithubTarget checks if url is a valid github target diff --git a/owner.go b/owner.go index b7e80eab..b62b69ab 100644 --- a/owner.go +++ b/owner.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" _ "fmt" "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -11,7 +12,6 @@ import ( "os/signal" "path" "strings" - "fmt" ) type Owner struct { @@ -25,8 +25,8 @@ type Owner struct { // newOwner instantiates an owner and creates any necessary resources for said owner. // newOwner returns a Owner struct pointer -func newOwner() (*Owner) { - name := ownerName() +func newOwner() *Owner { + name := ownerName() owner := &Owner{ name: name, url: opts.URL, @@ -44,7 +44,6 @@ func newOwner() (*Owner) { owner.rmTmp() }() - // if running on local repo, just go right to it. if opts.LocalMode { repo := newLocalRepo(opts.RepoPath) @@ -162,14 +161,14 @@ func (owner *Owner) addRepos(githubRepos []*github.Repository) { } // auditRepos -func (owner *Owner) auditRepos() (int) { +func (owner *Owner) auditRepos() int { exitCode := EXIT_CLEAN for _, repo := range owner.repos { leaksPst, err := repo.audit(owner) if err != nil { failF("%v\n", err) } - if leaksPst{ + if leaksPst { exitCode = EXIT_LEAKS } } @@ -190,8 +189,7 @@ func (owner *Owner) setupDir() error { if opts.Tmp { dir, err := ioutil.TempDir("", owner.name) if err != nil { - return err - owner.failF("Unabled to create temp directories for cloning") + fmt.Errorf("unable to create temp directories for cloning") } owner.path = dir } else { @@ -200,9 +198,6 @@ func (owner *Owner) setupDir() error { } } return nil - - // TODO could be handled via option - // owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name) } // rmTmp removes the temporary repo @@ -226,11 +221,11 @@ func ownerType() string { // ownerName returns the owner name extracted from the urls provided in opts. // If no RepoURL, OrgURL, or UserURL is provided, then owner will log an error // and gitleaks will exit. -func ownerName() (string) { +func ownerName() string { if opts.RepoMode { splitSlashes := strings.Split(opts.URL, "/") return splitSlashes[len(splitSlashes)-2] - } else if opts.UserMode|| opts.OrgMode { + } else if opts.UserMode || opts.OrgMode { _, ownerName := path.Split(opts.URL) return ownerName } diff --git a/repo.go b/repo.go index 0e70f2e8..6235adc6 100644 --- a/repo.go +++ b/repo.go @@ -8,9 +8,9 @@ import ( "io/ioutil" "os" "os/exec" + "path" "path/filepath" "sync" - "path" ) type Repo struct { @@ -53,22 +53,28 @@ func newLocalRepo(repoPath string) *Repo { func newRepo(name string, url string) *Repo { repo := &Repo{ - name: name, - url: url, + name: name, + url: url, // TODO handle existing one - path: opts.ClonePath + "/" + name, + path: opts.ClonePath + "/" + name, } return repo } -func (repo *Repo) rLog(msg string) { +func (repo *Repo) Info(msg string) { // logger should have these infos: msg, repo, owner, time - logger.Debug("Beginning audit", + logger.Info(msg, zap.String("repo", repo.name), zap.String("repo_path", repo.path), ) } +func (repo *Repo) PrettyPrintF(format string, args ...interface{}) { + if opts.PrettyPrint { + fmt.Fprintf(os.Stderr, format, args...) + } +} + // Audit operates on a single repo and searches the full or partial history of the repo. // A semaphore is declared for every repo to bind concurrency. If unbounded, the system will throw a // `too many open files` error. Eventually, gitleaks should use src-d/go-git to avoid shelling out @@ -84,7 +90,7 @@ func (repo *Repo) audit(owner *Owner) (bool, error) { gitLeaksChan = make(chan Leak) leaks []Leak semaphoreChan = make(chan struct{}, opts.Concurrency) - leaksPst bool + leaksPst bool ) dotGitPath := filepath.Join(repo.path, ".git") @@ -96,13 +102,15 @@ func (repo *Repo) audit(owner *Owner) (bool, error) { 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) + repo.Info("cloning") + repo.PrettyPrintF("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url) err = exec.Command("git", "clone", repo.url, repo.path).Run() 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) + repo.Info("fetching") + repo.PrettyPrintF("Fetching \x1b[37;1m%s\x1b[0m...\n", repo.url) err = exec.Command("git", "fetch").Run() if err != nil { return false, fmt.Errorf("cannot fetch %s from %s", repo.url, repo.path) @@ -141,7 +149,7 @@ func (repo *Repo) audit(owner *Owner) (bool, error) { go reportAggregator(&gitLeakReceiverWG, gitLeaksChan, &leaks) commitWG.Wait() gitLeakReceiverWG.Wait() - if len(leaks) != 0{ + if len(leaks) != 0 { leaksPst = true } @@ -170,7 +178,7 @@ func (repo *Repo) writeReport() error { if err != nil { return err } - fmt.Printf("Report written to %s\n", reportFile) + repo.Info(fmt.Sprintf("Report written to %s\n", reportFile)) return nil } @@ -201,13 +209,26 @@ func parseRevList(revList [][]byte) []Commit { // reportAggregator is a go func responsible for ... func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) { 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)) + logger.Info("leak", + zap.String("line", gitLeak.Line), + zap.String("commit", gitLeak.Commit), + zap.String("offender", gitLeak.Offender), + zap.String("Reason", gitLeak.Reason), + zap.String("author", gitLeak.Author), + zap.String("file", gitLeak.File), + zap.String("repoURL", gitLeak.RepoURL), + zap.String("timeOfCommit", gitLeak.Time), + ) *leaks = append(*leaks, gitLeak) + if opts.PrettyPrint { + b, err := json.MarshalIndent(gitLeak, "", " ") + if err != nil { + // handle this? + fmt.Println("failed to output leak:", err) + } + fmt.Println(string(b)) + } + gitLeakReceiverWG.Done() } } From 25736d115fcd0c66fda47257ecb185bf951c0124 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Wed, 28 Feb 2018 23:25:04 -0600 Subject: [PATCH 4/5] some barebones tests --- checks_test.go | 4 ++-- options.go | 2 +- repo.go | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/checks_test.go b/checks_test.go index 89a377de..56cb8cdf 100644 --- a/checks_test.go +++ b/checks_test.go @@ -6,7 +6,7 @@ import ( func TestCheckRegex(t *testing.T) { var results []Leak - opts := &Options{ + opts = &Options{ Concurrency: 10, B64EntropyCutoff: 70, HexEntropyCutoff: 40, @@ -22,7 +22,7 @@ func TestCheckRegex(t *testing.T) { } for k, v := range checks { - results = doChecks(k, commit, opts, repo) + results = doChecks(k, commit, &repo) if v != len(results) { t.Errorf("regexCheck failed on string %s", k) } diff --git a/options.go b/options.go index b9d03be2..9063c79d 100644 --- a/options.go +++ b/options.go @@ -317,6 +317,6 @@ func (opts *Options) setupLogger() { // isGithubTarget checks if url is a valid github target func isGithubTarget(url string) bool { - re := regexp.MustCompile("github\\.com\\/") + re := regexp.MustCompile("github.com") return re.MatchString(url) } diff --git a/repo.go b/repo.go index 6235adc6..f378d2dc 100644 --- a/repo.go +++ b/repo.go @@ -228,7 +228,6 @@ func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, lea } fmt.Println(string(b)) } - gitLeakReceiverWG.Done() } } From 9dd624affac83c0b84278556a7c135cf17b17dc6 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Thu, 1 Mar 2018 00:01:13 -0600 Subject: [PATCH 5/5] options and repo testing --- options_test.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ repo_test.go | 29 ++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 options_test.go create mode 100644 repo_test.go diff --git a/options_test.go b/options_test.go new file mode 100644 index 00000000..103c180c --- /dev/null +++ b/options_test.go @@ -0,0 +1,89 @@ +package main + +import ( + "testing" +) + +func TestNextInt(t *testing.T) { + args := []string{"-c", "10"} + i := 0 + opts, err := defaultOptions() + if err != nil { + t.Error() + } + n := opts.nextInt(args, &i) + if n != 10 { + t.Error() + } +} + +func TestNextString(t *testing.T) { + args := []string{"--fake", "flag"} + i := 0 + opts, err := defaultOptions() + if err != nil { + t.Error() + } + n := opts.nextString(args, &i) + if n != "flag" { + t.Error() + } +} + +func TestOptString(t *testing.T) { + opts, err := defaultOptions() + if err != nil { + t.Error() + } + match, n := opts.optString("--fake=flag", "--fake=") + if !match || n != "flag" { + t.Error() + } +} + +func TestOptInt(t *testing.T) { + opts, err := defaultOptions() + if err != nil { + t.Error() + } + match, n := opts.optInt("--fake=10", "--fake=") + if !match || n != 10 { + t.Error() + } +} + +func TestParseOptions(t *testing.T) { + opts, err := defaultOptions() + opts.URL = "github.com/sample" + if err != nil { + t.Error() + } + opts.RepoMode = false + opts.UserMode = true + opts.LocalMode = true + err = opts.guards() + if err == nil { + t.Error() + } + + opts.RepoMode = true + opts.UserMode = false + opts.LocalMode = false + err = opts.guards() + if err != nil { + t.Error() + } +} + +func TestGithubTarget(t *testing.T) { + if !isGithubTarget("github.com"){ + t.Error() + } + if !isGithubTarget("https://github.com/"){ + t.Error() + } + if !isGithubTarget("git@github.com:zricethezav/gitleaks.git"){ + t.Error() + } + +} diff --git a/repo_test.go b/repo_test.go new file mode 100644 index 00000000..909eb261 --- /dev/null +++ b/repo_test.go @@ -0,0 +1,29 @@ +package main + +import "testing" + +func TestNewRepo(t *testing.T) { + // TODO +} + +func TestNewLocalRepo(t *testing.T) { + // TODO +} + +func TestWriteReport(t *testing.T) { + // TODO + opts, err := defaultOptions() + r := newRepo("fakerepo", "github.com") + sampleLeak := Leak{ + Line: "yoo", + Commit: "mycommit", + Offender: "oh boy", + Reason: "hello", + Msg: "msg", + Time: "time", + Author: "lol", + RepoURL: "yooo", + } + r.leaks = []Leak{sampleLeak, sampleLeak} + r.writeReport() +}