Debian Codegraph @

neutron_lib/api/validators/dns.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import re
14
15 from oslo_config import cfg
16
17 from neutron_lib._i18n import _
18 from neutron_lib.api import validators
19 from neutron_lib import constants
20 from neutron_lib.db import constants as db_constants
21
22
23 def _validate_dns_format(data, max_len=db_constants.FQDN_FIELD_SIZE):
24 # NOTE: An individual name regex instead of an entire FQDN was used
25 # because its easier to make correct. The logic should validate that the
26 # dns_name matches RFC 1123 (section 2.1) and RFC 952.
27 if not data:
28 return
29 try:
30 # A trailing period is allowed to indicate that a name is fully
31 # qualified per RFC 1034 (page 7).
32 trimmed = data[:-1] if data.endswith('.') else data
33 if len(trimmed) > max_len:
34 raise TypeError(
35 _("'%(trimmed)s' exceeds the %(maxlen)s character FQDN "
36 "limit") % {'trimmed': trimmed, 'maxlen': max_len})
37 labels = trimmed.split('.')
38 for label in labels:
39 if not label:
40 raise TypeError(_("Encountered an empty component"))
41 if label.endswith('-') or label.startswith('-'):
42 raise TypeError(
43 _("Name '%s' must not start or end with a hyphen") % label)
44 if not re.match(constants.DNS_LABEL_REGEX, label):
45 raise TypeError(
46 _("Name '%s' must be 1-63 characters long, each of "
47 "which can only be alphanumeric or a hyphen") % label)
48 # RFC 1123 hints that a TLD can't be all numeric. last is a TLD if
49 # it's an FQDN.
50 if len(labels) > 1 and re.match("^[0-9]+$", labels[-1]):
51 raise TypeError(
52 _("TLD '%s' must not be all numeric") % labels[-1])
53 except TypeError as e:
54 msg = _("'%(data)s' not a valid PQDN or FQDN. Reason: %(reason)s") % {
55 'data': data, 'reason': e}
56 return msg
57
58
59 def _validate_dns_name_with_dns_domain(request_dns_name, dns_domain):
60 # If a PQDN was passed, make sure the FQDN that will be generated is of
61 # legal size
62 higher_labels = dns_domain
63 if dns_domain:
64 higher_labels = f'.{dns_domain}'
65 higher_labels_len = len(higher_labels)
66 dns_name_len = len(request_dns_name)
67 if not request_dns_name.endswith('.'):
68 if dns_name_len + higher_labels_len > db_constants.FQDN_FIELD_SIZE:
69 msg = _("The dns_name passed is a PQDN and its size is "
70 "'%(dns_name_len)s'. The dns_domain option in "
71 "neutron.conf is set to %(dns_domain)s, with a "
72 "length of '%(higher_labels_len)s'. When the two are "
73 "concatenated to form a FQDN (with a '.' at the end), "
74 "the resulting length exceeds the maximum size "
75 "of '%(fqdn_max_len)s'"
76 ) % {'dns_name_len': dns_name_len,
77 'dns_domain': cfg.CONF.dns_domain,
78 'higher_labels_len': higher_labels_len,
79 'fqdn_max_len': db_constants.FQDN_FIELD_SIZE}
80 return msg
81 return
82
83 # A FQDN was passed
84 if (dns_name_len <= higher_labels_len or not
85 request_dns_name.endswith(higher_labels)):
86 msg = _("The dns_name passed is a FQDN. Its higher level labels "
87 "must be equal to the dns_domain option in neutron.conf, "
88 "that has been set to '%(dns_domain)s'. It must also "
89 "include one or more valid DNS labels to the left "
90 "of '%(dns_domain)s'") % {'dns_domain':
91 cfg.CONF.dns_domain}
92 return msg
93
94
95 def _get_dns_domain_config():
96 if not cfg.CONF.dns_domain:
97 return ''
98 if cfg.CONF.dns_domain.endswith('.'):
99 return cfg.CONF.dns_domain
100 return f'{cfg.CONF.dns_domain}.'
101
102
103 def _get_request_dns_name(dns_name):
104 dns_domain = _get_dns_domain_config()
105 if (dns_domain and dns_domain != constants.DNS_DOMAIN_DEFAULT):
106 # If CONF.dns_domain is the default value 'openstacklocal',
107 # neutron don't let the user to assign dns_name to ports
108 return dns_name
109 return ''
110
111
112 def validate_dns_name(data, max_len=db_constants.FQDN_FIELD_SIZE):
113 """Validate DNS name.
114
115 This method validates dns name and also needs to have dns_domain in config
116 because this may call a method which uses the config.
117
118 :param data: The data to validate.
119 :param max_len: An optional cap on the length of the string.
120 :returns: None if data is valid, otherwise a human readable message
121 indicating why validation failed.
122 """
123 msg = _validate_dns_format(data, max_len)
124 if msg:
125 return msg
126
127 request_dns_name = _get_request_dns_name(data)
128 if request_dns_name:
129 dns_domain = _get_dns_domain_config()
130 msg = _validate_dns_name_with_dns_domain(request_dns_name, dns_domain)
131 if msg:
132 return msg
133
134
135 def validate_fip_dns_name(data, max_len=db_constants.FQDN_FIELD_SIZE):
136 """Validate DNS name for floating IP.
137
138 :param data: The data to validate.
139 :param max_len: An optional cap on the length of the string.
140 :returns: None if data is valid, otherwise a human readable message
141 indicating why validation failed.
142 """
143 msg = validators.validate_string(data)
144 if msg:
145 return msg
146 if not data:
147 return
148 if data.endswith('.'):
149 msg = _("'%s' is a FQDN. It should be a relative domain name") % data
150 return msg
151 msg = _validate_dns_format(data, max_len)
152 if msg:
153 return msg
154 length = len(data)
155 if length > max_len - 3:
156 msg = _("'%(data)s' contains %(length)s characters. Adding a "
157 "domain name will cause it to exceed the maximum length "
158 "of a FQDN of '%(max_len)s'") % {"data": data,
159 "length": length,
160 "max_len": max_len}
161 return msg
162
163
164 def validate_dns_domain(data, max_len=db_constants.FQDN_FIELD_SIZE):
165 """Validate DNS domain.
166
167 :param data: The data to validate.
168 :param max_len: An optional cap on the length of the string.
169 :returns: None if data is valid, otherwise a human readable message
170 indicating why validation failed.
171 """
172 msg = validators.validate_string(data)
173 if msg:
174 return msg
175 if not data:
176 return
177 if not data.endswith('.'):
178 msg = _("'%s' is not a FQDN") % data
179 return msg
180 msg = _validate_dns_format(data, max_len)
181 if msg:
182 return msg
183 length = len(data)
184 if length > max_len - 2:
185 msg = _("'%(data)s' contains %(length)s characters. Adding a "
186 "sub-domain will cause it to exceed the maximum length of a "
187 "FQDN of '%(max_len)s'") % {"data": data,
188 "length": length,
189 "max_len": max_len}
190 return msg