policy: remove `OwnershipValidationHook, update create_port` rule
The ``OwnershipValidationHook`` performed a network ownership check on every port and subnet POST request, fetching the network from the database to verify that the caller owned the network or that the network was shared. This check was redundant and has been removed. For subnets, the ``create_subnet`` policy already requires ``ADMIN_OR_NET_OWNER_MEMBER``, which is stricter than the hook (the hook allowed shared networks; the policy does not). Every request the hook would deny was also denied by the policy engine. For ports, the ``create_port`` policy only checked the port's ``project_id`` and had no network-level access check. The hook was the sole guard preventing port creation on non-shared networks belonging to other projects. The ``create_port`` rule is now updated to require network ownership or a shared network, making the hook fully redundant. This eliminates one ``get_network()`` database call per port and subnet create request and makes the authorization model fully declarative via policy rules. Closes-Bug: #2156444 Assisted-By: Claude Opus 4.6 Signed-off-by: Rodolfo Alonso Hernandez <ralonsoh@redhat.com> Change-Id: I3b9c9f4d362002ab35a237fe0383b664be9a5456
This commit is contained in:
1 parent
aee9ca0900
commit
6ffe5056af
8 files changed
+17
-84
No files matched your search
@@ -71,8 +71,10 @@ rules = [
|
||||
policy.DocumentedRuleDefault(
|
||||
name='create_port',
|
||||
check_str=neutron_policy.policy_or(
|
||||
lib_rules.ADMIN_OR_PROJECT_MEMBER,
|
||||
lib_rules.SERVICE),
|
||||
lib_rules.ADMIN_OR_SERVICE,
|
||||
base.NET_OWNER_MEMBER,
|
||||
'rule:shared',
|
||||
),
|
||||
scope_types=['project'],
|
||||
description='Create a port',
|
||||
operations=ACTION_POST,
|
||||
|
||||
@@ -33,7 +33,6 @@ def v2_factory(global_config, **local_config):
|
||||
hooks.ContextHook(), # priority 95
|
||||
hooks.ExceptionTranslationHook(), # priority 100
|
||||
hooks.BodyValidationHook(), # priority 120
|
||||
hooks.OwnershipValidationHook(), # priority 125
|
||||
hooks.QuotaEnforcementHook(), # priority 130
|
||||
hooks.NotifierHook(), # priority 135
|
||||
hooks.QueryParametersHook(), # priority 139
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
from neutron.pecan_wsgi.hooks import body_validation
|
||||
from neutron.pecan_wsgi.hooks import context
|
||||
from neutron.pecan_wsgi.hooks import notifier
|
||||
from neutron.pecan_wsgi.hooks import ownership_validation
|
||||
from neutron.pecan_wsgi.hooks import policy_enforcement
|
||||
from neutron.pecan_wsgi.hooks import query_parameters
|
||||
from neutron.pecan_wsgi.hooks import quota_enforcement
|
||||
@@ -27,7 +26,6 @@ from neutron.pecan_wsgi.hooks import userfilters
|
||||
ExceptionTranslationHook = translation.ExceptionTranslationHook
|
||||
ContextHook = context.ContextHook
|
||||
BodyValidationHook = body_validation.BodyValidationHook
|
||||
OwnershipValidationHook = ownership_validation.OwnershipValidationHook
|
||||
PolicyHook = policy_enforcement.PolicyHook
|
||||
QuotaEnforcementHook = quota_enforcement.QuotaEnforcementHook
|
||||
NotifierHook = notifier.NotifierHook
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
# Copyright (c) 2015 Mirantis, Inc.
|
||||
# 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 neutron_lib.plugins import directory
|
||||
from pecan import hooks
|
||||
import webob
|
||||
|
||||
from neutron._i18n import _
|
||||
|
||||
|
||||
class OwnershipValidationHook(hooks.PecanHook):
|
||||
|
||||
priority = 125
|
||||
|
||||
def before(self, state):
|
||||
if state.request.method != 'POST':
|
||||
return
|
||||
for item in state.request.context.get('resources', []):
|
||||
self._validate_network_project_ownership(state, item)
|
||||
|
||||
def _validate_network_project_ownership(self, state, resource_item):
|
||||
# TODO(salvatore-orlando): consider whether this check can be folded
|
||||
# in the policy engine
|
||||
neutron_context = state.request.context.get('neutron_context')
|
||||
resource = state.request.context.get('resource')
|
||||
if (neutron_context.is_admin or neutron_context.is_service_role or
|
||||
resource not in ('port', 'subnet')):
|
||||
return
|
||||
plugin = directory.get_plugin()
|
||||
network = plugin.get_network(neutron_context,
|
||||
resource_item['network_id'])
|
||||
# do not perform the check on shared networks
|
||||
if network.get('shared'):
|
||||
return
|
||||
|
||||
network_owner = network['project_id']
|
||||
|
||||
if network_owner != resource_item['project_id']:
|
||||
msg = _("Project %(project_id)s not allowed to "
|
||||
"create %(resource)s on this network")
|
||||
raise webob.exc.HTTPForbidden(msg % {
|
||||
"project_id": resource_item['project_id'],
|
||||
"resource": resource,
|
||||
})
|
||||
@@ -3409,7 +3409,7 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase):
|
||||
req = self.new_create_request('subnets', data,
|
||||
self.fmt, context=ctx)
|
||||
res = req.get_response(self.api)
|
||||
self._check_http_response(res, webob.exc.HTTPNotFound.code)
|
||||
self._check_http_response(res, webob.exc.HTTPForbidden.code)
|
||||
|
||||
def test_create_two_subnets(self):
|
||||
gateway_ips = ['10.0.0.1', '10.0.1.1']
|
||||
@@ -3868,7 +3868,7 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase):
|
||||
self._create_subnet(self.fmt,
|
||||
network['network']['id'],
|
||||
'10.0.2.0/24',
|
||||
webob.exc.HTTPNotFound.code,
|
||||
webob.exc.HTTPForbidden.code,
|
||||
ip_version=constants.IP_VERSION_4,
|
||||
project_id='bad_project_id',
|
||||
gateway_ip='10.0.2.1',
|
||||
|
||||
@@ -31,22 +31,6 @@ from neutron import policy
|
||||
from neutron.tests.functional.pecan_wsgi import test_functional
|
||||
|
||||
|
||||
class TestOwnershipHook(test_functional.PecanFunctionalTest):
|
||||
|
||||
def test_network_ownership_check(self):
|
||||
net_response = self.app.post_json(
|
||||
'/v2.0/networks.json',
|
||||
params={'network': {'name': 'meh'}},
|
||||
headers={'X-Project-Id': 'projid'})
|
||||
network_id = jsonutils.loads(net_response.body)['network']['id']
|
||||
port_response = self.app.post_json(
|
||||
'/v2.0/ports.json',
|
||||
params={'port': {'network_id': network_id,
|
||||
'admin_state_up': True}},
|
||||
headers={'X-Project-Id': 'projid'})
|
||||
self.assertEqual(201, port_response.status_int)
|
||||
|
||||
|
||||
class TestQueryParametersHook(test_functional.PecanFunctionalTest):
|
||||
|
||||
def test_if_match_on_update(self):
|
||||
|
||||
@@ -881,11 +881,7 @@ class ProjectManagerTests(AdminTests):
|
||||
self.context = self.project_manager_ctx
|
||||
|
||||
def test_create_port(self):
|
||||
self.assertTrue(
|
||||
policy.enforce(self.context, 'create_port', self.target))
|
||||
self.assertRaises(
|
||||
base_policy.PolicyNotAuthorized,
|
||||
policy.enforce, self.context, 'create_port', self.alt_target)
|
||||
self._assert_network_owner_policy('create_port')
|
||||
|
||||
def test_create_port_with_device_id(self):
|
||||
self.assertTrue(
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
upgrade:
|
||||
- |
|
||||
The ``OwnershipValidationHook`` Pecan hook has been removed. Network
|
||||
access validation for port creation is now handled entirely by the
|
||||
policy engine. The ``create_port`` policy rule has been updated to
|
||||
require network ownership or a shared network, replacing the
|
||||
previous hook-based check. Operators who have customized the
|
||||
``create_port`` rule in ``policy.yaml`` should review their
|
||||
overrides to ensure they include appropriate network access checks.
|
||||
Reference in new issue
Block a user