blob: 728196992aead5d54be116fefaf099c47964c376 [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 Monteiro48c913d2017-03-15 12:07:48 -040016import testtools
17
DavidPurcell029d8c32017-01-06 15:27:41 -050018from oslo_log import log as logging
DavidPurcell029d8c32017-01-06 15:27:41 -050019
Samantha Blanco0d880082017-03-23 18:14:37 -040020from tempest import config
21
Felipe Monteiro48c913d2017-03-15 12:07:48 -040022from patrole_tempest_plugin import rbac_exceptions
Felipe Monteiro322c5b62017-02-26 02:44:21 +000023from patrole_tempest_plugin import rbac_policy_parser
DavidPurcell029d8c32017-01-06 15:27:41 -050024
25LOG = logging.getLogger(__name__)
Samantha Blanco0d880082017-03-23 18:14:37 -040026CONF = config.CONF
DavidPurcell029d8c32017-01-06 15:27:41 -050027
28
29class RbacAuthority(object):
Felipe Monteiro889264e2017-03-01 17:19:35 -050030 def __init__(self, tenant_id, user_id, service=None):
Felipe Monteiro48c913d2017-03-15 12:07:48 -040031 self.policy_parser = rbac_policy_parser.RbacPolicyParser(
Felipe Monteiro889264e2017-03-01 17:19:35 -050032 tenant_id, user_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050033
Felipe Monteirob0595652017-01-23 16:51:58 -050034 def get_permission(self, rule_name, role):
DavidPurcell029d8c32017-01-06 15:27:41 -050035 try:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040036 is_allowed = self.policy_parser.allowed(rule_name, role)
Felipe Monteirob0595652017-01-23 16:51:58 -050037 if is_allowed:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040038 LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule_name,
39 role)
DavidPurcell029d8c32017-01-06 15:27:41 -050040 else:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040041 LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!",
Felipe Monteirob0595652017-01-23 16:51:58 -050042 rule_name, role)
43 return is_allowed
Felipe Monteiro48c913d2017-03-15 12:07:48 -040044 except rbac_exceptions.RbacParsingException as e:
Samantha Blanco0d880082017-03-23 18:14:37 -040045 if CONF.rbac.strict_policy_check:
46 raise e
47 else:
48 raise testtools.TestCase.skipException(str(e))
DavidPurcell029d8c32017-01-06 15:27:41 -050049 return False