2018-11-08 19:45:21 +00:00
|
|
|
"""
|
|
|
|
|
peeringdb model / field validators
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import ipaddress
|
2020-01-08 13:29:58 -06:00
|
|
|
import phonenumbers
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
|
|
from peeringdb_server.inet import network_is_pdb_valid
|
2019-07-30 13:48:52 -05:00
|
|
|
import peeringdb_server.models
|
2018-11-08 19:45:21 +00:00
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
|
2020-01-08 13:29:58 -06:00
|
|
|
def validate_phonenumber(phonenumber, country=None):
|
|
|
|
|
"""
|
|
|
|
|
Validate a phonenumber to E.164
|
|
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
- phonenumber (str)
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
- ValidationError if phone number isn't valid E.164 and cannot
|
|
|
|
|
be made E.164 valid
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- str: validated phonenumber
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if not phonenumber:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
parsed_number = phonenumbers.parse(phonenumber, country)
|
|
|
|
|
validated_number = phonenumbers.format_number(
|
|
|
|
|
parsed_number, phonenumbers.PhoneNumberFormat.E164
|
|
|
|
|
)
|
|
|
|
|
return u"{}".format(validated_number)
|
|
|
|
|
except phonenumbers.phonenumberutil.NumberParseException as exc:
|
|
|
|
|
raise ValidationError(_("Not a valid phone number (E.164)"))
|
|
|
|
|
|
|
|
|
|
|
2019-07-30 13:48:52 -05:00
|
|
|
def validate_prefix(prefix):
|
2018-11-08 19:45:21 +00:00
|
|
|
"""
|
2019-07-30 13:48:52 -05:00
|
|
|
validate ip prefix
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
- prefix: ipaddress.IPv4Network or an ipaddress.IPv6Network
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
- ValidationError on failed validation
|
2019-07-30 13:48:52 -05:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- ipaddress.ip_network instance
|
2018-11-08 19:45:21 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-01-08 13:29:58 -06:00
|
|
|
if isinstance(prefix, str):
|
2018-11-08 19:45:21 +00:00
|
|
|
try:
|
|
|
|
|
prefix = ipaddress.ip_network(prefix)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise ValidationError(_("Invalid prefix: {}").format(prefix))
|
2019-07-30 13:48:52 -05:00
|
|
|
return prefix
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_address_space(prefix):
|
|
|
|
|
"""
|
|
|
|
|
validate an ip prefix according to peeringdb specs
|
|
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
- prefix: ipaddress.IPv4Network or an ipaddress.IPv6Network
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
- ValidationError on failed validation
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
prefix = validate_prefix(prefix)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
if not network_is_pdb_valid(prefix):
|
|
|
|
|
raise ValidationError(_("Address space invalid: {}").format(prefix))
|
|
|
|
|
|
2019-12-05 16:57:52 +00:00
|
|
|
prefixlen_min = getattr(
|
|
|
|
|
settings, "DATA_QUALITY_MIN_PREFIXLEN_V{}".format(prefix.version)
|
|
|
|
|
)
|
|
|
|
|
prefixlen_max = getattr(
|
|
|
|
|
settings, "DATA_QUALITY_MAX_PREFIXLEN_V{}".format(prefix.version)
|
|
|
|
|
)
|
2019-04-18 13:14:52 -05:00
|
|
|
|
|
|
|
|
if prefix.prefixlen < prefixlen_min:
|
|
|
|
|
raise ValidationError(
|
2019-12-05 16:57:52 +00:00
|
|
|
_("Maximum allowed prefix length is {}").format(prefixlen_min)
|
|
|
|
|
)
|
2019-04-18 13:14:52 -05:00
|
|
|
elif prefix.prefixlen > prefixlen_max:
|
|
|
|
|
raise ValidationError(
|
2019-12-05 16:57:52 +00:00
|
|
|
_("Minimum allowed prefix length is {}").format(prefixlen_max)
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
def validate_info_prefixes4(value):
|
|
|
|
|
if value > settings.DATA_QUALITY_MAX_PREFIX_V4_LIMIT:
|
|
|
|
|
raise ValidationError(
|
|
|
|
|
_("Maximum value allowed {}").format(
|
2019-12-05 16:57:52 +00:00
|
|
|
settings.DATA_QUALITY_MAX_PREFIX_V4_LIMIT
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
if value < 0:
|
|
|
|
|
raise ValidationError(_("Negative value not allowed"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_info_prefixes6(value):
|
|
|
|
|
if value > settings.DATA_QUALITY_MAX_PREFIX_V6_LIMIT:
|
|
|
|
|
raise ValidationError(
|
|
|
|
|
_("Maximum value allowed {}").format(
|
2019-12-05 16:57:52 +00:00
|
|
|
settings.DATA_QUALITY_MAX_PREFIX_V6_LIMIT
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-11-08 19:45:21 +00:00
|
|
|
|
|
|
|
|
if value < 0:
|
|
|
|
|
raise ValidationError(_("Negative value not allowed"))
|
2019-07-30 13:48:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_prefix_overlap(prefix):
|
|
|
|
|
"""
|
|
|
|
|
validate that a prefix does not overlap with another prefix
|
|
|
|
|
on an already existing ixlan
|
|
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
- prefix: ipaddress.IPv4Network or an ipaddress.IPv6Network
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
- ValidationError on failed validation
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
prefix = validate_prefix(prefix)
|
2019-12-05 16:57:52 +00:00
|
|
|
qs = peeringdb_server.models.IXLanPrefix.objects.filter(
|
|
|
|
|
protocol="IPv{}".format(prefix.version), status="ok"
|
|
|
|
|
)
|
2019-07-30 13:48:52 -05:00
|
|
|
qs = qs.exclude(prefix=prefix)
|
|
|
|
|
for ixpfx in qs:
|
|
|
|
|
if ixpfx.prefix.overlaps(prefix):
|
2019-12-05 16:57:52 +00:00
|
|
|
raise ValidationError(
|
|
|
|
|
_(
|
|
|
|
|
"Prefix overlaps with {}'s prefix: {}".format(
|
|
|
|
|
ixpfx.ixlan.ix.name, ixpfx.prefix
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|