Add new default roles in os-instance-usage-audit-log policies

This adds new defaults roles in os-instance-usage-audit-log
API policies. This policy is default to SYSTEM_READER role.

Policy rules are made more granular to adopt the new defaults.

Also add tests to simulates the future where we drop the deprecation
fall back in the policy by overriding the rules with a version where
there are no deprecated rule options. Operators can do the same by
adding overrides in their policy files that match the default but
stop the rule deprecation fallback from happening.

Partial implement blueprint policy-defaults-refresh

Change-Id: I749300f949d9c46e79accbf847b3edb6864ff41b
This commit is contained in:
Ghanshyam Mann 2020-03-25 12:35:40 -05:00 committed by Stephen Finucane
parent 7b51647f17
commit dd2b748e58
5 changed files with 72 additions and 27 deletions

View File

@ -35,14 +35,14 @@ class InstanceUsageAuditLogController(wsgi.Controller):
@wsgi.expected_errors(())
def index(self, req):
context = req.environ['nova.context']
context.can(iual_policies.BASE_POLICY_NAME)
context.can(iual_policies.BASE_POLICY_NAME % 'list')
task_log = self._get_audit_task_logs(context)
return {'instance_usage_audit_logs': task_log}
@wsgi.expected_errors(400)
def show(self, req, id):
context = req.environ['nova.context']
context.can(iual_policies.BASE_POLICY_NAME)
context.can(iual_policies.BASE_POLICY_NAME % 'show')
try:
if '.' in id:
before_date = datetime.datetime.strptime(str(id),

View File

@ -18,27 +18,51 @@ from oslo_policy import policy
from nova.policies import base
BASE_POLICY_NAME = 'os_compute_api:os-instance-usage-audit-log'
BASE_POLICY_NAME = 'os_compute_api:os-instance-usage-audit-log:%s'
DEPRECATED_POLICY = policy.DeprecatedRule(
'os_compute_api:os-instance-usage-audit-log',
base.RULE_ADMIN_API,
)
DEPRECATED_REASON = """
Nova API policies are introducing new default roles with scope_type
capabilities. Old policies are deprecated and silently going to be ignored
in nova 23.0.0 release.
"""
instance_usage_audit_log_policies = [
policy.DocumentedRuleDefault(
name=BASE_POLICY_NAME,
check_str=base.RULE_ADMIN_API,
description="List all usage audits and that occurred before "
"a specified time for all servers on all compute hosts where "
"usage auditing is configured",
name=BASE_POLICY_NAME % 'list',
check_str=base.SYSTEM_READER,
description="List all usage audits.",
operations=[
{
'method': 'GET',
'path': '/os-instance_usage_audit_log'
},
],
scope_types=['system'],
deprecated_rule=DEPRECATED_POLICY,
deprecated_reason=DEPRECATED_REASON,
deprecated_since='21.0.0'),
policy.DocumentedRuleDefault(
name=BASE_POLICY_NAME % 'show',
check_str=base.SYSTEM_READER,
description="List all usage audits occurred before "
"a specified time for all servers on all compute hosts where "
"usage auditing is configured",
operations=[
{
'method': 'GET',
'path': '/os-instance_usage_audit_log/{before_timestamp}'
}
],
scope_types=['system']),
scope_types=['system'],
deprecated_rule=DEPRECATED_POLICY,
deprecated_reason=DEPRECATED_REASON,
deprecated_since='21.0.0'),
]

View File

