blob: e11ae4c4b8adc4d6545e8e3c08d40768b45831fa [file] [log] [blame]
DavidPurcell029d8c32017-01-06 15:27:41 -05001# Copyright 2017 AT&T Corp
2# 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
Rajiv Kumar645dfc92017-01-19 13:48:27 +053016from oslo_log import log as logging
DavidPurcell029d8c32017-01-06 15:27:41 -050017from tempest import config
18from tempest.lib import exceptions
19
20from patrole_tempest_plugin import rbac_auth
21from patrole_tempest_plugin import rbac_exceptions
22
23CONF = config.CONF
24LOG = logging.getLogger(__name__)
25
26
27def action(component, service, rule):
28 def decorator(func):
29 def wrapper(*args, **kwargs):
30 authority = rbac_auth.RbacAuthority(component, service)
31 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
32
33 try:
34 func(*args)
35 except exceptions.Forbidden as e:
36 if allowed:
37 msg = ("Role %s was not allowed to perform %s." %
38 (CONF.rbac.rbac_test_role, rule))
39 LOG.error(msg)
40 raise exceptions.Forbidden(
41 "%s exception was: %s" %
42 (msg, e))
43 except rbac_exceptions.RbacActionFailed 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 RbacActionFailed was: %s" %
50 (msg, e))
51 else:
52 if not allowed:
53 LOG.error("Role %s was allowed to perform %s" %
54 (CONF.rbac.rbac_test_role, rule))
55 raise rbac_exceptions.RbacOverPermission(
56 "OverPermission: Role %s was allowed to perform %s" %
57 (CONF.rbac.rbac_test_role, rule))
58 return wrapper
59 return decorator