blob: 7785eea6e7adf042680d1f57dd4224a512228391 [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
16import logging
17
18from 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
28def action(component, service, rule):
29 def decorator(func):
30 def wrapper(*args, **kwargs):
31 authority = rbac_auth.RbacAuthority(component, service)
32 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
33
34 try:
35 func(*args)
36 except exceptions.Forbidden as e:
37 if allowed:
38 msg = ("Role %s was not allowed to perform %s." %
39 (CONF.rbac.rbac_test_role, rule))
40 LOG.error(msg)
41 raise exceptions.Forbidden(
42 "%s exception was: %s" %
43 (msg, e))
44 except rbac_exceptions.RbacActionFailed as e:
45 if allowed:
46 msg = ("Role %s was not allowed to perform %s." %
47 (CONF.rbac.rbac_test_role, rule))
48 LOG.error(msg)
49 raise exceptions.Forbidden(
50 "%s RbacActionFailed was: %s" %
51 (msg, e))
52 else:
53 if not allowed:
54 LOG.error("Role %s was allowed to perform %s" %
55 (CONF.rbac.rbac_test_role, rule))
56 raise rbac_exceptions.RbacOverPermission(
57 "OverPermission: Role %s was allowed to perform %s" %
58 (CONF.rbac.rbac_test_role, rule))
59 return wrapper
60 return decorator