blob: 687c0a8db69e51f4aca0ba66f7ae22e22fdfee01 [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
Felipe Monteiro48c913d2017-03-15 12:07:48 -040020from patrole_tempest_plugin import rbac_exceptions
Felipe Monteiro322c5b62017-02-26 02:44:21 +000021from patrole_tempest_plugin import rbac_policy_parser
DavidPurcell029d8c32017-01-06 15:27:41 -050022
23LOG = logging.getLogger(__name__)
24
25
26class RbacAuthority(object):
Felipe Monteiro889264e2017-03-01 17:19:35 -050027 def __init__(self, tenant_id, user_id, service=None):
Felipe Monteiro48c913d2017-03-15 12:07:48 -040028 self.policy_parser = rbac_policy_parser.RbacPolicyParser(
Felipe Monteiro889264e2017-03-01 17:19:35 -050029 tenant_id, user_id, service)
DavidPurcell029d8c32017-01-06 15:27:41 -050030
Felipe Monteirob0595652017-01-23 16:51:58 -050031 def get_permission(self, rule_name, role):
DavidPurcell029d8c32017-01-06 15:27:41 -050032 try:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040033 is_allowed = self.policy_parser.allowed(rule_name, role)
Felipe Monteirob0595652017-01-23 16:51:58 -050034 if is_allowed:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040035 LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule_name,
36 role)
DavidPurcell029d8c32017-01-06 15:27:41 -050037 else:
Felipe Monteiro48c913d2017-03-15 12:07:48 -040038 LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!",
Felipe Monteirob0595652017-01-23 16:51:58 -050039 rule_name, role)
40 return is_allowed
Felipe Monteiro48c913d2017-03-15 12:07:48 -040041 except rbac_exceptions.RbacParsingException as e:
42 raise testtools.TestCase.skipException(str(e))
DavidPurcell029d8c32017-01-06 15:27:41 -050043 return False