From a77b6e4f8bf8bc79ddf20c3311c28fa6ef4d4f03 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sun, 18 Feb 2018 12:03:46 -0600 Subject: [PATCH 1/3] init org user scan work --- leaks.go | 7 ++++--- main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++--- options.go | 44 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 97 insertions(+), 9 deletions(-) diff --git a/leaks.go b/leaks.go index 135b9033..069507a0 100644 --- a/leaks.go +++ b/leaks.go @@ -14,20 +14,21 @@ import ( "syscall" ) +// LeakElem contains the line and commit of a leak type LeakElem struct { Line string `json:"line"` Commit string `json:"commit"` } -func start(_ *Options, repoURL string) { +func start(opts *Options) { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) - err := exec.Command("git", "clone", repoURL).Run() + err := exec.Command("git", "clone", opts.RepoURL).Run() if err != nil { log.Fatalf("failed to clone repo %v", err) } - repoName := getLocalRepoName(repoURL) + repoName := getLocalRepoName(opts.RepoURL) if err = os.Chdir(repoName); err != nil { log.Fatal(err) } diff --git a/main.go b/main.go index 07cf468e..4ad58c1c 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,14 @@ package main import ( + "encoding/json" + "fmt" + _ "io/ioutil" "log" + "net/http" "os" "regexp" + "strings" ) var ( @@ -35,8 +40,52 @@ func init() { } func main() { - args := os.Args[2:] - repoURL := os.Args[1] + args := os.Args[1:] opts := parseOptions(args) - start(opts, repoURL) + fmt.Println(opts) + if opts.RepoURL != "" { + start(opts) + } else if opts.UserURL != "" || opts.OrgURL != "" { + repoList := repoScan(opts) + fmt.Println(repoList) + for _, repo := range repoList { + fmt.Println("yoo") + fmt.Println(opts.RepoURL) + opts.RepoURL = repo.RepoURL + start(opts) + } + } +} + +// RepoElem used for parsing json from github api +type RepoElem struct { + RepoURL string `json:"html_url"` +} + +// repoScan attempts to parse all repo urls from an organization or user +func repoScan(opts *Options) []RepoElem { + var ( + targetURL string + target string + targetType string + repoList []RepoElem + ) + + if opts.UserURL != "" { + targetURL = opts.UserURL + targetType = "users" + } else { + targetURL = opts.OrgURL + targetType = "org" + } + splitTargetURL := strings.Split(targetURL, "/") + target = splitTargetURL[len(splitTargetURL)-1] + + resp, err := http.Get(fmt.Sprintf("https://api.github.com/%s/%s/repos", targetType, target)) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + json.NewDecoder(resp.Body).Decode(&repoList) + return repoList } diff --git a/options.go b/options.go index 5922871f..59973c69 100644 --- a/options.go +++ b/options.go @@ -16,15 +16,22 @@ Options: -h --help Display this message ` +// Options for gitleaks type Options struct { Concurrency int + UserURL string + OrgURL string + RepoURL string } +// 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 { if len(args) > *i+1 { *i++ @@ -39,20 +46,51 @@ func optionsNextInt(args []string, i *int) int { return argInt } +// optionsNextString is a parseOptions helper that returns the value (string) of an option +// if valid. +func optionsNextString(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() + } + return args[*i] +} + +// parseOptions func parseOptions(args []string) *Options { opts := &Options{} + + // default is repo if no additional options + if len(args) == 1 { + opts.RepoURL = args[0] + return opts + } + for i := 0; i < len(args); i++ { arg := args[i] switch arg { case "-c": opts.Concurrency = optionsNextInt(args, &i) + case "-o": + opts.OrgURL = optionsNextString(args, &i) + case "-u": + opts.UserURL = optionsNextString(args, &i) + case "-r": + opts.RepoURL = optionsNextString(args, &i) case "-h", "--help": help() return nil default: - fmt.Printf("Uknown option %s\n\n", arg) - help() - return nil + if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" && + opts.UserURL == "" { + opts.RepoURL = arg + } else { + fmt.Printf("Uknown option %s\n\n", arg) + help() + return nil + } } } From 5d924ecc5370cf7e25d7d3767ba6ff7757dde1c5 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sun, 18 Feb 2018 17:28:36 -0600 Subject: [PATCH 2/3] logging --- leaks.go | 7 ++++++- main.go | 3 --- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/leaks.go b/leaks.go index 069507a0..78c33866 100644 --- a/leaks.go +++ b/leaks.go @@ -21,12 +21,14 @@ type LeakElem struct { } func start(opts *Options) { + fmt.Printf("\nEvaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL) c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) err := exec.Command("git", "clone", opts.RepoURL).Run() if err != nil { - log.Fatalf("failed to clone repo %v", err) + log.Printf("failed to clone repo %v", err) + return } repoName := getLocalRepoName(opts.RepoURL) if err = os.Chdir(repoName); err != nil { @@ -39,6 +41,9 @@ func start(opts *Options) { }() report := getLeaks(repoName) + if len(report) == 0 { + fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL) + } cleanup(repoName) reportJSON, _ := json.MarshalIndent(report, "", "\t") err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJSON, 0644) diff --git a/main.go b/main.go index 4ad58c1c..60b2b5d7 100644 --- a/main.go +++ b/main.go @@ -47,10 +47,7 @@ func main() { start(opts) } else if opts.UserURL != "" || opts.OrgURL != "" { repoList := repoScan(opts) - fmt.Println(repoList) for _, repo := range repoList { - fmt.Println("yoo") - fmt.Println(opts.RepoURL) opts.RepoURL = repo.RepoURL start(opts) } From 825d434c662b4f04f8689c2f6039234b3519f8d1 Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sun, 18 Feb 2018 17:58:53 -0600 Subject: [PATCH 3/3] concurrency option functional --- leaks.go | 14 ++++++++++---- main.go | 1 - 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/leaks.go b/leaks.go index 78c33866..b46fa2a3 100644 --- a/leaks.go +++ b/leaks.go @@ -20,6 +20,7 @@ type LeakElem struct { Commit string `json:"commit"` } +// start clones and determines if there are any leaks func start(opts *Options) { fmt.Printf("\nEvaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL) c := make(chan os.Signal, 2) @@ -40,7 +41,7 @@ func start(opts *Options) { os.Exit(1) }() - report := getLeaks(repoName) + report := getLeaks(repoName, opts.Concurrency) if len(report) == 0 { fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL) } @@ -63,6 +64,7 @@ func getLocalRepoName(url string) string { return name } +// cleanup deletes the repo func cleanup(repoName string) { if err := os.Chdir(appRoot); err != nil { log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err) @@ -73,18 +75,22 @@ func cleanup(repoName string) { } } -func getLeaks(repoName string) []LeakElem { +// getLeaks will attempt to find gitleaks +func getLeaks(repoName string, concurrency int) []LeakElem { var ( out []byte err error commitWG sync.WaitGroup gitLeakReceiverWG sync.WaitGroup - concurrent = 100 - semaphoreChan = make(chan struct{}, concurrent) gitLeaks = make(chan LeakElem) report []LeakElem ) + if concurrency == 0 { + concurrency = 100 + } + semaphoreChan := make(chan struct{}, concurrency) + go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) { for gitLeak := range gitLeaks { fmt.Println(gitLeak) diff --git a/main.go b/main.go index 60b2b5d7..18a22c0d 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,6 @@ func init() { func main() { args := os.Args[1:] opts := parseOptions(args) - fmt.Println(opts) if opts.RepoURL != "" { start(opts) } else if opts.UserURL != "" || opts.OrgURL != "" {