Skip to content

Commit 904e4db

Browse files
author
Ghanshyam Mann
committed
Fix multinic policy for admin_or_owner
multinic API policy is default to admin_or_owner[1] but API is allowed for everyone. We can see the test trying with other project context can access the API - https://review.opendev.org/#/c/742315 This is because API does not pass the server project_id in policy target[2] and if no target is passed then, policy.py add the default targets which is nothing but context.project_id (allow for everyone who try to access)[3] This commit fix this policy by passing the server's project_id in policy target. Partial implement blueprint policy-defaults-refresh-deprecated-apis [1] https://github.com/openstack/nova/blob/cd16ae25c865f25dbb313976b3d8ef9372db80af/nova/policies/multinic.py#L27 [2] https://github.com/openstack/nova/blob/cd16ae25c865f25dbb313976b3d8ef9372db80af/nova/api/openstack/compute/multinic.py#L44 [3] https://github.com/openstack/nova/blob/c16315165ce307c605cf4b608b2df3aa06f46982/nova/policy.py#L191 Change-Id: Ie9b575cc15ae43d8b1eb1b74180ecead45702efe
1 parent 0a51759 commit 904e4db

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

nova/api/openstack/compute/multinic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ def __init__(self):
4141
def _add_fixed_ip(self, req, id, body):
4242
"""Adds an IP on a given network to an instance."""
4343
context = req.environ['nova.context']
44-
context.can(multinic_policies.BASE_POLICY_NAME)
45-
4644
instance = common.get_instance(self.compute_api, context, id)
45+
context.can(multinic_policies.BASE_POLICY_NAME,
46+
target={'project_id': instance.project_id})
47+
4748
network_id = body['addFixedIp']['networkId']
4849
try:
4950
self.compute_api.add_fixed_ip(context, instance, network_id)
@@ -58,9 +59,10 @@ def _add_fixed_ip(self, req, id, body):
5859
def _remove_fixed_ip(self, req, id, body):
5960
"""Removes an IP from an instance."""
6061
context = req.environ['nova.context']
61-
context.can(multinic_policies.BASE_POLICY_NAME)
62-
6362
instance = common.get_instance(self.compute_api, context, id)
63+
context.can(multinic_policies.BASE_POLICY_NAME,
64+
target={'project_id': instance.project_id})
65+
6466
address = body['removeFixedIp']['address']
6567

6668
try:

nova/tests/unit/api/openstack/compute/test_multinic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import fixtures
1617
import mock
1718
import webob
1819

@@ -22,6 +23,7 @@
2223
from nova import objects
2324
from nova import test
2425
from nova.tests.unit.api.openstack import fakes
26+
from nova.tests.unit import fake_instance
2527

2628

2729
UUID = '70f6db34-de8d-4fbd-aafb-4065bdfa6114'
@@ -66,6 +68,11 @@ def setUp(self):
6668
self.stub_out('nova.compute.api.API.get', compute_api_get)
6769
self.controller = self.controller_class.MultinicController()
6870
self.fake_req = fakes.HTTPRequest.blank('')
71+
self.mock_get = self.useFixture(
72+
fixtures.MockPatch('nova.api.openstack.common.get_instance')).mock
73+
self.mock_get.return_value = fake_instance.fake_instance_obj(
74+
self.fake_req.environ['nova.context'], uuid=UUID,
75+
project_id=self.fake_req.environ['nova.context'].project_id)
6976

7077
def test_add_fixed_ip(self):
7178
global last_add_fixed_ip
@@ -151,6 +158,11 @@ def setUp(self):
151158
super(MultinicPolicyEnforcementV21, self).setUp()
152159
self.controller = multinic_v21.MultinicController()
153160
self.req = fakes.HTTPRequest.blank('')
161+
self.mock_get = self.useFixture(
162+
fixtures.MockPatch('nova.api.openstack.common.get_instance')).mock
163+
self.mock_get.return_value = fake_instance.fake_instance_obj(
164+
self.req.environ['nova.context'],
165+
project_id=self.req.environ['nova.context'].project_id)
154166

155167
def test_add_fixed_ip_policy_failed(self):
156168
rule_name = "os_compute_api:os-multinic"

0 commit comments

Comments
 (0)