blob: 4b85187e8b84973736aa4306aaa3d4938e1e5824 [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
20
21from patrole_tempest_plugin import rbac_auth
22from patrole_tempest_plugin import rbac_exceptions
23
24CONF = config.CONF
25LOG = logging.getLogger(__name__)
26
27
Felipe Monteirob0595652017-01-23 16:51:58 -050028def action(service, rule):
DavidPurcell029d8c32017-01-06 15:27:41 -050029 def decorator(func):
30 def wrapper(*args, **kwargs):
Felipe Monteirocbd06172017-01-24 13:49:16 -050031 try:
32 tenant_id = args[0].auth_provider.credentials.tenant_id
33 except (IndexError, AttributeError) as e:
34 msg = ("{0}: tenant_id not found in "
35 "cls.auth_provider.credentials".format(e))
36 LOG.error(msg)
37 raise rbac_exceptions.RbacResourceSetupFailed(msg)
38 authority = rbac_auth.RbacAuthority(tenant_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050039 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
40
41 try:
42 func(*args)
43 except exceptions.Forbidden as e:
44 if allowed:
45 msg = ("Role %s was not allowed to perform %s." %
46 (CONF.rbac.rbac_test_role, rule))
47 LOG.error(msg)
48 raise exceptions.Forbidden(
49 "%s exception was: %s" %
50 (msg, e))
51 except rbac_exceptions.RbacActionFailed as e:
52 if allowed:
53 msg = ("Role %s was not allowed to perform %s." %
54 (CONF.rbac.rbac_test_role, rule))
55 LOG.error(msg)
56 raise exceptions.Forbidden(
57 "%s RbacActionFailed was: %s" %
58 (msg, e))
59 else:
60 if not allowed:
61 LOG.error("Role %s was allowed to perform %s" %
62 (CONF.rbac.rbac_test_role, rule))
63 raise rbac_exceptions.RbacOverPermission(
64 "OverPermission: Role %s was allowed to perform %s" %
65 (CONF.rbac.rbac_test_role, rule))
66 return wrapper
67 return decorator