blob: 284d8f096950c997944112067c8576fbaf89041d [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
Felipe Monteiro889264e2017-03-01 17:19:35 -050033 user_id = args[0].auth_provider.credentials.user_id
Felipe Monteirocbd06172017-01-24 13:49:16 -050034 except (IndexError, AttributeError) as e:
Felipe Monteiro889264e2017-03-01 17:19:35 -050035 msg = ("{0}: tenant_id/user_id not found in "
Felipe Monteirocbd06172017-01-24 13:49:16 -050036 "cls.auth_provider.credentials".format(e))
37 LOG.error(msg)
38 raise rbac_exceptions.RbacResourceSetupFailed(msg)
Felipe Monteiro889264e2017-03-01 17:19:35 -050039 authority = rbac_auth.RbacAuthority(tenant_id, user_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050040 allowed = authority.get_permission(rule, CONF.rbac.rbac_test_role)
41
42 try:
43 func(*args)
Rick Bartra503c5572017-03-09 13:49:58 -050044 except rbac_exceptions.RbacInvalidService as e:
45 msg = ("%s is not a valid service." % service)
46 LOG.error(msg)
47 raise exceptions.NotFound(
48 "%s RbacInvalidService was: %s" %
49 (msg, e))
DavidPurcell029d8c32017-01-06 15:27:41 -050050 except exceptions.Forbidden as e:
51 if allowed:
52 msg = ("Role %s was not allowed to perform %s." %
53 (CONF.rbac.rbac_test_role, rule))
54 LOG.error(msg)
55 raise exceptions.Forbidden(
56 "%s exception was: %s" %
57 (msg, e))
58 except rbac_exceptions.RbacActionFailed as e:
59 if allowed:
60 msg = ("Role %s was not allowed to perform %s." %
61 (CONF.rbac.rbac_test_role, rule))
62 LOG.error(msg)
63 raise exceptions.Forbidden(
64 "%s RbacActionFailed was: %s" %
65 (msg, e))
66 else:
67 if not allowed:
68 LOG.error("Role %s was allowed to perform %s" %
69 (CONF.rbac.rbac_test_role, rule))
70 raise rbac_exceptions.RbacOverPermission(
71 "OverPermission: Role %s was allowed to perform %s" %
72 (CONF.rbac.rbac_test_role, rule))
73 return wrapper
74 return decorator