From 1f4e4eefdab0f3d3ce56fe10b9a4c6c6c8615eae Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 19 Sep 2024 11:57:37 +0000 Subject: [PATCH 1/3] Regression test for #2989 NAV test data does not include a prefix 192.168.42.0/24. This should ensure the IPAM main API endpoint does not crash when asked to build a tree for a prefix not known to NAV. --- tests/integration/web/ipam/api_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/integration/web/ipam/api_test.py diff --git a/tests/integration/web/ipam/api_test.py b/tests/integration/web/ipam/api_test.py new file mode 100644 index 0000000000..b398aacd41 --- /dev/null +++ b/tests/integration/web/ipam/api_test.py @@ -0,0 +1,12 @@ +class TestPrefixViewSet: + def test_when_prefix_address_is_unknown_it_should_not_crash(self, client): + response = client.get( + "/ipam/api/", + follow=True, + data={ + "net_type": "all", + "within": "192.168.42.0/24", + "show_all": "True", + }, + ) + assert response.status_code == 200 From cedfaee60fe82704ffce492e0de0b1a26596f94d Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 19 Sep 2024 11:15:02 +0000 Subject: [PATCH 2/3] Fix incorrect handling of non-existent scopes The old code seemed to assume that `scope` would be `None` if no `Prefix` matching the network address could be found in the DB. But, of course, Django raises a `DoesNotExist` exception when using `Prefix.objects.get()` for this purpose. This changes the logic to catch the exception, but otherwise produce the originally intended response. --- python/nav/web/ipam/prefix_tree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/nav/web/ipam/prefix_tree.py b/python/nav/web/ipam/prefix_tree.py index b3d5e4c1d3..014ba843de 100644 --- a/python/nav/web/ipam/prefix_tree.py +++ b/python/nav/web/ipam/prefix_tree.py @@ -467,10 +467,10 @@ def make_tree(prefixes, family=None, root_ip=None, show_all=None, sort_by="ip"): init = [] if root_ip is not None and root_ip: - scope = Prefix.objects.get(net_address=root_ip) - if scope is not None: + try: + scope = Prefix.objects.get(net_address=root_ip) node = PrefixNode(scope) - else: + except Prefix.DoesNotExist: node = FauxNode(root_ip, "scope", "scope") init.append(node) From 4f8305ce04225f47e0c1936f06eafc40da83cbd8 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Thu, 19 Sep 2024 11:18:15 +0000 Subject: [PATCH 3/3] Add news fragment --- changelog.d/2989.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/2989.fixed.md diff --git a/changelog.d/2989.fixed.md b/changelog.d/2989.fixed.md new file mode 100644 index 0000000000..be2d1a7100 --- /dev/null +++ b/changelog.d/2989.fixed.md @@ -0,0 +1 @@ +Fix IPAM API crash bug that caused unnecessary error reports sent as e-mail to site admins