Compare commits

..

1 commit

Author SHA1 Message Date
marco
9acd23d0ac cscli: cleaner output 2024-04-26 11:08:36 +02:00
14 changed files with 49 additions and 62 deletions

View file

@ -194,15 +194,15 @@ func (cli *cliCapi) status() error {
Scenarios: scenarios,
}
log.Infof("Loaded credentials from %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
log.Infof("Trying to authenticate with username %s on %s", cfg.API.Server.OnlineClient.Credentials.Login, apiurl)
fmt.Printf("Loaded credentials from %s\n", cfg.API.Server.OnlineClient.CredentialsFilePath)
fmt.Printf("Trying to authenticate with username %s on %s\n", cfg.API.Server.OnlineClient.Credentials.Login, apiurl)
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
if err != nil {
return fmt.Errorf("failed to authenticate to Central API (CAPI): %w", err)
}
log.Info("You can successfully interact with Central API (CAPI)")
fmt.Println("You can successfully interact with Central API (CAPI)")
return nil
}

View file

@ -75,16 +75,16 @@ func (cli *cliLapi) status() error {
Scenarios: scenarios,
}
log.Infof("Loaded credentials from %s", cfg.API.Client.CredentialsFilePath)
fmt.Printf("Loaded credentials from %s\n", cfg.API.Client.CredentialsFilePath)
// use the original string because apiURL would print 'http://unix/'
log.Infof("Trying to authenticate with username %s on %s", login, origURL)
fmt.Printf("Trying to authenticate with username %s on %s\n", login, origURL)
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
if err != nil {
return fmt.Errorf("failed to authenticate to Local API (LAPI): %w", err)
}
log.Infof("You can successfully interact with Local API (LAPI)")
fmt.Println("You can successfully interact with Local API (LAPI)")
return nil
}

View file

