cleaning up path logic for clones and reports, local repos are supported now

This commit is contained in:
zricethezav committed 2018-03-01 19:08:58 -06:00
1 parent 648ae701bf
commit 39c9cb617c
5 files changed
+65 -93

No files matched your search

-2
View File
@@ -3,7 +3,6 @@ package main
import (
"fmt"
_ "fmt"
"go.uber.org/zap"
_ "io/ioutil"
"os"
"regexp"
@@ -22,7 +21,6 @@ var (
hexChars string
assignRegex *regexp.Regexp
fileDiffRegex *regexp.Regexp
logger *zap.Logger
opts *Options
)
+9 -49
View File
@@ -2,20 +2,13 @@ package main
import (
"fmt"
"github.com/mitchellh/go-homedir"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"path/filepath"
)
const DEBUG = 0
const INFO = 1
const ERROR = 2
const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
Options:
@@ -53,7 +46,6 @@ type Options struct {
URL string
RepoPath string
ClonePath string
ReportPath string
Concurrency int
@@ -144,12 +136,12 @@ func newOpts(args []string) *Options {
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
@@ -166,14 +158,12 @@ func defaultOptions() (*Options, error) {
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
}
@@ -225,7 +215,7 @@ func (opts *Options) parseOptions(args []string) error {
// TARGETS
if i == len(args)-1 {
if opts.LocalMode {
opts.RepoPath = args[i]
opts.RepoPath = filepath.Clean(args[i])
} else {
opts.URL = args[i]
}
@@ -235,8 +225,6 @@ func (opts *Options) parseOptions(args []string) error {
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 {
@@ -251,8 +239,8 @@ func (opts *Options) parseOptions(args []string) error {
}
}
err := opts.guards()
if err != nil {
fmt.Printf("%v", err)
if !opts.RepoMode && !opts.UserMode && !opts.OrgMode && !opts.LocalMode {
opts.RepoMode = true
}
return err
}
@@ -268,10 +256,10 @@ func (opts *Options) failF(format string, args ...interface{}) {
// 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 {
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.OrgMode || opts.UserMode) && !isGithubTarget(opts.URL) {
return fmt.Errorf("Not valid github target %s\n", opts.URL)
} 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 {
@@ -287,34 +275,6 @@ func (opts *Options) guards() error {
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)
}
// set to ErrorLevel if pretty printing
if opts.PrettyPrint{
atom.SetLevel(zap.ErrorLevel)
}
}
// isGithubTarget checks if url is a valid github target
func isGithubTarget(url string) bool {
re := regexp.MustCompile("github.com")
+43 -7
View File
@@ -12,6 +12,9 @@ import (
"os/signal"
"path"
"strings"
"github.com/mitchellh/go-homedir"
"path/filepath"
"log"
)
type Owner struct {
@@ -23,14 +26,42 @@ type Owner struct {
repos []Repo
}
func ownerPath(ownerName string) (string, error){
if opts.Tmp {
fmt.Println("creating tmp")
dir, err := ioutil.TempDir("", ownerName)
return dir, err
}
gitleaksHome := os.Getenv("GITLEAKS_HOME")
if gitleaksHome == "" {
homeDir, err := homedir.Dir()
if err != nil {
return "", 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 gitleaksHome + "/" + ownerName, nil
}
// newOwner instantiates an owner and creates any necessary resources for said owner.
// newOwner returns a Owner struct pointer
func newOwner() *Owner {
name := ownerName()
ownerPath, err := ownerPath(name)
if err != nil{
failF("%v", err)
}
owner := &Owner{
name: name,
url: opts.URL,
accountType: ownerType(),
path: ownerPath,
}
// listen for ctrl-c
@@ -51,11 +82,11 @@ func newOwner() *Owner {
return owner
}
/*
err := owner.setupDir()
if err != nil {
owner.failF("%v", err)
}
}*/
err = owner.fetchRepos()
if err != nil {
owner.failF("%v", err)
@@ -73,7 +104,7 @@ func (owner *Owner) fetchRepos() error {
if owner.accountType == "" {
// single repo, ambiguous account type
_, repoName := path.Split(opts.URL)
repo := newRepo(repoName, opts.URL)
repo := newRepo(repoName, opts.URL, owner.path + "/" + repoName)
owner.repos = append(owner.repos, *repo)
} else {
// org or user account type, would fail if not valid before
@@ -113,7 +144,7 @@ func (owner *Owner) fetchOrgRepos(orgOpts *github.RepositoryListByOrgOptions, gi
ctx, owner.name, orgOpts)
owner.addRepos(githubRepos)
if _, ok := err.(*github.RateLimitError); ok {
logger.Info("hit rate limit")
fmt.Println("Hit rate limit")
} else if err != nil {
return fmt.Errorf("failed fetching org repos, bad request")
} else if resp.NextPage == 0 {
@@ -140,7 +171,7 @@ func (owner *Owner) fetchUserRepos(userOpts *github.RepositoryListOptions, gitCl
ctx, owner.name, userOpts)
owner.addRepos(githubRepos)
if _, ok := err.(*github.RateLimitError); ok {
logger.Info("hit rate limit")
fmt.Println("Hit rate limit")
break
} else if err != nil {
return fmt.Errorf("failed fetching user repos, bad request")
@@ -156,7 +187,7 @@ func (owner *Owner) fetchUserRepos(userOpts *github.RepositoryListOptions, gitCl
// github's org/user response.
func (owner *Owner) addRepos(githubRepos []*github.Repository) {
for _, repo := range githubRepos {
owner.repos = append(owner.repos, *newRepo(*repo.Name, *repo.CloneURL))
owner.repos = append(owner.repos, *newRepo(*repo.Name, *repo.CloneURL, owner.path + "/" + *repo.Name))
}
}
@@ -169,7 +200,10 @@ func (owner *Owner) auditRepos() int {
failF("%v\n", err)
}
if leaksPst {
log.Printf("\x1b[31;2mLEAKS DETECTED for %s\x1b[0m!\n", repo.name)
exitCode = EXIT_LEAKS
} else {
log.Printf("No Leaks detected for \x1b[32;2m%s\x1b[0m\n", repo.name)
}
}
return exitCode
@@ -187,13 +221,14 @@ func (owner *Owner) failF(format string, args ...interface{}) {
// used for the owner repo clones.
func (owner *Owner) setupDir() error {
if opts.Tmp {
fmt.Println("creating tmp")
dir, err := ioutil.TempDir("", owner.name)
if err != nil {
fmt.Errorf("unable to create temp directories for cloning")
}
owner.path = dir
} else {
if _, err := os.Stat(opts.ClonePath); os.IsNotExist(err) {
if _, err := os.Stat(owner.path); os.IsNotExist(err) {
os.Mkdir(owner.path, os.ModePerm)
}
}
@@ -202,6 +237,7 @@ func (owner *Owner) setupDir() error {
// rmTmp removes the temporary repo
func (owner *Owner) rmTmp() {
log.Printf("removing tmp gitleaks repo for %s\n", owner.name)
os.RemoveAll(owner.path)
os.Exit(EXIT_FAILURE)
}
+11 -35
View File
@@ -4,13 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"go.uber.org/zap"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"log"
)
type Repo struct {
@@ -19,6 +19,7 @@ type Repo struct {
path string
status string // TODO
leaks []Leak
reportPath string
}
type Leak struct {
@@ -46,35 +47,23 @@ func newLocalRepo(repoPath string) *Repo {
repo := &Repo{
name: name,
path: repoPath,
reportPath: opts.ReportPath,
}
return repo
}
func newRepo(name string, url string) *Repo {
func newRepo(name string, url string, path string) *Repo {
repo := &Repo{
name: name,
url: url,
// TODO handle existing one
path: opts.ClonePath + "/" + name,
path: path,
reportPath: opts.ReportPath,
}
return repo
}
func (repo *Repo) Info(msg string) {
// logger should have these infos: msg, repo, owner, time
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
@@ -102,15 +91,13 @@ func (repo *Repo) audit(owner *Owner) (bool, error) {
return false, fmt.Errorf("%s does not exist", repo.path)
}
// no repo present, clone it
repo.Info("cloning")
repo.PrettyPrintF("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url)
log.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 {
return false, fmt.Errorf("cannot clone %s into %s", repo.url, repo.path)
}
} else {
repo.Info("fetching")
repo.PrettyPrintF("Fetching \x1b[37;1m%s\x1b[0m...\n", repo.url)
log.Printf("Fetching \x1b[37;1m%s\x1b[0m from %s ...\n", repo.name, repo.path)
err = exec.Command("git", "fetch").Run()
if err != nil {
return false, fmt.Errorf("cannot fetch %s from %s", repo.url, repo.path)
@@ -171,14 +158,13 @@ func (repo *Repo) writeReport() error {
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(opts.ReportPath, reportFileName)
reportFile := filepath.Join(repo.reportPath, reportFileName)
err := ioutil.WriteFile(reportFile, reportJSON, 0644)
if err != nil {
return err
}
repo.Info(fmt.Sprintf("Report written to %s\n", reportFile))
log.Printf("report for %s written to %s", repo.name, reportFileName)
return nil
}
@@ -209,22 +195,12 @@ 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 {
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.Printf("failed to output leak: %v", err)
}
fmt.Println(string(b))
}
+2
View File
@@ -12,6 +12,7 @@ func TestNewLocalRepo(t *testing.T) {
func TestWriteReport(t *testing.T) {
// TODO
/*
opts, err := defaultOptions()
r := newRepo("fakerepo", "github.com")
sampleLeak := Leak{
@@ -26,4 +27,5 @@ func TestWriteReport(t *testing.T) {
}
r.leaks = []Leak{sampleLeak, sampleLeak}
r.writeReport()
*/
}