Add oneline_string validators

Those new validators are used to validate extra_dhcp_opt's opt_name and
opt_value fields to not allow multi-line strings to be set there.

Related-Bug: #1939733
Change-Id: I4dc3a09847205a660dc966d8eabccb4946f9bbc6
This commit is contained in:
Slawek Kaplonski 2021-09-29 12:34:17 +02:00
parent f9b428667b
commit 1f4c4031ee
4 changed files with 97 additions and 2 deletions

View File

@ -33,9 +33,9 @@ EXTRA_DHCP_OPT_KEY_SPECS = [
'type:values': [4, 6],
'required': False}},
# key spec if opt_name not in _VALID_BLANK_EXTRA_DHCP_OPTS
{'opt_name': {'type:not_empty_string': DHCP_OPT_NAME_MAX_LEN,
{'opt_name': {'type:oneline_not_empty_string': DHCP_OPT_NAME_MAX_LEN,
'required': True},
'opt_value': {'type:not_empty_string_or_none':
'opt_value': {'type:oneline_not_empty_string_or_none':
DHCP_OPT_VALUE_MAX_LEN,
'required': True},
'ip_version': {'convert_to': converters.convert_to_int,

View File

@ -256,6 +256,36 @@ def validate_list_of_unique_strings(data, max_len=None):
return _validate_list_of_unique_strings(data, max_len=max_len)
def validate_oneline_not_empty_string(data, max_len=None):
"""Validate data is a non-empty string without newline character.
:param data: The data to validate.
:param max_len: An optional cap on the length of the string.
:returns: None if the data is a string without newline character,
otherwise a human readable message indicating why validation failed.
"""
msg = validate_not_empty_string(data, max_len=max_len)
if msg:
return msg
if len(data.splitlines()) > 1:
msg = _("Multi-line string is not allowed: '%s'") % data
LOG.debug(msg)
return msg
def validate_oneline_not_empty_string_or_none(data, max_len=None):
"""Validate data is a non-empty string without newline character or None.
:param data: The data to validate.
:param max_len: An optional cap on the length of the string data.
:returns: None if the data is None or a valid string without newline
character, otherwise a human readable message indicating why
validation failed.
"""
if data is not None:
return validate_oneline_not_empty_string(data, max_len=max_len)
def validate_boolean(data, valid_values=None):
"""Validate data is a python bool compatible object.
@ -1232,6 +1262,10 @@ validators = {'type:dict': validate_dict,
'type:not_empty_string': validate_not_empty_string,
'type:not_empty_string_or_none':
validate_not_empty_string_or_none,
'type:oneline_not_empty_string':
validate_oneline_not_empty_string,
'type:oneline_not_empty_string_or_none':
validate_oneline_not_empty_string_or_none,
'type:subnet': validate_subnet,
'type:subnet_list': validate_subnet_list,
'type:subnet_or_none': validate_subnet_or_none,

View File

@ -222,6 +222,59 @@ class TestAttributeValidation(base.BaseTestCase):
msg = validators.validate_list_of_unique_strings(data, None)
self.assertIsNone(msg)
def test_validate_oneline_not_empty_string(self):
data = "Test"
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertIsNone(msg)
data = "Test but this is too long"
max_len = 4
msg = validators.validate_oneline_not_empty_string(data, max_len)
self.assertEqual(
"'%s' exceeds maximum length of %s" % (data, max_len),
msg)
data = "First line\nsecond line"
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"Multi-line string is not allowed: '%s'" % data,
msg)
data = ""
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"'%s' Blank strings are not permitted" % data,
msg)
def test_validate_oneline_not_empty_string_or_none(self):
data = "Test"
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertIsNone(msg)
data = None
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertIsNone(msg)
data = "Test but this is too long"
max_len = 4
msg = validators.validate_oneline_not_empty_string_or_none(
data, max_len)
self.assertEqual(
"'%s' exceeds maximum length of %s" % (data, max_len),
msg)
data = "First line\nsecond line"
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertEqual(
"Multi-line string is not allowed: '%s'" % data,
msg)
data = ""
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"'%s' Blank strings are not permitted" % data,
msg)
def test_validate_boolean(self):
msg = validators.validate_boolean(True)
self.assertIsNone(msg)

View File

@ -0,0 +1,8 @@
---
features:
- |
New API validators ``validate_oneline_not_empty_string`` and
``validate_oneline_not_empty_string_or_none`` are added. Those validators
are now used to validate ``opt_name`` and ``opt_value`` fields of
the extra_dhcp_opt so strings with newline character are not valid for
dhcp extra option's name nor value.