-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path403_overlay_networks.py
More file actions
123 lines (106 loc) · 5.61 KB
/
Copy path403_overlay_networks.py
File metadata and controls
123 lines (106 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Rule:
id = "403"
description = "Verify Network attributes are enabled in fabric overlay services"
severity = "HIGH"
@classmethod
def match(cls, data_model):
results = []
fabric_netflow_status = False
fabric_trm_status = False
networks = []
# Map fabric types to the keys used in the data model based on controller fabric types
fabric_type_map = {
"VXLAN_EVPN": "ibgp",
"eBGP_VXLAN": "ebgp",
}
fabric_type = fabric_type_map.get(data_model['vxlan']['fabric']['type'])
netflow_keys = ['vxlan', 'global', fabric_type]
check = cls.data_model_key_check(data_model, netflow_keys)
if fabric_type in check['keys_found']:
netflow_keys = ['vxlan', 'global', fabric_type, 'netflow', 'enable']
check = cls.data_model_key_check(data_model, netflow_keys)
if fabric_type in check['keys_not_found'] or 'enable' in check['keys_not_found']:
netflow_keys = ['vxlan', 'global', 'netflow', 'enable']
check = cls.data_model_key_check(data_model, netflow_keys)
if 'enable' in check['keys_found']:
fabric_netflow_status = cls.safeget(data_model, netflow_keys)
if fabric_netflow_status is None:
fabric_netflow_status = False
underlay_trm_keys = ['vxlan', 'underlay', 'multicast', 'ipv4', 'trm_enable']
check = cls.data_model_key_check(data_model, underlay_trm_keys)
if 'trm_enable' in check['keys_found']:
# Cannot use safeget yet without updating code check below as it looks for False vs None
# fabric_trm_status = cls.safeget(data_model, trm_keys)
fabric_trm_status = data_model["vxlan"]["underlay"]["multicast"]["ipv4"].get("trm_enable", False)
network_keys = ['vxlan', 'overlay', 'networks']
check = cls.data_model_key_check(data_model, network_keys)
if 'networks' in check['keys_data']:
networks = data_model["vxlan"]["overlay"]["networks"]
else:
network_keys = ['vxlan', 'overlay_services', 'networks']
check = cls.data_model_key_check(data_model, network_keys)
if 'networks' in check['keys_data']:
networks = data_model["vxlan"]["overlay_services"]["networks"]
# if data_model.get("vxlan", None):
# if data_model["vxlan"].get("overlay", None) or data_model["vxlan"].get("overlay_services", None):
# if data_model["vxlan"].get("overlay").get("networks", None):
# networks = data_model["vxlan"]["overlay"]["networks"]
# elif data_model["vxlan"].get("overlay_services").get("networks", None):
# networks = data_model["vxlan"]["overlay_services"]["networks"]
for network in networks:
current_vlan_name = network.get("vlan_name", None)
if current_vlan_name is not None and ' ' in str(current_vlan_name):
results.append(
f"vxlan.overlay.networks.{network['name']}.vlan_name must not contain whitespace."
)
current_network_netflow_status = network.get("netflow_enable", None)
if current_network_netflow_status is not None:
if fabric_netflow_status is False and current_network_netflow_status is True:
results.append(
f"For vxlan.overlay.networks.{network['name']}.netflow_enable to be enabled, "
f"first vxlan.global.netflow.enable must be enabled (true)."
)
break
if fabric_netflow_status and current_network_netflow_status:
current_network_netflow_monitor = network.get("vlan_netflow_monitor", None)
if current_network_netflow_monitor is None:
results.append(
f"When vxlan.overlay.networks.{network['name']}.netflow_enable is enabled, "
f"then vxlan.overlay.networks.{network['name']}.vlan_netflow_monitor must be set "
"to a valid value from vxlan.global.netflow."
)
break
current_network_trm_status = network.get("trm_enable", None)
if current_network_trm_status is not None:
if fabric_trm_status is False and current_network_trm_status is True:
results.append(
f"For vxlan.overlay.networks.{network['name']}.trm_enable to be enabled, "
f"first vxlan.underlay.multicast.ipv4.trm_enable must be enabled (true)."
)
break
return results
@classmethod
def data_model_key_check(cls, tested_object, keys):
dm_key_dict = {'keys_found': [], 'keys_not_found': [], 'keys_data': [], 'keys_no_data': []}
for key in keys:
if tested_object and key in tested_object:
dm_key_dict['keys_found'].append(key)
tested_object = tested_object[key]
if tested_object:
dm_key_dict['keys_data'].append(key)
else:
dm_key_dict['keys_no_data'].append(key)
else:
dm_key_dict['keys_not_found'].append(key)
return dm_key_dict
@classmethod
def safeget(cls, dict, keys):
# Utility function to safely get nested dictionary values
for key in keys:
if dict is None:
return None
if key in dict:
dict = dict[key]
else:
return None
return dict