From 4027bbecdd8cbaf85bb48f750401df33b7ba40af Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Thu, 10 Sep 2015 13:30:39 +0800 Subject: [PATCH] Check unknown event name when create external server event The InstanceExternalEvent use enum type for name field, but nova api didn't validate the name field, it leads to return 500 when unknown event recived. InstanceExternalEvent's name field turn to enum type in patch https://review.openstack.org/#/c/183131, although it merged after v2.1 API release, but this API is only used by openstack services. So it's ok for fixing this without Microversions Change-Id: I38a6e26d5b66d67645be50c1a907489c8a2d40fe Closes-Bug: #1494125 --- .../compute/legacy_v2/contrib/server_external_events.py | 4 ++++ .../api/openstack/compute/schemas/server_external_events.py | 6 ++++-- .../api/openstack/compute/test_server_external_events.py | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nova/api/openstack/compute/legacy_v2/contrib/server_external_events.py b/nova/api/openstack/compute/legacy_v2/contrib/server_external_events.py index 7a015759e94d..65ba052ad73d 100644 --- a/nova/api/openstack/compute/legacy_v2/contrib/server_external_events.py +++ b/nova/api/openstack/compute/legacy_v2/contrib/server_external_events.py @@ -60,6 +60,10 @@ class ServerExternalEventsController(wsgi.Controller): raise webob.exc.HTTPBadRequest( _('Invalid event status `%s\'') % status) + if client_event.get('name') not in external_event_obj.EVENT_NAMES: + raise webob.exc.HTTPBadRequest( + _('Invalid event name %s') % client_event.get('name')) + try: event.instance_uuid = client_event.pop('server_uuid') event.name = client_event.pop('name') diff --git a/nova/api/openstack/compute/schemas/server_external_events.py b/nova/api/openstack/compute/schemas/server_external_events.py index 792720c4c1ee..c8c7ebdc238b 100644 --- a/nova/api/openstack/compute/schemas/server_external_events.py +++ b/nova/api/openstack/compute/schemas/server_external_events.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from nova.api.validation import parameter_types from nova.objects import external_event as external_event_obj create = { @@ -26,7 +25,10 @@ create = { 'server_uuid': { 'type': 'string', 'format': 'uuid' }, - 'name': parameter_types.name, + 'name': { + 'type': 'string', + 'enum': external_event_obj.EVENT_NAMES + }, 'status': { 'type': 'string', 'enum': external_event_obj.EVENT_STATUSES, diff --git a/nova/tests/unit/api/openstack/compute/test_server_external_events.py b/nova/tests/unit/api/openstack/compute/test_server_external_events.py index 7b35c094993b..f2dddbfd3b93 100644 --- a/nova/tests/unit/api/openstack/compute/test_server_external_events.py +++ b/nova/tests/unit/api/openstack/compute/test_server_external_events.py @@ -147,6 +147,12 @@ class ServerExternalEventsTestV21(test.NoDBTestCase): self.assertRaises(self.invalid_error, self.api.create, self.req, body=body) + def test_create_unkown_events(self): + self.event_1['name'] = 'unkown_event' + body = {'events': self.event_1} + self.assertRaises(self.invalid_error, + self.api.create, self.req, body=body) + @mock.patch('nova.objects.instance.Instance.get_by_uuid', fake_get_by_uuid) class ServerExternalEventsTestV2(ServerExternalEventsTestV21):