blob: 7f9d4d2427ebaa088fd1a608f70dce7fc5fe34d5 [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
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 Monteirob0595652017-01-23 16:51:58 -050031 authority = rbac_auth.RbacAuthority(
32 args[0].auth_provider.credentials.tenant_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050033 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
34
35 try:
36 func(*args)
37 except exceptions.Forbidden as e:
38 if allowed:
39 msg = ("Role %s was not allowed to perform %s." %
40 (CONF.rbac.rbac_test_role, rule))
41 LOG.error(msg)
42 raise exceptions.Forbidden(
43 "%s exception was: %s" %
44 (msg, e))
45 except rbac_exceptions.RbacActionFailed as e:
46 if allowed:
47 msg = ("Role %s was not allowed to perform %s." %
48 (CONF.rbac.rbac_test_role, rule))
49 LOG.error(msg)
50 raise exceptions.Forbidden(
51 "%s RbacActionFailed was: %s" %
52 (msg, e))
53 else:
54 if not allowed:
55 LOG.error("Role %s was allowed to perform %s" %
56 (CONF.rbac.rbac_test_role, rule))
57 raise rbac_exceptions.RbacOverPermission(
58 "OverPermission: Role %s was allowed to perform %s" %
59 (CONF.rbac.rbac_test_role, rule))
60 return wrapper
61 return decorator