DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 1 | # 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 Kumar | 645dfc9 | 2017-01-19 13:48:27 +0530 | [diff] [blame] | 16 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 17 | from tempest import config |
| 18 | from tempest.lib import exceptions |
| 19 | |
| 20 | from patrole_tempest_plugin import rbac_auth |
| 21 | from patrole_tempest_plugin import rbac_exceptions |
| 22 | |
| 23 | CONF = config.CONF |
| 24 | LOG = logging.getLogger(__name__) |
| 25 | |
| 26 | |
| 27 | def 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 |