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

Added validation to the SOA RNAME field #58

Merged
merged 1 commit into from
Sep 17, 2023
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
11 changes: 11 additions & 0 deletions netbox_dns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dns import rdata, rdatatype, rdataclass
from dns import name as dns_name
from dns.rdtypes.ANY import SOA
from dns.exception import DNSException

from netaddr import IPNetwork, AddrFormatError, IPAddress

Expand Down Expand Up @@ -484,6 +485,16 @@ def clean(self, *args, **kwargs):
}
) from None

try:
soa_rname = dns_name.from_text(self.soa_rname, origin=dns_name.root)
validate_fqdn(self.soa_rname)
except (DNSException, ValidationError) as exc:
raise ValidationError(
{
"soa_rname": exc,
}
) from None

if self.soa_serial is None and not self.soa_serial_auto:
raise ValidationError(
{
Expand Down
51 changes: 51 additions & 0 deletions netbox_dns/tests/zone/test_rname_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.test import TestCase
from django.core.exceptions import ValidationError

from netbox_dns.models import NameServer, Zone


class RNameValidationTest(TestCase):
zone_data = {
"default_ttl": 86400,
"soa_refresh": 172800,
"soa_retry": 7200,
"soa_expire": 2592000,
"soa_ttl": 86400,
"soa_minimum": 3600,
"soa_serial": 1,
"soa_serial_auto": False,
}

@classmethod
def setUpTestData(cls):
cls.nameserver = NameServer.objects.create(name="ns1.example.com")

def test_rname_validation_ok(self):
rnames = (
"hostmaster.example.com",
"hostmaster.example.com.",
)

for index, rname in enumerate(rnames):
zone = Zone.objects.create(
name=f"zone{index}.example.com",
soa_rname=rname,
**self.zone_data,
soa_mname=self.nameserver,
)
self.assertEqual(zone.soa_rname, rname)

def test_rname_validation_failure(self):
rnames = (
"hostmaster", # Not an FQDN
"[email protected]", # E-Mail address not converted to FQDN
)

for index, rname in enumerate(rnames):
with self.assertRaises(ValidationError):
Zone.objects.create(
name=f"zone{index}.example.com",
soa_rname=rname,
**self.zone_data,
soa_mname=self.nameserver,
)