structured logs

This commit is contained in:
zricethezav committed 2018-02-28 22:02:03 -06:00
1 parent 7919e15a65
commit 0d75136c1c
4 files changed
+50 -30

No files matched your search

+1 -3
View File
@@ -1,13 +1,13 @@
package main package main
import ( import (
"fmt"
_ "fmt" _ "fmt"
"go.uber.org/zap" "go.uber.org/zap"
_ "io/ioutil" _ "io/ioutil"
"os" "os"
"regexp" "regexp"
_ "time" _ "time"
"fmt"
) )
const EXIT_CLEAN = 0 const EXIT_CLEAN = 0
@@ -58,5 +58,3 @@ func failF(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...) fmt.Fprintf(os.Stderr, format, args...)
os.Exit(EXIT_FAILURE) os.Exit(EXIT_FAILURE)
} }
+15 -9
View File
@@ -2,14 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strconv"
"strings"
"regexp"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"path/filepath"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
) )
const DEBUG = 0 const DEBUG = 0
@@ -43,10 +43,10 @@ Other
-h --help Display this message -h --help Display this message
--token=<STR> Github API token --token=<STR> Github API token
--stopwords Enables stopwords --stopwords Enables stopwords
--pretty Enables pretty printing for humans, otherwise you'll get logs'
` `
// Options for gitleaks. need to support remote repo/owner // Options for gitleaks. need to support remote repo/owner
// and local repo/owner mode // and local repo/owner mode
type Options struct { type Options struct {
@@ -77,8 +77,8 @@ type Options struct {
Token string Token string
// LOGS/REPORT // LOGS/REPORT
Verbose bool
LogLevel int LogLevel int
PrettyPrint bool
} }
// help prints the usage string and exits // help prints the usage string and exits
@@ -135,7 +135,7 @@ func (opts *Options) optInt(arg string, prefixes ...string) (bool, int) {
} }
// newOpts generates opts and parses arguments // newOpts generates opts and parses arguments
func newOpts(args []string) (*Options) { func newOpts(args []string) *Options {
opts, err := defaultOptions() opts, err := defaultOptions()
if err != nil { if err != nil {
opts.failF("%v", err) opts.failF("%v", err)
@@ -211,6 +211,8 @@ func (opts *Options) parseOptions(args []string) error {
case "--report-out": case "--report-out":
opts.ReportOut = true opts.ReportOut = true
case "--pretty":
opts.PrettyPrint = true
case "-t", "--temp": case "-t", "--temp":
opts.Tmp = true opts.Tmp = true
@@ -222,7 +224,6 @@ func (opts *Options) parseOptions(args []string) error {
default: default:
// TARGETS // TARGETS
if i == len(args)-1 { if i == len(args)-1 {
fmt.Println(arg[i])
if opts.LocalMode { if opts.LocalMode {
opts.RepoPath = args[i] opts.RepoPath = args[i]
} else { } else {
@@ -307,6 +308,11 @@ func (opts *Options) setupLogger() {
case ERROR: case ERROR:
atom.SetLevel(zap.ErrorLevel) 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
+5 -10
View File
@@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
_ "fmt" _ "fmt"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@@ -11,7 +12,6 @@ import (
"os/signal" "os/signal"
"path" "path"
"strings" "strings"
"fmt"
) )
type Owner struct { type Owner struct {
@@ -25,7 +25,7 @@ type Owner struct {
// 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()
owner := &Owner{ owner := &Owner{
name: name, name: name,
@@ -44,7 +44,6 @@ func newOwner() (*Owner) {
owner.rmTmp() owner.rmTmp()
}() }()
// if running on local repo, just go right to it. // if running on local repo, just go right to it.
if opts.LocalMode { if opts.LocalMode {
repo := newLocalRepo(opts.RepoPath) repo := newLocalRepo(opts.RepoPath)
@@ -162,7 +161,7 @@ func (owner *Owner) addRepos(githubRepos []*github.Repository) {
} }
// auditRepos // auditRepos
func (owner *Owner) auditRepos() (int) { func (owner *Owner) auditRepos() int {
exitCode := EXIT_CLEAN exitCode := EXIT_CLEAN
for _, repo := range owner.repos { for _, repo := range owner.repos {
leaksPst, err := repo.audit(owner) leaksPst, err := repo.audit(owner)
@@ -190,8 +189,7 @@ func (owner *Owner) setupDir() error {
if opts.Tmp { if opts.Tmp {
dir, err := ioutil.TempDir("", owner.name) dir, err := ioutil.TempDir("", owner.name)
if err != nil { if err != nil {
return err fmt.Errorf("unable to create temp directories for cloning")
owner.failF("Unabled to create temp directories for cloning")
} }
owner.path = dir owner.path = dir
} else { } else {
@@ -200,9 +198,6 @@ func (owner *Owner) setupDir() error {
} }
} }
return nil return nil
// TODO could be handled via option
// owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name)
} }
// rmTmp removes the temporary repo // rmTmp removes the temporary repo
@@ -226,7 +221,7 @@ func ownerType() string {
// ownerName returns the owner name extracted from the urls provided in opts. // 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 // If no RepoURL, OrgURL, or UserURL is provided, then owner will log an error
// and gitleaks will exit. // and gitleaks will exit.
func ownerName() (string) { func ownerName() string {
if opts.RepoMode { if opts.RepoMode {
splitSlashes := strings.Split(opts.URL, "/") splitSlashes := strings.Split(opts.URL, "/")
return splitSlashes[len(splitSlashes)-2] return splitSlashes[len(splitSlashes)-2]
+29 -8
View File
@@ -8,9 +8,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"sync" "sync"
"path"
) )
type Repo struct { type Repo struct {
@@ -61,14 +61,20 @@ func newRepo(name string, url string) *Repo {
return repo return repo
} }
func (repo *Repo) rLog(msg string) { func (repo *Repo) Info(msg string) {
// logger should have these infos: msg, repo, owner, time // logger should have these infos: msg, repo, owner, time
logger.Debug("Beginning audit", logger.Info(msg,
zap.String("repo", repo.name), zap.String("repo", repo.name),
zap.String("repo_path", repo.path), 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
@@ -96,13 +102,15 @@ 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
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() 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 {
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() 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)
@@ -170,7 +178,7 @@ func (repo *Repo) writeReport() error {
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("Report written to %s\n", reportFile) repo.Info(fmt.Sprintf("Report written to %s\n", reportFile))
return nil return nil
} }
@@ -201,13 +209,26 @@ 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)
if opts.PrettyPrint {
b, err := json.MarshalIndent(gitLeak, "", " ") b, err := json.MarshalIndent(gitLeak, "", " ")
if err != nil { if err != nil {
// make waitgroup errArray // handle this?
fmt.Println("failed to output leak:", err) fmt.Println("failed to output leak:", err)
} }
fmt.Println(string(b)) fmt.Println(string(b))
*leaks = append(*leaks, gitLeak) }
gitLeakReceiverWG.Done() gitLeakReceiverWG.Done()
} }
} }