Пример #1
0
 def test_templatized_authorization(self):
     target_mine = {'project_id': 'fake'}
     target_not_mine = {'project_id': 'another'}
     action = "test:my_file"
     policy.authorize(self.context, action, target_mine)
     self.assertRaises(exception.PolicyNotAuthorized, policy.authorize,
                       self.context, action, target_not_mine)
Пример #2
0
 def test_templatized_authorization(self):
     target_mine = {'project_id': 'fake'}
     target_not_mine = {'project_id': 'another'}
     action = "test:my_file"
     policy.authorize(self.context, action, target_mine)
     self.assertRaises(exception.PolicyNotAuthorized, policy.authorize,
                       self.context, action, target_not_mine)
Пример #3
0
 def test_ignore_case_role_check(self):
     lowercase_action = "test:lowercase_admin"
     uppercase_action = "test:uppercase_admin"
     admin_context = context.RequestContext('admin',
                                            'fake',
                                            roles=['AdMiN'])
     policy.authorize(admin_context, lowercase_action, self.target)
     policy.authorize(admin_context, uppercase_action, self.target)
Пример #4
0
 def test_ignore_case_role_check(self):
     lowercase_action = "test:lowercase_admin"
     uppercase_action = "test:uppercase_admin"
     admin_context = context.RequestContext('admin',
                                            'fake',
                                            roles=['AdMiN'])
     policy.authorize(admin_context, lowercase_action, self.target)
     policy.authorize(admin_context, uppercase_action, self.target)
Пример #5
0
    def authorize(self, action, target=None, target_obj=None, fatal=True):
        """Verifies that the given action is valid on the target in this context.

        :param action: string representing the action to be checked.
        :param target: dictionary representing the object of the action
            for object creation this should be a dictionary representing the
            location of the object e.g. ``{'project_id': context.project_id}``.
            If None, then this default target will be considered:
            {'project_id': self.project_id, 'user_id': self.user_id}
        :param: target_obj: dictionary representing the object which will be
            used to update target.
        :param fatal: if False, will return False when an
            exception.NotAuthorized occurs.

        :raises cinder.exception.NotAuthorized: if verification fails and fatal
            is True.

        :return: returns a non-False value (not necessarily "True") if
            authorized and False if not authorized and fatal is False.
        """
        if target is None:
            target = {'project_id': self.project_id, 'user_id': self.user_id}
        if isinstance(target_obj, objects_base.CinderObject):
            # Turn object into dict so target.update can work
            target.update(
                target_obj.obj_to_primitive()['versioned_object.data'] or {})
        else:
            target.update(target_obj or {})
        try:
            return policy.authorize(self, action, target)
        except exception.NotAuthorized:
            if fatal:
                raise
            return False
Пример #6
0
    def authorize(self, action, target=None, target_obj=None, fatal=True):
        """Verifies that the given action is valid on the target in this context.

        :param action: string representing the action to be checked.
        :param target: dictionary representing the object of the action
            for object creation this should be a dictionary representing the
            location of the object e.g. ``{'project_id': context.project_id}``.
            If None, then this default target will be considered:
            {'project_id': self.project_id, 'user_id': self.user_id}
        :param: target_obj: dictionary representing the object which will be
            used to update target.
        :param fatal: if False, will return False when an
            exception.PolicyNotAuthorized occurs.

        :raises cinder.exception.NotAuthorized: if verification fails and fatal
            is True.

        :return: returns a non-False value (not necessarily "True") if
            authorized and False if not authorized and fatal is False.
        """
        if target is None:
            target = {'project_id': self.project_id,
                      'user_id': self.user_id}
        if isinstance(target_obj, objects_base.CinderObject):
            # Turn object into dict so target.update can work
            target.update(
                target_obj.obj_to_primitive()['versioned_object.data'] or {})
        else:
            target.update(target_obj or {})

        return policy.authorize(self, action, target, do_raise=fatal,
                                exc=exception.PolicyNotAuthorized)
