1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-06 22:32:54 +02:00

make blank target null

This commit is contained in:
Kelly Brazil
2023-11-04 15:58:12 -07:00
parent 2fcb32e26f
commit b881ad4ec0
4 changed files with 24 additions and 1 deletions

View File

@ -25,7 +25,7 @@ Schema:
"num" integer,
"pkts": integer,
"bytes": integer, # converted based on suffix
"target": string,
"target": string, # Null if blank
"prot": string,
"opt": string, # "--" = Null
"in": string,
@ -222,6 +222,10 @@ def _process(proc_data):
if rule['opt'] == '--':
rule['opt'] = None
if 'target' in rule:
if rule['target'] == '':
rule['target'] = None
return proc_data
@ -278,6 +282,8 @@ def parse(data, raw=False, quiet=False):
rule = line.split(maxsplit=len(headers) - 1)
temp_rule = dict(zip(headers, rule))
if temp_rule:
if temp_rule.get('target') == '\u2063':
temp_rule['target'] = ''
chain['rules'].append(temp_rule)
if chain:

View File

@ -0,0 +1 @@
[{"chain":"INPUT","rules":[{"target":null,"prot":"udp","opt":null,"source":"anywhere","destination":"anywhere"}]}]

View File

@ -0,0 +1,4 @@
Chain INPUT (policy ACCEPT)
target prot opt source destination
udp -- anywhere anywhere

View File

@ -45,6 +45,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-raw.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_iptables_raw = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/iptables-no-jump.out'), 'r', encoding='utf-8') as f:
generic_iptables_no_jump = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/iptables-filter.json'), 'r', encoding='utf-8') as f:
centos_7_7_iptables_filter_json = json.loads(f.read())
@ -82,6 +85,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/iptables-raw.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_iptables_raw_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/iptables-no-jump.json'), 'r', encoding='utf-8') as f:
generic_iptables_no_jump_json = json.loads(f.read())
def test_iptables_nodata(self):
"""
@ -161,6 +167,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.iptables.parse(self.ubuntu_18_4_iptables_raw, quiet=True), self.ubuntu_18_4_iptables_raw_json)
def test_iptables_no_jump_generic(self):
"""
Test 'sudo iptables' with no jump target
"""
self.assertEqual(jc.parsers.iptables.parse(self.generic_iptables_no_jump, quiet=True), self.generic_iptables_no_jump_json)
if __name__ == '__main__':
unittest.main()