| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import copy |
| 16 |
|
| 17 |
from oslo_log import log as logging |
| 18 |
from tempest import config |
| 19 |
from tempest.lib import exceptions |
| 20 |
from tempest import test |
| 21 |
|
| 22 |
from octavia_tempest_plugin.common import constants |
| 23 |
from octavia_tempest_plugin.tests import waiters |
| 24 |
|
| 25 |
CONF = config.CONF |
| 26 |
LOG = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
class RBACTestsMixin(test.BaseTestCase): |
| 30 |
|
| 31 |
def _get_client_method(self, cred_obj, client_str, method_str): |
| 32 |
"""Get requested method from registered clients in Tempest.""" |
| 33 |
lb_clients = getattr(cred_obj, 'load_balancer_v2') |
| 34 |
client = getattr(lb_clients, client_str) |
| 35 |
client_obj = client() |
| 36 |
method = getattr(client_obj, method_str) |
| 37 |
return method |
| 38 |
|
| 39 |
def _check_allowed(self, client_str, method_str, allowed_list, |
| 40 |
*args, **kwargs): |
| 41 |
"""Test an API call allowed RBAC enforcement. |
| 42 |
|
| 43 |
:param client_str: The service client to use for the test, without the |
| 44 |
credential. Example: 'AmphoraClient' |
| 45 |
:param method_str: The method on the client to call for the test. |
| 46 |
Example: 'list_amphorae' |
| 47 |
:param allowed_list: The list of credentials expected to be |
| 48 |
allowed. Example: ['os_roles_lb_member']. |
| 49 |
:param args: Any positional parameters needed by the method. |
| 50 |
:param kwargs: Any named parameters needed by the method. |
| 51 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 52 |
:raises Forbidden: Raised if a credential that should have access does |
| 53 |
not and is denied. |
| 54 |
:raises InvalidScope: Raised if a credential that should have the |
| 55 |
correct scope for access is denied. |
| 56 |
:returns: None on success |
| 57 |
""" |
| 58 |
for cred in allowed_list: |
| 59 |
try: |
| 60 |
cred_obj = getattr(self, cred) |
| 61 |
except AttributeError: |
| 62 |
self.fail('Credential {} "expected_allowed" for RBAC ' |
| 63 |
'testing was not created by tempest ' |
| 64 |
'credentials setup. This is likely a bug in the ' |
| 65 |
'test.'.format(cred)) |
| 66 |
method = self._get_client_method(cred_obj, client_str, method_str) |
| 67 |
try: |
| 68 |
method(*args, **kwargs) |
| 69 |
except exceptions.Forbidden as e: |
| 70 |
self.fail('Method {}.{} failed to allow access via RBAC using ' |
| 71 |
'credential {}. Error: {}'.format( |
| 72 |
client_str, method_str, cred, str(e))) |
| 73 |
|
| 74 |
def _check_disallowed(self, client_str, method_str, allowed_list, |
| 75 |
status_method=None, obj_id=None, *args, **kwargs): |
| 76 |
"""Test an API call disallowed RBAC enforcement. |
| 77 |
|
| 78 |
:param client_str: The service client to use for the test, without the |
| 79 |
credential. Example: 'AmphoraClient' |
| 80 |
:param method_str: The method on the client to call for the test. |
| 81 |
Example: 'list_amphorae' |
| 82 |
:param allowed_list: The list of credentials expected to be |
| 83 |
allowed. Example: ['os_roles_lb_member']. |
| 84 |
:param status_method: The service client method that will provide |
| 85 |
the object status for a status change waiter. |
| 86 |
:param obj_id: The ID of the object to check for the expected status |
| 87 |
update. |
| 88 |
:param args: Any positional parameters needed by the method. |
| 89 |
:param kwargs: Any named parameters needed by the method. |
| 90 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 91 |
:raises Forbidden: Raised if a credential that should have access does |
| 92 |
not and is denied. |
| 93 |
:raises InvalidScope: Raised if a credential that should have the |
| 94 |
correct scope for access is denied. |
| 95 |
:returns: None on success |
| 96 |
""" |
| 97 |
expected_disallowed = (set(self.allocated_credentials) - |
| 98 |
set(allowed_list)) |
| 99 |
for cred in expected_disallowed: |
| 100 |
cred_obj = getattr(self, cred) |
| 101 |
method = self._get_client_method(cred_obj, client_str, method_str) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
try: |
| 117 |
method(*args, **kwargs) |
| 118 |
except exceptions.Forbidden: |
| 119 |
if status_method: |
| 120 |
waiters.wait_for_status( |
| 121 |
status_method, obj_id, |
| 122 |
constants.PROVISIONING_STATUS, constants.ACTIVE, |
| 123 |
CONF.load_balancer.check_interval, |
| 124 |
CONF.load_balancer.check_timeout) |
| 125 |
|
| 126 |
continue |
| 127 |
self.fail('Method {}.{} failed to deny access via RBAC using ' |
| 128 |
'credential {}.'.format(client_str, method_str, cred)) |
| 129 |
|
| 130 |
def _list_get_RBAC_enforcement(self, client_str, method_str, |
| 131 |
expected_allowed, *args, **kwargs): |
| 132 |
"""Test an API call RBAC enforcement. |
| 133 |
|
| 134 |
:param client_str: The service client to use for the test, without the |
| 135 |
credential. Example: 'AmphoraClient' |
| 136 |
:param method_str: The method on the client to call for the test. |
| 137 |
Example: 'list_amphorae' |
| 138 |
:param expected_allowed: The list of credentials expected to be |
| 139 |
allowed. Example: ['os_roles_lb_member']. |
| 140 |
:param args: Any positional parameters needed by the method. |
| 141 |
:param kwargs: Any named parameters needed by the method. |
| 142 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 143 |
:raises Forbidden: Raised if a credential that should have access does |
| 144 |
not and is denied. |
| 145 |
:raises InvalidScope: Raised if a credential that should have the |
| 146 |
correct scope for access is denied. |
| 147 |
:returns: None on success |
| 148 |
""" |
| 149 |
|
| 150 |
allowed_list = copy.deepcopy(expected_allowed) |
| 151 |
|
| 152 |
|
| 153 |
self._check_disallowed(client_str, method_str, allowed_list, |
| 154 |
None, None, *args, **kwargs) |
| 155 |
|
| 156 |
|
| 157 |
self._check_allowed(client_str, method_str, allowed_list, |
| 158 |
*args, **kwargs) |
| 159 |
|
| 160 |
def check_show_RBAC_enforcement(self, client_str, method_str, |
| 161 |
expected_allowed, *args, **kwargs): |
| 162 |
"""Test an API show call RBAC enforcement. |
| 163 |
|
| 164 |
:param client_str: The service client to use for the test, without the |
| 165 |
credential. Example: 'AmphoraClient' |
| 166 |
:param method_str: The method on the client to call for the test. |
| 167 |
Example: 'list_amphorae' |
| 168 |
:param expected_allowed: The list of credentials expected to be |
| 169 |
allowed. Example: ['os_roles_lb_member']. |
| 170 |
:param args: Any positional parameters needed by the method. |
| 171 |
:param kwargs: Any named parameters needed by the method. |
| 172 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 173 |
:raises Forbidden: Raised if a credential that should have access does |
| 174 |
not and is denied. |
| 175 |
:raises InvalidScope: Raised if a credential that should have the |
| 176 |
correct scope for access is denied. |
| 177 |
:returns: None on success |
| 178 |
""" |
| 179 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 180 |
return |
| 181 |
self._list_get_RBAC_enforcement(client_str, method_str, |
| 182 |
expected_allowed, *args, **kwargs) |
| 183 |
|
| 184 |
def check_list_RBAC_enforcement(self, client_str, method_str, |
| 185 |
expected_allowed, *args, **kwargs): |
| 186 |
"""Test an API list call RBAC enforcement. |
| 187 |
|
| 188 |
:param client_str: The service client to use for the test, without the |
| 189 |
credential. Example: 'AmphoraClient' |
| 190 |
:param method_str: The method on the client to call for the test. |
| 191 |
Example: 'list_amphorae' |
| 192 |
:param expected_allowed: The list of credentials expected to be |
| 193 |
allowed. Example: ['os_roles_lb_member']. |
| 194 |
:param args: Any positional parameters needed by the method. |
| 195 |
:param kwargs: Any named parameters needed by the method. |
| 196 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 197 |
:raises Forbidden: Raised if a credential that should have access does |
| 198 |
not and is denied. |
| 199 |
:raises InvalidScope: Raised if a credential that should have the |
| 200 |
correct scope for access is denied. |
| 201 |
:returns: None on success |
| 202 |
""" |
| 203 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 204 |
return |
| 205 |
self._list_get_RBAC_enforcement(client_str, method_str, |
| 206 |
expected_allowed, *args, **kwargs) |
| 207 |
|
| 208 |
def _CUD_RBAC_enforcement(self, client_str, method_str, expected_allowed, |
| 209 |
status_method=None, obj_id=None, |
| 210 |
*args, **kwargs): |
| 211 |
"""Test an API create/update/delete call RBAC enforcement. |
| 212 |
|
| 213 |
:param client_str: The service client to use for the test, without the |
| 214 |
credential. Example: 'AmphoraClient' |
| 215 |
:param method_str: The method on the client to call for the test. |
| 216 |
Example: 'list_amphorae' |
| 217 |
:param expected_allowed: The list of credentials expected to be |
| 218 |
allowed. Example: ['os_roles_lb_member']. |
| 219 |
:param status_method: The service client method that will provide |
| 220 |
the object status for a status change waiter. |
| 221 |
:param obj_id: The ID of the object to check for the expected status |
| 222 |
update. |
| 223 |
:param args: Any positional parameters needed by the method. |
| 224 |
:param kwargs: Any named parameters needed by the method. |
| 225 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 226 |
:raises Forbidden: Raised if a credential that should have access does |
| 227 |
not and is denied. |
| 228 |
:raises InvalidScope: Raised if a credential that should have the |
| 229 |
correct scope for access is denied. |
| 230 |
:returns: None on success |
| 231 |
""" |
| 232 |
|
| 233 |
allowed_list = copy.deepcopy(expected_allowed) |
| 234 |
|
| 235 |
|
| 236 |
self._check_disallowed(client_str, method_str, allowed_list, |
| 237 |
status_method, obj_id, *args, **kwargs) |
| 238 |
|
| 239 |
def check_create_RBAC_enforcement( |
| 240 |
self, client_str, method_str, expected_allowed, |
| 241 |
status_method=None, obj_id=None, *args, **kwargs): |
| 242 |
"""Test an API create call RBAC enforcement. |
| 243 |
|
| 244 |
:param client_str: The service client to use for the test, without the |
| 245 |
credential. Example: 'AmphoraClient' |
| 246 |
:param method_str: The method on the client to call for the test. |
| 247 |
Example: 'list_amphorae' |
| 248 |
:param expected_allowed: The list of credentials expected to be |
| 249 |
allowed. Example: ['os_roles_lb_member']. |
| 250 |
:param status_method: The service client method that will provide |
| 251 |
the object status for a status change waiter. |
| 252 |
:param obj_id: The ID of the object to check for the expected status |
| 253 |
update. |
| 254 |
:param args: Any positional parameters needed by the method. |
| 255 |
:param kwargs: Any named parameters needed by the method. |
| 256 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 257 |
:raises Forbidden: Raised if a credential that should have access does |
| 258 |
not and is denied. |
| 259 |
:raises InvalidScope: Raised if a credential that should have the |
| 260 |
correct scope for access is denied. |
| 261 |
:returns: None on success |
| 262 |
""" |
| 263 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 264 |
return |
| 265 |
self._CUD_RBAC_enforcement(client_str, method_str, expected_allowed, |
| 266 |
status_method, obj_id, *args, **kwargs) |
| 267 |
|
| 268 |
def check_delete_RBAC_enforcement( |
| 269 |
self, client_str, method_str, expected_allowed, |
| 270 |
status_method=None, obj_id=None, *args, **kwargs): |
| 271 |
"""Test an API delete call RBAC enforcement. |
| 272 |
|
| 273 |
:param client_str: The service client to use for the test, without the |
| 274 |
credential. Example: 'AmphoraClient' |
| 275 |
:param method_str: The method on the client to call for the test. |
| 276 |
Example: 'list_amphorae' |
| 277 |
:param expected_allowed: The list of credentials expected to be |
| 278 |
allowed. Example: ['os_roles_lb_member']. |
| 279 |
:param status_method: The service client method that will provide |
| 280 |
the object status for a status change waiter. |
| 281 |
:param obj_id: The ID of the object to check for the expected status |
| 282 |
update. |
| 283 |
:param args: Any positional parameters needed by the method. |
| 284 |
:param kwargs: Any named parameters needed by the method. |
| 285 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 286 |
:raises Forbidden: Raised if a credential that should have access does |
| 287 |
not and is denied. |
| 288 |
:raises InvalidScope: Raised if a credential that should have the |
| 289 |
correct scope for access is denied. |
| 290 |
:returns: None on success |
| 291 |
""" |
| 292 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 293 |
return |
| 294 |
self._CUD_RBAC_enforcement(client_str, method_str, expected_allowed, |
| 295 |
status_method, obj_id, *args, **kwargs) |
| 296 |
|
| 297 |
def check_update_RBAC_enforcement( |
| 298 |
self, client_str, method_str, expected_allowed, |
| 299 |
status_method=None, obj_id=None, *args, **kwargs): |
| 300 |
"""Test an API update call RBAC enforcement. |
| 301 |
|
| 302 |
:param client_str: The service client to use for the test, without the |
| 303 |
credential. Example: 'AmphoraClient' |
| 304 |
:param method_str: The method on the client to call for the test. |
| 305 |
Example: 'list_amphorae' |
| 306 |
:param expected_allowed: The list of credentials expected to be |
| 307 |
allowed. Example: ['os_roles_lb_member']. |
| 308 |
:param status_method: The service client method that will provide |
| 309 |
the object status for a status change waiter. |
| 310 |
:param obj_id: The ID of the object to check for the expected status |
| 311 |
update. |
| 312 |
:param args: Any positional parameters needed by the method. |
| 313 |
:param kwargs: Any named parameters needed by the method. |
| 314 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 315 |
:raises Forbidden: Raised if a credential that should have access does |
| 316 |
not and is denied. |
| 317 |
:raises InvalidScope: Raised if a credential that should have the |
| 318 |
correct scope for access is denied. |
| 319 |
:returns: None on success |
| 320 |
""" |
| 321 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 322 |
return |
| 323 |
self._CUD_RBAC_enforcement(client_str, method_str, expected_allowed, |
| 324 |
status_method, obj_id, *args, **kwargs) |
| 325 |
|
| 326 |
def check_list_RBAC_enforcement_count( |
| 327 |
self, client_str, method_str, expected_allowed, expected_count, |
| 328 |
*args, **kwargs): |
| 329 |
"""Test an API list call RBAC enforcement result count. |
| 330 |
|
| 331 |
List APIs will return the object list for the project associated |
| 332 |
with the token used to access the API. This means most credentials |
| 333 |
will have access, but will get differing results. |
| 334 |
|
| 335 |
This test will query the list API using a list of credentials and |
| 336 |
will validate that only the expected count of results are returned. |
| 337 |
|
| 338 |
:param client_str: The service client to use for the test, without the |
| 339 |
credential. Example: 'AmphoraClient' |
| 340 |
:param method_str: The method on the client to call for the test. |
| 341 |
Example: 'list_amphorae' |
| 342 |
:param expected_allowed: The list of credentials expected to be |
| 343 |
allowed. Example: ['os_roles_lb_member']. |
| 344 |
:param expected_count: The number of results expected in the list |
| 345 |
returned from the API. |
| 346 |
:param args: Any positional parameters needed by the method. |
| 347 |
:param kwargs: Any named parameters needed by the method. |
| 348 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 349 |
:raises Forbidden: Raised if a credential that should have access does |
| 350 |
not and is denied. |
| 351 |
:raises InvalidScope: Raised if a credential that should have the |
| 352 |
correct scope for access is denied. |
| 353 |
:returns: None on success |
| 354 |
""" |
| 355 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 356 |
return |
| 357 |
|
| 358 |
allowed_list = copy.deepcopy(expected_allowed) |
| 359 |
|
| 360 |
for cred in allowed_list: |
| 361 |
try: |
| 362 |
cred_obj = getattr(self, cred) |
| 363 |
except AttributeError: |
| 364 |
self.fail('Credential {} "expected_allowed" for RBAC ' |
| 365 |
'testing was not created by tempest ' |
| 366 |
'credentials setup. This is likely a bug in the ' |
| 367 |
'test.'.format(cred)) |
| 368 |
method = self._get_client_method(cred_obj, client_str, method_str) |
| 369 |
try: |
| 370 |
result = method(*args, **kwargs) |
| 371 |
except exceptions.Forbidden as e: |
| 372 |
self.fail('Method {}.{} failed to allow access via RBAC using ' |
| 373 |
'credential {}. Error: {}'.format( |
| 374 |
client_str, method_str, cred, str(e))) |
| 375 |
self.assertEqual(expected_count, len(result), message='Credential ' |
| 376 |
'{} saw {} objects when {} was expected.'.format( |
| 377 |
cred, len(result), expected_count)) |
| 378 |
|
| 379 |
def check_list_IDs_RBAC_enforcement( |
| 380 |
self, client_str, method_str, expected_allowed, expected_ids, |
| 381 |
*args, **kwargs): |
| 382 |
"""Test an API list call RBAC enforcement result contains IDs. |
| 383 |
|
| 384 |
List APIs will return the object list for the project associated |
| 385 |
with the token used to access the API. This means most credentials |
| 386 |
will have access, but will get differing results. |
| 387 |
|
| 388 |
This test will query the list API using a list of credentials and |
| 389 |
will validate that the expected object Ids in included in the results. |
| 390 |
|
| 391 |
:param client_str: The service client to use for the test, without the |
| 392 |
credential. Example: 'AmphoraClient' |
| 393 |
:param method_str: The method on the client to call for the test. |
| 394 |
Example: 'list_amphorae' |
| 395 |
:param expected_allowed: The list of credentials expected to be |
| 396 |
allowed. Example: ['os_roles_lb_member']. |
| 397 |
:param expected_ids: The list of object IDs to validate are included |
| 398 |
in the returned list from the API. |
| 399 |
:param args: Any positional parameters needed by the method. |
| 400 |
:param kwargs: Any named parameters needed by the method. |
| 401 |
:raises AssertionError: Raised if the RBAC tests fail. |
| 402 |
:raises Forbidden: Raised if a credential that should have access does |
| 403 |
not and is denied. |
| 404 |
:raises InvalidScope: Raised if a credential that should have the |
| 405 |
correct scope for access is denied. |
| 406 |
:returns: None on success |
| 407 |
""" |
| 408 |
if CONF.load_balancer.RBAC_test_type == constants.NONE: |
| 409 |
return |
| 410 |
|
| 411 |
allowed_list = copy.deepcopy(expected_allowed) |
| 412 |
|
| 413 |
for cred in allowed_list: |
| 414 |
try: |
| 415 |
cred_obj = getattr(self, cred) |
| 416 |
except AttributeError: |
| 417 |
self.fail('Credential {} "expected_allowed" for RBAC ' |
| 418 |
'testing was not created by tempest ' |
| 419 |
'credentials setup. This is likely a bug in the ' |
| 420 |
'test.'.format(cred)) |
| 421 |
method = self._get_client_method(cred_obj, client_str, method_str) |
| 422 |
try: |
| 423 |
result = method(*args, **kwargs) |
| 424 |
except exceptions.Forbidden as e: |
| 425 |
self.fail('Method {}.{} failed to allow access via RBAC using ' |
| 426 |
'credential {}. Error: {}'.format( |
| 427 |
client_str, method_str, cred, str(e))) |
| 428 |
result_ids = [lb[constants.ID] for lb in result] |
| 429 |
self.assertTrue(set(expected_ids).issubset(set(result_ids))) |