Skip to content

Commit

Permalink
fix(log): unwanted json log when using unknown flag/command (#2967) (#…
Browse files Browse the repository at this point in the history
…2983)

* Removed log from unknown flag or command, since it happens before logger is set
Signed-off-by: Felipe Avelar <[email protected]>
  • Loading branch information
felipe-avelar authored Apr 26, 2021
1 parent 1463a4a commit 88a5703
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 18 deletions.
41 changes: 30 additions & 11 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var tests = []struct {
},
wantStatus: 126,
},
// E2E-CLI-004 - KICS scan command had a mandatory flag -p the CLI should exhibit
// E2E-CLI-004 - KICS has an invalid flag combination
// an error message and return exit code 1
{
name: "E2E-CLI-004",
Expand Down Expand Up @@ -138,7 +138,7 @@ var tests = []struct {
[]string{"scan", "-q", "../assets/queries", "-p", "fixtures/samples/terraform.tf"},
},
},
wantStatus: 0,
wantStatus: 50,
validation: func(outputText string) bool {
match1, _ := regexp.MatchString(`Files scanned: \d+`, outputText)
match2, _ := regexp.MatchString(`Parsed files: \d+`, outputText)
Expand All @@ -159,7 +159,7 @@ var tests = []struct {
},
expectedOut: []string{"E2E_CLI_008"},
},
wantStatus: 0,
wantStatus: 50,
},
// E2E-CLI-009 - kics scan with no-progress flag should perform a scan
// without showing progress bar in the CLI
Expand All @@ -170,7 +170,7 @@ var tests = []struct {
[]string{"scan", "-q", "../assets/queries", "-p", "fixtures/samples/terraform.tf", "--no-progress"},
},
},
wantStatus: 0,
wantStatus: 50,
validation: func(outputText string) bool {
getProgressRegex := "Executing queries:"
match, _ := regexp.MatchString(getProgressRegex, outputText)
Expand All @@ -188,11 +188,11 @@ var tests = []struct {
},
},
validation: func(outputText string) bool {
unknownArgRegex := regexp.MustCompile(`Error: unknown argument: \[xml\]`)
unknownArgRegex := regexp.MustCompile(`Error: unknown argument for --type: \[xml\]`)
match := unknownArgRegex.MatchString(outputText)
return match
},
wantStatus: 1,
wantStatus: 126,
},
// E2E-CLI-011 - KICS scan with a valid case insensitive --type flag
// must perform the scan successfully and return exit code 0
Expand All @@ -207,7 +207,7 @@ var tests = []struct {
"E2E_CLI_011_PAYLOAD",
},
},
wantStatus: 0,
wantStatus: 50,
removePayload: []string{"payload.json"},
},
// E2E-CLI-012 - kics scan with minimal-ui flag should perform a scan
Expand All @@ -219,7 +219,7 @@ var tests = []struct {
[]string{"scan", "-q", "../assets/queries", "-p", "../test/fixtures/tc-sim01/positive1.tf", "--minimal-ui"},
},
},
wantStatus: 0,
wantStatus: 50,
validation: func(outputText string) bool {
match1, _ := regexp.MatchString("Description:", outputText)
match2, _ := regexp.MatchString("Platform:", outputText)
Expand Down Expand Up @@ -257,7 +257,7 @@ var tests = []struct {
match2, _ := regexp.MatchString(`002\: publicly_accessible = false`, outputText)
return match1 && !match2
},
wantStatus: 0,
wantStatus: 40,
},
// E2E-CLI-015 KICS scan with --no-color flag
// must disable the colored outputs of kics in the CLI
Expand All @@ -275,7 +275,27 @@ var tests = []struct {
match4, _ := regexp.MatchString(`INFO: \d+`, outputText)
return match1 && match2 && match3 && match4
},
wantStatus: 0,
wantStatus: 50,
},
// E2E-CLI-016 - KICS has an invalid flag or invalid command
// an error message and return exit code 1
{
name: "E2E-CLI-016",
args: args{
args: []cmdArgs{
[]string{"scan", "--invalid-flag"},
[]string{"--invalid-flag"},
[]string{"invalid"},
[]string{"-i"},
},
expectedOut: []string{
"E2E_CLI_016_INVALID_SCAN_FLAG",
"E2E_CLI_016_INVALID_FLAG",
"E2E_CLI_016_INVALID_COMMAND",
"E2E_CLI_016_INVALID_SHOTHAND",
},
},
wantStatus: 126,
},
}