@ -278,6 +278,8 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
func main() {
cmd := newCliRoot().NewCommand()
if err := cmd.Execute(); err != nil {
log.Fatal(err)
color.New(color.FgRed).Fprint(os.Stderr, "Error: ")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

View file

@ -319,7 +319,7 @@ cscli support dump -f /tmp/crowdsec-support.zip
`,
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
Run: func(_ *cobra.Command, _ []string) {
var err error
var skipHub, skipDB, skipCAPI, skipLAPI, skipAgent bool
infos := map[string][]byte{
@ -473,19 +473,15 @@ cscli support dump -f /tmp/crowdsec-support.zip
err = zipWriter.Close()
if err != nil {
return fmt.Errorf("could not finalize zip file: %s", err)
log.Fatalf("could not finalize zip file: %s", err)
}
if outFile == "-" {
_, err = os.Stdout.Write(w.Bytes())
return err
}
err = os.WriteFile(outFile, w.Bytes(), 0o600)
if err != nil {
return fmt.Errorf("could not write zip file to %s: %s", outFile, err)
log.Fatalf("could not write zip file to %s: %s", outFile, err)
}
log.Infof("Written zip file to %s", outFile)
return nil
},
}

View file

@ -34,8 +34,7 @@ teardown() {
# no "usage" output after every error
rune -1 cscli blahblah
# error is displayed as log entry, not with print
assert_stderr --partial 'level=fatal msg="unknown command \"blahblah\" for \"cscli\""'
refute_stderr --partial 'unknown command "blahblah" for "cscli"'
assert_stderr --partial 'Error: unknown command "blahblah" for "cscli"'
}
@test "cscli version" {
@ -214,9 +213,9 @@ teardown() {
rune -0 ./instance-crowdsec start
rune -0 cscli lapi status
assert_stderr --partial "Loaded credentials from"
assert_stderr --partial "Trying to authenticate with username"
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_output --partial "Loaded credentials from"
assert_output --partial "Trying to authenticate with username"
assert_output --partial "You can successfully interact with Local API (LAPI)"
}
@test "cscli - missing LAPI credentials file" {
@ -261,9 +260,8 @@ teardown() {
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
config_set "${LOCAL_API_CREDENTIALS}" '.url="http://127.0.0.1:-80"'
rune -1 cscli lapi status -o json
rune -0 jq -r '.msg' <(stderr)
assert_output 'parsing api url: parse "http://127.0.0.1:-80/": invalid port ":-80" after host'
rune -1 cscli lapi status
assert_stderr 'Error: parsing api url: parse "http://127.0.0.1:-80/": invalid port ":-80" after host'
}
@test "cscli - bad LAPI password" {
@ -271,9 +269,8 @@ teardown() {
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
config_set "${LOCAL_API_CREDENTIALS}" '.password="meh"'
rune -1 cscli lapi status -o json
rune -0 jq -r '.msg' <(stderr)
assert_output 'failed to authenticate to Local API (LAPI): API error: incorrect Username or Password'
rune -1 cscli lapi status
assert_stderr 'Error: failed to authenticate to Local API (LAPI): API error: incorrect Username or Password'
}
@test "'cscli completion' with or without configuration file" {
@ -355,7 +352,7 @@ teardown() {
# it is possible to enable subcommands with feature flags defined in feature.yaml
rune -1 cscli setup
assert_stderr --partial 'unknown command \"setup\" for \"cscli\"'
assert_stderr 'Error: unknown command "setup" for "cscli"'
CONFIG_DIR=$(dirname "$CONFIG_YAML")
echo ' - cscli_setup' >> "$CONFIG_DIR"/feature.yaml
rune -0 cscli setup

View file

@ -76,7 +76,7 @@ teardown() {
config_disable_agent
./instance-crowdsec start
rune -0 cscli lapi status
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_line "You can successfully interact with Local API (LAPI)"
}
@test "cscli metrics" {

View file

@ -55,10 +55,10 @@ setup() {
rune -0 cscli scenarios install crowdsecurity/ssh-bf
rune -0 cscli capi status
assert_stderr --partial "Loaded credentials from"
assert_stderr --partial "Trying to authenticate with username"
assert_stderr --partial " on https://api.crowdsec.net/"
assert_stderr --partial "You can successfully interact with Central API (CAPI)"
assert_output --partial "Loaded credentials from"
assert_output --partial "Trying to authenticate with username"
assert_output --partial " on https://api.crowdsec.net/"
assert_output --partial "You can successfully interact with Central API (CAPI)"
}
@test "cscli alerts list: receive a community pull when capi is enabled" {
@ -85,7 +85,7 @@ setup() {
config_disable_agent
./instance-crowdsec start
rune -0 cscli capi status
assert_stderr --partial "You can successfully interact with Central API (CAPI)"
assert_line "You can successfully interact with Central API (CAPI)"
}
@test "capi register must be run from lapi" {

View file

@ -66,7 +66,7 @@ teardown() {
config_disable_capi
./instance-crowdsec start
rune -0 cscli lapi status
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_line "You can successfully interact with Local API (LAPI)"
}
@test "cscli metrics" {

View file

@ -25,7 +25,7 @@ teardown() {
@test "cscli metrics (crowdsec not running)" {
rune -1 cscli metrics
# crowdsec is down
assert_stderr --partial 'failed to fetch metrics: executing GET request for URL \"http://127.0.0.1:6060/metrics\" failed: Get \"http://127.0.0.1:6060/metrics\": dial tcp 127.0.0.1:6060: connect: connection refused'
assert_stderr --partial 'Error: failed to fetch metrics: executing GET request for URL "http://127.0.0.1:6060/metrics" failed: Get "http://127.0.0.1:6060/metrics": dial tcp 127.0.0.1:6060: connect: connection refused'
}
@test "cscli metrics (bad configuration)" {

View file

@ -37,22 +37,22 @@ teardown() {
./instance-crowdsec start
rune -0 cscli lapi status
assert_stderr --regexp "Trying to authenticate with username .* on $socket"
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_output --regexp "Trying to authenticate with username .* on $socket"
assert_line "You can successfully interact with Local API (LAPI)"
}
@test "crowdsec - listen on both socket and TCP" {
./instance-crowdsec start
rune -0 cscli lapi status
assert_stderr --regexp "Trying to authenticate with username .* on http://127.0.0.1:8080/"
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_output --regexp "Trying to authenticate with username .* on http://127.0.0.1:8080/"
assert_line "You can successfully interact with Local API (LAPI)"
config_set "$LOCAL_API_CREDENTIALS" ".url=strenv(socket)"
rune -0 cscli lapi status
assert_stderr --regexp "Trying to authenticate with username .* on $socket"
assert_stderr --partial "You can successfully interact with Local API (LAPI)"
assert_output --regexp "Trying to authenticate with username .* on $socket"
assert_line "You can successfully interact with Local API (LAPI)"
}
@test "cscli - authenticate new machine with socket" {

View file

@ -56,12 +56,9 @@ teardown() {
@test "we can't add the same bouncer twice" {
rune -0 cscli bouncers add ciTestBouncer
rune -1 cscli bouncers add ciTestBouncer -o json
rune -1 cscli bouncers add ciTestBouncer
# XXX temporary hack to filter out unwanted log lines that may appear before
# log configuration (= not json)
rune -0 jq -c '[.level,.msg]' <(stderr | grep "^{")
assert_output '["fatal","unable to create bouncer: bouncer ciTestBouncer already exists"]'
assert_stderr 'Error: unable to create bouncer: bouncer ciTestBouncer already exists'
rune -0 cscli bouncers list -o json
rune -0 jq '. | length' <(output)

View file

@ -80,10 +80,8 @@ teardown() {
echo "$new_hub" >"$INDEX_PATH"
rune -0 cscli collections install crowdsecurity/sshd
rune -1 cscli collections inspect crowdsecurity/sshd --no-metrics -o json
# XXX: we are on the verbose side here...
rune -0 jq -r ".msg" <(stderr)
assert_output --regexp "failed to read Hub index: failed to sync items: failed to scan .*: while syncing collections sshd.yaml: 1.2.3.4: Invalid Semantic Version. Run 'sudo cscli hub update' to download the index again"
rune -1 cscli collections inspect crowdsecurity/sshd --no-metrics
assert_stderr --regexp "Error: failed to read Hub index: failed to sync items: failed to scan .*: while syncing collections sshd.yaml: 1.2.3.4: Invalid Semantic Version. Run 'sudo cscli hub update' to download the index again"
}
@test "removing or purging an item already removed by hand" {

View file

@ -30,9 +30,8 @@ teardown() {
}
@test "don't overwrite local credentials by default" {
rune -1 cscli machines add local -a -o json
rune -0 jq -r '.msg' <(stderr)
assert_output --partial 'already exists: please remove it, use "--force" or specify a different file with "-f"'
rune -1 cscli machines add local -a
assert_stderr --regexp 'Error: credentials file '.*' already exists: please remove it, use "--force" or specify a different file with "-f"'
rune -0 cscli machines add local -a --force
assert_stderr --partial "Machine 'local' successfully added to the local API."
}

View file

@ -34,9 +34,8 @@ teardown() {
assert_line "Usage:"
assert_stderr --partial "missing arguments, a value is required (--ip, --range or --scope and --value)"
rune -1 cscli decisions add -o json
rune -0 jq -c '[ .level, .msg]' <(stderr | grep "^{")
assert_output '["fatal","missing arguments, a value is required (--ip, --range or --scope and --value)"]'
rune -1 cscli decisions add
assert_stderr 'Error: missing arguments, a value is required (--ip, --range or --scope and --value)'
}
@test "cscli decisions list, with and without --machine" {
@ -62,20 +61,19 @@ teardown() {
@test "cscli decisions list, incorrect parameters" {
rune -1 cscli decisions list --until toto
assert_stderr --partial 'unable to retrieve decisions: performing request: API error: while parsing duration: time: invalid duration \"toto\"'
rune -1 cscli decisions list --until toto -o json
rune -0 jq -c '[.level, .msg]' <(stderr | grep "^{")
assert_output '["fatal","unable to retrieve decisions: performing request: API error: while parsing duration: time: invalid duration \"toto\""]'
assert_stderr 'Error: unable to retrieve decisions: performing request: API error: while parsing duration: time: invalid duration "toto"'
rune -1 cscli decisions list --until toto
assert_stderr 'Error: unable to retrieve decisions: performing request: API error: while parsing duration: time: invalid duration "toto"'
}
@test "cscli decisions import" {
# required input
rune -1 cscli decisions import
assert_stderr --partial 'required flag(s) \"input\" not set"'
assert_stderr --partial 'Error: required flag(s) "input" not set'
# unsupported format
rune -1 cscli decisions import -i - <<<'value\n5.6.7.8' --format xml
assert_stderr --partial "invalid format 'xml', expected one of 'json', 'csv', 'values'"
assert_stderr "Error: invalid format 'xml', expected one of 'json', 'csv', 'values'"
# invalid defaults
rune -1 cscli decisions import --duration "" -i - <<<'value\n5.6.7.8' --format csv