Skip to content
Merged
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: 9 additions & 3 deletions netbox_diode_plugin/plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ def _parse_diode_target(target: str) -> tuple[str, str, bool]:
"""Parse the target into authority, path and tls_verify."""
parsed_target = urlparse(target)

if parsed_target.scheme not in ["grpc", "grpcs"]:
raise ValueError("target should start with grpc:// or grpcs://")
if parsed_target.scheme not in ["grpc", "grpcs", "http", "https"]:
raise ValueError("target should start with grpc://, grpcs://, http:// or https://")

tls_verify = parsed_target.scheme == "grpcs"
tls_verify = parsed_target.scheme in ["grpcs", "https"]

authority = parsed_target.netloc

if ":" not in authority:
if parsed_target.scheme in ["grpc", "http"]:
authority += ":80"
elif parsed_target.scheme in ["grpcs", "https"]:
authority += ":443"

return authority, parsed_target.path, tls_verify


Expand Down
46 changes: 45 additions & 1 deletion netbox_diode_plugin/tests/test_plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Copyright 2025 NetBox Labs, Inc.
"""Diode NetBox Plugin - Tests."""

import pytest
from django.contrib.auth import get_user_model
from django.test import TestCase

from netbox_diode_plugin.plugin_config import get_diode_auth_introspect_url, get_diode_user
from netbox_diode_plugin.plugin_config import _parse_diode_target, get_diode_auth_introspect_url, get_diode_user

User = get_user_model()

Expand All @@ -24,3 +25,46 @@ def test_get_diode_user(self):
expected_diode_user = User.objects.get(username="diode")
self.assertEqual(diode_user, expected_diode_user)

def test__parse_diode_target_handles_ftp_prefix(self):
"""Check that _parse_diode_target raises an error when the target contains ftp://."""
with pytest.raises(ValueError):
_parse_diode_target("ftp://localhost:8081")

def test__parse_diode_target_parses_authority_correctly(self):
"""Check that _parse_diode_target parses the authority correctly."""
authority, path, tls_verify = _parse_diode_target("grpc://localhost:8081")
assert authority == "localhost:8081"
assert path == ""
assert tls_verify is False

def test__parse_diode_target_adds_default_port_if_missing(self):
"""Check that _parse_diode_target adds the default port if missing."""
authority, _, _ = _parse_diode_target("grpc://localhost")
assert authority == "localhost:80"
authority, _, _ = _parse_diode_target("http://localhost")
assert authority == "localhost:80"
authority, _, _ = _parse_diode_target("grpcs://localhost")
assert authority == "localhost:443"
authority, _, _ = _parse_diode_target("https://localhost")
assert authority == "localhost:443"

def test__parse_diode_target_parses_path_correctly(self):
"""Check that _parse_diode_target parses the path correctly."""
_, path, _ = _parse_diode_target("grpc://localhost:8081/my/path")
assert path == "/my/path"

def test__parse_diode_target_handles_no_path(self):
"""Check that _parse_diode_target handles no path."""
_, path, _ = _parse_diode_target("grpc://localhost:8081")
assert path == ""

def test__parse_diode_target_parses_tls_verify_correctly(self):
"""Check that _parse_diode_target parses tls_verify correctly."""
_, _, tls_verify = _parse_diode_target("grpc://localhost:8081")
assert tls_verify is False
_, _, tls_verify = _parse_diode_target("http://localhost:8081")
assert tls_verify is False
_, _, tls_verify = _parse_diode_target("grpcs://localhost:8081")
assert tls_verify is True
_, _, tls_verify = _parse_diode_target("https://localhost:8081")
assert tls_verify is True