Пример #7
0
    def test_modified_policy_reloads(self):
        with utils.tempdir() as tmpdir:
            tmpfilename = os.path.join(tmpdir, 'policy')
            self.fixture.config(policy_file=tmpfilename, group='oslo_policy')
            policy.reset()
            policy.init()
            rule = oslo_policy.RuleDefault('example:test', "")
            policy._ENFORCER.register_defaults([rule])

            action = "example:test"
            with open(tmpfilename, "w") as policyfile:
                policyfile.write('{"example:test": ""}')
            policy.authorize(self.context, action, self.target)
            with open(tmpfilename, "w") as policyfile:
                policyfile.write('{"example:test": "!"}')
            policy._ENFORCER.load_rules(True)
            self.assertRaises(exception.PolicyNotAuthorized, policy.authorize,
                              self.context, action, self.target)
Пример #8
0
    def test_modified_policy_reloads(self):
        with utils.tempdir() as tmpdir:
            tmpfilename = os.path.join(tmpdir, 'policy')
            self.fixture.config(policy_file=tmpfilename, group='oslo_policy')
            policy.reset()
            policy.init()
            rule = oslo_policy.RuleDefault('example:test', "")
            policy._ENFORCER.register_defaults([rule])

            action = "example:test"
            with open(tmpfilename, "w") as policyfile:
                policyfile.write('{"example:test": ""}')
            policy.authorize(self.context, action, self.target)
            with open(tmpfilename, "w") as policyfile:
                policyfile.write('{"example:test": "!"}')
            policy._ENFORCER.load_rules(True)
            self.assertRaises(exception.PolicyNotAuthorized,
                              policy.authorize,
                              self.context, action, self.target)
Пример #9
0
    def authorize(self,
                  action: str,
                  target: Optional[dict] = None,
                  target_obj: Optional[dict] = None,
                  fatal: bool = True):
        """Verify that the given action is valid on the target in this context.

        :param action: string representing the action to be checked.
        :param target: dictionary representing the object of the action
            for object creation this should be a dictionary representing the
            location of the object e.g. ``{'project_id': context.project_id}``.
            If None, then this default target will be considered:
            {'project_id': self.project_id, 'user_id': self.user_id}
        :param target_obj: dictionary representing the object which will be
            used to update target.
        :param fatal: if False, will return False when an
            exception.PolicyNotAuthorized occurs.

        :raises cinder.exception.NotAuthorized: if verification fails and fatal
            is True.

        :return: returns a non-False value (not necessarily "True") if
            authorized and False if not authorized and fatal is False.
        """
        if target is None:
            target = {'project_id': self.project_id, 'user_id': self.user_id}
        if isinstance(target_obj, objects_base.CinderObject):
            # Turn object into dict so target.update can work
            target.update(
                target_obj.obj_to_primitive()['versioned_object.data'] or {})

            # Ensure 'project_id' and 'user_id' attributes are captured.
            # Some objects (e.g. attachments) have a project_id attribute
            # that isn't present in the dict. The try/except wrappers avoid
            # lazy-load issues when the attribute doesn't exist.
            try:
                target['project_id'] = target_obj.project_id
            except Exception:
                pass
            try:
                target['user_id'] = target_obj.user_id
            except Exception:
                pass
        else:
            target.update(target_obj or {})

        return policy.authorize(self,
                                action,
                                target,
                                do_raise=fatal,
                                exc=exception.PolicyNotAuthorized)
Пример #10
0
 def test_authorize_bad_action_noraise(self):
     action = "test:denied"
     result = policy.authorize(self.context, action, self.target, False)
     self.assertFalse(result)
Пример #11
0
 def test_early_OR_authorization(self):
     action = "test:early_or_success"
     policy.authorize(self.context, action, self.target)
Пример #12
0
 def test_authorize_good_action(self):
     action = "test:allowed"
     result = policy.authorize(self.context, action, self.target)
     self.assertTrue(result)
Пример #13
0
 def test_authorize_good_action(self):
     action = "test:allowed"
     result = policy.authorize(self.context, action, self.target)
     self.assertTrue(result)
Пример #14
0
 def test_authorize_bad_action_noraise(self):
     action = "test:denied"
     result = policy.authorize(self.context, action, self.target, False)
     self.assertFalse(result)
Пример #15
0
 def test_early_OR_authorization(self):
     action = "test:early_or_success"
     policy.authorize(self.context, action, self.target)