From 6018a6bb706505a8da841e259a422943fc77450d Mon Sep 17 00:00:00 2001 From: Eddie Austin <84474478+eddie-austin@users.noreply.github.com> Date: Mon, 6 Sep 2021 14:39:50 -0700 Subject: [PATCH] No longer generate empty reports (#577) * return nil when no leaks found and lowercase report format option (#3) * lowercase format options for consistency * lowercase report format description Co-authored-by: eddie-northcutt-wfp0 --- README.md | 4 ++-- options/options.go | 2 +- scan/report.go | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0d8bc486..ad4fe996 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Gitleaks is a SAST tool for detecting hardcoded secrets like passwords, api keys - Run [Gitleaks Action](https://github.com/marketplace/actions/gitleaks) in your CI/CD pipeline - [Custom rules](https://github.com/zricethezav/gitleaks#configuration) via toml configuration - Increased performance using [go-git](https://github.com/go-git/go-git) -- JSON, SARIF, and CSV reporting +- json, sarif, and csv reporting - Private repo scans using key or password based authentication @@ -94,7 +94,7 @@ Application Options: --append-repo-config Append the provided or default config with the repo config. --additional-config= Path to an additional gitleaks config to append with an existing config. Can be used with --append-repo-config to append up to three configurations -o, --report= Report output path - -f, --format= JSON, CSV, SARIF (default: json) + -f, --format= json, csv, sarif (default: json) --files-at-commit= Sha of commit to scan all files at commit --commit= Sha of commit to scan or "latest" to scan the last commit of the repository --commits= Comma separated list of a commits to scan diff --git a/options/options.go b/options/options.go index 9ebf87fc..df37ac61 100644 --- a/options/options.go +++ b/options/options.go @@ -44,7 +44,7 @@ type Options struct { // Report Options Report string `short:"o" long:"report" description:"Report output path"` - ReportFormat string `short:"f" long:"format" default:"json" description:"JSON, CSV, SARIF"` + ReportFormat string `short:"f" long:"format" default:"json" description:"json, csv, sarif"` // Commit Options FilesAtCommit string `long:"files-at-commit" description:"Sha of commit to scan all files at commit"` diff --git a/scan/report.go b/scan/report.go index 8d82a3f3..74a6e045 100644 --- a/scan/report.go +++ b/scan/report.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "encoding/json" "os" + "strings" "time" "github.com/sirupsen/logrus" @@ -27,28 +28,27 @@ func WriteReport(report Report, opts options.Options, cfg config.Config) error { logrus.Warn("leaks found: ", len(report.Leaks)) } else { logrus.Info("No leaks found") + return nil } if opts.Report == "" { return nil - } - - if opts.Redact { - var redactedLeaks []Leak - for _, leak := range report.Leaks { - redactedLeaks = append(redactedLeaks, RedactLeak(leak)) + } else { + if opts.Redact { + var redactedLeaks []Leak + for _, leak := range report.Leaks { + redactedLeaks = append(redactedLeaks, RedactLeak(leak)) + } + report.Leaks = redactedLeaks } - report.Leaks = redactedLeaks - } - file, err := os.Create(opts.Report) - if err != nil { - return err - } - defer rable(file.Close) + file, err := os.Create(opts.Report) + if err != nil { + return err + } + defer rable(file.Close) - if opts.Report != "" { - switch opts.ReportFormat { + switch strings.ToLower(opts.ReportFormat) { case "json": encoder := json.NewEncoder(file) encoder.SetIndent("", " ")