Browse Source

Fix DeprecationWarnings for RequestContext.tenant/user usage

RequestContext.tenant and user fields are deprecated in favor
of project_id and user_id respectively.

This change modifies the glance.context.RequestContext constructor
to transition usage of tenant/user to project_id/user_id until
all tests are moved over to the new attributes. Runtime usage of
the old fiels is updated.

To prevent new code from using the deprecated fields, a warnings
filter is added which will make tests fail if they hit code using
the old fields.

Co-Authored-By: Abhishek Kekane <akekane@redhat.com>
Change-Id: I351380840308a24769ece93abc6d1a9a6d6aa06f
tags/18.0.0.0rc1
Matt Riedemann 3 months ago
parent
commit
88a8ad7823

+ 3
- 3
glance/api/common.py View File

@@ -76,8 +76,8 @@ def image_send_notification(bytes_written, expected_size, image_meta, request,
76 76
             'bytes_sent': bytes_written,
77 77
             'image_id': image_meta['id'],
78 78
             'owner_id': image_meta['owner'],
79
-            'receiver_tenant_id': context.tenant,
80
-            'receiver_user_id': context.user,
79
+            'receiver_tenant_id': context.project_id,
80
+            'receiver_user_id': context.user_id,
81 81
             'destination_ip': request.remote_addr,
82 82
         }
83 83
         if bytes_written != expected_size:
@@ -156,7 +156,7 @@ def check_quota(context, image_size, db_api, image_id=None):
156 156
     if remaining is None:
157 157
         return
158 158
 
159
-    user = getattr(context, 'user', '<unknown>')
159
+    user = getattr(context, 'user_id', '<unknown>')
160 160
 
161 161
     if image_size is None:
162 162
         # NOTE(jbresnah) When the image size is None it means that it is

+ 1
- 1
glance/api/v2/image_data.py View File

@@ -133,7 +133,7 @@ class ImageDataController(object):
133 133
                                 'keystone.token_info')['token']['roles']:
134 134
                             roles.append(role_info['name'])
135 135
                         refresher = trust_auth.TokenRefresher(user_plugin,
136
-                                                              cxt.tenant,
136
+                                                              cxt.project_id,
137 137
                                                               roles)
138 138
                     except Exception as e:
