diff --git a/nova/api/openstack/compute/agents.py b/nova/api/openstack/compute/agents.py index d6247ddfb4de..9e7782b348dd 100644 --- a/nova/api/openstack/compute/agents.py +++ b/nova/api/openstack/compute/agents.py @@ -21,6 +21,7 @@ from nova.api.openstack import wsgi from nova.api import validation from nova import exception from nova import objects +from nova import utils ALIAS = "os-agents" @@ -88,6 +89,11 @@ class AgentController(wsgi.Controller): md5hash = para['md5hash'] version = para['version'] + try: + utils.validate_integer(id, 'id') + except exception.InvalidInput as exc: + raise webob.exc.HTTPBadRequest(explanation=exc.format_message()) + agent = objects.Agent(context=context, id=id) agent.obj_reset_changes() agent.version = version @@ -109,13 +115,18 @@ class AgentController(wsgi.Controller): # TODO(oomichi): Here should be 204(No Content) instead of 200 by v2.1 # +microversions because the resource agent has been deleted completely # when returning a response. - @extensions.expected_errors(404) + @extensions.expected_errors((400, 404)) @wsgi.response(200) def delete(self, req, id): """Deletes an existing agent build.""" context = req.environ['nova.context'] authorize(context) + try: + utils.validate_integer(id, 'id') + except exception.InvalidInput as exc: + raise webob.exc.HTTPBadRequest(explanation=exc.format_message()) + try: agent = objects.Agent(context=context, id=id) agent.destroy() diff --git a/nova/api/openstack/compute/legacy_v2/contrib/agents.py b/nova/api/openstack/compute/legacy_v2/contrib/agents.py index 296e340fef37..7e8fda2295ca 100644 --- a/nova/api/openstack/compute/legacy_v2/contrib/agents.py +++ b/nova/api/openstack/compute/legacy_v2/contrib/agents.py @@ -89,6 +89,7 @@ class AgentController(object): raise webob.exc.HTTPBadRequest(explanation=msg) try: + utils.validate_integer(id, 'id') utils.check_string_length(url, 'url', max_length=255) utils.check_string_length(md5hash, 'md5hash', max_length=255) utils.check_string_length(version, 'version', max_length=255) @@ -102,9 +103,6 @@ class AgentController(object): agent.url = url agent.md5hash = md5hash agent.save() - except ValueError as ex: - msg = _("Invalid request body: %s") % ex - raise webob.exc.HTTPBadRequest(explanation=msg) except exception.AgentBuildNotFound as ex: raise webob.exc.HTTPNotFound(explanation=ex.format_message()) @@ -123,6 +121,11 @@ class AgentController(object): # NOTE(alex_xu): back-compatible with db layer hard-code admin # permission checks. nova_context.require_admin_context(context) + try: + utils.validate_integer(id, 'id') + except exception.InvalidInput as exc: + raise webob.exc.HTTPBadRequest(explanation=exc.format_message()) + try: agent = objects.Agent(context=context, id=id) agent.destroy() diff --git a/nova/tests/unit/api/openstack/compute/test_agents.py b/nova/tests/unit/api/openstack/compute/test_agents.py index 4f8214014c12..297a895acca3 100644 --- a/nova/tests/unit/api/openstack/compute/test_agents.py +++ b/nova/tests/unit/api/openstack/compute/test_agents.py @@ -205,6 +205,10 @@ class AgentsTestV21(test.NoDBTestCase): self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete, self.req, 1) + def test_agents_delete_string_id(self): + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.delete, self.req, 'string_id') + def test_agents_list(self): res_dict = self.controller.index(self.req) agents_list = [{'hypervisor': 'kvm', 'os': 'win', @@ -298,6 +302,14 @@ class AgentsTestV21(test.NoDBTestCase): self.assertRaises(self.validation_error, self.controller.update, self.req, 1, body=body) + def test_agents_update_with_string_id(self): + body = {'para': {'version': '7.0', + 'url': 'http://example.com/path/to/resource', + 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.update, self.req, + 'string_id', body=body) + def _test_agents_update_with_invalid_length(self, key): body = {'para': {'version': '7.0', 'url': 'http://example.com/path/to/resource',