Skip to content

Commit

Permalink
Merge pull request #73 from peteeckel/fix/allow-cname-and-nsec
Browse files Browse the repository at this point in the history
CNAME and NSEC for the same name must be allowed
  • Loading branch information
peteeckel authored Oct 16, 2023
2 parents 7c1281f + 5265484 commit 8cb60bd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
7 changes: 5 additions & 2 deletions netbox_dns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,14 +970,17 @@ def clean(self, *args, **kwargs):
)

if self.type == RecordTypeChoices.CNAME:
if records.exists():
if records.exclude(type=RecordTypeChoices.NSEC).exists():
raise ValidationError(
{
"type": f"There is already an active record for name {self.name} in zone {self.zone}, CNAME is not allowed."
}
) from None

elif records.filter(type=RecordTypeChoices.CNAME).exists():
elif (
records.filter(type=RecordTypeChoices.CNAME).exists()
and self.type != RecordTypeChoices.NSEC
):
raise ValidationError(
{
"type": f"There is already an active CNAME record for name {self.name} in zone {self.zone}, no other record allowed."
Expand Down
53 changes: 53 additions & 0 deletions netbox_dns/tests/record/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@ def test_name_and_cname(self):
with self.assertRaises(ValidationError):
f_record2.save()

def test_nsec_and_cname(self):
f_zone = self.zones[0]

name1 = "test1"
name2 = "test2"

f_record1 = Record(
zone=f_zone,
name=name1,
type=RecordTypeChoices.NSEC,
value="test2.zone1.example.com. A MX RRSIG NSEC",
**self.record_data,
)
f_record1.save()

f_record2 = Record(
zone=f_zone,
name=name1,
type=RecordTypeChoices.CNAME,
value=name2,
**self.record_data,
)
f_record2.save()

self.assertEqual(Record.objects.filter(name=name1, zone=f_zone).count(), 2)

def test_cname_and_name(self):
f_zone = self.zones[0]

Expand Down Expand Up @@ -230,6 +256,33 @@ def test_cname_and_name(self):
with self.assertRaises(ValidationError):
f_record2.save()

def test_cname_and_nsec(self):
f_zone = self.zones[0]

name1 = "test1"
name2 = "test2"
address = "fe80:dead:beef:1::42"

f_record1 = Record(
zone=f_zone,
name=name1,
type=RecordTypeChoices.CNAME,
value=name2,
**self.record_data,
)
f_record1.save()

f_record2 = Record(
zone=f_zone,
name=name1,
type=RecordTypeChoices.NSEC,
value="test2.zone1.example.com. A MX RRSIG NSEC",
**self.record_data,
)
f_record2.save()

self.assertEqual(Record.objects.filter(name=name1, zone=f_zone).count(), 2)

def test_double_singletons(self):
f_zone = self.zones[1]

Expand Down

0 comments on commit 8cb60bd

Please sign in to comment.