mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
cleaning up path logic for clones and reports, local repos are supported now
This commit is contained in:
1 parent
648ae701bf
commit
39c9cb617c
5 files changed
+65
-93
No files matched your search
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "fmt"
|
_ "fmt"
|
||||||
"go.uber.org/zap"
|
|
||||||
_ "io/ioutil"
|
_ "io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -22,7 +21,6 @@ var (
|
|||||||
hexChars string
|
hexChars string
|
||||||
assignRegex *regexp.Regexp
|
assignRegex *regexp.Regexp
|
||||||
fileDiffRegex *regexp.Regexp
|
fileDiffRegex *regexp.Regexp
|
||||||
logger *zap.Logger
|
|
||||||
opts *Options
|
opts *Options
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+9
-49
@@ -2,20 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEBUG = 0
|
|
||||||
const INFO = 1
|
|
||||||
const ERROR = 2
|
|
||||||
|
|
||||||
const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
|
const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
@@ -53,7 +46,6 @@ type Options struct {
|
|||||||
URL string
|
URL string
|
||||||
RepoPath string
|
RepoPath string
|
||||||
|
|
||||||
ClonePath string
|
|
||||||
ReportPath string
|
ReportPath string
|
||||||
|
|
||||||
Concurrency int
|
Concurrency int
|
||||||
@@ -144,12 +136,12 @@ func newOpts(args []string) *Options {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
opts.failF("%v", err)
|
opts.failF("%v", err)
|
||||||
}
|
}
|
||||||
opts.setupLogger()
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
// deafultOptions provides the default options
|
// deafultOptions provides the default options
|
||||||
func defaultOptions() (*Options, error) {
|
func defaultOptions() (*Options, error) {
|
||||||
|
/*
|
||||||
// default GITLEAKS_HOME is $HOME/.gitleaks
|
// default GITLEAKS_HOME is $HOME/.gitleaks
|
||||||
// gitleaks will use this location for clones if
|
// gitleaks will use this location for clones if
|
||||||
// no clone-path is provided
|
// no clone-path is provided
|
||||||
@@ -166,14 +158,12 @@ func defaultOptions() (*Options, error) {
|
|||||||
if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) {
|
if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) {
|
||||||
os.Mkdir(gitleaksHome, os.ModePerm)
|
os.Mkdir(gitleaksHome, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
return &Options{
|
return &Options{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
B64EntropyCutoff: 70,
|
B64EntropyCutoff: 70,
|
||||||
HexEntropyCutoff: 40,
|
HexEntropyCutoff: 40,
|
||||||
LogLevel: INFO,
|
|
||||||
ClonePath: filepath.Join(gitleaksHome, "clone"),
|
|
||||||
ReportPath: filepath.Join(gitleaksHome, "report"),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +215,7 @@ func (opts *Options) parseOptions(args []string) error {
|
|||||||
// TARGETS
|
// TARGETS
|
||||||
if i == len(args)-1 {
|
if i == len(args)-1 {
|
||||||
if opts.LocalMode {
|
if opts.LocalMode {
|
||||||
opts.RepoPath = args[i]
|
opts.RepoPath = filepath.Clean(args[i])
|
||||||
} else {
|
} else {
|
||||||
opts.URL = args[i]
|
opts.URL = args[i]
|
||||||
}
|
}
|
||||||
@@ -235,8 +225,6 @@ func (opts *Options) parseOptions(args []string) error {
|
|||||||
opts.SinceCommit = value
|
opts.SinceCommit = value
|
||||||
} else if match, value := opts.optString(arg, "--report-path="); match {
|
} else if match, value := opts.optString(arg, "--report-path="); match {
|
||||||
opts.ReportPath = value
|
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 {
|
} else if match, value := opts.optInt(arg, "--log="); match {
|
||||||
opts.LogLevel = value
|
opts.LogLevel = value
|
||||||
} else if match, value := opts.optInt(arg, "--b64Entropy="); match {
|
} else if match, value := opts.optInt(arg, "--b64Entropy="); match {
|
||||||
@@ -251,8 +239,8 @@ func (opts *Options) parseOptions(args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := opts.guards()
|
err := opts.guards()
|
||||||
if err != nil {
|
if !opts.RepoMode && !opts.UserMode && !opts.OrgMode && !opts.LocalMode {
|
||||||
fmt.Printf("%v", err)
|
opts.RepoMode = true
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -268,10 +256,10 @@ func (opts *Options) failF(format string, args ...interface{}) {
|
|||||||
// guards will prevent gitleaks from continuing if any invalid options
|
// guards will prevent gitleaks from continuing if any invalid options
|
||||||
// are found.
|
// are found.
|
||||||
func (opts *Options) guards() error {
|
func (opts *Options) guards() error {
|
||||||
if (opts.RepoMode || opts.OrgMode || opts.UserMode) && !isGithubTarget(opts.URL) {
|
if (opts.RepoMode || opts.OrgMode || opts.UserMode) && opts.LocalMode {
|
||||||
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")
|
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 {
|
} else if (opts.RepoMode || opts.UserMode) && opts.OrgMode {
|
||||||
return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
|
return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
|
||||||
} else if (opts.OrgMode || opts.UserMode) && opts.RepoMode {
|
} else if (opts.OrgMode || opts.UserMode) && opts.RepoMode {
|
||||||
@@ -287,34 +275,6 @@ func (opts *Options) guards() error {
|
|||||||
return nil
|
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
|
// isGithubTarget checks if url is a valid github target
|
||||||
func isGithubTarget(url string) bool {
|
func isGithubTarget(url string) bool {
|
||||||
re := regexp.MustCompile("github.com")
|
re := regexp.MustCompile("github.com")
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
|
"path/filepath"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Owner struct {
|
type Owner struct {
|
||||||
@@ -23,14 +26,42 @@ type Owner struct {
|
|||||||
repos []Repo
|
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 instantiates an owner and creates any necessary resources for said owner.
|
||||||
// newOwner returns a Owner struct pointer
|
// newOwner returns a Owner struct pointer
|
||||||
func newOwner() *Owner {
|
func newOwner() *Owner {
|
||||||
name := ownerName()
|
name := ownerName()
|
||||||
|
ownerPath, err := ownerPath(name)
|
||||||
|
if err != nil{
|
||||||
|
failF("%v", err)
|
||||||
|
}
|
||||||
owner := &Owner{
|
owner := &Owner{
|
||||||
name: name,
|
name: name,
|
||||||
url: opts.URL,
|
url: opts.URL,
|
||||||
accountType: ownerType(),
|
accountType: ownerType(),
|
||||||
|
path: ownerPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
// listen for ctrl-c
|
// listen for ctrl-c
|
||||||
@@ -51,11 +82,11 @@ func newOwner() *Owner {
|
|||||||
return owner
|
return owner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
err := owner.setupDir()
|
err := owner.setupDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
owner.failF("%v", err)
|
owner.failF("%v", err)
|
||||||
}
|
}*/
|
||||||
|
|
||||||
err = owner.fetchRepos()
|
err = owner.fetchRepos()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
owner.failF("%v", err)
|
owner.failF("%v", err)
|
||||||
@@ -73,7 +104,7 @@ func (owner *Owner) fetchRepos() error {
|
|||||||
if owner.accountType == "" {
|
if owner.accountType == "" {
|
||||||
// single repo, ambiguous account type
|
// single repo, ambiguous account type
|
||||||
_, repoName := path.Split(opts.URL)
|
_, repoName := path.Split(opts.URL)
|
||||||
repo := newRepo(repoName, opts.URL)
|
repo := newRepo(repoName, opts.URL, owner.path + "/" + repoName)
|
||||||
owner.repos = append(owner.repos, *repo)
|
owner.repos = append(owner.repos, *repo)
|
||||||
} else {
|
} else {
|
||||||
// org or user account type, would fail if not valid before
|
// 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)
|
ctx, owner.name, orgOpts)
|
||||||
owner.addRepos(githubRepos)
|
owner.addRepos(githubRepos)
|
||||||
if _, ok := err.(*github.RateLimitError); ok {
|
if _, ok := err.(*github.RateLimitError); ok {
|
||||||
logger.Info("hit rate limit")
|
fmt.Println("Hit rate limit")
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("failed fetching org repos, bad request")
|
return fmt.Errorf("failed fetching org repos, bad request")
|
||||||
} else if resp.NextPage == 0 {
|
} else if resp.NextPage == 0 {
|
||||||
@@ -140,7 +171,7 @@ func (owner *Owner) fetchUserRepos(userOpts *github.RepositoryListOptions, gitCl
|
|||||||
ctx, owner.name, userOpts)
|
ctx, owner.name, userOpts)
|
||||||
owner.addRepos(githubRepos)
|
owner.addRepos(githubRepos)
|
||||||
if _, ok := err.(*github.RateLimitError); ok {
|
if _, ok := err.(*github.RateLimitError); ok {
|
||||||
logger.Info("hit rate limit")
|
fmt.Println("Hit rate limit")
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("failed fetching user repos, bad request")
|
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.
|
// github's org/user response.
|
||||||
func (owner *Owner) addRepos(githubRepos []*github.Repository) {
|
func (owner *Owner) addRepos(githubRepos []*github.Repository) {
|
||||||
for _, repo := range githubRepos {
|
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)
|
failF("%v\n", err)
|
||||||
}
|
}
|
||||||
if leaksPst {
|
if leaksPst {
|
||||||
|
log.Printf("\x1b[31;2mLEAKS DETECTED for %s\x1b[0m!\n", repo.name)
|
||||||
exitCode = EXIT_LEAKS
|
exitCode = EXIT_LEAKS
|
||||||
|
} else {
|
||||||
|
log.Printf("No Leaks detected for \x1b[32;2m%s\x1b[0m\n", repo.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return exitCode
|
return exitCode
|
||||||
@@ -187,13 +221,14 @@ func (owner *Owner) failF(format string, args ...interface{}) {
|
|||||||
// used for the owner repo clones.
|
// used for the owner repo clones.
|
||||||
func (owner *Owner) setupDir() error {
|
func (owner *Owner) setupDir() error {
|
||||||
if opts.Tmp {
|
if opts.Tmp {
|
||||||
|
fmt.Println("creating tmp")
|
||||||
dir, err := ioutil.TempDir("", owner.name)
|
dir, err := ioutil.TempDir("", owner.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("unable to create temp directories for cloning")
|
fmt.Errorf("unable to create temp directories for cloning")
|
||||||
}
|
}
|
||||||
owner.path = dir
|
owner.path = dir
|
||||||
} else {
|
} 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)
|
os.Mkdir(owner.path, os.ModePerm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,6 +237,7 @@ func (owner *Owner) setupDir() error {
|
|||||||
|
|
||||||
// rmTmp removes the temporary repo
|
// rmTmp removes the temporary repo
|
||||||
func (owner *Owner) rmTmp() {
|
func (owner *Owner) rmTmp() {
|
||||||
|
log.Printf("removing tmp gitleaks repo for %s\n", owner.name)
|
||||||
os.RemoveAll(owner.path)
|
os.RemoveAll(owner.path)
|
||||||
os.Exit(EXIT_FAILURE)
|
os.Exit(EXIT_FAILURE)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.uber.org/zap"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repo struct {
|
type Repo struct {
|
||||||
@@ -19,6 +19,7 @@ type Repo struct {
|
|||||||
path string
|
path string
|
||||||
status string // TODO
|
status string // TODO
|
||||||
leaks []Leak
|
leaks []Leak
|
||||||
|
reportPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Leak struct {
|
type Leak struct {
|
||||||
@@ -46,35 +47,23 @@ func newLocalRepo(repoPath string) *Repo {
|
|||||||
repo := &Repo{
|
repo := &Repo{
|
||||||
name: name,
|
name: name,
|
||||||
path: repoPath,
|
path: repoPath,
|
||||||
|
reportPath: opts.ReportPath,
|
||||||
}
|
}
|
||||||
return repo
|
return repo
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRepo(name string, url string) *Repo {
|
func newRepo(name string, url string, path string) *Repo {
|
||||||
repo := &Repo{
|
repo := &Repo{
|
||||||
name: name,
|
name: name,
|
||||||
url: url,
|
url: url,
|
||||||
// TODO handle existing one
|
// TODO handle existing one
|
||||||
path: opts.ClonePath + "/" + name,
|
path: path,
|
||||||
|
reportPath: opts.ReportPath,
|
||||||
}
|
}
|
||||||
return repo
|
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.
|
// 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
|
// 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
|
// `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)
|
return false, fmt.Errorf("%s does not exist", repo.path)
|
||||||
}
|
}
|
||||||
// no repo present, clone it
|
// no repo present, clone it
|
||||||
repo.Info("cloning")
|
log.Printf("Cloning \x1b[37;1m%s\x1b[0m into %s...\n", repo.url, repo.path)
|
||||||
repo.PrettyPrintF("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url)
|
|
||||||
err = exec.Command("git", "clone", repo.url, repo.path).Run()
|
err = exec.Command("git", "clone", repo.url, repo.path).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("cannot clone %s into %s", repo.url, repo.path)
|
return false, fmt.Errorf("cannot clone %s into %s", repo.url, repo.path)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
repo.Info("fetching")
|
log.Printf("Fetching \x1b[37;1m%s\x1b[0m from %s ...\n", repo.name, repo.path)
|
||||||
repo.PrettyPrintF("Fetching \x1b[37;1m%s\x1b[0m...\n", repo.url)
|
|
||||||
err = exec.Command("git", "fetch").Run()
|
err = exec.Command("git", "fetch").Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("cannot fetch %s from %s", repo.url, repo.path)
|
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) {
|
if _, err := os.Stat(opts.ReportPath); os.IsNotExist(err) {
|
||||||
os.Mkdir(opts.ReportPath, os.ModePerm)
|
os.Mkdir(opts.ReportPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
reportFileName := fmt.Sprintf("%s_leaks.json", repo.name)
|
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)
|
err := ioutil.WriteFile(reportFile, reportJSON, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,22 +195,12 @@ func parseRevList(revList [][]byte) []Commit {
|
|||||||
// reportAggregator is a go func responsible for ...
|
// reportAggregator is a go func responsible for ...
|
||||||
func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) {
|
func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) {
|
||||||
for gitLeak := range gitLeaks {
|
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)
|
*leaks = append(*leaks, gitLeak)
|
||||||
if opts.PrettyPrint {
|
if opts.PrettyPrint {
|
||||||
b, err := json.MarshalIndent(gitLeak, "", " ")
|
b, err := json.MarshalIndent(gitLeak, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle this?
|
// handle this?
|
||||||
fmt.Println("failed to output leak:", err)
|
fmt.Printf("failed to output leak: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(b))
|
fmt.Println(string(b))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ func TestNewLocalRepo(t *testing.T) {
|
|||||||
|
|
||||||
func TestWriteReport(t *testing.T) {
|
func TestWriteReport(t *testing.T) {
|
||||||
// TODO
|
// TODO
|
||||||
|
/*
|
||||||
opts, err := defaultOptions()
|
opts, err := defaultOptions()
|
||||||
r := newRepo("fakerepo", "github.com")
|
r := newRepo("fakerepo", "github.com")
|
||||||
sampleLeak := Leak{
|
sampleLeak := Leak{
|
||||||
@@ -26,4 +27,5 @@ func TestWriteReport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
r.leaks = []Leak{sampleLeak, sampleLeak}
|
r.leaks = []Leak{sampleLeak, sampleLeak}
|
||||||
r.writeReport()
|
r.writeReport()
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user