This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Pull builtin checks from IP Fabric
- Loading branch information
Showing
6 changed files
with
189 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from tabulate import tabulate | ||
|
||
from ipfabric import IPFClient | ||
from ipfabric.tools import SinglePointsFailure, NonRedundantLinks | ||
|
||
if __name__ == "__main__": | ||
# USE SPARINGLY THIS WILL CREATE A GRAPH OF ALL YOUR SITES. | ||
# NOT TESTED ON AN IP FABRIC INSTANCE WITH MORE THAN 1,000 DEVICES | ||
ipf = IPFClient('https://demo3.ipfabric.io') | ||
nrl = NonRedundantLinks(ipf) | ||
links = nrl.list() | ||
|
||
print(tabulate(links, headers="keys")) | ||
""" | ||
source source_int target target_int | ||
-------------------- ------------- ----------------- ------------ | ||
L71R3 Et1/1 L71FW10-HA2/root vl201 | ||
L67FW31 ge-0/0/3.0 L67P13 ge-0/0/4.0 | ||
L71-CP1/0 wrp0 L71EXR2 Et0/3 | ||
L71-CP1/BadCompany eth1 L71R4 Et1/1 | ||
""" | ||
print() | ||
|
||
spf = SinglePointsFailure(ipf) | ||
nodes = spf.list() | ||
print(tabulate(nodes, headers="keys")) | ||
""" | ||
device type | ||
-------------------- ------------------ | ||
HWLAB router | ||
HWLAB-sw3_5650TD switch | ||
HWLAB-FW-RBSH1 waas | ||
HWLAB-FW-C5510/admin fw | ||
HWLAB-WLC-A620 wlc | ||
HWLAB-FPR-1010 fw | ||
HWLAB-WLC-C4400 wlc | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .builtin_checks import SinglePointsFailure, NonRedundantLinks | ||
from .configuration import DeviceConfigs | ||
from .sites import UpdateSiteNames | ||
from .vulnerabilities import Vulnerabilities | ||
|
||
__all__ = [DeviceConfigs, UpdateSiteNames, Vulnerabilities] | ||
__all__ = [DeviceConfigs, UpdateSiteNames, Vulnerabilities, SinglePointsFailure, NonRedundantLinks] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Any | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
|
||
def sites_names(ipf): | ||
sites = [site['siteName'] for site in ipf.inventory.sites.all(columns=['siteName'])] | ||
sites.append('$main') | ||
return sites | ||
|
||
|
||
@dataclass | ||
class NonRedundantLinks: | ||
ipf: Any | ||
|
||
@staticmethod | ||
def _get_int_names(labels): | ||
source_int = None | ||
for label in labels['source']: | ||
if label['type'] == "intName": | ||
source_int = label['text'] | ||
break | ||
target_int = None | ||
for label in labels['target']: | ||
if label['type'] == "intName": | ||
target_int = label['text'] | ||
break | ||
return source_int, target_int | ||
|
||
def list(self): | ||
sites = sites_names(self.ipf) | ||
data = self.ipf.graphs.site(sites, overlay={"type": "intent", "intentRuleId": "nonRedundantEdges"}) | ||
nodes = {node['id']: node['label'] for node in data['graphResult']['graphData']['nodes'].values()} | ||
links = list() | ||
for link in data['graphResult']['graphData']['edges'].values(): | ||
if link['intentCheckResult']: | ||
source_int, target_int = self._get_int_names(link['labels']) | ||
source = nodes[link['source']] if link['source'] in nodes else link['source'] | ||
target = nodes[link['target']] if link['target'] in nodes else link['target'] | ||
links.append(dict(source=source, source_int=source_int, target=target, target_int=target_int)) | ||
return links | ||
|
||
|
||
@dataclass | ||
class SinglePointsFailure: | ||
ipf: Any | ||
|
||
def list(self): | ||
sites = sites_names(self.ipf) | ||
data = self.ipf.graphs.site(sites, overlay={"type": "intent", "intentRuleId": "singlePointsOfFailure"}) | ||
nodes = list() | ||
for node in data['graphResult']['graphData']['nodes'].values(): | ||
if 'intentCheckResult' in node and node['intentCheckResult']: | ||
nodes.append(dict(device=node['label'], type=node['type'])) | ||
return nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from ipfabric.tools.builtin_checks import NonRedundantLinks, SinglePointsFailure | ||
|
||
|
||
class Checks(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.ipf = MagicMock() | ||
self.ipf.inventory.sites.all.return_value = [{'siteName': 'Test'}] | ||
self.ipf.graphs.site.return_value = { | ||
"graphResult": { | ||
"graphData": { | ||
"edges": { | ||
"39380579": { | ||
"source": "69240849", | ||
"target": "9A0MD0N6T4F", | ||
"intentCheckResult": 30, | ||
"labels": { | ||
"source": [{ | ||
"type": "intName", | ||
"text": "Et0/0" | ||
} | ||
], | ||
"target": [{ | ||
"type": "intName", | ||
"text": "GigabitEthernet0/1" | ||
} | ||
] | ||
} | ||
}, | ||
"39380580": { | ||
"source": "SOURCE", | ||
"target": "TARGET", | ||
"intentCheckResult": 0, | ||
"labels": { | ||
"source": [{ | ||
"type": "intName", | ||
"text": "Et0/0" | ||
} | ||
], | ||
"target": [{ | ||
"type": "intName", | ||
"text": "GigabitEthernet0/1" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"nodes": { | ||
"69240849": { | ||
"id": "69240849", | ||
"label": "L1R13", | ||
"type": "router", | ||
"intentCheckResult": 30, | ||
}, | ||
"9A0MD0N6T4F": { | ||
"id": "9A0MD0N6T4F", | ||
"label": "L1FW1", | ||
"type": "fw" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def test_single_points(self): | ||
spf = SinglePointsFailure(self.ipf) | ||
devices = spf.list() | ||
self.assertEqual(devices[0], {'device': 'L1R13', 'type': 'router'}) | ||
|
||
def test_non_redundant_links(self): | ||
nrl = NonRedundantLinks(self.ipf) | ||
links = nrl.list() | ||
self.assertEqual( | ||
links[0], | ||
{'source': 'L1R13', 'source_int': 'Et0/0', 'target': 'L1FW1', 'target_int': 'GigabitEthernet0/1'} | ||
) |