Skip to content

Commit

Permalink
make blank target null
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil committed Nov 4, 2023
1 parent 2fcb32e commit b881ad4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion jc/parsers/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/generic/iptables-no-jump.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"chain":"INPUT","rules":[{"target":null,"prot":"udp","opt":null,"source":"anywhere","destination":"anywhere"}]}]
4 changes: 4 additions & 0 deletions tests/fixtures/generic/iptables-no-jump.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Chain INPUT (policy ACCEPT)
target prot opt source destination
udp -- anywhere anywhere

12 changes: 12 additions & 0 deletions tests/test_iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()

0 comments on commit b881ad4

Please sign in to comment.