Browse Source

agent: fix message when -dsn is provided without -type (#2009)

mmetc 2 years ago
parent
commit
d369656b26
2 changed files with 26 additions and 14 deletions
  1. 9 4
      cmd/crowdsec/main.go
  2. 17 10
      tests/bats/40_cold-logs.bats

+ 9 - 4
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

+ 17 - 10
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'
 }