From b6b7cfb6d3646d4bd97aabb12ef28d7b60ac3523 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Tue, 16 Aug 2022 19:36:25 -0500 Subject: [PATCH] draft: bump gitdiff, add git.Err state, better log messages (#954) * bump gitdiff, add git.Err state, better log messages * remove cmd.Start * forgot to start... --- cmd/detect.go | 30 ++++++++++++++++++++++-------- detect/detect.go | 3 +++ detect/git/git.go | 31 +++++++++++++++++-------------- go.mod | 4 ++-- go.sum | 4 ++-- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/cmd/detect.go b/cmd/detect.go index 539d7b02..a77cd6b4 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -94,38 +94,52 @@ func runDetect(cmd *cobra.Command, args []string) { findings, err = detector.DetectFiles(source) if err != nil { // don't exit on error, just log it - log.Error().Err(err) + log.Error().Msg(err.Error()) } } else { - logOpts, err := cmd.Flags().GetString("log-opts") + var logOpts string + logOpts, err = cmd.Flags().GetString("log-opts") if err != nil { log.Fatal().Err(err) } findings, err = detector.DetectGit(source, logOpts, detect.DetectType) if err != nil { // don't exit on error, just log it - log.Error().Err(err) + log.Error().Msg(err.Error()) } } // log info about the scan - log.Info().Msgf("scan completed in %s", time.Since(start)) - if len(findings) != 0 { - log.Warn().Msgf("leaks found: %d", len(findings)) + if err == nil { + log.Info().Msgf("scan completed in %s", time.Since(start)) + if len(findings) != 0 { + log.Warn().Msgf("leaks found: %d", len(findings)) + } else { + log.Info().Msg("no leaks found") + } } else { - log.Info().Msg("no leaks found") + log.Warn().Msgf("partial scan completed in %s", time.Since(start)) + if len(findings) != 0 { + log.Warn().Msgf("%d leaks found in partial scan", len(findings)) + } else { + log.Warn().Msg("no leaks found in partial scan") + } } // write report if desired reportPath, _ := cmd.Flags().GetString("report-path") ext, _ := cmd.Flags().GetString("report-format") if reportPath != "" { - if err = report.Write(findings, cfg, ext, reportPath); err != nil { + if err := report.Write(findings, cfg, ext, reportPath); err != nil { log.Fatal().Err(err) } } + if err != nil { + os.Exit(1) + } + if len(findings) != 0 { os.Exit(exitCode) } diff --git a/detect/detect.go b/detect/detect.go index a840b99f..3b520d22 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -342,6 +342,9 @@ func (d *Detector) DetectGit(source string, logOpts string, gitScanType GitScanT return d.findings, err } log.Debug().Msgf("%d commits scanned. Note: this number might be smaller than expected due to commits with no additions", len(d.commitMap)) + if git.ErrEncountered { + return d.findings, fmt.Errorf("%s", "git error encountered, see logs") + } return d.findings, nil } diff --git a/detect/git/git.go b/detect/git/git.go index bbbb4c1f..944fd492 100644 --- a/detect/git/git.go +++ b/detect/git/git.go @@ -3,7 +3,6 @@ package git import ( "bufio" "io" - "os" "os/exec" "path/filepath" "strings" @@ -13,6 +12,8 @@ import ( "github.com/rs/zerolog/log" ) +var ErrEncountered bool + // GitLog returns a channel of gitdiff.File objects from the // git log -p command for the given source. func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) { @@ -37,15 +38,16 @@ func GitLog(source string, logOpts string) (<-chan *gitdiff.File, error) { if err != nil { return nil, err } + + go listenForStdErr(stderr) + if err := cmd.Start(); err != nil { return nil, err } - - go listenForStdErr(stderr) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722 time.Sleep(50 * time.Millisecond) - return gitdiff.Parse(stdout) + return gitdiff.Parse(cmd, stdout) } // GitDiff returns a channel of gitdiff.File objects from @@ -68,22 +70,22 @@ func GitDiff(source string, staged bool) (<-chan *gitdiff.File, error) { if err != nil { return nil, err } + + go listenForStdErr(stderr) + if err := cmd.Start(); err != nil { return nil, err } - - go listenForStdErr(stderr) // HACK: to avoid https://github.com/zricethezav/gitleaks/issues/722 time.Sleep(50 * time.Millisecond) - return gitdiff.Parse(stdout) + return gitdiff.Parse(cmd, stdout) } // listenForStdErr listens for stderr output from git and prints it to stdout // then exits with exit code 1 func listenForStdErr(stderr io.ReadCloser) { scanner := bufio.NewScanner(stderr) - errEncountered := false for scanner.Scan() { // if git throws one of the following errors: // @@ -105,14 +107,15 @@ func listenForStdErr(stderr io.ReadCloser) { "inexact rename detection was skipped") || strings.Contains(scanner.Text(), "you may want to set your diff.renameLimit") { - log.Warn().Msg(scanner.Text()) } else { - log.Error().Msg(scanner.Text()) - errEncountered = true + log.Error().Msgf("[git] %s", scanner.Text()) + + // asynchronously set this error flag to true so that we can + // capture a log message and exit with a non-zero exit code + // This value should get set before the `git` command exits so it's + // safe-ish, although I know I know, bad practice. + ErrEncountered = true } } - if errEncountered { - os.Exit(1) - } } diff --git a/go.mod b/go.mod index 6e80853c..7ff9298f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.17 require ( github.com/fatih/semgroup v1.2.0 - github.com/gitleaks/go-gitdiff v0.7.6 + github.com/gitleaks/go-gitdiff v0.8.0 + github.com/h2non/filetype v1.1.3 github.com/rs/zerolog v1.26.1 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 @@ -14,7 +15,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect - github.com/h2non/filetype v1.1.3 github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb diff --git a/go.sum b/go.sum index a8449e00..e371e003 100644 --- a/go.sum +++ b/go.sum @@ -72,8 +72,8 @@ github.com/fatih/semgroup v1.2.0/go.mod h1:1KAD4iIYfXjE4U13B48VM4z9QUwV5Tt8O4rS8 github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gitleaks/go-gitdiff v0.7.6 h1:atcfoNPD9erzPs9C89a+i2Y+EUmR2QKB5QHJTfB4n60= -github.com/gitleaks/go-gitdiff v0.7.6/go.mod h1:pKz0X4YzCKZs30BL+weqBIG7mx0jl4tF1uXV9ZyNvrA= +github.com/gitleaks/go-gitdiff v0.8.0 h1:7aExTZm+K/M/EQKOyYcub8rIAdWK6ONxPGuRzxmWW+0= +github.com/gitleaks/go-gitdiff v0.8.0/go.mod h1:pKz0X4YzCKZs30BL+weqBIG7mx0jl4tF1uXV9ZyNvrA= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=