How to use do_raise method in Slash

Best Python code snippet using slash

policy.py

Source:policy.py Github

copy

Full Screen

1# Copyright (c) 2011 OpenStack, LLC.2# All Rights Reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License"); you may5# not use this file except in compliance with the License. You may obtain6# a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13# License for the specific language governing permissions and limitations14# under the License.15"""Policy Engine For Manila"""16import functools17from oslo_config import cfg18from oslo_policy import policy19from manila import exception20CONF = cfg.CONF21_ENFORCER = None22def reset():23 global _ENFORCER24 if _ENFORCER:25 _ENFORCER.clear()26 _ENFORCER = None27def init(policy_path=None):28 global _ENFORCER29 if not _ENFORCER:30 _ENFORCER = policy.Enforcer(CONF)31 if policy_path:32 _ENFORCER.policy_path = policy_path33 _ENFORCER.load_rules()34def enforce(context, action, target, do_raise=True):35 """Verifies that the action is valid on the target in this context.36 :param context: manila context37 :param action: string representing the action to be checked,38 this should be colon separated for clarity.39 i.e. ``compute:create_instance``,40 ``compute:attach_volume``,41 ``volume:attach_volume``42 :param target: dictionary representing the object of the action43 for object creation, this should be a dictionary representing the44 location of the object e.g. ``{'project_id': context.project_id}``45 :param do_raise: Whether to raise an exception if check fails.46 :returns: When ``do_raise`` is ``False``, returns a value that47 evaluates as ``True`` or ``False`` depending on whether48 the policy allows action on the target.49 :raises: manila.exception.PolicyNotAuthorized if verification fails50 and ``do_raise`` is ``True``.51 """52 init()53 if not isinstance(context, dict):54 context = context.to_dict()55 # Add the exception arguments if asked to do a raise56 extra = {}57 if do_raise:58 extra.update(exc=exception.PolicyNotAuthorized, action=action,59 do_raise=do_raise)60 return _ENFORCER.enforce(action, target, context, **extra)61def check_is_admin(roles):62 """Whether or not roles contain 'admin' role according to policy setting.63 """64 init()65 # include project_id on target to avoid KeyError if context_is_admin66 # policy definition is missing, and default admin_or_owner rule67 # attempts to apply. Since our credentials dict does not include a68 # project_id, this target can never match as a generic rule.69 target = {'project_id': ''}70 credentials = {'roles': roles}71 return _ENFORCER.enforce("context_is_admin", target, credentials)72def wrap_check_policy(resource):73 """Check policy corresponding to the wrapped methods prior to execution."""74 def check_policy_wraper(func):75 @functools.wraps(func)76 def wrapped(self, context, target_obj, *args, **kwargs):77 check_policy(context, resource, func.__name__, target_obj)78 return func(self, context, target_obj, *args, **kwargs)79 return wrapped80 return check_policy_wraper81def check_policy(context, resource, action, target_obj=None):82 target = {83 'project_id': context.project_id,84 'user_id': context.user_id,85 }86 target.update(target_obj or {})87 _action = '%s:%s' % (resource, action)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful