Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn when cached data is used #500

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/bearer/curio/pkg/commands/process/worker/work"
"github.com/bearer/curio/pkg/flag"
reportoutput "github.com/bearer/curio/pkg/report/output"
"github.com/bearer/curio/pkg/util/output"
outputhandler "github.com/bearer/curio/pkg/util/output"

"github.com/bearer/curio/pkg/types"
Expand All @@ -43,6 +42,8 @@ type ScannerConfig struct {
}

type Runner interface {
// Cached returns true if cached data was used in scan
CacheUsed() bool
// ScanFilesystem scans a filesystem
ScanFilesystem(ctx context.Context, opts flag.Options) (types.Report, error)
// ScanRepository scans repository
Expand Down Expand Up @@ -154,6 +155,10 @@ func buildScanID(scanSettings settings.Config) (string, error) {
return scanID, nil
}

func (r *runner) CacheUsed() bool {
return r.reuseDetection
}

// Close closes everything
func (r *runner) Close(ctx context.Context) error {
return nil
Expand Down Expand Up @@ -219,7 +224,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
case TargetFilesystem:
if report, err = r.ScanFilesystem(ctx, opts); err != nil {
if errors.Is(err, balancer.ErrFileListEmpty) {
output.StdOutLogger().Msgf("directory empty: %s", err)
outputhandler.StdOutLogger().Msgf("directory empty: %s", err)
os.Exit(0)
return
}
Expand Down Expand Up @@ -251,6 +256,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
}

func (r *runner) Report(config settings.Config, report types.Report) (bool, error) {
cacheUsed := r.CacheUsed()
// if output is defined we want to write only to file
logger := outputhandler.StdOutLogger()
if config.Report.Output != "" {
Expand All @@ -261,13 +267,19 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
logger = outputhandler.PlainLogger(reportFile)
}

if cacheUsed && !config.Scan.Quiet {
// output cached data warning at start of report
outputhandler.StdErrLogger().Msg("Using cached data")
}

if config.Report.Format == flag.FormatEmpty {
if config.Report.Report == flag.ReportSummary {
// for policy report, default report format is NOT JSON
reportPassed, err := reportoutput.ReportSummary(report, logger, config)
if err != nil {
return false, fmt.Errorf("error generating report %w", err)
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return reportPassed, nil
}
if config.Report.Report == flag.ReportPrivacy {
Expand All @@ -276,6 +288,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
if err != nil {
return false, fmt.Errorf("error generating report %w", err)
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return true, nil
}
}
Expand All @@ -293,5 +306,14 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
return false, fmt.Errorf("error generating report %w", err)
}
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return true, nil
}

func outputCachedDataWarning(cacheUsed bool, quietMode bool) {
if quietMode || !cacheUsed {
return
}

outputhandler.StdErrLogger().Msg("Cached data used (no code changes detected). Unexpected? Use --force to force a re-scan.\n")
}