From b881ad4ec0ebb43ebd49333388def366ab497309 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sat, 4 Nov 2023 15:58:12 -0700 Subject: [PATCH] make blank target null --- jc/parsers/iptables.py | 8 +++++++- tests/fixtures/generic/iptables-no-jump.json | 1 + tests/fixtures/generic/iptables-no-jump.out | 4 ++++ tests/test_iptables.py | 12 ++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/generic/iptables-no-jump.json create mode 100644 tests/fixtures/generic/iptables-no-jump.out diff --git a/jc/parsers/iptables.py b/jc/parsers/iptables.py index 704756ac5..307315a81 100644 --- a/jc/parsers/iptables.py +++ b/jc/parsers/iptables.py @@ -25,7 +25,7 @@ "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: diff --git a/tests/fixtures/generic/iptables-no-jump.json b/tests/fixtures/generic/iptables-no-jump.json new file mode 100644 index 000000000..5a4a42980 --- /dev/null +++ b/tests/fixtures/generic/iptables-no-jump.json @@ -0,0 +1 @@ +[{"chain":"INPUT","rules":[{"target":null,"prot":"udp","opt":null,"source":"anywhere","destination":"anywhere"}]}] diff --git a/tests/fixtures/generic/iptables-no-jump.out b/tests/fixtures/generic/iptables-no-jump.out new file mode 100644 index 000000000..ce6ec3d56 --- /dev/null +++ b/tests/fixtures/generic/iptables-no-jump.out @@ -0,0 +1,4 @@ +Chain INPUT (policy ACCEPT) +target prot opt source destination + udp -- anywhere anywhere + diff --git a/tests/test_iptables.py b/tests/test_iptables.py index 70e987478..26fd6752f 100644 --- a/tests/test_iptables.py +++ b/tests/test_iptables.py @@ -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 @@ def test_iptables_raw_ubuntu_18_4(self): """ 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()