@ -53,7 +53,8 @@ policy_data = """
"os_compute_api:os-instance-actions:show": "",
"os_compute_api:os-instance-actions:events": "",
"os_compute_api:os-instance-actions:events:details": "",
"os_compute_api:os-instance-usage-audit-log": "",
"os_compute_api:os-instance-usage-audit-log:list": "",
"os_compute_api:os-instance-usage-audit-log:show": "",
"os_compute_api:os-lock-server:lock": "",
"os_compute_api:os-lock-server:unlock": "",

View File

@ -13,6 +13,7 @@
import mock
from nova.api.openstack.compute import instance_usage_audit_log as iual
from nova.policies import base as base_policy
from nova.policies import instance_usage_audit_log as iual_policies
from nova.tests.unit.api.openstack import fakes
from nova.tests.unit.policies import base
@ -35,28 +36,33 @@ class InstanceUsageAuditLogPolicyTest(base.BasePolicyTest):
self.controller.host_api.service_get_all = mock.MagicMock()
# Check that admin is able to get instance usage audit log.
self.admin_authorized_contexts = [
# NOTE(gmann): Until old default rule which is admin_api is
# deprecated and not removed, project admin and legacy admin
# will be able to read the agent data. This make sure that existing
# tokens will keep working even we have changed this policy defaults
# to reader role.
self.reader_authorized_contexts = [
self.legacy_admin_context, self.system_admin_context,
self.project_admin_context]
self.project_admin_context, self.system_member_context,
self.system_reader_context]
# Check that non-admin is not able to get instance usage audit log.
self.admin_unauthorized_contexts = [
self.system_member_context, self.system_reader_context,
self.reader_unauthorized_contexts = [
self.system_foo_context, self.project_member_context,
self.other_project_member_context,
self.project_foo_context, self.project_reader_context
]
def test_show_policy(self):
rule_name = iual_policies.BASE_POLICY_NAME
self.common_policy_check(self.admin_authorized_contexts,
self.admin_unauthorized_contexts,
rule_name = iual_policies.BASE_POLICY_NAME % 'show'
self.common_policy_check(self.reader_authorized_contexts,
self.reader_unauthorized_contexts,
rule_name, self.controller.show,
self.req, '2020-03-25 14:40:00')
def test_index_policy(self):
rule_name = iual_policies.BASE_POLICY_NAME
self.common_policy_check(self.admin_authorized_contexts,
self.admin_unauthorized_contexts,
rule_name = iual_policies.BASE_POLICY_NAME % 'list'
self.common_policy_check(self.reader_authorized_contexts,
self.reader_unauthorized_contexts,
rule_name, self.controller.index,
self.req)
@ -76,15 +82,28 @@ class InstanceUsageScopeTypePolicyTest(InstanceUsageAuditLogPolicyTest):
super(InstanceUsageScopeTypePolicyTest, self).setUp()
self.flags(enforce_scope=True, group="oslo_policy")
# Check that system admin is able to get instance usage audit log.
self.admin_authorized_contexts = [
self.system_admin_context]
# Check that system reader is able to get instance usage audit log.
self.reader_authorized_contexts = [
self.system_admin_context, self.system_member_context,
self.system_reader_context]
# Check that non-system-admin is not able to get instance
# usage audit log.
self.admin_unauthorized_contexts = [
self.legacy_admin_context, self.system_member_context,
self.system_reader_context, self.project_admin_context,
self.reader_unauthorized_contexts = [
self.legacy_admin_context, self.project_admin_context,
self.system_foo_context, self.project_member_context,
self.other_project_member_context,
self.project_foo_context, self.project_reader_context
]
class InstanceUsageNoLegacyPolicyTest(InstanceUsageScopeTypePolicyTest):
"""Test Instance Usage Audit Log APIs policies with system scope enabled,
and no more deprecated rules.
"""
without_deprecated_rules = True
rules_without_deprecation = {
iual_policies.BASE_POLICY_NAME % 'list':
base_policy.SYSTEM_READER,
iual_policies.BASE_POLICY_NAME % 'show':
base_policy.SYSTEM_READER,
}

View File

@ -343,7 +343,6 @@ class RealRolePolicyTestCase(test.NoDBTestCase):
"os_compute_api:os-hosts",
"os_compute_api:os-hypervisors",
"os_compute_api:os-instance-actions:events",
"os_compute_api:os-instance-usage-audit-log",
"os_compute_api:os-lock-server:unlock:unlock_override",
"os_compute_api:os-migrate-server:migrate",
"os_compute_api:os-migrate-server:migrate_live",
@ -457,6 +456,8 @@ class RealRolePolicyTestCase(test.NoDBTestCase):
self.system_reader_rules = (
"os_compute_api:os-services:list",
"os_compute_api:os-instance-actions:events:details",
"os_compute_api:os-instance-usage-audit-log:list",
"os_compute_api:os-instance-usage-audit-log:show",
)
self.system_reader_or_owner_rules = (