From d369656b26f9a69f0f0265478045ae342f7ad00b Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:14:26 +0100 Subject: [PATCH] agent: fix message when -dsn is provided without -type (#2009) --- cmd/crowdsec/main.go | 13 +++++++++---- tests/bats/40_cold-logs.bats | 27 +++++++++++++++++---------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index 1e668b6b8..d012d24fd 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -100,10 +100,7 @@ func LoadBuckets(cConfig *csconfig.Config) error { func LoadAcquisition(cConfig *csconfig.Config) error { var err error - if flags.SingleFileType != "" || flags.OneShotDSN != "" { - if flags.OneShotDSN == "" || flags.SingleFileType == "" { - return fmt.Errorf("-type requires a -dsn argument") - } + if flags.SingleFileType != "" && flags.OneShotDSN != "" { flags.Labels = labels flags.Labels["type"] = flags.SingleFileType @@ -248,6 +245,14 @@ func LoadConfig(cConfig *csconfig.Config) error { cConfig.Crowdsec.LintOnly = true } + if flags.OneShotDSN != "" && flags.SingleFileType == "" { + return errors.New("-dsn requires a -type argument") + } + + if flags.SingleFileType != "" && flags.OneShotDSN == "" { + return errors.New("-type requires a -dsn argument") + } + if flags.SingleFileType != "" && flags.OneShotDSN != "" { if cConfig.API != nil && cConfig.API.Server != nil { cConfig.API.Server.OnlineClient = nil diff --git a/tests/bats/40_cold-logs.bats b/tests/bats/40_cold-logs.bats index 465dfb6eb..bc8d927ca 100644 --- a/tests/bats/40_cold-logs.bats +++ b/tests/bats/40_cold-logs.bats @@ -28,36 +28,43 @@ setup() { #---------- +@test "-type and -dsn are required together" { + rune -1 "${CROWDSEC}" -no-api -type syslog + assert_stderr --partial "-type requires a -dsn argument" + rune -1 "${CROWDSEC}" -no-api -dsn file:///dev/fd/0 + assert_stderr --partial "-dsn requires a -type argument" +} + @test "we have one decision" { - run -0 --separate-stderr cscli decisions list -o json - run -0 jq '. | length' <(output) + rune -0 cscli decisions list -o json + rune -0 jq '. | length' <(output) assert_output 1 } @test "1.1.1.172 has been banned" { - run -0 --separate-stderr cscli decisions list -o json - run -0 jq -r '.[].decisions[0].value' <(output) + rune -0 cscli decisions list -o json + rune -0 jq -r '.[].decisions[0].value' <(output) assert_output '1.1.1.172' } @test "1.1.1.172 has been banned (range/contained: -r 1.1.1.0/24 --contained)" { - run -0 --separate-stderr cscli decisions list -r 1.1.1.0/24 --contained -o json - run -0 jq -r '.[].decisions[0].value' <(output) + rune -0 cscli decisions list -r 1.1.1.0/24 --contained -o json + rune -0 jq -r '.[].decisions[0].value' <(output) assert_output '1.1.1.172' } @test "1.1.1.172 has not been banned (range/NOT-contained: -r 1.1.2.0/24)" { - run -0 --separate-stderr cscli decisions list -r 1.1.2.0/24 -o json + rune -0 cscli decisions list -r 1.1.2.0/24 -o json assert_output 'null' } @test "1.1.1.172 has been banned (exact: -i 1.1.1.172)" { - run -0 --separate-stderr cscli decisions list -i 1.1.1.172 -o json - run -0 jq -r '.[].decisions[0].value' <(output) + rune -0 cscli decisions list -i 1.1.1.172 -o json + rune -0 jq -r '.[].decisions[0].value' <(output) assert_output '1.1.1.172' } @test "1.1.1.173 has not been banned (exact: -i 1.1.1.173)" { - run -0 --separate-stderr cscli decisions list -i 1.1.1.173 -o json + rune -0 cscli decisions list -i 1.1.1.173 -o json assert_output 'null' }