Fix VIM register API 500 error issue

If user passes non-dict or empty dict to the auth_cred and
vim_project parameters, then tacker returns 500 error.

This patch fixes the issue by adding a new validator type
"dict_not_empty" and using at the required places. Now, if
users passes non-dict or empty dict to VIM Register API,
then it will return 400(BadRequest) error.

Change-Id: Ic144df6af606286ed3eea7f7071f061bceffaa7f
This commit is contained in:
openstack 2018-08-01 14:07:29 +05:30
parent 210d8f93bb
commit 66677ea869
4 changed files with 52 additions and 2 deletions

View File

@ -449,6 +449,16 @@ def _validate_dict_or_nodata(data, key_specs=None):
return _validate_dict(data, key_specs)
def _validate_dict_not_empty(data, key_specs=None):
if not isinstance(data, dict) or len(data) == 0:
msg = _("'%s' is not a valid dictionary or it is an"
" empty dictionary") % data
LOG.debug(msg)
return msg
else:
return _validate_dict(data, key_specs)
def _validate_non_negative(data, valid_values=None):
try:
data = int(data)
@ -553,6 +563,7 @@ validators = {'type:dict': _validate_dict,
'type:dict_or_none': _validate_dict_or_none,
'type:dict_or_empty': _validate_dict_or_empty,
'type:dict_or_nodata': _validate_dict_or_nodata,
'type:dict_not_empty': _validate_dict_not_empty,
'type:fixed_ips': _validate_fixed_ips,
'type:hostroutes': _validate_hostroutes,
'type:ip_address': _validate_ip_address,

View File

@ -312,13 +312,13 @@ RESOURCE_ATTRIBUTE_MAP = {
'auth_cred': {
'allow_post': True,
'allow_put': True,
'validate': {'type:dict_or_nodata': None},
'validate': {'type:dict_not_empty': None},
'is_visible': True,
},
'vim_project': {
'allow_post': True,
'allow_put': True,
'validate': {'type:dict_or_nodata': None},
'validate': {'type:dict_not_empty': None},
'is_visible': True,
},
'name': {

View File

@ -137,3 +137,33 @@ class VIMCreateTestCase(base.TestCase):
self.assertRaises(exc.HTTPBadRequest,
self.controller.create,
request, vim_dict)
@ddt.data('', 'testing', {})
def test_create_vim_with_invalid_auth_cred(self, value):
vim_dict = get_vim_config()
vim_dict['vim']['auth_cred'] = value
request = wsgi.Request.blank("/vims.json", method='POST',
headers={'Content-Type': "application/json"})
request.environ['tacker.context'] = self.fake_admin_context()
msg = ("Invalid input for auth_cred. Reason: '%s' is "
"not a valid dictionary or it is an empty"
" dictionary.") % vim_dict['vim']['auth_cred']
exp = self.assertRaises(exc.HTTPBadRequest,
self.controller.create,
request, vim_dict)
self.assertEqual(msg, six.text_type(exp))
@ddt.data('', 'testing', {})
def test_create_vim_invalid_vim_project(self, value):
vim_dict = get_vim_config()
vim_dict['vim']['vim_project'] = value
request = wsgi.Request.blank("/vims.json", method='POST',
headers={'Content-Type': "application/json"})
request.environ['tacker.context'] = self.fake_admin_context()
msg = ("Invalid input for vim_project. Reason: '%s' is"
" not a valid dictionary or it is an empty"
" dictionary.") % vim_dict['vim']['vim_project']
exp = self.assertRaises(exc.HTTPBadRequest,
self.controller.create,
request, vim_dict)
self.assertEqual(msg, six.text_type(exp))

View File

@ -680,6 +680,15 @@ class TestAttributes(base.BaseTestCase):
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_dict_or_not_empty(self):
dictionary, constraints = self._construct_dict_and_constraints()
msg = attributes._validate_dict_not_empty({}, constraints)
self.assertEqual(msg, u"'{}' is not a valid dictionary or it is"
u" an empty dictionary")
msg = attributes._validate_dict_not_empty("", constraints)
self.assertEqual(msg, u"'' is not a valid dictionary or it is an"
u" empty dictionary")
def test_validate_non_negative(self):
for value in (-1, '-2'):
self.assertEqual("'%s' should be non-negative" % value,