Skip to content

Commit

Permalink
Merge "Change RBAC relationship loading method to "joined"" into stab…
Browse files Browse the repository at this point in the history
…le/zed
  • Loading branch information
Zuul authored and openstack-gerrit committed Jun 6, 2023
2 parents 1a711f3 + d3b403b commit 23d8237
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion neutron/db/models/address_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ class AddressGroup(standard_attr.HasStandardAttributes,
cascade='all, delete-orphan')
rbac_entries = sa.orm.relationship(rbac_db_models.AddressGroupRBAC,
backref='address_groups',
lazy='subquery',
lazy='joined',
cascade='all, delete, delete-orphan')
api_collections = [ag.ALIAS]
2 changes: 1 addition & 1 deletion neutron/db/models/address_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class AddressScope(model_base.BASEV2, model_base.HasId, model_base.HasProject):

rbac_entries = sa.orm.relationship(rbac_db_models.AddressScopeRBAC,
backref='address_scopes',
lazy='subquery',
lazy='joined',
cascade='all, delete, delete-orphan')
2 changes: 1 addition & 1 deletion neutron/db/models/securitygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SecurityGroup(standard_attr.HasStandardAttributes, model_base.BASEV2,
nullable=False)
rbac_entries = sa.orm.relationship(rbac_db_models.SecurityGroupRBAC,
backref='security_group',
lazy='subquery',
lazy='joined',
cascade='all, delete, delete-orphan')
api_collections = [sg.SECURITYGROUPS]
collection_resource_map = {sg.SECURITYGROUPS: 'security_group'}
Expand Down
6 changes: 3 additions & 3 deletions neutron/db/models_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Subnet(standard_attr.HasStandardAttributes, model_base.BASEV2,
# subnets don't have their own rbac_entries, they just inherit from
# the network rbac entries
rbac_entries = orm.relationship(
rbac_db_models.NetworkRBAC, lazy='subquery', uselist=True,
rbac_db_models.NetworkRBAC, lazy='joined', uselist=True,
foreign_keys='Subnet.network_id',
primaryjoin='Subnet.network_id==NetworkRBAC.object_id',
viewonly=True)
Expand Down Expand Up @@ -282,7 +282,7 @@ class SubnetPool(standard_attr.HasStandardAttributes, model_base.BASEV2,
lazy='subquery')
rbac_entries = sa.orm.relationship(rbac_db_models.SubnetPoolRBAC,
backref='subnetpools',
lazy='subquery',
lazy='joined',
cascade='all, delete, delete-orphan')
api_collections = [subnetpool_def.COLLECTION_NAME]
collection_resource_map = {subnetpool_def.COLLECTION_NAME:
Expand All @@ -304,7 +304,7 @@ class Network(standard_attr.HasStandardAttributes, model_base.BASEV2,
rbac_entries = orm.relationship(rbac_db_models.NetworkRBAC,
backref=orm.backref('network',
load_on_pending=True),
lazy='subquery',
lazy='joined',
cascade='all, delete, delete-orphan')
availability_zone_hints = sa.Column(sa.String(255))
mtu = sa.Column(sa.Integer, nullable=False,
Expand Down
2 changes: 1 addition & 1 deletion neutron/db/qos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class QosPolicy(standard_attr.HasStandardAttributes, model_base.BASEV2,
__tablename__ = 'qos_policies'
name = sa.Column(sa.String(db_const.NAME_FIELD_SIZE))
rbac_entries = sa.orm.relationship(rbac_db_models.QosPolicyRBAC,
backref='qos_policy', lazy='subquery',
backref='qos_policy', lazy='joined',
cascade='all, delete, delete-orphan')
api_collections = ['policies']
collection_resource_map = {'policies': 'policy'}
Expand Down
1 change: 1 addition & 0 deletions neutron/tests/unit/objects/test_address_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class AddressGroupRBACDbObjectTestCase(test_rbac.TestRBACObjectMixin,
testlib_api.SqlTestCase):

_test_class = address_group.AddressGroupRBAC
_parent_class = address_group.AddressGroup

def setUp(self):
super(AddressGroupRBACDbObjectTestCase, self).setUp()
Expand Down
1 change: 1 addition & 0 deletions neutron/tests/unit/objects/test_address_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AddressScopeRBACDbObjectTestCase(test_rbac.TestRBACObjectMixin,
testlib_api.SqlTestCase):

_test_class = address_scope.AddressScopeRBAC
_parent_class = address_scope.AddressScope

def setUp(self):
super(AddressScopeRBACDbObjectTestCase, self).setUp()
Expand Down
10 changes: 10 additions & 0 deletions neutron/tests/unit/objects/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from unittest import mock

from neutron_lib.api.definitions import availability_zone as az_def

from neutron.db import rbac_db_models
from neutron.objects import base as obj_base
from neutron.objects import network
Expand All @@ -27,6 +29,7 @@ class NetworkRBACDbObjectTestCase(test_rbac.TestRBACObjectMixin,
testlib_api.SqlTestCase):

_test_class = network.NetworkRBAC
_parent_class = network.Network

def setUp(self):
self._mock_get_valid_actions = mock.patch.object(
Expand All @@ -50,6 +53,13 @@ def test_object_version_degradation_1_1_to_1_0_no_id_no_project_id(self):
network_rbac_obj['versioned_object.data'])
self.assertNotIn('id', network_rbac_obj['versioned_object.data'])

def _create_random_parent_object(self):
objclass_fields = self.get_random_db_fields(self._parent_class)
objclass_fields.pop(az_def.AZ_HINTS)
_obj = self._parent_class(self.context, **objclass_fields)
_obj.create()
return _obj


class NetworkRBACIfaceOjectTestCase(test_rbac.TestRBACObjectMixin,
obj_test_base.BaseObjectIfaceTestCase):
Expand Down
37 changes: 36 additions & 1 deletion neutron/tests/unit/objects/test_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import random

import random
from unittest import mock

from neutron_lib import context

from neutron.db import rbac_db_models
from neutron.objects import address_group
from neutron.objects import address_scope
from neutron.objects import network
Expand All @@ -26,6 +29,9 @@

class TestRBACObjectMixin(object):

_test_class = None
_parent_class = None

def get_random_object_fields(self, obj_cls=None):
fields = (super(TestRBACObjectMixin, self).
get_random_object_fields(obj_cls))
Expand All @@ -34,6 +40,35 @@ def get_random_object_fields(self, obj_cls=None):
fields['action'] = rnd_actions[idx]
return fields

def _create_random_parent_object(self):
objclass_fields = self.get_random_db_fields(self._parent_class)
_obj = self._parent_class(self.context, **objclass_fields)
_obj.create()
return _obj

def test_rbac_shared_on_parent_object(self):
if not self._test_class or not self._parent_class:
self.skipTest('Mixin class, skipped test')
project_id = self.objs[0].project_id
_obj_shared = self._create_random_parent_object()
# Create a second object that won't be shared and thus won't be
# retrieved by the non-admin users.
self._create_random_parent_object()
for idx in range(3):
project = 'project_%s' % idx
rbac = self._test_class(
self.context, project_id=project_id, target_project=project,
action=rbac_db_models.ACCESS_SHARED,
object_id=_obj_shared.id)
rbac.create()

for idx in range(3):
project = 'project_%s' % idx
ctx_no_admin = context.Context(user_id='user', tenant_id=project,
is_admin=False)
objects = self._parent_class.get_objects(ctx_no_admin)
self.assertEqual([_obj_shared.id], [_obj.id for _obj in objects])


class RBACBaseObjectTestCase(neutron_test_base.BaseTestCase):

Expand Down
1 change: 1 addition & 0 deletions neutron/tests/unit/objects/test_securitygroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SecurityGroupRBACDbObjectTestCase(test_rbac.TestRBACObjectMixin,
testlib_api.SqlTestCase):

_test_class = securitygroup.SecurityGroupRBAC
_parent_class = securitygroup.SecurityGroup

def setUp(self):
super(SecurityGroupRBACDbObjectTestCase, self).setUp()
Expand Down
1 change: 1 addition & 0 deletions neutron/tests/unit/objects/test_subnetpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class SubnetPoolRBACDbObjectTestCase(test_rbac.TestRBACObjectMixin,
SubnetPoolTestMixin):

_test_class = subnetpool.SubnetPoolRBAC
_parent_class = subnetpool.SubnetPool

def setUp(self):
super(SubnetPoolRBACDbObjectTestCase, self).setUp()
Expand Down

0 comments on commit 23d8237

Please sign in to comment.