Add default policy in code for the trigger resource

Leverage oslo.policy to register default policies in the
code. Administrator only need to update the specified
policy in the config file.

Change-Id: I713f8d944e3e206f4542fa0dcb55b3a48d033bb7
Partial-Implements: blueprint policy-in-code
This commit is contained in:
chenying 2017-09-28 17:10:02 +08:00
parent 97c82c84dd
commit 6afc133e7a
5 changed files with 92 additions and 17 deletions

View File

@ -1,10 +1,4 @@
{
"trigger:create": "",
"trigger:delete": "rule:admin_or_owner",
"trigger:update": "rule:admin_or_owner",
"trigger:get": "rule:admin_or_owner",
"trigger:list": "",
"scheduled_operation:create": "",
"scheduled_operation:delete": "rule:admin_or_owner",
"scheduled_operation:get": "rule:admin_or_owner",

View File

@ -22,18 +22,13 @@ from karbor.api.openstack import wsgi
from karbor import exception
from karbor.i18n import _
from karbor import objects
from karbor import policy
from karbor.policies import triggers as trigger_policy
from karbor.services.operationengine import api as operationengine_api
from karbor import utils
LOG = logging.getLogger(__name__)
def check_policy(context, action, target_obj=None):
_action = 'trigger:%s' % action
policy.enforce(context, _action, target_obj)
class TriggerViewBuilder(common.ViewBuilder):
"""Model a trigger API response as a python dictionary."""
@ -90,7 +85,7 @@ class TriggersController(wsgi.Controller):
LOG.debug('Create a trigger, request body: %s', body)
context = req.environ['karbor.context']
check_policy(context, 'create')
context.can(trigger_policy.CREATE_POLICY)
trigger_info = body['trigger_info']
trigger_name = trigger_info.get("name", None)
@ -130,7 +125,7 @@ class TriggersController(wsgi.Controller):
context = req.environ['karbor.context']
trigger = self._get_trigger_by_id(context, id)
check_policy(context, 'delete', trigger)
context.can(trigger_policy.DELETE_POLICY, trigger)
try:
operations = objects.ScheduledOperationList.get_by_filters(
@ -160,7 +155,7 @@ class TriggersController(wsgi.Controller):
context = req.environ['karbor.context']
trigger = self._get_trigger_by_id(context, id)
check_policy(context, 'update', trigger)
context.can(trigger_policy.UPDATE_POLICY, trigger)
trigger_info = body['trigger_info']
trigger_name = trigger_info.get("name", None)
@ -197,14 +192,14 @@ class TriggersController(wsgi.Controller):
context = req.environ['karbor.context']
trigger = self._get_trigger_by_id(context, id)
check_policy(context, 'get', trigger)
context.can(trigger_policy.GET_POLICY, trigger)
return self._view_builder.detail(req, trigger)
def index(self, req):
"""Returns a list of triggers, transformed through view builder."""
context = req.environ['karbor.context']
check_policy(context, 'list')
context.can(trigger_policy.GET_ALL_POLICY)
params = req.params.copy()
LOG.debug('List triggers start, params=%s', params)

View File

@ -19,6 +19,7 @@ from karbor.policies import plans
from karbor.policies import protectables
from karbor.policies import providers
from karbor.policies import restores
from karbor.policies import triggers
def list_rules():
@ -28,4 +29,5 @@ def list_rules():
restores.list_rules(),
protectables.list_rules(),
providers.list_rules(),
triggers.list_rules(),
)

View File

@ -0,0 +1,82 @@
# Copyright (c) 2017 Huawei Technologies Co., Ltd.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_policy import policy
from karbor.policies import base
CREATE_POLICY = 'trigger:create'
UPDATE_POLICY = 'trigger:update'
DELETE_POLICY = 'trigger:delete'
GET_POLICY = 'trigger:get'
GET_ALL_POLICY = 'trigger:list'
triggers_policies = [
policy.DocumentedRuleDefault(
name=CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create a trigger.""",
operations=[
{
'method': 'POST',
'path': '/triggers'
}
]),
policy.DocumentedRuleDefault(
name=UPDATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER,
description="""Update a trigger.""",
operations=[
{
'method': 'PUT',
'path': '/triggers/{trigger_id}'
}
]),
policy.DocumentedRuleDefault(
name=DELETE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER,
description="""Delete a trigger.""",
operations=[
{
'method': 'DELETE',
'path': '/triggers/{trigger_id}'
}
]),
policy.DocumentedRuleDefault(
name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get a trigger.""",
operations=[
{
'method': 'GET',
'path': '/triggers/{trigger_id}'
}
]),
policy.DocumentedRuleDefault(
name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get triggerss.""",
operations=[
{
'method': 'GET',
'path': '/triggers'
}
]),
]
def list_rules():
return triggers_policies

View File

@ -54,6 +54,8 @@ class TriggerApiTest(base.TestCase):
"pattern": "* * * * *"
},
}
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
def test_create_trigger_InvalidBody(self):
self.assertRaises(exc.HTTPUnprocessableEntity,