Browse Source

Fix users in group and groups for user exact filters

List users in a group by name and list groups for user by name
throws HTTP 500 error in case of exact filters because filter_by
query used in case of exact filter is not able to filter using
'name' attribute.
This patch fixes this issue by replacing filter_by with filter query
in case of exact_filter similar to the inexact_filter query.

Closes-Bug: 1521772
Change-Id: Ib44513540370c0701a439f11a49e0dc338601fae
(cherry picked from commit ee2859a61a)
tags/8.1.0
Ankit Agrawal 3 years ago
parent
commit
016efe0e59

+ 13
- 14
keystone/common/sql/core.py View File

@@ -325,42 +325,41 @@ def _filter(model, query, hints):
325 325
         satisfied_filters.append(filter_)
326 326
         return query.filter(query_term)
327 327
 
328
-    def exact_filter(model, filter_, cumulative_filter_dict):
328
+    def exact_filter(model, query, filter_, satisfied_filters):
329 329
         """Applies an exact filter to a query.
330 330
 
331 331
         :param model: the table model in question
332
+        :param query: query to apply filters to
332 333
         :param dict filter_: describes this filter
333
-        :param dict cumulative_filter_dict: describes the set of exact filters
334
-                                            built up so far
335
-
334
+        :param list satisfied_filters: filter_ will be added if it is
335
+                                       satisfied.
336
+        :returns query: query updated to add any exact filters we could
337
+                        satisfy
336 338
         """
337 339
         key = filter_['name']
338 340
 
339 341
         col = getattr(model, key)
340 342
         if isinstance(col.property.columns[0].type, sql.types.Boolean):
341
-            cumulative_filter_dict[key] = (
342
-                utils.attr_as_boolean(filter_['value']))
343
+            filter_val = utils.attr_as_boolean(filter_['value'])
343 344
         else:
344 345
             _WontMatch.check(filter_['value'], col)
345
-            cumulative_filter_dict[key] = filter_['value']
346
+            filter_val = filter_['value']
347
+
348
+        satisfied_filters.append(filter_)
349
+        return query.filter(col == filter_val)
346 350
 
347 351
     try:
348
-        filter_dict = {}
349 352
         satisfied_filters = []
350 353
         for filter_ in hints.filters:
351 354
             if filter_['name'] not in model.attributes:
352 355
                 continue
353 356
             if filter_['comparator'] == 'equals':
354
-                exact_filter(model, filter_, filter_dict)
355
-                satisfied_filters.append(filter_)
357
+                query = exact_filter(model, query, filter_,
358
+                                     satisfied_filters)
356 359
             else:
357 360
                 query = inexact_filter(model, query, filter_,
358 361
                                        satisfied_filters)
359 362
 
360
-        # Apply any exact filters we built up
361
-        if filter_dict:
362
-            query = query.filter_by(**filter_dict)
363
-
364 363
         # Remove satisfied filters, then the caller will know remaining filters
365 364
         for filter_ in satisfied_filters:
366 365
             hints.filters.remove(filter_)

+ 0
- 2
keystone/tests/unit/test_backend.py View File

@@ -6727,7 +6727,6 @@ class FilterTests(filtering.FilterTests):
6727 6727
         self._delete_test_data('user', user_list)
6728 6728
         self._delete_test_data('group', group_list)
6729 6729
 
6730
-    @test_utils.wip('Waiting on bug #1521772 to be fixed')
6731 6730
     def test_groups_for_user_exact_filtered(self):
6732 6731
         """Test exact filters doesn't break groups_for_user listing."""
6733 6732
         group_list, user_list, hints = self._groups_for_user_data()
@@ -6804,7 +6803,6 @@ class FilterTests(filtering.FilterTests):
6804 6803
         self._delete_test_data('user', user_list)
6805 6804
         self._delete_entity('group')(group['id'])
6806 6805
 
6807
-    @test_utils.wip('Waiting on bug #1521772 to be fixed')
6808 6806
     def test_list_users_in_group_exact_filtered(self):
6809 6807
         user_list, group, hints = self._list_users_in_group_data()
6810 6808
         hints.add_filter('name', 'Arthur Rimbaud', comparator='equals')

+ 0
- 4
keystone/tests/unit/test_backend_ldap.py View File

@@ -3357,7 +3357,3 @@ class LdapFilterTests(test_backend.FilterTests, unit.TestCase):
3357 3357
         # The LDAP identity driver currently does not support filtering on the
3358 3358
         # listing users for a given group, so will fail this test.
3359 3359
         super(LdapFilterTests, self).test_list_users_in_group_exact_filtered()
3360
-
3361
-    @wip('Waiting on bug #1521772 to be fixed')
3362
-    def test_groups_for_user_exact_filtered(self):
3363
-        super(LdapFilterTests, self).test_groups_for_user_exact_filtered()

Loading…
Cancel
Save