From 4ec1f8a0120548da213d45e6f98712c6eb52ac0d Mon Sep 17 00:00:00 2001 From: Boris Bobrov Date: Tue, 7 Apr 2026 23:45:15 +0200 Subject: [PATCH 2/6] Block restricted app creds from creating EC2 credentials via /credentials The POST /v3/credentials endpoint accepted EC2 credential creation from restricted application credential tokens, bypassing the guard on the dedicated OS-EC2 endpoint. Add the same unrestricted application credential check to the generic credentials API for EC2-type credentials, and update the existing test to use an unrestricted application credential. Related-Bug: #2142138 Generated-By: claude-opus-4-6 (OpenCode) Signed-off-by: Boris Bobrov Change-Id: Idb192a2fd370fc26c7d76788e9ad1856483d3239 (cherry picked from commit d6a3dc511057d6ac25bd2d75776a4233c5608684) --- keystone/api/credentials.py | 21 ++++++++---- keystone/tests/unit/test_v3_credential.py | 39 ++++++++++++++++++++++- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/keystone/api/credentials.py b/keystone/api/credentials.py index 9d1d777ad..c9a61ff47 100644 --- a/keystone/api/credentials.py +++ b/keystone/api/credentials.py @@ -33,6 +33,16 @@ PROVIDERS = provider_api.ProviderAPIs ENFORCER = rbac_enforcer.RBACEnforcer +def _check_unrestricted_application_credential(token): + if 'application_credential' in token.methods: + if not token.application_credential['unrestricted']: + action = _( + "Using method 'application_credential' is not " + "allowed for managing additional credentials." + ) + raise exception.ForbiddenAction(action=action) + + def _build_target_enforcement(): target = {} try: @@ -187,13 +197,12 @@ class CredentialsResource(ks_flask.ResourceBase): ENFORCER.enforce_call( action='identity:create_credential', target_attr=target ) + token = self.auth_context['token'] + if credential.get('type', '').lower() == 'ec2': + _check_unrestricted_application_credential(token) trust_id = getattr(self.oslo_context, 'trust_id', None) - app_cred_id = getattr( - self.auth_context['token'], 'application_credential_id', None - ) - access_token_id = getattr( - self.auth_context['token'], 'access_token_id', None - ) + app_cred_id = getattr(token, 'application_credential_id', None) + access_token_id = getattr(token, 'access_token_id', None) ref = self._assign_unique_id( self._normalize_dict(credential), trust_id=trust_id, diff --git a/keystone/tests/unit/test_v3_credential.py b/keystone/tests/unit/test_v3_credential.py index 87e57f809..538b48045 100644 --- a/keystone/tests/unit/test_v3_credential.py +++ b/keystone/tests/unit/test_v3_credential.py @@ -719,9 +719,11 @@ class TestCredentialAppCreds(CredentialBaseTestCase): Call ``POST /credentials``. """ - # Create the app cred + # Create an unrestricted app cred (restricted app creds are + # blocked from creating EC2 credentials) ref = unit.new_application_credential_ref(roles=[{'id': self.role_id}]) del ref['id'] + ref['unrestricted'] = True r = self.post( f'/users/{self.user_id}/application_credentials', body={'application_credential': ref}, @@ -781,6 +783,41 @@ class TestCredentialAppCreds(CredentialBaseTestCase): expected_status=http.client.CONFLICT, ) + def _get_app_cred_token(self, unrestricted=False): + """Create an application credential and return its token.""" + ref = unit.new_application_credential_ref(roles=[{'id': self.role_id}]) + del ref['id'] + if unrestricted: + ref['unrestricted'] = True + r = self.post( + f'/users/{self.user_id}/application_credentials', + body={'application_credential': ref}, + ) + app_cred = r.result['application_credential'] + auth_data = self.build_authentication_request( + app_cred_id=app_cred['id'], secret=app_cred['secret'] + ) + r = self.v3_create_token(auth_data) + return r.headers.get('X-Subject-Token') + + def test_restricted_app_cred_cannot_create_ec2_credential(self): + """Test that a restricted app cred cannot create EC2 credentials. + + A restricted application credential must not be allowed to + create EC2 credentials via POST /credentials either, as this + would bypass the guard on the OS-EC2 endpoint. + """ + token_id = self._get_app_cred_token(unrestricted=False) + blob, ref = unit.new_ec2_credential( + user_id=self.user_id, project_id=self.project_id + ) + self.post( + '/credentials', + body={'credential': ref}, + token=token_id, + expected_status=http.client.FORBIDDEN, + ) + class TestCredentialAccessToken(CredentialBaseTestCase): """Test credential with access token.""" -- 2.47.3