Skip to content

Commit

Permalink
[VNC API Server] Ignore malformed UUID in filters
Browse files Browse the repository at this point in the history
If filters that should contain UUID v4 string have some malformed
values, we ignore them and just return list for valid ones

Change-Id: I2995ad9cceb6b0254a7f98338c2e356cc3082eac
Closes-bug: #1612557
  • Loading branch information
Édouard Thuleau committed Aug 12, 2016
1 parent 6c83b2a commit 1a09f64
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
71 changes: 70 additions & 1 deletion src/config/api-server/tests/test_crud_basic.py
Expand Up @@ -1313,6 +1313,42 @@ def test_list_bulk_collection(self):
self.assertThat(set(vmi_uuids), Equals(set(ret_vmi_uuids)))
# end test_list_bulk_collection

def test_list_bulk_collection_with_malformed_filters(self):
obj_count = self._vnc_lib.POST_FOR_LIST_THRESHOLD + 1
vn_objs, _, _, _ = self._create_vn_ri_vmi()
vn_uuid = vn_objs[0].uuid
vn_uuids = [vn_uuid] +\
['bad-uuid'] * self._vnc_lib.POST_FOR_LIST_THRESHOLD

try:
results = self._vnc_lib.resource_list('virtual-network',
obj_uuids=vn_uuids)
self.assertEqual(len(results['virtual-networks']), 1)
self.assertEqual(results['virtual-networks'][0]['uuid'], vn_uuid)
except HttpError:
self.fail('Malformed object UUID filter was not ignored')

try:
results = self._vnc_lib.resource_list('routing-instance',
parent_id=vn_uuids,
detail=True)
self.assertEqual(len(results), 2)
for ri_obj in results:
self.assertEqual(ri_obj.parent_uuid, vn_uuid)
except HttpError:
self.fail('Malformed parent UUID filter was not ignored')

try:
results = self._vnc_lib.resource_list('virtual-machine-interface',
back_ref_id=vn_uuids,
detail=True)
self.assertEqual(len(results), 1)
vmi_obj = results[0]
self.assertEqual(vmi_obj.get_virtual_network_refs()[0]['uuid'],
vn_uuid)
except HttpError:
self.fail('Malformed back-ref UUID filter was not ignored')

def test_list_lib_api(self):
num_objs = 5
proj_obj = Project('%s-project' %(self.id()))
Expand Down Expand Up @@ -1522,9 +1558,42 @@ def fake_non_admin_request(orig_method, *args, **kwargs):
self.assertEqual(read_vn_dicts[0]['uuid'], vn1_obj.uuid)
self.assertEqual(read_vn_dicts[0]['is_shared'], True)
self.assertEqual(read_vn_dicts[0]['router_external'], False)

# end test_list_for_coverage

def test_list_with_malformed_filters(self):
vn_objs, _, _, _ = self._create_vn_ri_vmi()
vn_uuid = vn_objs[0].uuid
vn_uuids = [vn_uuid, 'bad-uuid']

try:
results = self._vnc_lib.resource_list('virtual-network',
obj_uuids=vn_uuids)
self.assertEqual(len(results['virtual-networks']), 1)
self.assertEqual(results['virtual-networks'][0]['uuid'], vn_uuid)
except HttpError:
self.fail('Malformed object UUID filter was not ignored')

try:
results = self._vnc_lib.resource_list('routing-instance',
parent_id=vn_uuids,
detail=True)
self.assertEqual(len(results), 2)
for ri_obj in results:
self.assertEqual(ri_obj.parent_uuid, vn_uuid)
except HttpError:
self.fail('Malformed parent UUID filter was not ignored')

try:
results = self._vnc_lib.resource_list('virtual-machine-interface',
back_ref_id=vn_uuids,
detail=True)
self.assertEqual(len(results), 1)
vmi_obj = results[0]
self.assertEqual(vmi_obj.get_virtual_network_refs()[0]['uuid'],
vn_uuid)
except HttpError:
self.fail('Malformed back-ref UUID filter was not ignored')

def test_create_with_wrong_type(self):
vn_obj = VirtualNetwork('%s-bad-prop-type' %(self.id()))
vn_obj.virtual_network_properties = 'foo' #VirtualNetworkType
Expand Down
15 changes: 5 additions & 10 deletions src/config/api-server/vnc_cfg_api_server.py
Expand Up @@ -979,11 +979,9 @@ def http_resource_list(self, obj_type):
parent_type = parent_class.object_type
parent_uuids = [self._db_conn.fq_name_to_uuid(parent_type, parent_fq_name)]
elif 'parent_id' in get_request().query:
parent_ids = get_request().query.parent_id.split(',')
parent_uuids = [str(uuid.UUID(p_uuid)) for p_uuid in parent_ids]
parent_uuids = get_request().query.parent_id.split(',')
if 'back_ref_id' in get_request().query:
back_ref_ids = get_request().query.back_ref_id.split(',')
back_ref_uuids = [str(uuid.UUID(b_uuid)) for b_uuid in back_ref_ids]
back_ref_uuids = get_request().query.back_ref_id.split(',')
if 'obj_uuids' in get_request().query:
obj_uuids = get_request().query.obj_uuids.split(',')

Expand Down Expand Up @@ -2473,20 +2471,17 @@ def list_bulk_collection_http_post(self):
"Bad Request, Unknown type %s in POST body" % (resource_type))

try:
parent_ids = get_request().json['parent_id'].split(',')
parent_uuids = [str(uuid.UUID(p_uuid)) for p_uuid in parent_ids]
parent_uuids = get_request().json['parent_id'].split(',')
except KeyError:
parent_uuids = None

try:
back_ref_ids = get_request().json['back_ref_id'].split(',')
back_ref_uuids = [str(uuid.UUID(b_uuid)) for b_uuid in back_ref_ids]
back_ref_uuids = get_request().json['back_ref_id'].split(',')
except KeyError:
back_ref_uuids = None

try:
obj_ids = get_request().json['obj_uuids'].split(',')
obj_uuids = [str(uuid.UUID(b_uuid)) for b_uuid in obj_ids]
obj_uuids = get_request().json['obj_uuids'].split(',')
except KeyError:
obj_uuids = None

Expand Down

0 comments on commit 1a09f64

Please sign in to comment.