From f600675b571745fc846c97bc4ea973f00dc9317a Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 12 Jul 2017 11:16:32 +0100 Subject: [PATCH 1/3] Use ast instead of dangerous eval --- napalm_iosxr/iosxr.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/napalm_iosxr/iosxr.py b/napalm_iosxr/iosxr.py index 40f275d..bd5e182 100644 --- a/napalm_iosxr/iosxr.py +++ b/napalm_iosxr/iosxr.py @@ -17,6 +17,7 @@ # import stdlib import re +import ast import copy import socket from collections import defaultdict @@ -818,7 +819,7 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout): bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/RoutePolicyIn') export_policy = napalm_base.helpers.find_txt( bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/RoutePolicyOut') - multipath = eval((napalm_base.helpers.find_txt( + multipath = ast.literal_eval((napalm_base.helpers.find_txt( bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/Multipath') or 'false').title()) peer_as = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(bgp_group, 'RemoteAS/AS_YY'), 0) @@ -949,7 +950,7 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): napalm_base.helpers.find_txt(neighbor, 'Naming/NeighborAddress/IPV4Address') or napalm_base.helpers.find_txt(neighbor, 'Naming/NeighborAddress/IPV6Address') ) - local_address_configured = eval(napalm_base.helpers.find_txt( + local_address_configured = ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'IsLocalAddressConfigured', 'false').title()) local_address = napalm_base.helpers.ip( napalm_base.helpers.find_txt(neighbor, 'ConnectionLocalAddress/IPV4Address') or @@ -963,11 +964,11 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): ) remote_port = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(neighbor, 'ConnectionRemotePort')) - multihop = eval(napalm_base.helpers.find_txt( + multihop = ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'IsExternalNeighborNotDirectlyConnected', 'false').title()) - remove_private_as = eval(napalm_base.helpers.find_txt( + remove_private_as = ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/RemovePrivateASFromUpdates', 'false').title()) - multipath = eval(napalm_base.helpers.find_txt( + multipath = ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/SelectiveMultipathEligible', 'false').title()) import_policy = napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/RoutePolicyIn') @@ -1000,9 +1001,9 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): advertised_prefix_count = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/PrefixesAdvertised'), 0) - suppress_4byte_as = eval(napalm_base.helpers.find_txt( + suppress_4byte_as = ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'Suppress4ByteAs', 'false').title()) - local_as_prepend = not eval(napalm_base.helpers.find_txt( + local_as_prepend = not ast.literal_eval(napalm_base.helpers.find_txt( neighbor, 'LocalASNoPrepend', 'false').title()) holdtime = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(neighbor, 'HoldTime'), 0) or vrf_holdtime @@ -1142,7 +1143,7 @@ def get_ntp_stats(self): xpath = './/NodeTable/Node/Associations/PeerSummaryInfo/Entry/PeerInfoCommon' for node in result_tree.xpath(xpath): - synchronized = eval(napalm_base.helpers.find_txt(node, 'IsSysPeer', 'false').title()) + synchronized = ast.literal_eval(napalm_base.helpers.find_txt(node, 'IsSysPeer', 'false').title()) address = napalm_base.helpers.find_txt(node, 'Address') if address == 'DLRSC node': continue @@ -1365,7 +1366,7 @@ def get_route_to(self, destination='', protocol=''): for bgp_path in bgp_route_tree.xpath('.//Path'): single_route_details = route_details.copy() if 'NotFound' not in bgp_path.keys(): - best_path = eval(napalm_base.helpers.find_txt( + best_path = ast.literal_eval(napalm_base.helpers.find_txt( bgp_path, 'PathInformation/IsBestPath', 'false').title()) local_preference = napalm_base.helpers.convert( int, From 9e46bc721dacf6f206affaf2581ae726eb717ffc Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 12 Jul 2017 11:20:55 +0100 Subject: [PATCH 2/3] Correct line length --- napalm_iosxr/iosxr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/napalm_iosxr/iosxr.py b/napalm_iosxr/iosxr.py index bd5e182..f2a63af 100644 --- a/napalm_iosxr/iosxr.py +++ b/napalm_iosxr/iosxr.py @@ -1143,7 +1143,8 @@ def get_ntp_stats(self): xpath = './/NodeTable/Node/Associations/PeerSummaryInfo/Entry/PeerInfoCommon' for node in result_tree.xpath(xpath): - synchronized = ast.literal_eval(napalm_base.helpers.find_txt(node, 'IsSysPeer', 'false').title()) + synchronized = ast.literal_eval( + napalm_base.helpers.find_txt(node, 'IsSysPeer', 'false').title()) address = napalm_base.helpers.find_txt(node, 'Address') if address == 'DLRSC node': continue From 18a5a1285f12a0088265ed000a2b3767f79425ef Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 12 Jul 2017 11:27:58 +0100 Subject: [PATCH 3/3] Get rid of stupid eval --- napalm_iosxr/iosxr.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/napalm_iosxr/iosxr.py b/napalm_iosxr/iosxr.py index f2a63af..7be99bc 100644 --- a/napalm_iosxr/iosxr.py +++ b/napalm_iosxr/iosxr.py @@ -17,7 +17,6 @@ # import stdlib import re -import ast import copy import socket from collections import defaultdict @@ -819,8 +818,8 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout): bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/RoutePolicyIn') export_policy = napalm_base.helpers.find_txt( bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/RoutePolicyOut') - multipath = ast.literal_eval((napalm_base.helpers.find_txt( - bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/Multipath') or 'false').title()) + multipath = napalm_base.helpers.find_txt( + bgp_group, 'NeighborGroupAFTable/NeighborGroupAF/Multipath') == 'true' peer_as = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(bgp_group, 'RemoteAS/AS_YY'), 0) local_as = napalm_base.helpers.convert( @@ -950,8 +949,8 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): napalm_base.helpers.find_txt(neighbor, 'Naming/NeighborAddress/IPV4Address') or napalm_base.helpers.find_txt(neighbor, 'Naming/NeighborAddress/IPV6Address') ) - local_address_configured = ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'IsLocalAddressConfigured', 'false').title()) + local_address_configured = napalm_base.helpers.find_txt( + neighbor, 'IsLocalAddressConfigured') == 'true' local_address = napalm_base.helpers.ip( napalm_base.helpers.find_txt(neighbor, 'ConnectionLocalAddress/IPV4Address') or napalm_base.helpers.find_txt(neighbor, 'ConnectionLocalAddress/IPV6Address') @@ -964,12 +963,12 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): ) remote_port = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(neighbor, 'ConnectionRemotePort')) - multihop = ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'IsExternalNeighborNotDirectlyConnected', 'false').title()) - remove_private_as = ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'AFData/Entry/RemovePrivateASFromUpdates', 'false').title()) - multipath = ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'AFData/Entry/SelectiveMultipathEligible', 'false').title()) + multihop = napalm_base.helpers.find_txt( + neighbor, 'IsExternalNeighborNotDirectlyConnected') == 'true' + remove_private_as = napalm_base.helpers.find_txt( + neighbor, 'AFData/Entry/RemovePrivateASFromUpdates') == 'true' + multipath = napalm_base.helpers.find_txt( + neighbor, 'AFData/Entry/SelectiveMultipathEligible') == 'true' import_policy = napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/RoutePolicyIn') export_policy = napalm_base.helpers.find_txt( @@ -1001,10 +1000,10 @@ def get_bgp_neighbors_detail(self, neighbor_address=''): advertised_prefix_count = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt( neighbor, 'AFData/Entry/PrefixesAdvertised'), 0) - suppress_4byte_as = ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'Suppress4ByteAs', 'false').title()) - local_as_prepend = not ast.literal_eval(napalm_base.helpers.find_txt( - neighbor, 'LocalASNoPrepend', 'false').title()) + suppress_4byte_as = napalm_base.helpers.find_txt( + neighbor, 'Suppress4ByteAs') == 'true' + local_as_prepend = napalm_base.helpers.find_txt( + neighbor, 'LocalASNoPrepend') != 'true' holdtime = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(neighbor, 'HoldTime'), 0) or vrf_holdtime configured_holdtime = napalm_base.helpers.convert( @@ -1143,8 +1142,7 @@ def get_ntp_stats(self): xpath = './/NodeTable/Node/Associations/PeerSummaryInfo/Entry/PeerInfoCommon' for node in result_tree.xpath(xpath): - synchronized = ast.literal_eval( - napalm_base.helpers.find_txt(node, 'IsSysPeer', 'false').title()) + synchronized = napalm_base.helpers.find_txt(node, 'IsSysPeer') == 'true' address = napalm_base.helpers.find_txt(node, 'Address') if address == 'DLRSC node': continue @@ -1367,8 +1365,8 @@ def get_route_to(self, destination='', protocol=''): for bgp_path in bgp_route_tree.xpath('.//Path'): single_route_details = route_details.copy() if 'NotFound' not in bgp_path.keys(): - best_path = ast.literal_eval(napalm_base.helpers.find_txt( - bgp_path, 'PathInformation/IsBestPath', 'false').title()) + best_path = napalm_base.helpers.find_txt( + bgp_path, 'PathInformation/IsBestPath') == 'true' local_preference = napalm_base.helpers.convert( int, napalm_base.helpers.find_txt(