From ad108dafe2bdd1b6523d6f45315bb136b9b01408 Mon Sep 17 00:00:00 2001 From: Lance Bragstad Date: Wed, 20 Mar 2019 21:40:55 +0000 Subject: [PATCH] Update system grant policies for system admin This commit updates the policies for adding and removing system assignments from users to be consistent with other system-scoped policies. - domain user test coverage - project user test coverage Change-Id: Ia24a81669477ca5c737d0dedefac0c8fb0edc51a Related-Bug: 1805368 Related-Bug: 1750669 Related-Bug: 1806762 (cherry picked from commit 8f4e179c69eae7ced731776717c09a979bd67cc5) --- keystone/common/policies/grant.py | 22 ++++++-- .../protection/v3/test_system_assignments.py | 54 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/keystone/common/policies/grant.py b/keystone/common/policies/grant.py index 84e291b757..d99db48748 100644 --- a/keystone/common/policies/grant.py +++ b/keystone/common/policies/grant.py @@ -23,6 +23,14 @@ deprecated_list_system_grants_for_user = policy.DeprecatedRule( name=base.IDENTITY % 'list_system_grants_for_user', check_str=base.RULE_ADMIN_REQUIRED ) +deprecated_create_system_grant_for_user = policy.DeprecatedRule( + name=base.IDENTITY % 'create_system_grant_for_user', + check_str=base.RULE_ADMIN_REQUIRED +) +deprecated_revoke_system_grant_for_user = policy.DeprecatedRule( + name=base.IDENTITY % 'revoke_system_grant_for_user', + check_str=base.RULE_ADMIN_REQUIRED +) DEPRECATED_REASON = """ As of the Stein release, the system assignment API now understands default @@ -163,7 +171,7 @@ grant_policies = [ ), policy.DocumentedRuleDefault( name=base.IDENTITY % 'create_system_grant_for_user', - check_str=base.RULE_ADMIN_REQUIRED, + check_str=base.SYSTEM_ADMIN, scope_types=['system'], description='Grant a user a role on the system.', operations=[ @@ -171,11 +179,14 @@ grant_policies = [ 'path': '/v3/system/users/{user_id}/roles/{role_id}', 'method': ['PUT'] } - ] + ], + deprecated_rule=deprecated_create_system_grant_for_user, + deprecated_reason=DEPRECATED_REASON, + deprecated_since=versionutils.deprecated.STEIN ), policy.DocumentedRuleDefault( name=base.IDENTITY % 'revoke_system_grant_for_user', - check_str=base.RULE_ADMIN_REQUIRED, + check_str=base.SYSTEM_ADMIN, scope_types=['system'], description='Remove a role from a user on the system.', operations=[ @@ -183,7 +194,10 @@ grant_policies = [ 'path': '/v3/system/users/{user_id}/roles/{role_id}', 'method': ['DELETE'] } - ] + ], + deprecated_rule=deprecated_revoke_system_grant_for_user, + deprecated_reason=DEPRECATED_REASON, + deprecated_since=versionutils.deprecated.STEIN ), policy.DocumentedRuleDefault( name=base.IDENTITY % 'list_system_grants_for_group', diff --git a/keystone/tests/unit/protection/v3/test_system_assignments.py b/keystone/tests/unit/protection/v3/test_system_assignments.py index f0aef3e89d..7b924cd77c 100644 --- a/keystone/tests/unit/protection/v3/test_system_assignments.py +++ b/keystone/tests/unit/protection/v3/test_system_assignments.py @@ -168,3 +168,57 @@ class SystemMemberTests(base_classes.TestCaseWithBootstrap, r = c.post('/v3/auth/tokens', json=auth) self.token_id = r.headers['X-Subject-Token'] self.headers = {'X-Auth-Token': self.token_id} + + +class SystemAdminTests(base_classes.TestCaseWithBootstrap, + common_auth.AuthTestMixin, + _SystemUserSystemAssignmentTests): + + def setUp(self): + super(SystemAdminTests, self).setUp() + self.loadapp() + self.useFixture(ksfixtures.Policy(self.config_fixture)) + self.config_fixture.config(group='oslo_policy', enforce_scope=True) + + self.user_id = self.bootstrapper.admin_user_id + self.expected = [] + + auth = self.build_authentication_request( + user_id=self.user_id, password=self.bootstrapper.admin_password, + system=True + ) + + # Grab a token using the persona we're testing and prepare headers + # for requests we'll be making in the tests. + with self.test_client() as c: + r = c.post('/v3/auth/tokens', json=auth) + self.token_id = r.headers['X-Subject-Token'] + self.headers = {'X-Auth-Token': self.token_id} + + def test_user_can_grant_system_assignments(self): + user = PROVIDERS.identity_api.create_user( + unit.new_user_ref(CONF.identity.default_domain_id) + ) + + with self.test_client() as c: + c.put( + '/v3/system/users/%s/roles/%s' % ( + user['id'], self.bootstrapper.member_role_id + ), headers=self.headers, + ) + + def test_user_can_revoke_system_assignments(self): + user = PROVIDERS.identity_api.create_user( + unit.new_user_ref(CONF.identity.default_domain_id) + ) + + PROVIDERS.assignment_api.create_system_grant_for_user( + user['id'], self.bootstrapper.member_role_id + ) + + with self.test_client() as c: + c.delete( + '/v3/system/users/%s/roles/%s' % ( + user['id'], self.bootstrapper.member_role_id + ), headers=self.headers + )