From 27842c375048bbe141fddd829e895a72809f7d7e Mon Sep 17 00:00:00 2001 From: Shivani Bhardwaj Date: Fri, 8 Feb 2019 15:32:24 +0530 Subject: [PATCH] suricatasc: Use better exception message, sort imports 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 | 11 +++++++++-- python/suricata/sc/suricatasc.py | 16 +++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/python/suricata/sc/specs.py b/python/suricata/sc/specs.py index e62dc4508d..562d435a08 100644 --- a/python/suricata/sc/specs.py +++ b/python/suricata/sc/specs.py @@ -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, }, { diff --git a/python/suricata/sc/suricatasc.py b/python/suricata/sc/suricatasc.py index 2f2d03bcde..53a5b2f12f 100644 --- a/python/suricata/sc/suricatasc.py +++ b/python/suricata/sc/suricatasc.py @@ -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)