| 1 |
# All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from tempest import clients |
| 16 |
from tempest import config |
| 17 |
from tempest.lib import auth |
| 18 |
from tempest.lib.common.utils import data_utils |
| 19 |
|
| 20 |
from magnum_tempest_plugin.common import base |
| 21 |
from magnum_tempest_plugin.common import config as magnum_config |
| 22 |
|
| 23 |
CONF = config.CONF |
| 24 |
|
| 25 |
|
| 26 |
class RbacBaseTests(base.BaseMagnumTest): |
| 27 |
|
| 28 |
credentials = ['project_admin', 'project_member', 'project_reader'] |
| 29 |
identity_version = 'v3' |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def skip_checks(cls): |
| 33 |
super(RbacBaseTests, cls).skip_checks() |
| 34 |
if not CONF.enforce_scope.magnum: |
| 35 |
raise cls.skipException( |
| 36 |
"Tempest is not configured to enforce_scope for magnum, " |
| 37 |
"skipping RBAC tests. To enable these tests set " |
| 38 |
"`tempest.conf [enforce_scope] magnum=True`." |
| 39 |
) |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def setUpClass(cls): |
| 43 |
magnum_config.Config.setUp() |
| 44 |
super(RbacBaseTests, cls).setUpClass() |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def setup_clients(cls): |
| 48 |
super(RbacBaseTests, cls).setup_clients() |
| 49 |
# default to os_project_admin in base class |
| 50 |
cls.persona = getattr(cls, f"os_{cls.credentials[0]}") |
| 51 |
cls.magnum_clients = cls.persona.magnum_v1 |
| 52 |
cls.project_admin = cls.os_project_admin.magnum_v1 |
| 53 |
cls.project_reader = cls.os_project_reader.magnum_v1 |
| 54 |
cls.project_member = cls.os_project_member.magnum_v1 |
| 55 |
|
| 56 |
cls.ct_reader_client = cls.project_reader.ClusterTemplateClient() |
| 57 |
cls.ct_member_client = cls.project_member.ClusterTemplateClient() |
| 58 |
cls.ct_admin_client = cls.project_admin.ClusterTemplateClient() |
| 59 |
cls.cluster_reader_client = cls.project_reader.ClusterClient() |
| 60 |
cls.cluster_member_client = cls.project_member.ClusterClient() |
| 61 |
cls.cluster_admin_client = cls.project_admin.ClusterClient() |
| 62 |
cls.cert_reader_client = cls.project_reader.CertClient() |
| 63 |
cls.cert_member_client = cls.project_member.CertClient() |
| 64 |
cls.cert_admin_client = cls.project_admin.CertClient() |
| 65 |
cls.service_member_client = cls.project_member.MagnumServiceClient() |
| 66 |
cls.service_admin_client = cls.project_admin.MagnumServiceClient() |
| 67 |
|
| 68 |
@classmethod |
| 69 |
def setup_credentials(cls): |
| 70 |
super().setup_credentials() |
| 71 |
cls.os_primary = getattr(cls, f'os_{cls.credentials[0]}') |
| 72 |
|
| 73 |
def do_request(self, method, expected_status=200, client=None, **payload): |
| 74 |
if not client: |
| 75 |
client = self.client |
| 76 |
if isinstance(expected_status, type(Exception)): |
| 77 |
self.assertRaises(expected_status, |
| 78 |
getattr(client, method), |
| 79 |
**payload) |
| 80 |
else: |
| 81 |
response, mode = getattr(client, method)(**payload) |
| 82 |
self.assertEqual(response.status, expected_status) |
| 83 |
return response, mode |
| 84 |
|
| 85 |
def setup_user_client(self, project_id=None): |
| 86 |
"""Set up project user with its own client. |
| 87 |
|
| 88 |
This is useful for testing protection of resources in separate |
| 89 |
projects. |
| 90 |
|
| 91 |
Returns a client object and the user's ID. |
| 92 |
""" |
| 93 |
user_dict = { |
| 94 |
'name': data_utils.rand_name('user'), |
| 95 |
'password': data_utils.rand_password(), |
| 96 |
} |
| 97 |
user_id = self.os_system_admin.users_v3_client.create_user( |
| 98 |
**user_dict)['user']['id'] |
| 99 |
self.addCleanup(self.os_system_admin.users_v3_client.delete_user, |
| 100 |
user_id) |
| 101 |
|
| 102 |
if not project_id: |
| 103 |
project_id = self.os_system_admin.projects_client.create_project( |
| 104 |
data_utils.rand_name())['project']['id'] |
| 105 |
self.addCleanup( |
| 106 |
self.os_system_admin.projects_client.delete_project, |
| 107 |
project_id) |
| 108 |
|
| 109 |
member_role_id = self.os_system_admin.roles_v3_client.list_roles( |
| 110 |
name='member')['roles'][0]['id'] |
| 111 |
self.os_system_admin.roles_v3_client.create_user_role_on_project( |
| 112 |
project_id, user_id, member_role_id) |
| 113 |
creds = auth.KeystoneV3Credentials( |
| 114 |
user_id=user_id, |
| 115 |
password=user_dict['password'], |
| 116 |
project_id=project_id) |
| 117 |
auth_provider = clients.get_auth_provider(creds) |
| 118 |
creds = auth_provider.fill_credentials() |
| 119 |
client = clients.Manager(credentials=creds) |
| 120 |
return client |