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
```
pull/3661/head
Shivani Bhardwaj 7 years ago committed by Victor Julien
parent bf37e3f5da
commit 27842c3750

@ -31,6 +31,11 @@ argsd = {
"name": "output-dir", "name": "output-dir",
"required": 1, "required": 1,
}, },
{
"name": "continuous",
"val": True,
"required": 1,
},
{ {
"name": "tenant", "name": "tenant",
"type": int, "type": int,
@ -55,7 +60,8 @@ argsd = {
], ],
"unregister-tenant-handler": [ "unregister-tenant-handler": [
{ {
"name": "tenantid", "name": "id",
"type": int,
"required": 1, "required": 1,
}, },
{ {
@ -70,7 +76,8 @@ argsd = {
], ],
"register-tenant-handler": [ "register-tenant-handler": [
{ {
"name": "tenantid", "name": "id",
"type": int,
"required": 1, "required": 1,
}, },
{ {

@ -19,9 +19,9 @@ try:
except ImportError: except ImportError:
import json import json
import readline import readline
from socket import socket, AF_UNIX, error
import select import select
import sys import sys
from socket import AF_UNIX, error, socket
from suricata.sc.specs import argsd from suricata.sc.specs import argsd
@ -92,8 +92,8 @@ class SuricataSC:
"iface-list", "iface-list",
] ]
self.fn_commands = [ self.fn_commands = [
"pcap-file ", "pcap-file",
"pcap-file-continuous ", "pcap-file-continuous",
"iface-stat", "iface-stat",
"conf-get", "conf-get",
"unregister-tenant-handler", "unregister-tenant-handler",
@ -198,21 +198,27 @@ class SuricataSC:
full_cmd = command.split() full_cmd = command.split()
cmd = full_cmd[0] cmd = full_cmd[0]
cmd_specs = argsd[cmd] 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() arguments = dict()
for c, spec in enumerate(cmd_specs, 1): for c, spec in enumerate(cmd_specs, 1):
spec_type = str if "type" not in spec else spec["type"] spec_type = str if "type" not in spec else spec["type"]
if spec["required"]: if spec["required"]:
if spec.get("val"):
arguments[spec["name"]] = spec_type(spec["val"])
continue
try: try:
arguments[spec["name"]] = spec_type(full_cmd[c]) arguments[spec["name"]] = spec_type(full_cmd[c])
except IndexError: 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): elif c < len(full_cmd):
arguments[spec["name"]] = spec_type(full_cmd[c]) arguments[spec["name"]] = spec_type(full_cmd[c])
return cmd, arguments return cmd, arguments
def parse_command(self, command): def parse_command(self, command):
arguments = None 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.cmd_list:
if cmd in self.fn_commands: if cmd in self.fn_commands:
cmd, arguments = getattr(self, "execute")(command=command) cmd, arguments = getattr(self, "execute")(command=command)

Loading…
Cancel
Save