From 53c1969ad36adafe2eb23e698f5eaad3eae9cabf Mon Sep 17 00:00:00 2001 From: Rohith Asrk Date: Mon, 25 Dec 2017 15:11:36 +0530 Subject: [PATCH] Added a label in OpenvpnParser output --- netdiff/parsers/openvpn.py | 1 + tests/static/openvpn-2-links.txt | 4 ++-- tests/static/openvpn-5-links-tap.txt | 20 ++++++++++---------- tests/test_openvpn.py | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/netdiff/parsers/openvpn.py b/netdiff/parsers/openvpn.py index 459e2b9..6abcb59 100644 --- a/netdiff/parsers/openvpn.py +++ b/netdiff/parsers/openvpn.py @@ -28,6 +28,7 @@ def parse(self, data): # add clients in graph as nodes for client in data.client_list.values(): client_properties = { + 'label': client.common_name, 'real_address': str(client.real_address.host), 'port': client.real_address.port, 'connected_since': client.connected_since.strftime('%Y-%m-%dT%H:%M:%SZ'), diff --git a/tests/static/openvpn-2-links.txt b/tests/static/openvpn-2-links.txt index 6662f21..44189ea 100644 --- a/tests/static/openvpn-2-links.txt +++ b/tests/static/openvpn-2-links.txt @@ -1,8 +1,8 @@ OpenVPN CLIENT LIST Updated,Thu Jun 18 08:12:15 2015 Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since -node1,87.18.10.87:49502,334948,1973012,Thu Jun 18 04:23:03 2015 -node2,93.40.230.50:64169,1817262,28981224,Thu Jun 18 04:08:39 2015 +nodeA,87.18.10.87:49502,334948,1973012,Thu Jun 18 04:23:03 2015 +nodeB,93.40.230.50:64169,1817262,28981224,Thu Jun 18 04:08:39 2015 ROUTING TABLE Virtual Address,Common Name,Real Address,Last Ref 192.168.255.134,node1,87.18.10.87:49502,Thu Jun 18 08:12:09 2015 diff --git a/tests/static/openvpn-5-links-tap.txt b/tests/static/openvpn-5-links-tap.txt index 4a05c19..ffe294f 100644 --- a/tests/static/openvpn-5-links-tap.txt +++ b/tests/static/openvpn-5-links-tap.txt @@ -1,18 +1,18 @@ OpenVPN CLIENT LIST Updated,Fri Oct 20 12:52:38 2017 Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since -Kali-Matera,2.226.154.66:45641,126345570,264437194,Sun Oct 15 11:03:54 2017 -Kali-Oppido,93.40.230.50:37394,3625584,7923472,Fri Oct 20 09:16:31 2017 -rosario,95.250.161.57:53084,5742,382894,Fri Oct 20 12:45:30 2017 -Syskrack,79.18.21.144:63859,481360,33208728,Fri Oct 20 00:20:32 2017 -pomezia,87.3.36.166:60485,395226,33194538,Fri Oct 20 00:20:27 2017 +nodeA,2.226.154.66:45641,126345570,264437194,Sun Oct 15 11:03:54 2017 +nodeB,93.40.230.50:37394,3625584,7923472,Fri Oct 20 09:16:31 2017 +nodeC,95.250.161.57:53084,5742,382894,Fri Oct 20 12:45:30 2017 +nodeD,79.18.21.144:63859,481360,33208728,Fri Oct 20 00:20:32 2017 +nodeE,87.3.36.166:60485,395226,33194538,Fri Oct 20 00:20:27 2017 ROUTING TABLE Virtual Address,Common Name,Real Address,Last Ref -46:8a:69:e7:46:24,Kali-Matera,2.226.154.66:45641,Fri Oct 20 12:52:26 2017 -c0:4a:00:2d:05:fc,pomezia,87.3.36.166:60485,Fri Oct 20 12:52:26 2017 -aa:c5:99:28:91:a4,rosario,95.250.161.57:53084,Fri Oct 20 12:52:26 2017 -c4:6e:1f:ff:02:23,Kali-Oppido,93.40.230.50:37394,Fri Oct 20 12:52:26 2017 -9e:c2:f2:55:f4:33,Syskrack,79.18.21.144:63859,Fri Oct 20 12:52:26 2017 +46:8a:69:e7:46:24,nodeA,2.226.154.66:45641,Fri Oct 20 12:52:26 2017 +c0:4a:00:2d:05:fc,nodeE,87.3.36.166:60485,Fri Oct 20 12:52:26 2017 +aa:c5:99:28:91:a4,nodeC,95.250.161.57:53084,Fri Oct 20 12:52:26 2017 +c4:6e:1f:ff:02:23,nodeB,93.40.230.50:37394,Fri Oct 20 12:52:26 2017 +9e:c2:f2:55:f4:33,nodeD,79.18.21.144:63859,Fri Oct 20 12:52:26 2017 GLOBAL STATS Max bcast/mcast queue length,6 END diff --git a/tests/test_openvpn.py b/tests/test_openvpn.py index d93ce68..d9916a3 100644 --- a/tests/test_openvpn.py +++ b/tests/test_openvpn.py @@ -30,6 +30,14 @@ def test_json_dict(self): self.assertIsInstance(data['links'], list) self.assertEqual(len(data['nodes']), 3) self.assertEqual(len(data['links']), 2) + # check presence of labels + labels = [] + for node in data['nodes']: + if 'label' in node: + labels.append(node['label']) + self.assertEqual(len(labels), 2) + self.assertIn('nodeA', labels) + self.assertIn('nodeB', labels) def test_json_dict_tap(self): p = OpenvpnParser(links2_tap) @@ -44,3 +52,13 @@ def test_json_dict_tap(self): self.assertIsInstance(data['links'], list) self.assertEqual(len(data['nodes']), 6) self.assertEqual(len(data['links']), 5) + labels = [] + for node in data['nodes']: + if 'label' in node: + labels.append(node['label']) + self.assertEqual(len(labels), 5) + self.assertIn('nodeA', labels) + self.assertIn('nodeB', labels) + self.assertIn('nodeC', labels) + self.assertIn('nodeD', labels) + self.assertIn('nodeE', labels)