Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: 17292 - Detect infinite loop condition while iterating terminations in CablePath.from_origin #17293

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions netbox/dcim/models/cables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import logging
from collections import defaultdict

from django.contrib.contenttypes.fields import GenericForeignKey
Expand Down Expand Up @@ -27,6 +28,8 @@
'CableTermination',
)

logger = logging.getLogger('netbox.dcim.cable')


trace_paths = Signal()

Expand Down Expand Up @@ -543,6 +546,7 @@ def from_origin(cls, terminations):
is_split = False

while terminations:
prev_path = path.copy()

# Terminations must all be of the same type
assert all(isinstance(t, type(terminations[0])) for t in terminations[1:])
Expand Down Expand Up @@ -714,6 +718,14 @@ def from_origin(cls, terminations):
is_split = True
break

# Detect infinite loop in cabling topology
num_elements_added = len(path) - len(prev_path)
prev_elements_added = prev_path[-num_elements_added:]
elements_added = path[-num_elements_added:]
if elements_added == prev_elements_added:
logger.warning('Infinite loop detected while updating cable path trace')
break

return cls(
path=path,
is_complete=is_complete,
Expand Down
39 changes: 39 additions & 0 deletions netbox/dcim/tests/test_cablepaths.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType

from circuits.models import *
from dcim.choices import LinkStatusChoices
Expand Down Expand Up @@ -2300,3 +2301,41 @@ def test_401_exclude_midspan_devices(self):
is_active=True
)
self.assertEqual(CablePath.objects.count(), 0)

def test_detect_infinite_loop(self):
"""
Tests the ability to detect a non-resolving path and break out with a logged warning.
No assertion; test will fail by falling into a non-terminating loop in CablePath.from_origin()
[IF1] --C1-- [FP1][Test Device][Rear Splice]
[FP2] --C2-- [ Rear Splice
"""
manufacturer = Manufacturer.objects.create(name='Infinite Loop Co', slug='infinite-loop-co')
role = DeviceRole.objects.create(name='Infinite Loop Device Role', slug='infinite-loop-device-role')
device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Infinite Loop Device Type', slug='infinite-loop-device-type')
device = Device.objects.create(site=self.site, device_type=device_type, role=role, name='Infinite Loop Device')
interface = Interface.objects.create(device=device, name='Interface 1')

patch_panel_device_type = DeviceType.objects.create(
manufacturer=manufacturer,
front_port_template_count=48,
rear_port_template_count=48,
)
patch_panel = Device.objects.create(site=self.site, device_type=patch_panel_device_type, role=role)
rear_splice = RearPort.objects.create(device=patch_panel, positions=48, name='Rear Splice')
front_port_1 = FrontPort.objects.create(device=patch_panel, rear_port=rear_splice, rear_port_position=1, name='Front Port 1')
front_port_2 = FrontPort.objects.create(device=patch_panel, rear_port=rear_splice, rear_port_position=2, name='Front Port 2')

ct_interface = ContentType.objects.get(app_label='dcim', model='interface')
ct_frontport = ContentType.objects.get(app_label='dcim', model='frontport')
ct_rearport = ContentType.objects.get(app_label='dcim', model='rearport')

cable_1 = Cable.objects.create()
termination_1a = CableTermination.objects.create(cable=cable_1, cable_end='A', termination_type=ct_interface, termination_id=interface.id)
termination_1b = CableTermination.objects.create(cable=cable_1, cable_end='B', termination_type=ct_frontport, termination_id=front_port_1.id)

cable_2 = Cable.objects.create()
termination_2a = CableTermination.objects.create(cable=cable_2, cable_end='A', termination_type=ct_frontport, termination_id=front_port_2.id)
termination_2b = CableTermination.objects.create(cable=cable_2, cable_end='B', termination_type=ct_rearport, termination_id=rear_splice.id)

cable_1._terminations_modified = True
cable_1.save()