DavidPurcell | b25f93d | 2017-01-27 12:46:27 -0500 | [diff] [blame] | 1 | # Copyright 2017 AT&T Corporation. |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 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 Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 16 | import testtools |
| 17 | |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 18 | from oslo_log import log as logging |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 19 | |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 20 | from patrole_tempest_plugin import rbac_exceptions |
Felipe Monteiro | 322c5b6 | 2017-02-26 02:44:21 +0000 | [diff] [blame] | 21 | from patrole_tempest_plugin import rbac_policy_parser |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | class RbacAuthority(object): |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 27 | def __init__(self, tenant_id, user_id, service=None): |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 28 | self.policy_parser = rbac_policy_parser.RbacPolicyParser( |
Felipe Monteiro | 889264e | 2017-03-01 17:19:35 -0500 | [diff] [blame] | 29 | tenant_id, user_id, service) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 30 | |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 31 | def get_permission(self, rule_name, role): |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 32 | try: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 33 | is_allowed = self.policy_parser.allowed(rule_name, role) |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 34 | if is_allowed: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 35 | LOG.debug("[Action]: %s, [Role]: %s is allowed!", rule_name, |
| 36 | role) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 37 | else: |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 38 | LOG.debug("[Action]: %s, [Role]: %s is NOT allowed!", |
Felipe Monteiro | b059565 | 2017-01-23 16:51:58 -0500 | [diff] [blame] | 39 | rule_name, role) |
| 40 | return is_allowed |
Felipe Monteiro | 48c913d | 2017-03-15 12:07:48 -0400 | [diff] [blame^] | 41 | except rbac_exceptions.RbacParsingException as e: |
| 42 | raise testtools.TestCase.skipException(str(e)) |
DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 43 | return False |