mirror of
https://github.com/ansible/awx.git
synced 2026-04-12 13:39:24 -02:30
Merge pull request #4784 from fosterseth/fix-3646-ldapserverfielduri
fix for LDAPServerURIField error if number present in top-level-domain Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
@@ -121,11 +121,14 @@ class URLField(CharField):
|
|||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
schemes = kwargs.pop('schemes', None)
|
schemes = kwargs.pop('schemes', None)
|
||||||
|
regex = kwargs.pop('regex', None)
|
||||||
self.allow_plain_hostname = kwargs.pop('allow_plain_hostname', False)
|
self.allow_plain_hostname = kwargs.pop('allow_plain_hostname', False)
|
||||||
super(URLField, self).__init__(**kwargs)
|
super(URLField, self).__init__(**kwargs)
|
||||||
validator_kwargs = dict(message=_('Enter a valid URL'))
|
validator_kwargs = dict(message=_('Enter a valid URL'))
|
||||||
if schemes is not None:
|
if schemes is not None:
|
||||||
validator_kwargs['schemes'] = schemes
|
validator_kwargs['schemes'] = schemes
|
||||||
|
if regex is not None:
|
||||||
|
validator_kwargs['regex'] = regex
|
||||||
self.validators.append(URLValidator(**validator_kwargs))
|
self.validators.append(URLValidator(**validator_kwargs))
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import awx
|
|||||||
# Django
|
# Django
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.core.validators import URLValidator, _lazy_re_compile
|
||||||
|
|
||||||
# Django Auth LDAP
|
# Django Auth LDAP
|
||||||
import django_auth_ldap.config
|
import django_auth_ldap.config
|
||||||
@@ -233,12 +234,34 @@ class AuthenticationBackendsField(fields.StringListField):
|
|||||||
|
|
||||||
class LDAPServerURIField(fields.URLField):
|
class LDAPServerURIField(fields.URLField):
|
||||||
|
|
||||||
|
tld_re = (
|
||||||
|
r'\.' # dot
|
||||||
|
r'(?!-)' # can't start with a dash
|
||||||
|
r'(?:[a-z' + URLValidator.ul + r'0-9' + '-]{2,63}' # domain label, this line was changed from the original URLValidator
|
||||||
|
r'|xn--[a-z0-9]{1,59})' # or punycode label
|
||||||
|
r'(?<!-)' # can't end with a dash
|
||||||
|
r'\.?' # may have a trailing dot
|
||||||
|
)
|
||||||
|
|
||||||
|
host_re = '(' + URLValidator.hostname_re + URLValidator.domain_re + tld_re + '|localhost)'
|
||||||
|
|
||||||
|
regex = _lazy_re_compile(
|
||||||
|
r'^(?:[a-z0-9\.\-\+]*)://' # scheme is validated separately
|
||||||
|
r'(?:[^\s:@/]+(?::[^\s:@/]*)?@)?' # user:pass authentication
|
||||||
|
r'(?:' + URLValidator.ipv4_re + '|' + URLValidator.ipv6_re + '|' + host_re + ')'
|
||||||
|
r'(?::\d{2,5})?' # port
|
||||||
|
r'(?:[/?#][^\s]*)?' # resource path
|
||||||
|
r'\Z', re.IGNORECASE)
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
|
|
||||||
kwargs.setdefault('schemes', ('ldap', 'ldaps'))
|
kwargs.setdefault('schemes', ('ldap', 'ldaps'))
|
||||||
kwargs.setdefault('allow_plain_hostname', True)
|
kwargs.setdefault('allow_plain_hostname', True)
|
||||||
|
kwargs.setdefault('regex', LDAPServerURIField.regex)
|
||||||
super(LDAPServerURIField, self).__init__(**kwargs)
|
super(LDAPServerURIField, self).__init__(**kwargs)
|
||||||
|
|
||||||
def run_validators(self, value):
|
def run_validators(self, value):
|
||||||
|
|
||||||
for url in filter(None, re.split(r'[, ]', (value or ''))):
|
for url in filter(None, re.split(r'[, ]', (value or ''))):
|
||||||
super(LDAPServerURIField, self).run_validators(url)
|
super(LDAPServerURIField, self).run_validators(url)
|
||||||
return value
|
return value
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from awx.sso.fields import (
|
|||||||
SAMLOrgAttrField,
|
SAMLOrgAttrField,
|
||||||
SAMLTeamAttrField,
|
SAMLTeamAttrField,
|
||||||
LDAPGroupTypeParamsField,
|
LDAPGroupTypeParamsField,
|
||||||
|
LDAPServerURIField
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -114,3 +115,20 @@ class TestLDAPGroupTypeParamsField():
|
|||||||
with pytest.raises(ValidationError) as e:
|
with pytest.raises(ValidationError) as e:
|
||||||
field.to_internal_value(data)
|
field.to_internal_value(data)
|
||||||
assert e.value.detail == expected
|
assert e.value.detail == expected
|
||||||
|
|
||||||
|
|
||||||
|
class TestLDAPServerURIField():
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("ldap_uri, exception, expected", [
|
||||||
|
(r'ldap://servername.com:444', None, r'ldap://servername.com:444'),
|
||||||
|
(r'ldap://servername.so3:444', None, r'ldap://servername.so3:444'),
|
||||||
|
(r'ldaps://servername3.s300:344', None, r'ldaps://servername3.s300:344'),
|
||||||
|
(r'ldap://servername.-so3:444', ValidationError, None),
|
||||||
|
])
|
||||||
|
def test_run_validators_valid(self, ldap_uri, exception, expected):
|
||||||
|
field = LDAPServerURIField()
|
||||||
|
if exception is None:
|
||||||
|
assert field.run_validators(ldap_uri) == expected
|
||||||
|
else:
|
||||||
|
with pytest.raises(exception):
|
||||||
|
field.run_validators(ldap_uri)
|
||||||
|
|||||||
Reference in New Issue
Block a user