139 139
                         LOG.info(_LI("Unable to create trust: %s "

+ 11
- 2
glance/context.py View File

@@ -28,6 +28,15 @@ class RequestContext(context.RequestContext):
28 28
 
29 29
     def __init__(self, owner_is_tenant=True, service_catalog=None,
30 30
                  policy_enforcer=None, **kwargs):
31
+        # TODO(mriedem): Remove usage of user and tenant from old tests.
32
+        if 'tenant' in kwargs:
33
+            # Prefer project_id if passed, otherwise alias tenant as project_id
34
+            tenant = kwargs.pop('tenant')
35
+            kwargs['project_id'] = kwargs.get('project_id', tenant)
36
+        if 'user' in kwargs:
37
+            # Prefer user_id if passed, otherwise alias user as user_id
38
+            user = kwargs.pop('user')
39
+            kwargs['user_id'] = kwargs.get('user_id', user)
31 40
         super(RequestContext, self).__init__(**kwargs)
32 41
         self.owner_is_tenant = owner_is_tenant
33 42
         self.service_catalog = service_catalog
@@ -45,8 +54,8 @@ class RequestContext(context.RequestContext):
45 54
 
46 55
     def to_policy_values(self):
47 56
         pdict = super(RequestContext, self).to_policy_values()
48
-        pdict['user'] = self.user_id
49
-        pdict['tenant'] = self.project_id
57
+        pdict['user_id'] = self.user_id
58
+        pdict['project_id'] = self.project_id
50 59
         return pdict
51 60
 
52 61
     @classmethod

+ 2
- 2
glance/notifier.py View File

@@ -394,8 +394,8 @@ class ImageProxy(NotificationProxy, domain_proxy.Image):
394 394
             'bytes_sent': bytes_sent,
395 395
             'image_id': self.repo.image_id,
396 396
             'owner_id': self.repo.owner,
397
-            'receiver_tenant_id': self.context.tenant,
398
-            'receiver_user_id': self.context.user,
397
+            'receiver_tenant_id': self.context.project_id,
398
+            'receiver_user_id': self.context.user_id,
399 399
         }
400 400
 
401 401
     def _get_chunk_data_iterator(self, data, chunk_size=None):

+ 2
- 2
glance/registry/client/v1/api.py View File

@@ -153,8 +153,8 @@ def get_registry_client(cxt):
153 153
 
154 154
     if CONF.send_identity_headers:
155 155
         identity_headers = {
156
-            'X-User-Id': cxt.user or '',
157
-            'X-Tenant-Id': cxt.tenant or '',
156
+            'X-User-Id': cxt.user_id or '',
157
+            'X-Tenant-Id': cxt.project_id or '',
158 158
             'X-Roles': ','.join(cxt.roles),
159 159
             'X-Identity-Status': 'Confirmed',
160 160
             'X-Service-Catalog': jsonutils.dumps(cxt.service_catalog),

+ 5
- 5
glance/tests/functional/db/base.py View File

@@ -1580,7 +1580,7 @@ class TaskTests(test_utils.BaseTestCase):
1580 1580
         db_tests.reset_db(self.db_api)
1581 1581
 
1582 1582
     def build_task_fixtures(self):
1583
-        self.context.tenant = str(uuid.uuid4())
1583
+        self.context.project_id = str(uuid.uuid4())
1584 1584
         fixtures = [
1585 1585
             {
1586 1586
                 'owner': self.context.owner,
@@ -1816,7 +1816,7 @@ class TaskTests(test_utils.BaseTestCase):
1816 1816
 
1817 1817
     def test_task_create(self):
1818 1818
         task_id = str(uuid.uuid4())
1819
-        self.context.tenant = self.context.owner
1819
+        self.context.project_id = self.context.owner
1820 1820
         values = {
1821 1821
             'id': task_id,
1822 1822
             'owner': self.context.owner,
@@ -1834,7 +1834,7 @@ class TaskTests(test_utils.BaseTestCase):
1834 1834
 
1835 1835
     def test_task_create_with_all_task_info_null(self):
1836 1836
         task_id = str(uuid.uuid4())
1837
-        self.context.tenant = str(uuid.uuid4())
1837
+        self.context.project_id = str(uuid.uuid4())
1838 1838
         values = {
1839 1839
             'id': task_id,
1840 1840
             'owner': self.context.owner,
@@ -1856,7 +1856,7 @@ class TaskTests(test_utils.BaseTestCase):
1856 1856
         self.assertIsNone(task['message'])
1857 1857
 
1858 1858
     def test_task_update(self):
1859
-        self.context.tenant = str(uuid.uuid4())
1859
+        self.context.project_id = str(uuid.uuid4())
1860 1860
         result = {'foo': 'bar'}
1861 1861
         task_values = build_task_fixture(owner=self.context.owner,
1862 1862
                                          result=result)
@@ -1883,7 +1883,7 @@ class TaskTests(test_utils.BaseTestCase):
1883 1883
         self.assertGreater(task['updated_at'], task['created_at'])
1884 1884
 
1885 1885
     def test_task_update_with_all_task_info_null(self):
1886
-        self.context.tenant = str(uuid.uuid4())
1886
+        self.context.project_id = str(uuid.uuid4())
1887 1887
         task_values = build_task_fixture(owner=self.context.owner,
1888 1888
                                          input=None,
1889 1889
                                          result=None,

+ 4
- 4
glance/tests/functional/v2/registry_data_api.py View File

@@ -29,10 +29,10 @@ def patched_bulk_request(self, commands):
29 29
     # present)
30 30
     body = self._serializer.to_json(commands)
31 31
     headers = {"X-Identity-Status": "Confirmed", 'X-Roles': 'member'}
32
-    if self.context.user is not None:
33
-        headers['X-User-Id'] = self.context.user
34
-    if self.context.tenant is not None:
35
-        headers['X-Tenant-Id'] = self.context.tenant
32
+    if self.context.user_id is not None:
33
+        headers['X-User-Id'] = self.context.user_id
34
+    if self.context.project_id is not None:
35
+        headers['X-Tenant-Id'] = self.context.project_id
36 36
     response = super(RPCClient, self).do_request('POST',
37 37
                                                  self.base_path,
38 38
                                                  body,

+ 9
- 9
glance/tests/functional/v2/test_images.py View File

@@ -1653,7 +1653,7 @@ class TestImages(functional.FunctionalTest):
1653 1653
             "default": "",
1654 1654
             "add_image": "",
1655 1655
             "get_image": "",
1656
-            "modify_image": "tenant:%(owner)s",
1656
+            "modify_image": "project_id:%(owner)s",
1657 1657
             "upload_image": "",
1658 1658
             "get_image_location": "",
1659 1659
             "delete_image": "",
@@ -1744,7 +1744,7 @@ class TestImages(functional.FunctionalTest):
1744 1744
             "restricted":
1745 1745
             "not ('aki':%(container_format)s and role:_member_)",
1746 1746
             "download_image": "role:admin or rule:restricted",
1747
-            "add_member": "tenant:%(owner)s",
1747
+            "add_member": "project_id:%(owner)s",
1748 1748
         }
1749 1749
 
1750 1750
         self.set_policy_rules(rules)
@@ -1809,7 +1809,7 @@ class TestImages(functional.FunctionalTest):
1809 1809
             "context_is_admin": "role:admin",
1810 1810
             "default": "",
1811 1811
             "add_image": "",
1812
-            "get_image": "tenant:%(owner)s",
1812
+            "get_image": "project_id:%(owner)s",
1813 1813
             "modify_image": "",
1814 1814
             "upload_image": "",
1815 1815
             "get_image_location": "",
@@ -1858,8 +1858,8 @@ class TestImages(functional.FunctionalTest):
1858 1858
             "context_is_admin": "role:admin",
1859 1859
             "default": "",
1860 1860
             "add_image": "",
1861
-            "publicize_image": "tenant:%(owner)s",
1862
-            "get_image": "tenant:%(owner)s",
1861
+            "publicize_image": "project_id:%(owner)s",
1862
+            "get_image": "project_id:%(owner)s",
1863 1863
             "modify_image": "",
1864 1864
             "upload_image": "",
1865 1865
             "get_image_location": "",
@@ -1900,8 +1900,8 @@ class TestImages(functional.FunctionalTest):
1900 1900
             "context_is_admin": "role:admin",
1901 1901
             "default": "",
1902 1902
             "add_image": "",
1903
-            "communitize_image": "tenant:%(owner)s",
1904
-            "get_image": "tenant:%(owner)s",
1903
+            "communitize_image": "project_id:%(owner)s",
1904
+            "get_image": "project_id:%(owner)s",
1905 1905
             "modify_image": "",
1906 1906
             "upload_image": "",
1907 1907
             "get_image_location": "",
@@ -1942,8 +1942,8 @@ class TestImages(functional.FunctionalTest):
1942 1942
             "context_is_admin": "role:admin",
1943 1943
             "default": "",
1944 1944
             "add_image": "",
1945
-            "publicize_image": "tenant:%(owner)s",
1946
-            "get_image": "tenant:%(owner)s",
1945
+            "publicize_image": "project_id:%(owner)s",
1946
+            "get_image": "project_id:%(owner)s",
1947 1947
             "modify_image": "",
1948 1948
             "upload_image": "",
1949 1949
             "get_image_location": "",

+ 5
- 0
glance/tests/unit/fixtures.py View File

@@ -131,4 +131,9 @@ class WarningsFixture(pyfixtures.Fixture):
131 131
                     ' This key is deprecated. Please update your policy '
132 132
                     'file to use the standard policy values.')
133 133
 
134
+        # NOTE(mriedem): user/tenant is deprecated in oslo.context so don't
135
+        # let anything new use it
136
+        warnings.filterwarnings(
137
+            'error', message="Property '.*' has moved to '.*'")
138
+
134 139
         self.addCleanup(warnings.resetwarnings)

+ 6
- 6
glance/tests/unit/test_context_middleware.py View File

@@ -41,8 +41,8 @@ class TestContextMiddleware(base.IsolatedUnitTest):
41 41
         req = self._build_request()
42 42
         self._build_middleware().process_request(req)
43 43
         self.assertEqual('token1', req.context.auth_token)
44
-        self.assertEqual('user1', req.context.user)
45
-        self.assertEqual('tenant1', req.context.tenant)
44
+        self.assertEqual('user1', req.context.user_id)
45
+        self.assertEqual('tenant1', req.context.project_id)
46 46
         self.assertEqual(['role1', 'role2'], req.context.roles)
47 47
 
48 48
     def test_is_admin_flag(self):
@@ -93,8 +93,8 @@ class TestContextMiddleware(base.IsolatedUnitTest):
93 93
         middleware = self._build_middleware()
94 94
         middleware.process_request(req)
95 95
         self.assertIsNone(req.context.auth_token)
96
-        self.assertIsNone(req.context.user)
97
-        self.assertIsNone(req.context.tenant)
96
+        self.assertIsNone(req.context.user_id)
97
+        self.assertIsNone(req.context.project_id)
98 98
         self.assertEqual([], req.context.roles)
99 99
         self.assertFalse(req.context.is_admin)
100 100
         self.assertTrue(req.context.read_only)
@@ -141,8 +141,8 @@ class TestUnauthenticatedContextMiddleware(base.IsolatedUnitTest):
141 141
         req = webob.Request.blank('/')
142 142
         middleware.process_request(req)
143 143
         self.assertIsNone(req.context.auth_token)
144
-        self.assertIsNone(req.context.user)
145
-        self.assertIsNone(req.context.tenant)
144
+        self.assertIsNone(req.context.user_id)
145
+        self.assertIsNone(req.context.project_id)
146 146
         self.assertEqual([], req.context.roles)
147 147
         self.assertTrue(req.context.is_admin)
148 148
 

+ 4
- 4
glance/tests/unit/utils.py View File

@@ -230,9 +230,9 @@ class FakeStoreAPI(object):
230 230
             size = len(data.data.fd)
231 231
         if (current_store_size + size) > store_max_size:
232 232
             raise exception.StorageFull()
233
-        if context.user == USER2:
233
+        if context.user_id == USER2:
234 234
             raise exception.Forbidden()
235
-        if context.user == USER3:
235
+        if context.user_id == USER3:
236 236
             raise exception.StorageWriteDenied()
237 237
         self.data[image_id] = (data, size)
238 238
         checksum = 'Z'
@@ -253,9 +253,9 @@ class FakeStoreAPI(object):
253 253
             size = len(data.data.fd)
254 254
         if (current_store_size + size) > store_max_size:
255 255
             raise exception.StorageFull()
256
-        if context.user == USER2:
256
+        if context.user_id == USER2:
257 257
             raise exception.Forbidden()
258
-        if context.user == USER3:
258
+        if context.user_id == USER3:
259 259
             raise exception.StorageWriteDenied()
260 260
         self.data[image_id] = (data, size)
261 261
         checksum = 'Z'

+ 1
- 1
glance/tests/unit/v2/test_image_data_resource.py View File

@@ -403,7 +403,7 @@ class TestImagesController(base.StoreClearingUnitTest):
403 403
         # check that token has been correctly acquired and deleted
404 404
         mock_refresher.assert_called_once_with(
405 405
             request.environ['keystone.token_auth'],
406
-            request.context.tenant, ['FakeRole'])
406
+            request.context.project_id, ['FakeRole'])
407 407
         refresher.refresh_token.assert_called_once_with()
408 408
         refresher.release_resources.assert_called_once_with()
409 409
         self.assertEqual("fake_token", request.context.auth_token)

+ 2
- 2
glance/tests/unit/v2/test_images_resource.py View File

@@ -651,7 +651,7 @@ class TestImagesController(base.IsolatedUnitTest):
651 651
 
652 652
     def test_show_not_allowed(self):
653 653
         request = unit_test_utils.get_fake_request()
654
-        self.assertEqual(TENANT1, request.context.tenant)
654
+        self.assertEqual(TENANT1, request.context.project_id)
655 655
         self.assertRaises(webob.exc.HTTPNotFound,
656 656
                           self.controller.show, request, UUID4)
657 657
 
@@ -2669,7 +2669,7 @@ class TestImagesController(base.IsolatedUnitTest):
2669 2669
         request = unit_test_utils.get_fake_request()
2670 2670
         # NOTE(abhishekk): For coverage purpose setting tenant to
2671 2671
         # None. It is not expected to do in normal scenarios.
2672
-        request.context.tenant = None
2672
+        request.context.project_id = None
2673 2673
         with mock.patch.object(
2674 2674
                 glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
2675 2675
             mock_get.return_value = FakeImage(status='uploading')

+ 1
- 1
glance/tests/unit/v2/test_tasks_resource.py View File

@@ -288,7 +288,7 @@ class TestTasksController(test_utils.BaseTestCase):
288 288
 
289 289
     def test_get_not_allowed(self):
290 290
         request = unit_test_utils.get_fake_request()
291
-        self.assertEqual(TENANT1, request.context.tenant)
291
+        self.assertEqual(TENANT1, request.context.project_id)
292 292
         self.assertRaises(webob.exc.HTTPNotFound,
293 293
                           self.controller.get, request, UUID4)
294 294
 

Loading…
Cancel
Save