| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
from oslo_config import cfg |
| 16 |
from oslo_context import context as oslo_context |
| 17 |
from testtools import matchers |
| 18 |
|
| 19 |
from neutron_lib import context |
| 20 |
from neutron_lib.tests import _base |
| 21 |
|
| 22 |
|
| 23 |
class TestNeutronContext(_base.BaseTestCase): |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
db_api = 'neutron_lib.db.api.get_writer_session' |
| 28 |
self._db_api_session_patcher = mock.patch(db_api) |
| 29 |
self.db_api_session = self._db_api_session_patcher.start() |
| 30 |
|
| 31 |
def test_neutron_context_create_positional(self): |
| 32 |
ctx = context.Context('user_id', 'project_id') |
| 33 |
self.assertEqual('user_id', ctx.user_id) |
| 34 |
self.assertEqual('project_id', ctx.project_id) |
| 35 |
self.assertEqual('project_id', ctx.tenant_id) |
| 36 |
request_id = ctx.request_id |
| 37 |
if isinstance(request_id, bytes): |
| 38 |
request_id = request_id.decode('utf-8') |
| 39 |
self.assertThat(request_id, matchers.StartsWith('req-')) |
| 40 |
self.assertIsNone(ctx.user_name) |
| 41 |
self.assertIsNone(ctx.tenant_name) |
| 42 |
self.assertIsNone(ctx.project_name) |
| 43 |
self.assertIsNone(ctx.auth_token) |
| 44 |
|
| 45 |
def _test_neutron_context_create(self, project_id, tenant_id): |
| 46 |
p_id = project_id or tenant_id |
| 47 |
ctx = context.Context('user_id', project_id=project_id, |
| 48 |
tenant_id=tenant_id) |
| 49 |
self.assertEqual('user_id', ctx.user_id) |
| 50 |
self.assertEqual(p_id, ctx.project_id) |
| 51 |
self.assertEqual(p_id, ctx.tenant_id) |
| 52 |
request_id = ctx.request_id |
| 53 |
if isinstance(request_id, bytes): |
| 54 |
request_id = request_id.decode('utf-8') |
| 55 |
self.assertThat(request_id, matchers.StartsWith('req-')) |
| 56 |
self.assertIsNone(ctx.user_name) |
| 57 |
self.assertIsNone(ctx.tenant_name) |
| 58 |
self.assertIsNone(ctx.project_name) |
| 59 |
self.assertIsNone(ctx.auth_token) |
| 60 |
|
| 61 |
def test_neutron_context_create(self): |
| 62 |
self._test_neutron_context_create('project_id', None) |
| 63 |
|
| 64 |
def test_neutron_context_create_tenant_id(self): |
| 65 |
self._test_neutron_context_create(None, 'tenant_id') |
| 66 |
|
| 67 |
def _test_neutron_context_getter_setter(self, project_id, tenant_id): |
| 68 |
p_id = project_id or tenant_id |
| 69 |
ctx = context.Context('Anakin', project_id=project_id, |
| 70 |
tenant_id=tenant_id) |
| 71 |
self.assertEqual('Anakin', ctx.user_id) |
| 72 |
self.assertEqual(p_id, ctx.tenant_id) |
| 73 |
ctx.user_id = 'Darth' |
| 74 |
ctx.project_id = 'Vader' |
| 75 |
self.assertEqual('Darth', ctx.user_id) |
| 76 |
self.assertEqual('Vader', ctx.tenant_id) |
| 77 |
self.assertEqual('Vader', ctx.project_id) |
| 78 |
|
| 79 |
def test_neutron_context_getter_setter(self): |
| 80 |
self._test_neutron_context_getter_setter('Skywalker', None) |
| 81 |
|
| 82 |
def test_neutron_context_getter_setter_tenant_id(self): |
| 83 |
self._test_neutron_context_getter_setter(None, 'Skywalker') |
| 84 |
|
| 85 |
def _test_neutron_context_create_with_name( |
| 86 |
self, project_name, tenant_name): |
| 87 |
project_name = project_name or tenant_name |
| 88 |
ctx = context.Context('user_id', 'project_id', |
| 89 |
tenant_name=tenant_name, user_name='user_name', |
| 90 |
project_name=project_name) |
| 91 |
# Check name is set |
| 92 |
self.assertEqual('user_name', ctx.user_name) |
| 93 |
self.assertEqual(project_name, ctx.tenant_name) |
| 94 |
self.assertEqual(project_name, ctx.project_name) |
| 95 |
# Check user/tenant contains its ID even if user/tenant_name is passed |
| 96 |
self.assertEqual('user_id', ctx.user_id) |
| 97 |
self.assertEqual('project_id', ctx.project_id) |
| 98 |
self.assertEqual('project_id', ctx.tenant_id) |
| 99 |
|
| 100 |
def test_neutron_context_create_with_name(self): |
| 101 |
self._test_neutron_context_create_with_name('project_name', None) |
| 102 |
|
| 103 |
def test_neutron_context_create_with_name_tenant_name(self): |
| 104 |
self._test_neutron_context_create_with_name(None, 'tenant_name') |
| 105 |
|
| 106 |
def test_neutron_context_create_with_request_id(self): |
| 107 |
ctx = context.Context('user_id', 'project_id', request_id='req_id_xxx') |
| 108 |
self.assertEqual('req_id_xxx', ctx.request_id) |
| 109 |
|
| 110 |
def test_neutron_context_create_with_timestamp(self): |
| 111 |
now = "Right Now!" |
| 112 |
ctx = context.Context('user_id', 'project_id', timestamp=now) |
| 113 |
self.assertEqual(now, ctx.timestamp) |
| 114 |
|
| 115 |
def test_neutron_context_create_is_advsvc(self): |
| 116 |
ctx = context.Context('user_id', 'project_id', is_advsvc=True) |
| 117 |
self.assertFalse(ctx.is_admin) |
| 118 |
self.assertTrue(ctx.is_advsvc) |
| 119 |
self.assertFalse(ctx.has_global_access) |
| 120 |
self.assertTrue(ctx.can_set_project_id) |
| 121 |
|
| 122 |
def test_neutron_context_create_is_service_role(self): |
| 123 |
ctx = context.Context('user_id', 'project_id', roles=['service']) |
| 124 |
self.assertFalse(ctx.is_admin) |
| 125 |
self.assertTrue(ctx.is_service_role) |
| 126 |
self.assertFalse(ctx.has_global_access) |
| 127 |
self.assertTrue(ctx.can_set_project_id) |
| 128 |
|
| 129 |
def test_neutron_context_create_with_auth_token(self): |
| 130 |
ctx = context.Context('user_id', 'project_id', |
| 131 |
auth_token='auth_token_xxx') |
| 132 |
self.assertEqual('auth_token_xxx', ctx.auth_token) |
| 133 |
|
| 134 |
def test_neutron_context_from_dict(self): |
| 135 |
owner = {'user_id': 'Luke', 'project_id': 'Skywalker'} |
| 136 |
ctx = context.Context.from_dict(owner) |
| 137 |
self.assertEqual(owner['user_id'], ctx.user_id) |
| 138 |
self.assertEqual(owner['project_id'], ctx.project_id) |
| 139 |
self.assertEqual(owner['project_id'], ctx.tenant_id) |
| 140 |
|
| 141 |
def _test_neutron_context_from_dict_validate( |
| 142 |
self, project_id, project_name, tenant_id, tenant_name): |
| 143 |
project_id = project_id or tenant_id |
| 144 |
project_name = project_name or tenant_name |
| 145 |
ctx = context.Context(user_id='user_id1', |
| 146 |
user_name='user', |
| 147 |
project_id=project_id, |
| 148 |
project_name=project_name, |
| 149 |
tenant_id=tenant_id, |
| 150 |
tenant_name=tenant_name, |
| 151 |
request_id='request_id1', |
| 152 |
auth_token='auth_token1') |
| 153 |
values = {'user_id': 'user_id1', |
| 154 |
'user_name': 'user', |
| 155 |
'project_id': project_id, |
| 156 |
'project_name': project_name, |
| 157 |
'tenant_id': project_id, |
| 158 |
'tenant_name': project_name, |
| 159 |
'request_id': 'request_id1', |
| 160 |
'auth_token': 'auth_token1'} |
| 161 |
ctx2 = context.Context.from_dict(values) |
| 162 |
|
| 163 |
ctx_dict = ctx.to_dict() |
| 164 |
ctx_dict.pop('timestamp') |
| 165 |
ctx2_dict = ctx2.to_dict() |
| 166 |
ctx2_dict.pop('timestamp') |
| 167 |
self.assertEqual(ctx_dict, ctx2_dict) |
| 168 |
|
| 169 |
def test_neutron_context_from_dict_validate(self): |
| 170 |
self._test_neutron_context_from_dict_validate( |
| 171 |
'project_id', 'project_name', None, None) |
| 172 |
|
| 173 |
def test_neutron_context_from_dict_validate_tenant(self): |
| 174 |
self._test_neutron_context_from_dict_validate( |
| 175 |
None, None, 'tenant_id', 'tenant_name') |
| 176 |
|
| 177 |
def _test_neutron_context_to_dict(self, project_id, tenant_id): |
| 178 |
project_id = project_id or tenant_id |
| 179 |
ctx = context.Context('user_id', project_id, tenant_id=tenant_id) |
| 180 |
ctx_dict = ctx.to_dict() |
| 181 |
self.assertEqual('user_id', ctx_dict['user_id']) |
| 182 |
self.assertEqual(project_id, ctx_dict['project_id']) |
| 183 |
self.assertEqual(project_id, ctx_dict['tenant_id']) |
| 184 |
self.assertEqual(ctx.request_id, ctx_dict['request_id']) |
| 185 |
self.assertEqual('user_id', ctx_dict['user']) |
| 186 |
self.assertIsNone(ctx_dict['user_name']) |
| 187 |
self.assertIsNone(ctx_dict['tenant_name']) |
| 188 |
self.assertIsNone(ctx_dict['project_name']) |
| 189 |
self.assertIsNone(ctx_dict['auth_token']) |
| 190 |
|
| 191 |
def test_neutron_context_to_dict(self): |
| 192 |
self._test_neutron_context_to_dict('project_id', None) |
| 193 |
|
| 194 |
def test_neutron_context_to_dict_tenant(self): |
| 195 |
self._test_neutron_context_to_dict(None, 'tenant_id') |
| 196 |
|
| 197 |
def _test_neutron_context_to_dict_with_name( |
| 198 |
self, project_name, tenant_name): |
| 199 |
project_name = project_name or tenant_name |
| 200 |
ctx = context.Context('user_id', 'project_id', |
| 201 |
project_name=project_name, |
| 202 |
tenant_name=tenant_name, |
| 203 |
user_name='user_name') |
| 204 |
ctx_dict = ctx.to_dict() |
| 205 |
self.assertEqual('user_name', ctx_dict['user_name']) |
| 206 |
self.assertEqual(project_name, ctx_dict['tenant_name']) |
| 207 |
self.assertEqual(project_name, ctx_dict['project_name']) |
| 208 |
|
| 209 |
def test_neutron_context_to_dict_with_name(self): |
| 210 |
self._test_neutron_context_to_dict_with_name('project_name', None) |
| 211 |
|
| 212 |
def test_neutron_context_to_dict_with_name_tenant(self): |
| 213 |
self._test_neutron_context_to_dict_with_name(None, 'tenant_name') |
| 214 |
|
| 215 |
def test_neutron_context_to_dict_with_auth_token(self): |
| 216 |
ctx = context.Context('user_id', 'project_id', |
| 217 |
auth_token='auth_token_xxx') |
| 218 |
ctx_dict = ctx.to_dict() |
| 219 |
self.assertEqual('auth_token_xxx', ctx_dict['auth_token']) |
| 220 |
|
| 221 |
def test_neutron_context_admin_to_dict(self): |
| 222 |
self.db_api_session.return_value = 'fakesession' |
| 223 |
ctx = context.get_admin_context() |
| 224 |
ctx_dict = ctx.to_dict() |
| 225 |
self.assertIsNone(ctx_dict['user_id']) |
| 226 |
self.assertIsNone(ctx_dict['project_id']) |
| 227 |
self.assertIsNone(ctx_dict['tenant_id']) |
| 228 |
self.assertIsNone(ctx_dict['auth_token']) |
| 229 |
self.assertTrue(ctx_dict['is_admin']) |
| 230 |
self.assertTrue(ctx_dict['has_global_access']) |
| 231 |
self.assertTrue(ctx_dict['can_set_project_id']) |
| 232 |
self.assertIn('admin', ctx_dict['roles']) |
| 233 |
self.assertIsNotNone(ctx.session) |
| 234 |
self.assertNotIn('session', ctx_dict) |
| 235 |
|
| 236 |
def test_neutron_context_admin_without_session_to_dict(self): |
| 237 |
ctx = context.get_admin_context_without_session() |
| 238 |
ctx_dict = ctx.to_dict() |
| 239 |
self.assertIsNone(ctx_dict['user_id']) |
| 240 |
self.assertIsNone(ctx_dict['project_id']) |
| 241 |
self.assertIsNone(ctx_dict['tenant_id']) |
| 242 |
self.assertIsNone(ctx_dict['auth_token']) |
| 243 |
self.assertIn('admin', ctx_dict['roles']) |
| 244 |
self.assertFalse(hasattr(ctx, 'session')) |
| 245 |
|
| 246 |
def test_neutron_context_elevated_retains_request_id(self): |
| 247 |
expected_roles = ['admin', 'member', 'reader'] |
| 248 |
ctx = context.Context('user_id', 'project_id') |
| 249 |
self.assertFalse(ctx.is_admin) |
| 250 |
req_id_before = ctx.request_id |
| 251 |
|
| 252 |
elevated_ctx = ctx.elevated() |
| 253 |
self.assertTrue(elevated_ctx.is_admin) |
| 254 |
self.assertEqual(req_id_before, elevated_ctx.request_id) |
| 255 |
for expected_role in expected_roles: |
| 256 |
self.assertIn(expected_role, elevated_ctx.roles) |
| 257 |
|
| 258 |
def test_neutron_context_elevated_idempotent(self): |
| 259 |
ctx = context.Context('user_id', 'project_id') |
| 260 |
expected_roles = ['admin', 'member', 'reader'] |
| 261 |
self.assertFalse(ctx.is_admin) |
| 262 |
elevated_ctx = ctx.elevated() |
| 263 |
self.assertTrue(elevated_ctx.is_admin) |
| 264 |
for expected_role in expected_roles: |
| 265 |
self.assertIn(expected_role, elevated_ctx.roles) |
| 266 |
elevated2_ctx = elevated_ctx.elevated() |
| 267 |
self.assertTrue(elevated2_ctx.is_admin) |
| 268 |
for expected_role in expected_roles: |
| 269 |
self.assertIn(expected_role, elevated_ctx.roles) |
| 270 |
|
| 271 |
def test_neutron_context_elevated_system_scope_for_new_policies(self): |
| 272 |
cfg.CONF.set_override('enforce_scope', True, group='oslo_policy') |
| 273 |
expected_roles = ['admin', 'member', 'reader'] |
| 274 |
ctx = context.Context('user_id', 'project_id') |
| 275 |
self.assertFalse(ctx.is_admin) |
| 276 |
self.assertNotEqual('all', ctx.system_scope) |
| 277 |
elevated_ctx = ctx.elevated() |
| 278 |
self.assertTrue(elevated_ctx.is_admin) |
| 279 |
self.assertTrue(elevated_ctx.has_global_access) |
| 280 |
self.assertTrue(elevated_ctx.can_set_project_id) |
| 281 |
for expected_role in expected_roles: |
| 282 |
self.assertIn(expected_role, elevated_ctx.roles) |
| 283 |
# make sure we do not set the system scope in context |
| 284 |
self.assertNotEqual('all', elevated_ctx.system_scope) |
| 285 |
|
| 286 |
def test_neutron_context_elevated_keeps_custom_roles(self): |
| 287 |
expected_admin_roles = ['admin', 'member', 'reader'] |
| 288 |
custom_roles = ['custom_role'] |
| 289 |
ctx = context.Context('user_id', 'project_id', roles=custom_roles) |
| 290 |
self.assertFalse(ctx.is_admin) |
| 291 |
self.assertFalse(ctx.has_global_access) |
| 292 |
self.assertFalse(ctx.can_set_project_id) |
| 293 |
self.assertNotEqual('all', ctx.system_scope) |
| 294 |
for expected_admin_role in expected_admin_roles: |
| 295 |
self.assertNotIn(expected_admin_role, ctx.roles) |
| 296 |
for custom_role in custom_roles: |
| 297 |
self.assertIn(custom_role, ctx.roles) |
| 298 |
|
| 299 |
elevated_ctx = ctx.elevated() |
| 300 |
self.assertTrue(elevated_ctx.is_admin) |
| 301 |
self.assertTrue(elevated_ctx.has_global_access) |
| 302 |
self.assertTrue(elevated_ctx.can_set_project_id) |
| 303 |
for expected_admin_role in expected_admin_roles: |
| 304 |
self.assertIn(expected_admin_role, elevated_ctx.roles) |
| 305 |
for custom_role in custom_roles: |
| 306 |
self.assertIn(custom_role, ctx.roles) |
| 307 |
|
| 308 |
def test_neutron_context_overwrite(self): |
| 309 |
ctx1 = context.Context('user_id', 'project_id') |
| 310 |
self.assertEqual(ctx1.request_id, |
| 311 |
oslo_context.get_current().request_id) |
| 312 |
|
| 313 |
# If overwrite is not specified, request_id should be updated. |
| 314 |
ctx2 = context.Context('user_id', 'project_id') |
| 315 |
self.assertNotEqual(ctx2.request_id, ctx1.request_id) |
| 316 |
self.assertEqual(ctx2.request_id, |
| 317 |
oslo_context.get_current().request_id) |
| 318 |
|
| 319 |
# If overwrite is specified, request_id should be kept. |
| 320 |
ctx3 = context.Context('user_id', 'project_id', overwrite=False) |
| 321 |
self.assertNotEqual(ctx3.request_id, ctx2.request_id) |
| 322 |
self.assertEqual(ctx2.request_id, |
| 323 |
oslo_context.get_current().request_id) |
| 324 |
|
| 325 |
def test_neutron_context_get_admin_context_not_update_local_store(self): |
| 326 |
ctx = context.Context('user_id', 'project_id') |
| 327 |
req_id_before = oslo_context.get_current().request_id |
| 328 |
self.assertEqual(ctx.request_id, req_id_before) |
| 329 |
|
| 330 |
ctx_admin = context.get_admin_context() |
| 331 |
self.assertEqual(req_id_before, oslo_context.get_current().request_id) |
| 332 |
self.assertNotEqual(req_id_before, ctx_admin.request_id) |
| 333 |
|
| 334 |
def test_neutron_context_has_global_access(self): |
| 335 |
with mock.patch('neutron_lib.policy._engine.check_has_global_access', |
| 336 |
return_value=False): |
| 337 |
ctx = context.Context('user_id', 'project_id') |
| 338 |
self.assertFalse(ctx.has_global_access) |
| 339 |
|
| 340 |
with mock.patch('neutron_lib.policy._engine.check_has_global_access', |
| 341 |
return_value=True): |
| 342 |
ctx = context.Context('user_id', 'project_id') |
| 343 |
self.assertTrue(ctx.has_global_access) |
| 344 |
|
| 345 |
def test_neutron_context_can_set_project_id(self): |
| 346 |
with mock.patch('neutron_lib.policy._engine.check_can_set_project_id', |
| 347 |
return_value=False): |
| 348 |
ctx = context.Context('user_id', 'project_id') |
| 349 |
self.assertFalse(ctx.can_set_project_id) |
| 350 |
|
| 351 |
with mock.patch('neutron_lib.policy._engine.check_can_set_project_id', |
| 352 |
return_value=True): |
| 353 |
ctx = context.Context('user_id', 'project_id') |
| 354 |
self.assertTrue(ctx.can_set_project_id) |
| 355 |
|
| 356 |
def test_to_policy_values(self): |
| 357 |
values = { |
| 358 |
'user_id': 'user_id', |
| 359 |
'project_id': 'project_id', |
| 360 |
'is_admin': 'is_admin', |
| 361 |
'project_name': 'project_name', |
| 362 |
'user_name': 'user_name', |
| 363 |
'domain_id': 'domain', |
| 364 |
'user_domain_id': 'user_domain', |
| 365 |
'project_domain_id': 'project_domain', |
| 366 |
} |
| 367 |
additional_values = { |
| 368 |
'user': 'user_id', |
| 369 |
'tenant': 'project_id', |
| 370 |
'tenant_id': 'project_id', |
| 371 |
'tenant_name': 'project_name', |
| 372 |
} |
| 373 |
ctx = context.Context(**values) |
| 374 |
# apply dict() to get a real dictionary, needed for newer oslo.context |
| 375 |
# that returns _DeprecatedPolicyValues object instead |
| 376 |
policy_values = dict(ctx.to_policy_values()) |
| 377 |
self.assertDictSupersetOf(values, policy_values) |
| 378 |
self.assertDictSupersetOf(additional_values, policy_values) |
| 379 |
|
| 380 |
def test_session_cached(self): |
| 381 |
ctx = context.Context('user_id', 'project_id') |
| 382 |
session1 = ctx.session |
| 383 |
session2 = ctx.session |
| 384 |
self.assertIs(session1, session2) |
| 385 |
|
| 386 |
def test_add_get_remove_constraint(self): |
| 387 |
ctx = context.Context('user_id', 'project_id') |
| 388 |
self.assertIsNone(ctx.get_transaction_constraint()) |
| 389 |
ctx.set_transaction_constraint('networks', 'net_id', 44) |
| 390 |
constraint = ctx.get_transaction_constraint() |
| 391 |
self.assertEqual(44, constraint.if_revision_match) |
| 392 |
self.assertEqual('networks', constraint.resource) |
| 393 |
self.assertEqual('net_id', constraint.resource_id) |
| 394 |
ctx.clear_transaction_constraint() |
| 395 |
self.assertIsNone(ctx.get_transaction_constraint()) |