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: AMA-3307-fix-scan-usage-showing-on-error-vjeran #305

Merged
merged 8 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions integration/flags/report_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ func TestReportFlags(t *testing.T) {

testhelper.RunTests(t, tests)
}

func TestReportFlagsShouldFail(t *testing.T) {
tests := []testhelper.TestCase{
newScanTest("invalid-report-flag", []string{"--report=testing"}, ""),
newScanTest("invalid-format-flag", []string{"--format=testing"}, ""),
newScanTest("invalid-context-flag", []string{"--format=testing"}, ""),
}

for i := range tests {
tests[i].ShouldSucceed = false
}

testhelper.RunTests(t, tests)

}
8 changes: 4 additions & 4 deletions integration/internal/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var TestTimeout = 1 * time.Minute
type TestCase struct {
name string
arguments []string
shouldSucceed bool
ShouldSucceed bool
options TestCaseOptions
displayStdErr bool
ignoreForce bool
Expand All @@ -32,7 +32,7 @@ func NewTestCase(name string, arguments []string, options TestCaseOptions) TestC
return TestCase{
name: name,
arguments: arguments,
shouldSucceed: true,
ShouldSucceed: true,
options: options,
displayStdErr: options.DisplayStdErr,
ignoreForce: options.IgnoreForce,
Expand Down Expand Up @@ -120,10 +120,10 @@ func RunTests(t *testing.T, tests []TestCase) {
cupaloy.SnapshotT(t, combinedOutput)

if err != nil {
if test.shouldSucceed {
if test.ShouldSucceed {
t.Errorf("Expected application to succeed, but it failed: %s", err)
}
} else if !test.shouldSucceed {
} else if !test.ShouldSucceed {
t.Error("Expected application to fail, but it did not")
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
logger = outputhandler.PlainLogger(reportFile)
}

if config.Report.Report == flag.ReportPolicies && config.Report.Format == "" {
if config.Report.Report == flag.ReportPolicies && config.Report.Format == flag.FormatEmpty {
// for policy report, default report format is NOT JSON
reportPassed, err := reportoutput.ReportPolicies(report, logger, config)
if err != nil {
Expand All @@ -199,7 +199,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
}

switch config.Report.Format {
case "", flag.FormatJSON:
case flag.FormatEmpty, flag.FormatJSON:
// default report format for is JSON
err := reportoutput.ReportJSON(report, logger, config)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func NewScanCommand() *cobra.Command {
return cmd.Help()
}

cmd.SilenceUsage = true

return artifact.Run(cmd.Context(), options, artifact.TargetFilesystem)
},
SilenceErrors: false,
Expand Down
7 changes: 5 additions & 2 deletions pkg/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ func (f *Flags) ToOptions(args []string) (Options, error) {
}

if f.ReportFlagGroup != nil {
opts.ReportOptions = f.ReportFlagGroup.ToOptions()
opts.ReportOptions, err = f.ReportFlagGroup.ToOptions()
if err != nil {
return Options{}, fmt.Errorf("report flags error: %w", err)
}
}

if f.PolicyFlagGroup != nil {
Expand All @@ -259,7 +262,7 @@ func (f *Flags) ToOptions(args []string) (Options, error) {
if f.ScanFlagGroup != nil {
opts.ScanOptions, err = f.ScanFlagGroup.ToOptions(args)
if err != nil {
return Options{}, xerrors.Errorf("scan flag error: %w", err)
return Options{}, fmt.Errorf("scan flag error: %w", err)
}
}

Expand Down
41 changes: 34 additions & 7 deletions pkg/flag/report_flags.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package flag

import (
"errors"
)

type Severity int

var (
FormatJSON = "json"
FormatYAML = "yaml"
FormatJSON = "json"
FormatYAML = "yaml"
FormatEmpty = ""

ReportDetectors = "detectors"
ReportDataFlow = "dataflow"
ReportPolicies = "policies"
ReportStats = "stats"
)

var ErrInvalidFormat = errors.New("you've specified invalid format, valid ones are json,yaml")
vjerci marked this conversation as resolved.
Show resolved Hide resolved
var ErrInvalidReport = errors.New("you've specified invalid report, valid ones are detectors, dataflow, policies, stats")
vjerci marked this conversation as resolved.
Show resolved Hide resolved

var (
FormatFlag = Flag{
Name: "format",
ConfigName: "report.format",
Shorthand: "f",
Value: "",
Value: FormatEmpty,
Usage: "Specify report format (json, yaml)",
}
ReportFlag = Flag{
Expand Down Expand Up @@ -66,10 +74,29 @@ func (f *ReportFlagGroup) Flags() []*Flag {
}
}

func (f *ReportFlagGroup) ToOptions() ReportOptions {
func (f *ReportFlagGroup) ToOptions() (ReportOptions, error) {
format := getString(f.Format)
switch format {
case FormatYAML:
case FormatJSON:
case FormatEmpty:
default:
return ReportOptions{}, ErrInvalidFormat
}

report := getString(f.Report)
switch report {
case ReportDetectors:
case ReportDataFlow:
case ReportPolicies:
case ReportStats:
default:
return ReportOptions{}, ErrInvalidReport
}

return ReportOptions{
Format: getString(f.Format),
Report: getString(f.Report),
Format: format,
Report: report,
Output: getString(f.Output),
}
}, nil
}
13 changes: 12 additions & 1 deletion pkg/flag/scan_flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag

import (
"errors"
"strings"
"time"
)
Expand All @@ -9,8 +10,11 @@ type Context string

const (
Health Context = "health"
Empty Context = ""
)

var ErrInvalidContext = errors.New("you've specified invalid context, valid one is health")
vjerci marked this conversation as resolved.
Show resolved Hide resolved

var (
SkipPathFlag = Flag{
Name: "skip-path",
Expand Down Expand Up @@ -121,13 +125,20 @@ func (f *ScanFlagGroup) ToOptions(args []string) (ScanOptions, error) {
target = args[0]
}

context := getContext(f.ContextFlag)
switch context {
case Empty, Health:
default:
return ScanOptions{}, ErrInvalidContext
}

return ScanOptions{
SkipPath: getStringSlice(f.SkipPathFlag),
Debug: getBool(f.DebugFlag),
DisableDomainResolution: getBool(f.DisableDomainResolutionFlag),
DomainResolutionTimeout: getDuration(f.DomainResolutionTimeoutFlag),
InternalDomains: getStringSlice(f.InternalDomainsFlag),
Context: getContext(f.ContextFlag),
Context: context,
Quiet: getBool(f.QuietFlag),
Force: getBool(f.ForceFlag),
Target: target,
Expand Down