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

fix(log): unwanted json log when using unknown flag/command (#2967) #2983

Merged
merged 5 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 19 additions & 1 deletion e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var tests = []struct {
},
wantStatus: 1,
},
// 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 @@ -109,6 +109,24 @@ var tests = []struct {
wantStatus: 0,
removePayload: []string{"payload.json"},
},
// 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"},
},
expectedOut: []string{
"E2E_CLI_016_INVALID_SCAN_FLAG",
"E2E_CLI_016_INVALID_FLAG",
"E2E_CLI_016_INVALID_COMMAND",
},
},
wantStatus: 1,
},
}

func Test_E2E_CLI(t *testing.T) {
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 @@ -37,4 +37,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"}
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.

39 changes: 39 additions & 0 deletions e2e/fixtures/E2E_CLI_016_INVALID_SCAN_FLAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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...'
-h, --help help for scan
--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)

7 changes: 6 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,11 @@ 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 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 @@ -86,11 +86,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