Use config opt value to determine import methods

Instead of validating import requests by checking the provided
import-method against a hard-coded list, use the values in the
appropriate configuration option.

Change-Id: Iefac190a4adf5f08df538e04db3e07e261ad1bd9
Closes-bug: #1754634
This commit is contained in:
Brian Rosmaita 2018-03-09 08:06:47 -05:00
parent f119fc05bb
commit e1738033df
2 changed files with 22 additions and 8 deletions

View File

@ -817,7 +817,7 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
except KeyError:
msg = _("Import request requires a 'name' field.")
raise webob.exc.HTTPBadRequest(explanation=msg)
if method_name not in ['glance-direct', 'web-download']:
if method_name not in CONF.enabled_import_methods:
msg = _("Unknown import method name '%s'.") % method_name
raise webob.exc.HTTPBadRequest(explanation=msg)

View File

@ -3158,10 +3158,13 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
def test_image_import(self):
self.config(enable_image_import=True)
# Bug 1754634: make sure that what's considered valid
# is determined by the config option
self.config(enabled_import_methods=['party-time'])
request = unit_test_utils.get_fake_request()
import_body = {
"method": {
"name": "glance-direct"
"name": "party-time"
}
}
request.body = jsonutils.dump_as_bytes(import_body)
@ -3202,18 +3205,29 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
self.deserializer.import_image,
request)
def test_import_image_invalid_import_method(self):
self.config(enable_image_import=True)
def _get_request_for_method(self, method_name):
request = unit_test_utils.get_fake_request()
import_body = {
"method": {
"name": "abcd"
"name": method_name
}
}
request.body = jsonutils.dump_as_bytes(import_body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.deserializer.import_image,
request)
return request
KNOWN_IMPORT_METHODS = ['glance-direct', 'web-download']
def test_import_image_invalid_import_method(self):
self.config(enable_image_import=True)
# Bug 1754634: make sure that what's considered valid
# is determined by the config option. So put known bad
# name in config, and known good name in request
self.config(enabled_import_methods=['bad-method-name'])
for m in self.KNOWN_IMPORT_METHODS:
request = self._get_request_for_method(m)
self.assertRaises(webob.exc.HTTPBadRequest,
self.deserializer.import_image,
request)
class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase):