From e1bca52d57440ceecded5387d35a82e17a77f845 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 25 Apr 2019 14:49:52 -0400 Subject: [PATCH] Fix regex for IPAddress.dns_name (but see #3106) --- .../migrations/0027_ipaddress_add_dns_name.py | 2 +- netbox/ipam/models.py | 19 ++++++++++++++----- netbox/ipam/validators.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/netbox/ipam/migrations/0027_ipaddress_add_dns_name.py b/netbox/ipam/migrations/0027_ipaddress_add_dns_name.py index b4a0e3a32..534957ce1 100644 --- a/netbox/ipam/migrations/0027_ipaddress_add_dns_name.py +++ b/netbox/ipam/migrations/0027_ipaddress_add_dns_name.py @@ -14,6 +14,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='ipaddress', name='dns_name', - field=models.CharField(blank=True, max_length=255, validators=[django.core.validators.RegexValidator(code='invalid', message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names', regex='^[0-9A-Za-z\\.-]+$')]), + field=models.CharField(blank=True, max_length=255, validators=[django.core.validators.RegexValidator(code='invalid', message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names', regex='^[0-9A-Za-z.-]+$')]), ), ] diff --git a/netbox/ipam/models.py b/netbox/ipam/models.py index 77ec7409b..373128a8f 100644 --- a/netbox/ipam/models.py +++ b/netbox/ipam/models.py @@ -368,11 +368,15 @@ class Prefix(ChangeLoggedModel, CustomFieldModel): }) def save(self, *args, **kwargs): - if self.prefix: + + if isinstance(self.prefix, netaddr.IPNetwork): + # Clear host bits from prefix self.prefix = self.prefix.cidr - # Infer address family from IPNetwork object + + # Record address family self.family = self.prefix.version + super().save(*args, **kwargs) def to_csv(self): @@ -579,7 +583,7 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel): blank=True, validators=[DNSValidator], verbose_name='DNS Name', - help_text='Hostname or FQDN' + help_text='Hostname or FQDN (not case-sensitive)' ) description = models.CharField( max_length=100, @@ -633,9 +637,14 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel): }) def save(self, *args, **kwargs): - if self.address: - # Infer address family from IPAddress object + + # Record address family + if isinstance(self.address, netaddr.IPNetwork): self.family = self.address.version + + # Force dns_name to lowercase + self.dns_name = self.dns_name.lower() + super().save(*args, **kwargs) def log_change(self, user, request_id, action): diff --git a/netbox/ipam/validators.py b/netbox/ipam/validators.py index 710f05348..6669b7ec5 100644 --- a/netbox/ipam/validators.py +++ b/netbox/ipam/validators.py @@ -2,7 +2,7 @@ from django.core.validators import RegexValidator DNSValidator = RegexValidator( - regex='^[a-z]+$', + regex='^[0-9A-Za-z.-]+$', message='Only alphanumeric characters, hyphens, and periods are allowed in DNS names', code='invalid' )