Merge "Update Cinder test that incorrectly handles 404"
diff --git a/patrole_tempest_plugin/rbac_auth.py b/patrole_tempest_plugin/rbac_auth.py
index e4e35b1..687c0a8 100644
--- a/patrole_tempest_plugin/rbac_auth.py
+++ b/patrole_tempest_plugin/rbac_auth.py
@@ -13,8 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.
+import testtools
+
from oslo_log import log as logging
+from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_policy_parser
LOG = logging.getLogger(__name__)
@@ -22,20 +25,19 @@
class RbacAuthority(object):
def __init__(self, tenant_id, user_id, service=None):
- self.converter = rbac_policy_parser.RbacPolicyParser(
+ self.policy_parser = rbac_policy_parser.RbacPolicyParser(
tenant_id, user_id, service)
def get_permission(self, rule_name, role):
try:
- is_allowed = self.converter.allowed(rule_name, role)
+ is_allowed = self.policy_parser.allowed(rule_name, role)
if is_allowed:
- LOG.debug("[API]: %s, [Role]: %s is allowed!", rule_name, role)
+ LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule_name,
+ role)
else:
- LOG.debug("[API]: %s, [Role]: %s is NOT allowed!",
+ LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!",
rule_name, role)
return is_allowed
- except KeyError:
- LOG.debug("[API]: %s, [Role]: %s is NOT allowed!",
- rule_name, role)
- return False
+ except rbac_exceptions.RbacParsingException as e:
+ raise testtools.TestCase.skipException(str(e))
return False
diff --git a/patrole_tempest_plugin/rbac_exceptions.py b/patrole_tempest_plugin/rbac_exceptions.py
index ee42e19..aa3135e 100644
--- a/patrole_tempest_plugin/rbac_exceptions.py
+++ b/patrole_tempest_plugin/rbac_exceptions.py
@@ -30,3 +30,11 @@
class RbacInvalidService (exceptions.TempestException):
message = "Attempted to test an invalid service"
+
+
+class RbacParsingException (exceptions.TempestException):
+ message = "Attempted to test an invalid policy file or action"
+
+
+class RbacInvalidErrorCode (exceptions.TempestException):
+ message = "Unsupported error code passed in test"
diff --git a/patrole_tempest_plugin/rbac_policy_parser.py b/patrole_tempest_plugin/rbac_policy_parser.py
index 9926613..62113ac 100644
--- a/patrole_tempest_plugin/rbac_policy_parser.py
+++ b/patrole_tempest_plugin/rbac_policy_parser.py
@@ -90,7 +90,7 @@
policy_data = policy_data[:-2] + "\n}"
# Otherwise raise an exception.
else:
- raise rbac_exceptions.RbacResourceSetupFailed(
+ raise rbac_exceptions.RbacParsingException(
'Policy file for service: {0}, {1} not found.'
.format(service, self.path))
@@ -172,8 +172,12 @@
rule = self.rules[apply_rule]
return rule(target, access_data, o)
except KeyError as e:
- LOG.debug("{0} not found in policy file.".format(apply_rule))
- return False
+ message = "Policy action: {0} not found in policy file: {1}."\
+ .format(apply_rule, self.path)
+ LOG.debug(message)
+ raise rbac_exceptions.RbacParsingException(message)
except Exception as e:
- LOG.debug("Exception: {0} for rule: {1}.".format(e, apply_rule))
- return False
+ message = "Unknown exception: {0} for policy action: {1} in "\
+ "policy file: {2}.".format(e, apply_rule, self.path)
+ LOG.debug(message)
+ raise rbac_exceptions.RbacParsingException(message)
diff --git a/patrole_tempest_plugin/rbac_rule_validation.py b/patrole_tempest_plugin/rbac_rule_validation.py
index 463adce..33ee666 100644
--- a/patrole_tempest_plugin/rbac_rule_validation.py
+++ b/patrole_tempest_plugin/rbac_rule_validation.py
@@ -26,7 +26,7 @@
LOG = logging.getLogger(__name__)
-def action(service, rule):
+def action(service, rule, expected_error_code=403):
def decorator(func):
def wrapper(*args, **kwargs):
try:
@@ -43,16 +43,18 @@
authority = rbac_auth.RbacAuthority(tenant_id, user_id, service)
allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
+ expected_exception, irregular_msg = _get_exception_type(
+ expected_error_code)
try:
func(*args)
except rbac_exceptions.RbacInvalidService as e:
- msg = ("%s is not a valid service." % service)
- LOG.error(msg)
- raise exceptions.NotFound(
- "%s RbacInvalidService was: %s" %
- (msg, e))
- except exceptions.Forbidden as e:
+ msg = ("%s is not a valid service." % service)
+ LOG.error(msg)
+ raise exceptions.NotFound(
+ "%s RbacInvalidService was: %s" %
+ (msg, e))
+ except expected_exception as e:
if allowed:
msg = ("Role %s was not allowed to perform %s." %
(CONF.rbac.rbac_test_role, rule))
@@ -60,6 +62,8 @@
raise exceptions.Forbidden(
"%s exception was: %s" %
(msg, e))
+ if irregular_msg:
+ LOG.warning(irregular_msg.format(rule, service))
except rbac_exceptions.RbacActionFailed as e:
if allowed:
msg = ("Role %s was not allowed to perform %s." %
@@ -80,3 +84,23 @@
switchToRbacRole=False)
return wrapper
return decorator
+
+
+def _get_exception_type(expected_error_code):
+ expected_exception = None
+ irregular_msg = None
+ supported_error_codes = [403, 404]
+ if expected_error_code == 403:
+ expected_exception = exceptions.Forbidden
+ elif expected_error_code == 404:
+ expected_exception = exceptions.NotFound
+ irregular_msg = ("NotFound exception was caught for policy action "
+ "{0}. The service {1} throws a 404 instead of a 403, "
+ "which is irregular.")
+ else:
+ msg = ("Please pass an expected error code. Currently "
+ "supported codes: {0}".format(str(supported_error_codes)))
+ LOG.error(msg)
+ raise rbac_exceptions.RbacInvalidErrorCode()
+
+ return expected_exception, irregular_msg
diff --git a/patrole_tempest_plugin/rbac_utils.py b/patrole_tempest_plugin/rbac_utils.py
index abbb435..18f132e 100644
--- a/patrole_tempest_plugin/rbac_utils.py
+++ b/patrole_tempest_plugin/rbac_utils.py
@@ -87,12 +87,18 @@
raise
finally:
- if BaseTestCase.get_identity_version() != 'v3':
- test_obj.auth_provider.clear_auth()
- # Sleep to avoid 401 errors caused by rounding in timing of
- # fernet token creation.
- time.sleep(1)
- test_obj.auth_provider.set_auth()
+ # NOTE(felipemonteiro): These two comments below are copied from
+ # tempest.api.identity.v2/v3.test_users.
+ #
+ # Reset auth again to verify the password restore does work.
+ # Clear auth restores the original credentials and deletes
+ # cached auth data.
+ test_obj.auth_provider.clear_auth()
+ # Fernet tokens are not subsecond aware and Keystone should only be
+ # precise to the second. Sleep to ensure we are passing the second
+ # boundary before attempting to authenticate.
+ time.sleep(1)
+ test_obj.auth_provider.set_auth()
def _clear_user_roles(cls, user_id, tenant_id):
roles = cls.creds_client.roles_client.list_user_roles_on_project(
diff --git a/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py
index 356a74a..1704644 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py
@@ -13,18 +13,17 @@
# License for the specific language governing permissions and limitations
# under the License.
+from oslo_config import cfg
from oslo_log import log
-from tempest import config
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
+from tempest import test
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.compute import rbac_base
-CONF = config.CONF
+CONF = cfg.CONF
LOG = log.getLogger(__name__)
@@ -38,29 +37,27 @@
@classmethod
def skip_checks(cls):
super(FlavorAccessAdminRbacTest, cls).skip_checks()
- if not CONF.compute_feature_enabled.api_extensions:
- raise cls.skipException(
- '%s skipped as no compute extensions enabled' % cls.__name__)
+ if not test.is_extension_enabled('OS-FLV-EXT-DATA', 'compute'):
+ msg = "%s skipped as OS-FLV-EXT-DATA extension not enabled."\
+ % cls.__name__
+ raise cls.skipException(msg)
@classmethod
def resource_setup(cls):
super(FlavorAccessAdminRbacTest, cls).resource_setup()
cls.flavor_id = cls._create_flavor(is_public=False)['id']
+ cls.public_flavor_id = CONF.compute.flavor_ref
cls.tenant_id = cls.auth_provider.credentials.tenant_id
@decorators.idempotent_id('a2bd3740-765d-4c95-ac98-9e027378c75e')
@rbac_rule_validation.action(
service="nova",
rule="os_compute_api:os-flavor-access")
- def test_list_flavor_access(self):
+ def test_show_flavor(self):
+ # NOTE(felipemonteiro): show_flavor enforces the specified policy
+ # action, but only works if a public flavor is passed.
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.client.list_flavor_access(self.flavor_id)
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.client.show_flavor(self.public_flavor_id)['flavor']
@decorators.idempotent_id('39cb5c8f-9990-436f-9282-fc76a41d9bac')
@rbac_rule_validation.action(
@@ -69,7 +66,8 @@
def test_add_flavor_access(self):
self.rbac_utils.switch_role(self, switchToRbacRole=True)
self.client.add_flavor_access(
- flavor_id=self.flavor_id, tenant_id=self.tenant_id)
+ flavor_id=self.flavor_id, tenant_id=self.tenant_id)[
+ 'flavor_access']
self.addCleanup(self.client.remove_flavor_access,
flavor_id=self.flavor_id, tenant_id=self.tenant_id)
diff --git a/patrole_tempest_plugin/tests/api/compute/test_instance_actions_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_instance_actions_rbac.py
index 2903342..dcf3c90 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_instance_actions_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_instance_actions_rbac.py
@@ -16,6 +16,7 @@
from tempest.lib import decorators
from tempest import test
+from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.compute import rbac_base
@@ -54,5 +55,7 @@
rule="os_compute_api:os-instance-actions:events")
def test_get_instance_action(self):
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.client.show_instance_action(
+ instance_action = self.client.show_instance_action(
self.server['id'], self.request_id)['instanceAction']
+ if 'events' not in instance_action:
+ raise rbac_exceptions.RbacActionFailed
diff --git a/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py
index 0e1b00b..d466ded 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py
@@ -18,6 +18,8 @@
from tempest.common import waiters
from tempest import config
+from tempest.lib.common.utils import data_utils
+from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
from tempest.lib import exceptions as lib_exc
from tempest import test
@@ -36,17 +38,34 @@
def setup_clients(cls):
super(ServerActionsRbacTest, cls).setup_clients()
cls.client = cls.servers_client
+ cls.snapshots_client = cls.snapshots_extensions_client
@classmethod
def resource_setup(cls):
cls.set_validation_resources()
super(ServerActionsRbacTest, cls).resource_setup()
+ # Create test server
cls.server_id = cls.create_test_server(wait_until='ACTIVE',
validatable=True)['id']
cls.flavor_ref = CONF.compute.flavor_ref
cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
cls.image_ref = CONF.compute.image_ref
+ # Create a volume
+ volume_name = data_utils.rand_name(cls.__name__ + '-volume')
+ name_field = 'name'
+ if not CONF.volume_feature_enabled.api_v2:
+ name_field = 'display_name'
+
+ params = {name_field: volume_name,
+ 'imageRef': CONF.compute.image_ref,
+ 'size': CONF.volume.volume_size}
+ volume = cls.volumes_client.create_volume(**params)['volume']
+ waiters.wait_for_volume_resource_status(cls.volumes_client,
+ volume['id'], 'available')
+ cls.volumes.append(volume)
+ cls.volume_id = volume['id']
+
def setUp(self):
super(ServerActionsRbacTest, self).setUp()
try:
@@ -64,6 +83,56 @@
self.__class__.server_id = self.rebuild_server(
self.server_id, validatable=True)
+ @classmethod
+ def resource_cleanup(cls):
+ # If a test case creates an image from a server that is created with
+ # a volume, a volume snapshot will automatically be created by default.
+ # We need to delete the volume snapshot.
+ try:
+ body = cls.snapshots_extensions_client.list_snapshots()
+ volume_snapshots = body['snapshots']
+ except Exception:
+ LOG.info("Cannot retrieve snapshots for cleanup.")
+ else:
+ for snapshot in volume_snapshots:
+ if snapshot['volumeId'] == cls.volume_id:
+ # Wait for snapshot status to become 'available' before
+ # deletion
+ waiters.wait_for_volume_resource_status(
+ cls.snapshots_client, snapshot['id'], 'available')
+ test_utils.call_and_ignore_notfound_exc(
+ cls.snapshots_client.delete_snapshot, snapshot['id'])
+
+ for snapshot in volume_snapshots:
+ if snapshot['volumeId'] == cls.volume_id:
+ test_utils.call_and_ignore_notfound_exc(
+ cls.snapshots_client.wait_for_resource_deletion,
+ snapshot['id'])
+
+ super(ServerActionsRbacTest, cls).resource_cleanup()
+
+ def _create_test_server_with_volume(self, volume_id):
+ # Create a server with the volume created earlier
+ server_name = data_utils.rand_name(self.__class__.__name__ + "-server")
+ bd_map_v2 = [{'uuid': volume_id,
+ 'source_type': 'volume',
+ 'destination_type': 'volume',
+ 'boot_index': 0,
+ 'delete_on_termination': True}]
+ device_mapping = {'block_device_mapping_v2': bd_map_v2}
+
+ # Since the server is booted from volume, the imageRef does not need
+ # to be specified.
+ server = self.client.create_server(name=server_name,
+ imageRef='',
+ flavorRef=CONF.compute.flavor_ref,
+ **device_mapping)['server']
+
+ waiters.wait_for_server_status(self.client, server['id'], 'ACTIVE')
+
+ self.servers.append(server)
+ return server
+
def _test_start_server(self):
self.client.start_server(self.server_id)
waiters.wait_for_server_status(self.client, self.server_id,
@@ -206,6 +275,29 @@
self.rbac_utils.switch_role(self, switchToRbacRole=True)
self.client.show_server(self.server_id)
+ @rbac_rule_validation.action(
+ service="nova",
+ rule="os_compute_api:servers:create_image")
+ @decorators.idempotent_id('ba0ac859-99f4-4055-b5e0-e0905a44d331')
+ def test_create_image(self):
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+
+ # This function will also call show image
+ self.create_image_from_server(self.server_id,
+ wait_until='ACTIVE')
+
+ @rbac_rule_validation.action(
+ service="nova",
+ rule="os_compute_api:servers:create_image:allow_volume_backed")
+ @decorators.idempotent_id('8b869f73-49b3-4cc4-a0ce-ef64f8e1d6f9')
+ def test_create_image_volume_backed(self):
+ server = self._create_test_server_with_volume(self.volume_id)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+
+ # This function will also call show image
+ self.create_image_from_server(server['id'],
+ wait_until='ACTIVE')
+
class ServerActionsV216RbacTest(rbac_base.BaseV2ComputeRbacTest):
diff --git a/patrole_tempest_plugin/tests/api/compute/test_server_password_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_server_password_rbac.py
index 5ca7b16..849a19a 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_server_password_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_server_password_rbac.py
@@ -38,9 +38,9 @@
@classmethod
def resource_setup(cls):
super(ServerPasswordRbacTest, cls).resource_setup()
- cls.server = cls.create_test_server()
+ cls.server = cls.create_test_server(wait_until="ACTIVE")
- @decorators.idempotent_id('43ad7995-2f12-41cd-8ef1-bae9ffc36818')
+ @decorators.idempotent_id('aaf43f78-c178-4581-ac18-14afd3f1f6ba')
@rbac_rule_validation.action(
service="nova",
rule="os_compute_api:os-server-password")
diff --git a/patrole_tempest_plugin/tests/api/identity/v3/test_users_rbac.py b/patrole_tempest_plugin/tests/api/identity/v3/test_users_rbac.py
index 66798cd..3ae4c21 100644
--- a/patrole_tempest_plugin/tests/api/identity/v3/test_users_rbac.py
+++ b/patrole_tempest_plugin/tests/api/identity/v3/test_users_rbac.py
@@ -26,6 +26,10 @@
class IdentityUserV3AdminRbacTest(
rbac_base.BaseIdentityV3RbacAdminTest):
+ def setUp(self):
+ super(IdentityUserV3AdminRbacTest, self).setUp()
+ self.default_user_id = self.auth_provider.credentials.user_id
+
@rbac_rule_validation.action(service="keystone",
rule="identity:create_user")
@decorators.idempotent_id('0f148510-63bf-11e6-4522-080044d0d904')
@@ -82,16 +86,13 @@
@rbac_rule_validation.action(service="keystone",
rule="identity:get_user")
@decorators.idempotent_id('0f148510-63bf-11e6-4522-080044d0d908')
- def test_show_user(self):
+ def test_show_own_user(self):
"""Get one user.
RBAC test for Keystone: identity:get_user
"""
- user_name = data_utils.rand_name('test_get_user')
- user = self._create_test_user(name=user_name, password=None)
-
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.non_admin_users_client.show_user(user['id'])
+ self.non_admin_users_client.show_user(self.default_user_id)
@rbac_rule_validation.action(service="keystone",
rule="identity:change_password")
@@ -102,37 +103,33 @@
RBAC test for Keystone: identity:change_password
"""
user_name = data_utils.rand_name('test_change_password')
- user = self._create_test_user(name=user_name, password='nova')
+ original_password = data_utils.rand_password()
+ user = self._create_test_user(name=user_name,
+ password=original_password)
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.non_admin_users_client \
- .update_user_password(user['id'],
- original_password='nova',
- password='neutron')
+ self.non_admin_users_client.update_user_password(
+ user['id'], original_password=original_password,
+ password=data_utils.rand_password())
@rbac_rule_validation.action(service="keystone",
rule="identity:list_groups_for_user")
@decorators.idempotent_id('bd5946d4-46d2-423d-a800-a3e7aabc18b3')
- def test_list_group_user(self):
+ def test_list_own_user_group(self):
"""Lists groups which a user belongs to.
RBAC test for Keystone: identity:list_groups_for_user
"""
- user_name = data_utils.rand_name('User')
- user = self._create_test_user(name=user_name, password=None)
-
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.non_admin_users_client.list_user_groups(user['id'])
+ self.non_admin_users_client.list_user_groups(self.default_user_id)
@rbac_rule_validation.action(service="keystone",
rule="identity:list_user_projects")
@decorators.idempotent_id('0f148510-63bf-11e6-1564-080044d0d909')
- def test_list_user_projects(self):
+ def test_list_own_user_projects(self):
"""List User's Projects.
RBAC test for Keystone: identity:list_user_projects
"""
- user = self.setup_test_user()
-
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.non_admin_users_client.list_user_projects(user['id'])
+ self.non_admin_users_client.list_user_projects(self.default_user_id)
diff --git a/patrole_tempest_plugin/tests/api/image/v2/test_images_member_rbac.py b/patrole_tempest_plugin/tests/api/image/v2/test_images_member_rbac.py
index bbd5a79..7d99d55 100644
--- a/patrole_tempest_plugin/tests/api/image/v2/test_images_member_rbac.py
+++ b/patrole_tempest_plugin/tests/api/image/v2/test_images_member_rbac.py
@@ -16,9 +16,7 @@
from oslo_log import log as logging
from tempest import config
from tempest.lib import decorators
-from tempest.lib import exceptions
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.image import rbac_base as base
@@ -79,7 +77,8 @@
self.alt_tenant_id)
@rbac_rule_validation.action(service="glance",
- rule="get_member")
+ rule="get_member",
+ expected_error_code=404)
@decorators.idempotent_id('c01fd308-6484-11e6-881e-080027d0d606')
def test_show_image_member(self):
@@ -87,24 +86,16 @@
RBAC test for the glance get_member policy
"""
- try:
- image_id = self.create_image()['id']
- self.image_member_client.create_image_member(
- image_id,
- member=self.alt_tenant_id)
+ image_id = self.create_image()['id']
+ self.image_member_client.create_image_member(
+ image_id,
+ member=self.alt_tenant_id)
- # Toggle role and get image member
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.image_member_client.show_image_member(
- image_id,
- self.alt_tenant_id)
- except exceptions.NotFound as e:
- '''If the role doesn't have access to an image, a 404 exception is
- thrown when the roles tries to show an image member'''
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the image and tries to show "
- "image members")
- raise rbac_exceptions.RbacActionFailed(e)
+ # Toggle role and get image member
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.image_member_client.show_image_member(
+ image_id,
+ self.alt_tenant_id)
@rbac_rule_validation.action(service="glance",
rule="modify_member")
diff --git a/patrole_tempest_plugin/tests/api/network/test_floating_ips_rbac.py b/patrole_tempest_plugin/tests/api/network/test_floating_ips_rbac.py
index d186f38..bd562ec 100644
--- a/patrole_tempest_plugin/tests/api/network/test_floating_ips_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_floating_ips_rbac.py
@@ -19,9 +19,7 @@
from tempest import config
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -108,7 +106,9 @@
self.floating_ips_client.update_floatingip(
floating_ip['id'], port_id=None)
- @rbac_rule_validation.action(service="neutron", rule="get_floatingip")
+ @rbac_rule_validation.action(service="neutron",
+ rule="get_floatingip",
+ expected_error_code=404)
@decorators.idempotent_id('f8846fd0-c976-48fe-a148-105303931b32')
def test_show_floating_ip(self):
"""Show floating IP.
@@ -117,18 +117,12 @@
"""
floating_ip = self._create_floatingip()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
-
- try:
- # Show floating IP
- self.floating_ips_client.show_floatingip(floating_ip['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ # Show floating IP
+ self.floating_ips_client.show_floatingip(floating_ip['id'])
@rbac_rule_validation.action(service="neutron",
- rule="delete_floatingip")
+ rule="delete_floatingip",
+ expected_error_code=404)
@decorators.idempotent_id('2611b068-30d4-4241-a78f-1b801a14db7e')
def test_delete_floating_ip(self):
"""Delete floating IP.
@@ -137,13 +131,5 @@
"""
floating_ip = self._create_floatingip()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
-
- try:
- # Delete the floating IP
- self.floating_ips_client.delete_floatingip(floating_ip['id'])
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ # Delete the floating IP
+ self.floating_ips_client.delete_floatingip(floating_ip['id'])
diff --git a/patrole_tempest_plugin/tests/api/network/test_metering_label_rules_rbac.py b/patrole_tempest_plugin/tests/api/network/test_metering_label_rules_rbac.py
index 21c2f96..10a20f4 100644
--- a/patrole_tempest_plugin/tests/api/network/test_metering_label_rules_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_metering_label_rules_rbac.py
@@ -18,10 +18,8 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
from tempest import test
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -81,7 +79,8 @@
self._create_metering_label_rule(self.label)
@rbac_rule_validation.action(service="neutron",
- rule="get_metering_label_rule")
+ rule="get_metering_label_rule",
+ expected_error_code=404)
@decorators.idempotent_id('e21b40c3-d44d-412f-84ea-836ca8603bcb')
def test_show_metering_label_rule(self):
"""Show metering label rule.
@@ -90,17 +89,12 @@
"""
label_rule = self._create_metering_label_rule(self.label)
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.metering_label_rules_client.show_metering_label_rule(
- label_rule['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.metering_label_rules_client.show_metering_label_rule(
+ label_rule['id'])
@rbac_rule_validation.action(service="neutron",
- rule="delete_metering_label_rule")
+ rule="delete_metering_label_rule",
+ expected_error_code=404)
@decorators.idempotent_id('e3adc88c-05c0-43a7-8e32-63947ae4890e')
def test_delete_metering_label_rule(self):
"""Delete metering label rule.
@@ -109,11 +103,5 @@
"""
label_rule = self._create_metering_label_rule(self.label)
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.metering_label_rules_client.delete_metering_label_rule(
- label_rule['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.metering_label_rules_client.delete_metering_label_rule(
+ label_rule['id'])
diff --git a/patrole_tempest_plugin/tests/api/network/test_metering_labels_rbac.py b/patrole_tempest_plugin/tests/api/network/test_metering_labels_rbac.py
index 09e298c..eb17df4 100644
--- a/patrole_tempest_plugin/tests/api/network/test_metering_labels_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_metering_labels_rbac.py
@@ -17,10 +17,8 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
from tempest import test
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -63,7 +61,8 @@
self._create_metering_label()
@rbac_rule_validation.action(service="neutron",
- rule="get_metering_label")
+ rule="get_metering_label",
+ expected_error_code=404)
@decorators.idempotent_id('c57f6636-c702-4755-8eac-5e73bc1f7d14')
def test_show_metering_label(self):
"""Show metering label.
@@ -72,16 +71,11 @@
"""
label = self._create_metering_label()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.metering_labels_client.show_metering_label(label['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.metering_labels_client.show_metering_label(label['id'])
@rbac_rule_validation.action(service="neutron",
- rule="delete_metering_label")
+ rule="delete_metering_label",
+ expected_error_code=404)
@decorators.idempotent_id('1621ccfe-2e3f-4d16-98aa-b620f9d00404')
def test_delete_metering_label(self):
"""Delete metering label.
@@ -90,10 +84,4 @@
"""
label = self._create_metering_label()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.metering_labels_client.delete_metering_label(label['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.metering_labels_client.delete_metering_label(label['id'])
diff --git a/patrole_tempest_plugin/tests/api/network/test_ports_rbac.py b/patrole_tempest_plugin/tests/api/network/test_ports_rbac.py
index 518c71b..093459e 100644
--- a/patrole_tempest_plugin/tests/api/network/test_ports_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_ports_rbac.py
@@ -21,9 +21,7 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -143,61 +141,43 @@
self.rbac_utils.switch_role(self, switchToRbacRole=True)
self._create_port(**post_body)
- @rbac_rule_validation.action(service="neutron", rule="get_port")
+ @rbac_rule_validation.action(service="neutron",
+ rule="get_port",
+ expected_error_code=404)
@decorators.idempotent_id('a9d41cb8-78a2-4b97-985c-44e4064416f4')
def test_show_port(self):
-
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
-
- self.ports_client.show_port(self.admin_port['id'])
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.show_port(self.admin_port['id'])
@rbac_rule_validation.action(service="neutron",
- rule="get_port:binding:vif_type")
+ rule="get_port:binding:vif_type",
+ expected_error_code=404)
@decorators.idempotent_id('125aff0b-8fed-4f8e-8410-338616594b06')
def test_show_port_binding_vif_type(self):
# Verify specific fields of a port
fields = ['binding:vif_type']
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.ports_client.show_port(self.admin_port['id'],
- fields=fields)
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.show_port(self.admin_port['id'],
+ fields=fields)
@rbac_rule_validation.action(service="neutron",
- rule="get_port:binding:vif_details")
+ rule="get_port:binding:vif_details",
+ expected_error_code=404)
@decorators.idempotent_id('e42bfd77-fcce-45ee-9728-3424300f0d6f')
def test_show_port_binding_vif_details(self):
# Verify specific fields of a port
fields = ['binding:vif_details']
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.ports_client.show_port(self.admin_port['id'],
- fields=fields)
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.show_port(self.admin_port['id'],
+ fields=fields)
@rbac_rule_validation.action(service="neutron",
- rule="get_port:binding:host_id")
+ rule="get_port:binding:host_id",
+ expected_error_code=404)
@decorators.idempotent_id('8e61bcdc-6f81-443c-833e-44410266551e')
def test_show_port_binding_host_id(self):
@@ -207,19 +187,13 @@
'binding:host_id': data_utils.rand_name('host-id')}
port = self._create_port(**post_body)
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.ports_client.show_port(port['id'],
- fields=fields)
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.show_port(port['id'],
+ fields=fields)
@rbac_rule_validation.action(service="neutron",
- rule="get_port:binding:profile")
+ rule="get_port:binding:profile",
+ expected_error_code=404)
@decorators.idempotent_id('d497cea9-c4ad-42e0-acc9-8d257d6b01fc')
def test_show_port_binding_profile(self):
@@ -230,16 +204,9 @@
'binding:profile': binding_profile}
port = self._create_port(**post_body)
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.ports_client.show_port(port['id'],
- fields=fields)
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.show_port(port['id'],
+ fields=fields)
@rbac_rule_validation.action(service="neutron",
rule="update_port")
@@ -337,17 +304,11 @@
allowed_address_pairs=address_pairs)
@rbac_rule_validation.action(service="neutron",
- rule="delete_port")
+ rule="delete_port",
+ expected_error_code=404)
@decorators.idempotent_id('1cf8e582-bc09-46cb-b32a-82bf991ad56f')
def test_delete_port(self):
- try:
- port = self._create_port(network_id=self.admin_network['id'])
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.ports_client.delete_port(port['id'])
-
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ port = self._create_port(network_id=self.admin_network['id'])
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.ports_client.delete_port(port['id'])
diff --git a/patrole_tempest_plugin/tests/api/network/test_routers_rbac.py b/patrole_tempest_plugin/tests/api/network/test_routers_rbac.py
index a227e5c..69aa32a 100644
--- a/patrole_tempest_plugin/tests/api/network/test_routers_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_routers_rbac.py
@@ -21,10 +21,8 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
from tempest import test
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -96,6 +94,7 @@
create_router:external_gateway_info:external_fixed_ips policy
"""
name = data_utils.rand_name('snat-router')
+
# Pick an ip address within the allocation_pools range
ip_address = random.choice(list(self.admin_ip_range))
external_fixed_ips = {'subnet_id': self.admin_subnet['id'],
@@ -111,7 +110,9 @@
self.addCleanup(self.routers_client.delete_router,
router['router']['id'])
- @rbac_rule_validation.action(service="neutron", rule="get_router")
+ @rbac_rule_validation.action(service="neutron",
+ rule="get_router",
+ expected_error_code=404)
@decorators.idempotent_id('bfbdbcff-f115-4d3e-8cd5-6ada33fd0e21')
def test_show_router(self):
"""Get Router
@@ -119,13 +120,7 @@
RBAC test for the neutron get_router policy
"""
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.routers_client.show_router(self.admin_router['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.routers_client.show_router(self.admin_router['id'])
@rbac_rule_validation.action(
service="neutron", rule="update_router")
@@ -167,6 +162,10 @@
self.routers_client.update_router(
self.admin_router['id'],
external_gateway_info={'network_id': self.admin_network['id']})
+ self.addCleanup(
+ self.routers_client.update_router,
+ self.admin_router['id'],
+ external_gateway_info=None)
@rbac_rule_validation.action(
service="neutron",
@@ -183,6 +182,10 @@
self.admin_router['id'],
external_gateway_info={'network_id': self.admin_network['id'],
'enable_snat': True})
+ self.addCleanup(
+ self.routers_client.update_router,
+ self.admin_router['id'],
+ external_gateway_info=None)
@rbac_rule_validation.action(
service="neutron",
@@ -211,7 +214,8 @@
external_gateway_info=None)
@rbac_rule_validation.action(service="neutron",
- rule="delete_router")
+ rule="delete_router",
+ expected_error_code=404)
@decorators.idempotent_id('c0634dd5-0467-48f7-a4ae-1014d8edb2a7')
def test_delete_router(self):
"""Delete Router
@@ -220,16 +224,11 @@
"""
router = self.create_router()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.routers_client.delete_router(router['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.routers_client.delete_router(router['id'])
@rbac_rule_validation.action(service="neutron",
- rule="add_router_interface")
+ rule="add_router_interface",
+ expected_error_code=404)
@decorators.idempotent_id('a0627778-d68d-4913-881b-e345360cca19')
def test_add_router_interfaces(self):
"""Add Router Interface
@@ -241,22 +240,17 @@
router = self.create_router()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.routers_client.add_router_interface(
- router['id'], subnet_id=subnet['id'])
- self.addCleanup(
- test_utils.call_and_ignore_notfound_exc,
- self.routers_client.remove_router_interface,
- router['id'],
- subnet_id=subnet['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.routers_client.add_router_interface(
+ router['id'], subnet_id=subnet['id'])
+ self.addCleanup(
+ test_utils.call_and_ignore_notfound_exc,
+ self.routers_client.remove_router_interface,
+ router['id'],
+ subnet_id=subnet['id'])
@rbac_rule_validation.action(service="neutron",
- rule="remove_router_interface")
+ rule="remove_router_interface",
+ expected_error_code=404)
@decorators.idempotent_id('ff2593a4-2bff-4c27-97d3-dd3702b27dfb')
def test_remove_router_interfaces(self):
"""Remove Router Interface
@@ -276,12 +270,6 @@
subnet_id=subnet['id'])
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.routers_client.remove_router_interface(
- router['id'],
- subnet_id=subnet['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.routers_client.remove_router_interface(
+ router['id'],
+ subnet_id=subnet['id'])
diff --git a/patrole_tempest_plugin/tests/api/network/test_security_groups_rbac.py b/patrole_tempest_plugin/tests/api/network/test_security_groups_rbac.py
index cf76836..289c669 100644
--- a/patrole_tempest_plugin/tests/api/network/test_security_groups_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_security_groups_rbac.py
@@ -18,9 +18,7 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -80,53 +78,40 @@
self._create_security_group()
@rbac_rule_validation.action(service="neutron",
- rule="get_security_group")
+ rule="get_security_group",
+ expected_error_code=404)
@decorators.idempotent_id('56335e77-aef2-4b54-86c7-7f772034b585')
def test_show_security_groups(self):
- try:
- self.rbac_utils.switch_role(self, switchToRbacRole=True)
- self.security_groups_client.show_security_group(
- self.secgroup['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.rbac_utils.switch_role(self, switchToRbacRole=True)
+ self.security_groups_client.show_security_group(
+ self.secgroup['id'])
@rbac_rule_validation.action(service="neutron",
- rule="delete_security_group")
+ rule="delete_security_group",
+ expected_error_code=404)
@decorators.idempotent_id('0b1330fd-dd28-40f3-ad73-966052e4b3de')
def test_delete_security_group(self):
# Create a security group
secgroup_id = self._create_security_group()['id']
+
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.security_groups_client.delete_security_group(secgroup_id)
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.security_groups_client.delete_security_group(secgroup_id)
@rbac_rule_validation.action(service="neutron",
- rule="update_security_group")
+ rule="update_security_group",
+ expected_error_code=404)
@decorators.idempotent_id('56c5e4dc-f8aa-11e6-bc64-92361f002671')
def test_update_security_group(self):
# Create a security group
secgroup_id = self._create_security_group()['id']
+
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.security_groups_client.update_security_group(
- secgroup_id,
- description="test description")
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.security_groups_client.update_security_group(
+ secgroup_id,
+ description="test description")
@rbac_rule_validation.action(service="neutron",
rule="get_security_groups")
@@ -145,38 +130,26 @@
self._create_security_group_rule()
@rbac_rule_validation.action(service="neutron",
- rule="delete_security_group_rule")
+ rule="delete_security_group_rule",
+ expected_error_code=404)
@decorators.idempotent_id('2262539e-b7d9-438c-acf9-a5ce0613be28')
def test_delete_security_group_rule(self):
sec_group_rule = self._create_security_group_rule()
-
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.security_group_rules_client.delete_security_group_rule(
- sec_group_rule['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.security_group_rules_client.delete_security_group_rule(
+ sec_group_rule['id'])
@rbac_rule_validation.action(service="neutron",
- rule="get_security_group_rule")
+ rule="get_security_group_rule",
+ expected_error_code=404)
@decorators.idempotent_id('84b4038c-261e-4a94-90d5-c885739ab0d5')
def test_show_security_group_rule(self):
sec_group_rule = self._create_security_group_rule()
-
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.security_group_rules_client.show_security_group_rule(
- sec_group_rule['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.security_group_rules_client.show_security_group_rule(
+ sec_group_rule['id'])
@rbac_rule_validation.action(service="neutron",
rule="get_security_group_rules")
diff --git a/patrole_tempest_plugin/tests/api/network/test_subnetpools_rbac.py b/patrole_tempest_plugin/tests/api/network/test_subnetpools_rbac.py
index 35ad335..3667212 100644
--- a/patrole_tempest_plugin/tests/api/network/test_subnetpools_rbac.py
+++ b/patrole_tempest_plugin/tests/api/network/test_subnetpools_rbac.py
@@ -18,10 +18,8 @@
from tempest.lib.common.utils import data_utils
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
-from tempest.lib import exceptions
from tempest import test
-from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation
from patrole_tempest_plugin.tests.api.network import rbac_base as base
@@ -79,7 +77,8 @@
self._create_subnetpool(shared=True)
@rbac_rule_validation.action(service="neutron",
- rule="get_subnetpool")
+ rule="get_subnetpool",
+ expected_error_code=404)
@decorators.idempotent_id('4f5aee26-0507-4b6d-b44c-3128a25094d2')
def test_show_subnetpool(self):
"""Show subnetpool.
@@ -88,13 +87,7 @@
"""
subnetpool = self._create_subnetpool()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.subnetpools_client.show_subnetpool(subnetpool['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.subnetpools_client.show_subnetpool(subnetpool['id'])
@rbac_rule_validation.action(service="neutron",
rule="update_subnetpool")
@@ -110,7 +103,8 @@
min_prefixlen=24)
@rbac_rule_validation.action(service="neutron",
- rule="delete_subnetpool")
+ rule="delete_subnetpool",
+ expected_error_code=404)
@decorators.idempotent_id('50f5944e-43e5-457b-ab50-fb48a73f0d3e')
def test_delete_subnetpool(self):
"""Delete subnetpool.
@@ -119,10 +113,4 @@
"""
subnetpool = self._create_subnetpool()
self.rbac_utils.switch_role(self, switchToRbacRole=True)
- try:
- self.subnetpools_client.delete_subnetpool(subnetpool['id'])
- except exceptions.NotFound as e:
- LOG.info("NotFound exception caught. Exception is thrown when "
- "role doesn't have access to the endpoint."
- "This is irregular and should be fixed.")
- raise rbac_exceptions.RbacActionFailed(e)
+ self.subnetpools_client.delete_subnetpool(subnetpool['id'])
diff --git a/patrole_tempest_plugin/tests/unit/test_rbac_policy_parser.py b/patrole_tempest_plugin/tests/unit/test_rbac_policy_parser.py
index edc63b8..8583eb5 100644
--- a/patrole_tempest_plugin/tests/unit/test_rbac_policy_parser.py
+++ b/patrole_tempest_plugin/tests/unit/test_rbac_policy_parser.py
@@ -82,7 +82,7 @@
test_tenant_id = mock.sentinel.tenant_id
test_user_id = mock.sentinel.user_id
- converter = rbac_policy_parser.RbacPolicyParser(
+ parser = rbac_policy_parser.RbacPolicyParser(
test_tenant_id, test_user_id, "test", self.custom_policy_file)
expected = {
@@ -95,24 +95,16 @@
'policy_action_6': ['eight'],
}
- fake_rule = 'fake_rule'
-
- for role in default_roles:
- self.assertFalse(converter.allowed(fake_rule, role))
- m_log.debug.assert_called_once_with(
- "{0} not found in policy file.".format('fake_rule'))
- m_log.debug.reset_mock()
-
for rule, role_list in expected.items():
for role in role_list:
- self.assertTrue(converter.allowed(rule, role))
+ self.assertTrue(parser.allowed(rule, role))
for role in set(default_roles) - set(role_list):
- self.assertFalse(converter.allowed(rule, role))
+ self.assertFalse(parser.allowed(rule, role))
def test_admin_policy_file_with_admin_role(self):
test_tenant_id = mock.sentinel.tenant_id
test_user_id = mock.sentinel.user_id
- converter = rbac_policy_parser.RbacPolicyParser(
+ parser = rbac_policy_parser.RbacPolicyParser(
test_tenant_id, test_user_id, "test", self.admin_policy_file)
role = 'admin'
@@ -122,17 +114,17 @@
disallowed_rules = ['non_admin_rule']
for rule in allowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertTrue(allowed)
for rule in disallowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertFalse(allowed)
def test_admin_policy_file_with_member_role(self):
test_tenant_id = mock.sentinel.tenant_id
test_user_id = mock.sentinel.user_id
- converter = rbac_policy_parser.RbacPolicyParser(
+ parser = rbac_policy_parser.RbacPolicyParser(
test_tenant_id, test_user_id, "test", self.admin_policy_file)
role = 'Member'
@@ -143,17 +135,17 @@
'admin_rule', 'is_admin_rule', 'alt_admin_rule']
for rule in allowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertTrue(allowed)
for rule in disallowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertFalse(allowed)
def test_admin_policy_file_with_context_is_admin(self):
test_tenant_id = mock.sentinel.tenant_id
test_user_id = mock.sentinel.user_id
- converter = rbac_policy_parser.RbacPolicyParser(
+ parser = rbac_policy_parser.RbacPolicyParser(
test_tenant_id, test_user_id, "test", self.alt_admin_policy_file)
role = 'fake_admin'
@@ -161,11 +153,11 @@
disallowed_rules = ['admin_rule']
for rule in allowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertTrue(allowed)
for rule in disallowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertFalse(allowed)
role = 'super_admin'
@@ -173,11 +165,11 @@
disallowed_rules = ['non_admin_rule']
for rule in allowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertTrue(allowed)
for rule in disallowed_rules:
- allowed = converter.allowed(rule, role)
+ allowed = parser.allowed(rule, role)
self.assertFalse(allowed)
def test_tenant_user_policy(self):
@@ -189,28 +181,28 @@
"""
test_tenant_id = mock.sentinel.tenant_id
test_user_id = mock.sentinel.user_id
- converter = rbac_policy_parser.RbacPolicyParser(
+ parser = rbac_policy_parser.RbacPolicyParser(
test_tenant_id, test_user_id, "test", self.tenant_policy_file)
# Check whether Member role can perform expected actions.
allowed_rules = ['rule1', 'rule2', 'rule3', 'rule4']
for rule in allowed_rules:
- allowed = converter.allowed(rule, 'Member')
+ allowed = parser.allowed(rule, 'Member')
self.assertTrue(allowed)
disallowed_rules = ['admin_tenant_rule', 'admin_user_rule']
for disallowed_rule in disallowed_rules:
- self.assertFalse(converter.allowed(disallowed_rule, 'Member'))
+ self.assertFalse(parser.allowed(disallowed_rule, 'Member'))
# Check whether admin role can perform expected actions.
allowed_rules.extend(disallowed_rules)
for rule in allowed_rules:
- allowed = converter.allowed(rule, 'admin')
+ allowed = parser.allowed(rule, 'admin')
self.assertTrue(allowed)
# Check whether _try_rule is called with the correct target dictionary.
with mock.patch.object(
- converter, '_try_rule', return_value=True, autospec=True) \
+ parser, '_try_rule', return_value=True, autospec=True) \
as mock_try_rule:
expected_target = {
@@ -230,7 +222,7 @@
}
for rule in allowed_rules:
- allowed = converter.allowed(rule, 'Member')
+ allowed = parser.allowed(rule, 'Member')
self.assertTrue(allowed)
mock_try_rule.assert_called_once_with(
rule, expected_target, expected_access_data, mock.ANY)
@@ -265,3 +257,41 @@
m_log.debug.assert_called_once_with(
"{0} is NOT a valid service.".format(str(service)))
+
+ @mock.patch.object(rbac_policy_parser, 'LOG', autospec=True)
+ def test_invalid_policy_rule_throws_rbac_parsing_exception(self, m_log):
+ test_tenant_id = mock.sentinel.tenant_id
+ test_user_id = mock.sentinel.user_id
+
+ parser = rbac_policy_parser.RbacPolicyParser(
+ test_tenant_id, test_user_id, "test", self.custom_policy_file)
+
+ fake_rule = 'fake_rule'
+ expected_message = "Policy action: {0} not found in policy file: {1}."\
+ .format(fake_rule, self.custom_policy_file)
+
+ e = self.assertRaises(rbac_exceptions.RbacParsingException,
+ parser.allowed, fake_rule, None)
+ self.assertIn(expected_message, str(e))
+ m_log.debug.assert_called_once_with(expected_message)
+
+ @mock.patch.object(rbac_policy_parser, 'LOG', autospec=True)
+ def test_unknown_exception_throws_rbac_parsing_exception(self, m_log):
+ test_tenant_id = mock.sentinel.tenant_id
+ test_user_id = mock.sentinel.user_id
+
+ parser = rbac_policy_parser.RbacPolicyParser(
+ test_tenant_id, test_user_id, "test", self.custom_policy_file)
+ parser.rules = mock.MagicMock(
+ **{'__getitem__.return_value.side_effect': Exception(
+ mock.sentinel.error)})
+
+ expected_message = "Unknown exception: {0} for policy action: {1} in "\
+ "policy file: {2}.".format(mock.sentinel.error,
+ mock.sentinel.rule,
+ self.custom_policy_file)
+
+ e = self.assertRaises(rbac_exceptions.RbacParsingException,
+ parser.allowed, mock.sentinel.rule, None)
+ self.assertIn(expected_message, str(e))
+ m_log.debug.assert_called_once_with(expected_message)
diff --git a/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py b/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
index 1e78a7d..9fa0d98 100644
--- a/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
+++ b/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
@@ -13,14 +13,19 @@
# under the License.
import mock
+import testtools
+from patrole_tempest_plugin import rbac_auth
from patrole_tempest_plugin import rbac_exceptions
from patrole_tempest_plugin import rbac_rule_validation as rbac_rv
+from tempest import config
from tempest.lib import exceptions
from tempest import test
from tempest.tests import base
+CONF = config.CONF
+
class RBACRuleValidationTest(base.TestCase):
@@ -29,7 +34,14 @@
self.mock_args = mock.Mock(spec=test.BaseTestCase)
self.mock_args.auth_provider = mock.Mock()
self.mock_args.rbac_utils = mock.Mock()
- self.mock_args.auth_provider.credentials.tenant_id = 'tenant_id'
+ self.mock_args.auth_provider.credentials.tenant_id = \
+ mock.sentinel.tenant_id
+ self.mock_args.auth_provider.credentials.user_id = \
+ mock.sentinel.user_id
+
+ CONF.set_override('rbac_test_role', 'Member', group='rbac',
+ enforce_type=True)
+ self.addCleanup(CONF.clear_override, 'rbac_test_role', group='rbac')
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
def test_RBAC_rv_happy_path(self, mock_auth):
@@ -39,27 +51,58 @@
wrapper((self.mock_args))
self.assertTrue(mock_function.called)
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
- def test_RBAC_rv_forbidden(self, mock_auth):
- decorator = rbac_rv.action("", "")
+ def test_RBAC_rv_forbidden(self, mock_auth, mock_log):
+ decorator = rbac_rv.action(mock.sentinel.service, mock.sentinel.action)
mock_function = mock.Mock()
mock_function.side_effect = exceptions.Forbidden
wrapper = decorator(mock_function)
- self.assertRaises(exceptions.Forbidden, wrapper, self.mock_args)
+ e = self.assertRaises(exceptions.Forbidden, wrapper, self.mock_args)
+ self.assertIn(
+ "Role Member was not allowed to perform sentinel.action.",
+ e.__str__())
+ mock_log.error.assert_called_once_with("Role Member was not allowed to"
+ " perform sentinel.action.")
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
- def test_RBAC_rv_rbac_action_failed(self, mock_auth):
- decorator = rbac_rv.action("", "")
+ def test_expect_not_found_but_raises_forbidden(self, mock_auth, mock_log):
+ decorator = rbac_rv.action(mock.sentinel.service,
+ mock.sentinel.action,
+ 404)
+ mock_function = mock.Mock()
+ mock_function.side_effect = exceptions.NotFound
+ wrapper = decorator(mock_function)
+
+ e = self.assertRaises(exceptions.Forbidden, wrapper, self.mock_args)
+ self.assertIn(
+ "Role Member was not allowed to perform sentinel.action.",
+ e.__str__())
+ mock_log.error.assert_called_once_with("Role Member was not allowed to"
+ " perform sentinel.action.")
+
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
+ @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
+ def test_RBAC_rv_rbac_action_failed(self, mock_auth, mock_log):
+ decorator = rbac_rv.action(mock.sentinel.service, mock.sentinel.action)
mock_function = mock.Mock()
mock_function.side_effect = rbac_exceptions.RbacActionFailed
-
wrapper = decorator(mock_function)
- self.assertRaises(exceptions.Forbidden, wrapper, self.mock_args)
+ e = self.assertRaises(exceptions.Forbidden, wrapper, self.mock_args)
+ self.assertIn(
+ "Role Member was not allowed to perform sentinel.action.",
+ e.__str__())
+
+ mock_log.error.assert_called_once_with("Role Member was not allowed to"
+ " perform sentinel.action.")
+
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
- def test_RBAC_rv_not_allowed(self, mock_auth):
- decorator = rbac_rv.action("", "")
+ def test_RBAC_rv_not_allowed(self, mock_auth, mock_log):
+ decorator = rbac_rv.action(mock.sentinel.service, mock.sentinel.action)
mock_function = mock.Mock()
wrapper = decorator(mock_function)
@@ -68,8 +111,13 @@
mock_permission.get_permission.return_value = False
mock_auth.return_value = mock_permission
- self.assertRaises(rbac_exceptions.RbacOverPermission, wrapper,
- self.mock_args)
+ e = self.assertRaises(rbac_exceptions.RbacOverPermission, wrapper,
+ self.mock_args)
+ self.assertIn(("OverPermission: Role Member was allowed to perform "
+ "sentinel.action"), e.__str__())
+
+ mock_log.error.assert_called_once_with(
+ "Role Member was allowed to perform sentinel.action")
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
def test_RBAC_rv_forbidden_not_allowed(self, mock_auth):
@@ -85,6 +133,29 @@
self.assertIsNone(wrapper(self.mock_args))
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
+ @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
+ def test_expect_not_found_and_not_allowed(self, mock_auth, mock_log):
+ decorator = rbac_rv.action(mock.sentinel.service,
+ mock.sentinel.action,
+ 404)
+
+ mock_function = mock.Mock()
+ mock_function.side_effect = exceptions.NotFound
+ wrapper = decorator(mock_function)
+
+ mock_permission = mock.Mock()
+ mock_permission.get_permission.return_value = False
+ mock_auth.return_value = mock_permission
+
+ self.assertIsNone(wrapper(self.mock_args))
+
+ mock_log.warning.assert_called_once_with(
+ 'NotFound exception was caught for policy action sentinel.action. '
+ 'The service sentinel.service throws a 404 instead of a 403, '
+ 'which is irregular.')
+ mock_log.error.assert_not_called()
+
@mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
def test_RBAC_rv_rbac_action_failed_not_allowed(self, mock_auth):
decorator = rbac_rv.action("", "")
@@ -98,3 +169,56 @@
mock_auth.return_value = mock_permission
self.assertIsNone(wrapper(self.mock_args))
+
+ @mock.patch.object(rbac_auth, 'rbac_policy_parser', autospec=True)
+ def test_invalid_policy_rule_throws_skip_exception(
+ self, mock_rbac_policy_parser):
+ mock_rbac_policy_parser.RbacPolicyParser.return_value.allowed.\
+ side_effect = rbac_exceptions.RbacParsingException
+
+ decorator = rbac_rv.action(mock.sentinel.service,
+ mock.sentinel.policy_rule)
+ wrapper = decorator(mock.Mock())
+
+ e = self.assertRaises(testtools.TestCase.skipException, wrapper,
+ self.mock_args)
+ self.assertEqual('Attempted to test an invalid policy file or action',
+ str(e))
+
+ mock_rbac_policy_parser.RbacPolicyParser.assert_called_once_with(
+ mock.sentinel.tenant_id, mock.sentinel.user_id,
+ mock.sentinel.service)
+
+ @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
+ def test_get_exception_type_404(self, mock_auth):
+ expected_exception = exceptions.NotFound
+ expected_irregular_msg = ("NotFound exception was caught for policy "
+ "action {0}. The service {1} throws a 404 "
+ "instead of a 403, which is irregular.")
+
+ actual_exception, actual_irregular_msg = \
+ rbac_rv._get_exception_type(404)
+
+ self.assertEqual(expected_exception, actual_exception)
+ self.assertEqual(expected_irregular_msg, actual_irregular_msg)
+
+ @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
+ def test_get_exception_type_403(self, mock_auth):
+ expected_exception = exceptions.Forbidden
+ expected_irregular_msg = None
+
+ actual_exception, actual_irregular_msg = \
+ rbac_rv._get_exception_type(403)
+
+ self.assertEqual(expected_exception, actual_exception)
+ self.assertEqual(expected_irregular_msg, actual_irregular_msg)
+
+ @mock.patch.object(rbac_rv, 'LOG', autospec=True)
+ @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority')
+ def test_exception_thrown_when_type_is_not_int(self, mock_auth, mock_log):
+ self.assertRaises(rbac_exceptions.RbacInvalidErrorCode,
+ rbac_rv._get_exception_type, "403")
+
+ mock_log.error.assert_called_once_with("Please pass an expected error "
+ "code. Currently supported "
+ "codes: [403, 404]")