blob: a6490e7c2430261db6c9318a647e94b1350e6150 [file] [log] [blame]
DavidPurcellb25f93d2017-01-27 12:46:27 -05001# Copyright 2017 AT&T Corporation.
DavidPurcell029d8c32017-01-06 15:27:41 -05002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Felipe Monteirob0595652017-01-23 16:51:58 -050016import logging
17
DavidPurcell029d8c32017-01-06 15:27:41 -050018from tempest import config
19from tempest.lib import exceptions
raiesmh088590c0c2017-03-14 18:06:52 +053020from tempest import test
DavidPurcell029d8c32017-01-06 15:27:41 -050021
22from patrole_tempest_plugin import rbac_auth
23from patrole_tempest_plugin import rbac_exceptions
24
25CONF = config.CONF
26LOG = logging.getLogger(__name__)
27
28
Felipe Monteirob0595652017-01-23 16:51:58 -050029def action(service, rule):
DavidPurcell029d8c32017-01-06 15:27:41 -050030 def decorator(func):
31 def wrapper(*args, **kwargs):
Felipe Monteirocbd06172017-01-24 13:49:16 -050032 try:
raiesmh088590c0c2017-03-14 18:06:52 +053033 caller_ref = None
34 if args and isinstance(args[0], test.BaseTestCase):
35 caller_ref = args[0]
36 tenant_id = caller_ref.auth_provider.credentials.tenant_id
37 user_id = caller_ref.auth_provider.credentials.user_id
38 except AttributeError as e:
Felipe Monteiro889264e2017-03-01 17:19:35 -050039 msg = ("{0}: tenant_id/user_id not found in "
Felipe Monteirocbd06172017-01-24 13:49:16 -050040 "cls.auth_provider.credentials".format(e))
41 LOG.error(msg)
42 raise rbac_exceptions.RbacResourceSetupFailed(msg)
raiesmh088590c0c2017-03-14 18:06:52 +053043
Felipe Monteiro889264e2017-03-01 17:19:35 -050044 authority = rbac_auth.RbacAuthority(tenant_id, user_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050045 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
46
47 try:
48 func(*args)
Rick Bartra503c5572017-03-09 13:49:58 -050049 except rbac_exceptions.RbacInvalidService as e:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040050 msg = ("%s is not a valid service." % service)
51 LOG.error(msg)
52 raise exceptions.NotFound(
53 "%s RbacInvalidService was: %s" %
54 (msg, e))
DavidPurcell029d8c32017-01-06 15:27:41 -050055 except exceptions.Forbidden as e:
56 if allowed:
57 msg = ("Role %s was not allowed to perform %s." %
58 (CONF.rbac.rbac_test_role, rule))
59 LOG.error(msg)
60 raise exceptions.Forbidden(
61 "%s exception was: %s" %
62 (msg, e))
63 except rbac_exceptions.RbacActionFailed as e:
64 if allowed:
65 msg = ("Role %s was not allowed to perform %s." %
66 (CONF.rbac.rbac_test_role, rule))
67 LOG.error(msg)
68 raise exceptions.Forbidden(
69 "%s RbacActionFailed was: %s" %
70 (msg, e))
71 else:
72 if not allowed:
73 LOG.error("Role %s was allowed to perform %s" %
74 (CONF.rbac.rbac_test_role, rule))
75 raise rbac_exceptions.RbacOverPermission(
76 "OverPermission: Role %s was allowed to perform %s" %
77 (CONF.rbac.rbac_test_role, rule))
raiesmh088590c0c2017-03-14 18:06:52 +053078 finally:
79 caller_ref.rbac_utils.switch_role(caller_ref,
80 switchToRbacRole=False)
DavidPurcell029d8c32017-01-06 15:27:41 -050081 return wrapper
82 return decorator