|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import fixtures |
| 14 | +import mock |
| 15 | +from oslo_utils.fixture import uuidsentinel as uuids |
| 16 | +from oslo_utils import timeutils |
| 17 | + |
| 18 | +from nova.api.openstack.compute import admin_actions |
| 19 | +from nova.compute import vm_states |
| 20 | +from nova.tests.unit.api.openstack import fakes |
| 21 | +from nova.tests.unit import fake_instance |
| 22 | +from nova.tests.unit.policies import base |
| 23 | + |
| 24 | + |
| 25 | +class AdminActionsPolicyTest(base.BasePolicyTest): |
| 26 | + """Test Admin Actions APIs policies with all possible context. |
| 27 | +
|
| 28 | + This class defines the set of context with different roles |
| 29 | + which are allowed and not allowed to pass the policy checks. |
| 30 | + With those set of context, it will call the API operation and |
| 31 | + verify the expected behaviour. |
| 32 | + """ |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + super(AdminActionsPolicyTest, self).setUp() |
| 36 | + self.controller = admin_actions.AdminActionsController() |
| 37 | + self.req = fakes.HTTPRequest.blank('') |
| 38 | + self.mock_get = self.useFixture( |
| 39 | + fixtures.MockPatch('nova.compute.api.API.get')).mock |
| 40 | + uuid = uuids.fake_id |
| 41 | + self.instance = fake_instance.fake_instance_obj( |
| 42 | + self.project_member_context, |
| 43 | + id=1, uuid=uuid, vm_state=vm_states.ACTIVE, |
| 44 | + task_state=None, launched_at=timeutils.utcnow()) |
| 45 | + self.mock_get.return_value = self.instance |
| 46 | + # Check that admin is able to change the service |
| 47 | + self.admin_authorized_contexts = [ |
| 48 | + self.legacy_admin_context, self.system_admin_context, |
| 49 | + self.project_admin_context] |
| 50 | + # Check that non-admin is not able to change the service |
| 51 | + self.admin_unauthorized_contexts = [ |
| 52 | + self.system_member_context, self.system_reader_context, |
| 53 | + self.system_foo_context, self.project_member_context, |
| 54 | + self.other_project_member_context, |
| 55 | + self.project_foo_context, self.project_reader_context |
| 56 | + ] |
| 57 | + |
| 58 | + @mock.patch('nova.objects.Instance.save') |
| 59 | + def test_reset_state_policy(self, mock_save): |
| 60 | + rule_name = "os_compute_api:os-admin-actions:reset_state" |
| 61 | + self.common_policy_check(self.admin_authorized_contexts, |
| 62 | + self.admin_unauthorized_contexts, |
| 63 | + rule_name, self.controller._reset_state, |
| 64 | + self.req, self.instance.uuid, |
| 65 | + body={'os-resetState': {'state': 'active'}}) |
| 66 | + |
| 67 | + def test_inject_network_info_policy(self): |
| 68 | + rule_name = "os_compute_api:os-admin-actions:inject_network_info" |
| 69 | + with mock.patch.object(self.controller.compute_api, |
| 70 | + "inject_network_info"): |
| 71 | + self.common_policy_check(self.admin_authorized_contexts, |
| 72 | + self.admin_unauthorized_contexts, |
| 73 | + rule_name, |
| 74 | + self.controller._inject_network_info, |
| 75 | + self.req, self.instance.uuid, body={}) |
| 76 | + |
| 77 | + def test_reset_network_policy(self): |
| 78 | + rule_name = "os_compute_api:os-admin-actions:reset_network" |
| 79 | + with mock.patch.object(self.controller.compute_api, "reset_network"): |
| 80 | + self.common_policy_check(self.admin_authorized_contexts, |
| 81 | + self.admin_unauthorized_contexts, |
| 82 | + rule_name, self.controller._reset_network, |
| 83 | + self.req, self.instance.uuid, body={}) |
| 84 | + |
| 85 | + |
| 86 | +class AdminActionsScopeTypePolicyTest(AdminActionsPolicyTest): |
| 87 | + """Test Admin Actions APIs policies with system scope enabled. |
| 88 | +
|
| 89 | + This class set the nova.conf [oslo_policy] enforce_scope to True |
| 90 | + so that we can switch on the scope checking on oslo policy side. |
| 91 | + It defines the set of context with scopped token |
| 92 | + which are allowed and not allowed to pass the policy checks. |
| 93 | + With those set of context, it will run the API operation and |
| 94 | + verify the expected behaviour. |
| 95 | + """ |
| 96 | + |
| 97 | + def setUp(self): |
| 98 | + super(AdminActionsScopeTypePolicyTest, self).setUp() |
| 99 | + self.flags(enforce_scope=True, group="oslo_policy") |
0 commit comments