Răsfoiți Sursa

init org user scan work

zricethezav 8 ani în urmă
părinte
comite
a77b6e4f8b
3 a modificat fișierele cu 97 adăugiri și 9 ștergeri
  1. 4 3
      leaks.go
  2. 52 3
      main.go
  3. 41 3
      options.go

+ 4 - 3
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)
 	}

+ 52 - 3
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
 }

+ 41 - 3
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
+			}
 		}
 	}