]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricatasc: Use better exception message, sort imports
authorShivani Bhardwaj <shivanib134@gmail.com>
Fri, 8 Feb 2019 10:02:24 +0000 (15:32 +0530)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Feb 2019 09:56:58 +0000 (10:56 +0100)
Up until now, suricatasc gives a message as follows in case a command is
missing arguments:
```
>>> list-hostbit
Arguments to command 'list-hostbit' is missing
```

Fix this up and provide a better message:
```
>>> list-hostbit
Missing arguments: expected 1
>>> pcap-file-continuous
Missing arguments: expected at least 2
```

python/suricata/sc/specs.py
python/suricata/sc/suricatasc.py

index e62dc4508def18d6a908b908c492b01e69086d70..562d435a0818079ac0ba89927918683f0c0e8643 100644 (file)
@@ -31,6 +31,11 @@ argsd = {
             "name": "output-dir",
             "required": 1,
         },
+        {
+            "name": "continuous",
+            "val": True,
+            "required": 1,
+        },
         {
             "name": "tenant",
             "type": int,
@@ -55,7 +60,8 @@ argsd = {
     ],
     "unregister-tenant-handler": [
         {
-            "name": "tenantid",
+            "name": "id",
+            "type": int,
             "required": 1,
         },
         {
@@ -70,7 +76,8 @@ argsd = {
     ],
     "register-tenant-handler": [
         {
-            "name": "tenantid",
+            "name": "id",
+            "type": int,
             "required": 1,
         },
         {
index 2f2d03bcde60ac72fcc6340f0e6452845cb58d09..53a5b2f12fc250bc34bff8cd210a7eae3aee579c 100644 (file)
@@ -19,9 +19,9 @@ try:
 except ImportError:
     import json
 import readline
-from socket import socket, AF_UNIX, error
 import select
 import sys
+from socket import AF_UNIX, error, socket
 
 from suricata.sc.specs import argsd
 
@@ -92,8 +92,8 @@ class SuricataSC:
                 "iface-list",
                 ]
         self.fn_commands = [
-                "pcap-file ",
-                "pcap-file-continuous ",
+                "pcap-file",
+                "pcap-file-continuous",
                 "iface-stat",
                 "conf-get",
                 "unregister-tenant-handler",
@@ -198,21 +198,27 @@ class SuricataSC:
         full_cmd = command.split()
         cmd = full_cmd[0]
         cmd_specs = argsd[cmd]
+        required_args_count = len([d["required"] for d in cmd_specs if d["required"] and not "val" in d])
         arguments = dict()
         for c, spec in enumerate(cmd_specs, 1):
             spec_type = str if "type" not in spec else spec["type"]
             if spec["required"]:
+                if spec.get("val"):
+                    arguments[spec["name"]] = spec_type(spec["val"])
+                    continue
                 try:
                     arguments[spec["name"]] = spec_type(full_cmd[c])
                 except IndexError:
-                    raise SuricataCommandException("Missing arguments")
+                    phrase = " at least" if required_args_count != len(cmd_specs) else ""
+                    msg = "Missing arguments: expected{} {}".format(phrase, required_args_count)
+                    raise SuricataCommandException(msg)
             elif c < len(full_cmd):
                 arguments[spec["name"]] = spec_type(full_cmd[c])
         return cmd, arguments
 
     def parse_command(self, command):
         arguments = None
-        cmd = command.split(maxsplit=2)[0]
+        cmd = command.split(maxsplit=1)[0] if command else None
         if cmd in self.cmd_list:
             if cmd in self.fn_commands:
                 cmd, arguments = getattr(self, "execute")(command=command)