Expand Down Expand Up @@ -340,7 +360,6 @@ func prepareExpected(path string) ([]string, error) {
}

return strings.Split(cont, "\n"), nil

}

func checkLine(t *testing.T, expec, want string, line int) {
Expand Down
3 changes: 1 addition & 2 deletions e2e/fixtures/E2E_CLI_004
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Error: can't provide 'verbose' and 'ci' flags simultaneously
Error: initialization error - can't provide 'verbose' and 'ci' flags simultaneously
Usage:
kics scan [flags]

Expand Down Expand Up @@ -43,4 +43,3 @@ Global Flags:
-s, --silent silence stdout messages (mutually exclusive with verbose and ci)
-v, --verbose write logs to stdout too (mutually exclusive with silent)

{"level":"error","error":"can't provide 'verbose' and 'ci' flags simultaneously","time":"2021-04-16T15:41:12+01:00","message":"Failed to run application"}
11 changes: 9 additions & 2 deletions e2e/fixtures/E2E_CLI_010
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Error: unknown argument: [xml]
Error: unknown argument for --type: [xml]
valid arguments:
CloudFormation
OpenAPI
Expand All @@ -23,7 +23,13 @@ Flags:
-x, --exclude-results strings exclude results by providing the similarity ID of a result
can be provided multiple times or as a comma separated string
example: 'fec62a97d569662093dbb9739360942f...,31263s5696620s93dbb973d9360942fc2a...'
--fail-on strings which kind of results should return an exit code different from 0
accetps: high, medium, low and info
example: "high,low" (default [high,medium,low,info])
-h, --help help for scan
--ignore-on-exit string defines which kind of non-zero exits code should be ignored
accepts: all, results, errors, none
example: if 'results' is set, only engine errors will make KICS exit code different from 0 (default "none")
--minimal-ui simplified version of CLI output
--no-progress hides the progress bar
-o, --output-path string directory path to store reports
Expand All @@ -42,4 +48,5 @@ Global Flags:
--log-path string path to log files, (defaults to ${PWD}/info.log)
--no-color disable CLI color output
-s, --silent silence stdout messages (mutually exclusive with verbose and ci)
-v, --verbose write logs to stdout too (mutually exclusive with silent)
-v, --verbose write logs to stdout too (mutually exclusive with silent)

2 changes: 2 additions & 0 deletions e2e/fixtures/E2E_CLI_016_INVALID_COMMAND
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error: unknown command "invalid" for "kics"
Run 'kics --help' for usage.
23 changes: 23 additions & 0 deletions e2e/fixtures/E2E_CLI_016_INVALID_FLAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Error: unknown flag: --invalid-flag
Usage:
kics [command]

Available Commands:
generate-id Generates uuid for query
help Help about any command
list-platforms List supported platforms
scan Executes a scan analysis
version Displays the current version

Flags:
--ci display only log messages to CLI output (mutually exclusive with silent)
-h, --help help for kics
-f, --log-format string determines log format (pretty,json) (default "pretty")
--log-level string determines log level (TRACE,DEBUG,INFO,WARN,ERROR,FATAL) (default "INFO")
--log-path string path to log files, (defaults to ${PWD}/info.log)
--no-color disable CLI color output
-s, --silent silence stdout messages (mutually exclusive with verbose and ci)
-v, --verbose write logs to stdout too (mutually exclusive with silent)

Use "kics [command] --help" for more information about a command.

45 changes: 45 additions & 0 deletions e2e/fixtures/E2E_CLI_016_INVALID_SCAN_FLAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Error: unknown flag: --invalid-flag
Usage:
kics scan [flags]

Flags:
--config string path to configuration file
--exclude-categories strings exclude categories by providing its name
can be provided multiple times or as a comma separated string
example: 'Access control,Best practices'
-e, --exclude-paths strings exclude paths from scan
supports glob and can be provided multiple times or as a quoted comma separated string
example: './shouldNotScan/*,somefile.txt'
--exclude-queries strings exclude queries by providing the query ID
can be provided multiple times or as a comma separated string
example: 'e69890e6-fce5-461d-98ad-cb98318dfc96,4728cd65-a20c-49da-8b31-9c08b423e4db'
-x, --exclude-results strings exclude results by providing the similarity ID of a result
can be provided multiple times or as a comma separated string
example: 'fec62a97d569662093dbb9739360942f...,31263s5696620s93dbb973d9360942fc2a...'
--fail-on strings which kind of results should return an exit code different from 0
accetps: high, medium, low and info
example: "high,low" (default [high,medium,low,info])
-h, --help help for scan
--ignore-on-exit string defines which kind of non-zero exits code should be ignored
accepts: all, results, errors, none
example: if 'results' is set, only engine errors will make KICS exit code different from 0 (default "none")
--minimal-ui simplified version of CLI output
--no-progress hides the progress bar
-o, --output-path string directory path to store reports
-p, --path string path or directory path to scan
-d, --payload-path string path to store internal representation JSON file
--preview-lines int number of lines to be display in CLI results (min: 1, max: 30) (default 3)
-q, --queries-path string path to directory with queries (default "./assets/queries")
--report-formats strings formats in which the results will be exported (json, sarif, html)
-t, --type strings case insensitive list of platform types to scan
(Ansible, CloudFormation, Dockerfile, Kubernetes, OpenAPI, Terraform)

Global Flags:
--ci display only log messages to CLI output (mutually exclusive with silent)
-f, --log-format string determines log format (pretty,json) (default "pretty")
--log-level string determines log level (TRACE,DEBUG,INFO,WARN,ERROR,FATAL) (default "INFO")
--log-path string path to log files, (defaults to ${PWD}/info.log)
--no-color disable CLI color output
-s, --silent silence stdout messages (mutually exclusive with verbose and ci)
-v, --verbose write logs to stdout too (mutually exclusive with silent)

23 changes: 23 additions & 0 deletions e2e/fixtures/E2E_CLI_016_INVALID_SHOTHAND
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Error: unknown shorthand flag: 'i' in -i
Usage:
kics [command]

Available Commands:
generate-id Generates uuid for query
help Help about any command
list-platforms List supported platforms
scan Executes a scan analysis
version Displays the current version

Flags:
--ci display only log messages to CLI output (mutually exclusive with silent)
-h, --help help for kics
-f, --log-format string determines log format (pretty,json) (default "pretty")
--log-level string determines log level (TRACE,DEBUG,INFO,WARN,ERROR,FATAL) (default "INFO")
--log-path string path to log files, (defaults to ${PWD}/info.log)
--no-color disable CLI color output
-s, --silent silence stdout messages (mutually exclusive with verbose and ci)
-v, --verbose write logs to stdout too (mutually exclusive with silent)

Use "kics [command] --help" for more information about a command.

8 changes: 7 additions & 1 deletion internal/console/kics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/Checkmarx/kics/internal/console/printer"
Expand Down Expand Up @@ -140,7 +141,12 @@ func Execute() error {

if err := rootCmd.ExecuteContext(ctx); err != nil {
sentry.CaptureException(err)
log.Err(err).Msg("Failed to run application")
if !(strings.HasPrefix(err.Error(), "unknown shorthand flag") ||
strings.HasPrefix(err.Error(), "unknown flag") ||
strings.HasPrefix(err.Error(), "unknown command") ||
strings.HasPrefix(err.Error(), "initialization error -")) {
log.Err(err).Msg("Failed to run application")
}
return err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/console/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ func NewScanCmd() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := initializeConfig(cmd)
if err != nil {
return err
return errors.New("initialization error - " + err.Error())
}
err = internalPrinter.SetupPrinter(cmd.InheritedFlags())
if err != nil {
return err
return errors.New("initialization error - " + err.Error())
}
return nil
},
Expand Down

0 comments on commit 88a5703

Please sign in to comment.