"Fossies" - the Fresh Open Source Software Archive

Member "keystone-17.0.0/keystone/tests/unit/test_v3_credential.py" (13 May 2020, 40404 Bytes) of package /linux/misc/openstack/keystone-17.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_v3_credential.py": 16.0.1_vs_17.0.0.

    1 # Copyright 2013 OpenStack Foundation
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 import hashlib
   16 import json
   17 from unittest import mock
   18 import uuid
   19 
   20 import http.client
   21 from keystoneclient.contrib.ec2 import utils as ec2_utils
   22 from oslo_db import exception as oslo_db_exception
   23 from testtools import matchers
   24 import urllib
   25 
   26 from keystone.api import ec2tokens
   27 from keystone.common import provider_api
   28 from keystone.common import utils
   29 from keystone.credential.providers import fernet as credential_fernet
   30 from keystone import exception
   31 from keystone import oauth1
   32 from keystone.tests import unit
   33 from keystone.tests.unit import ksfixtures
   34 from keystone.tests.unit import test_v3
   35 
   36 
   37 PROVIDERS = provider_api.ProviderAPIs
   38 CRED_TYPE_EC2 = ec2tokens.CRED_TYPE_EC2
   39 
   40 
   41 class CredentialBaseTestCase(test_v3.RestfulTestCase):
   42 
   43     def setUp(self):
   44         super(CredentialBaseTestCase, self).setUp()
   45         self.useFixture(
   46             ksfixtures.KeyRepository(
   47                 self.config_fixture,
   48                 'credential',
   49                 credential_fernet.MAX_ACTIVE_KEYS
   50             )
   51         )
   52 
   53     def _create_dict_blob_credential(self):
   54         blob, credential = unit.new_ec2_credential(user_id=self.user['id'],
   55                                                    project_id=self.project_id)
   56 
   57         # Store the blob as a dict *not* JSON ref bug #1259584
   58         # This means we can test the dict->json workaround, added
   59         # as part of the bugfix for backwards compatibility works.
   60         credential['blob'] = blob
   61         credential_id = credential['id']
   62 
   63         # Create direct via the DB API to avoid validation failure
   64         PROVIDERS.credential_api.create_credential(credential_id, credential)
   65 
   66         return json.dumps(blob), credential_id
   67 
   68     def _test_get_token(self, access, secret):
   69         """Test signature validation with the access/secret provided."""
   70         signer = ec2_utils.Ec2Signer(secret)
   71         params = {'SignatureMethod': 'HmacSHA256',
   72                   'SignatureVersion': '2',
   73                   'AWSAccessKeyId': access}
   74         request = {'host': 'foo',
   75                    'verb': 'GET',
   76                    'path': '/bar',
   77                    'params': params}
   78         signature = signer.generate(request)
   79 
   80         # Now make a request to validate the signed dummy request via the
   81         # ec2tokens API.  This proves the v3 ec2 credentials actually work.
   82         sig_ref = {'access': access,
   83                    'signature': signature,
   84                    'host': 'foo',
   85                    'verb': 'GET',
   86                    'path': '/bar',
   87                    'params': params}
   88         r = self.post(
   89             '/ec2tokens',
   90             body={'ec2Credentials': sig_ref},
   91             expected_status=http.client.OK)
   92         self.assertValidTokenResponse(r)
   93         return r.result['token']
   94 
   95 
   96 class CredentialTestCase(CredentialBaseTestCase):
   97     """Test credential CRUD."""
   98 
   99     def setUp(self):
  100 
  101         super(CredentialTestCase, self).setUp()
  102 
  103         self.credential = unit.new_credential_ref(user_id=self.user['id'],
  104                                                   project_id=self.project_id)
  105 
  106         PROVIDERS.credential_api.create_credential(
  107             self.credential['id'],
  108             self.credential)
  109 
  110     def test_credential_api_delete_credentials_for_project(self):
  111         PROVIDERS.credential_api.delete_credentials_for_project(
  112             self.project_id
  113         )
  114         # Test that the credential that we created in .setUp no longer exists
  115         # once we delete all credentials for self.project_id
  116         self.assertRaises(exception.CredentialNotFound,
  117                           PROVIDERS.credential_api.get_credential,
  118                           credential_id=self.credential['id'])
  119 
  120     def test_credential_api_delete_credentials_for_user(self):
  121         PROVIDERS.credential_api.delete_credentials_for_user(self.user_id)
  122         # Test that the credential that we created in .setUp no longer exists
  123         # once we delete all credentials for self.user_id
  124         self.assertRaises(exception.CredentialNotFound,
  125                           PROVIDERS.credential_api.get_credential,
  126                           credential_id=self.credential['id'])
  127 
  128     def test_list_credentials(self):
  129         """Call ``GET /credentials``."""
  130         r = self.get('/credentials')
  131         self.assertValidCredentialListResponse(r, ref=self.credential)
  132 
  133     def test_list_credentials_filtered_by_user_id(self):
  134         """Call ``GET  /credentials?user_id={user_id}``."""
  135         credential = unit.new_credential_ref(user_id=uuid.uuid4().hex)
  136         PROVIDERS.credential_api.create_credential(
  137             credential['id'], credential
  138         )
  139 
  140         r = self.get('/credentials?user_id=%s' % self.user['id'])
  141         self.assertValidCredentialListResponse(r, ref=self.credential)
  142         for cred in r.result['credentials']:
  143             self.assertEqual(self.user['id'], cred['user_id'])
  144 
  145     def test_list_credentials_filtered_by_type(self):
  146         """Call ``GET  /credentials?type={type}``."""
  147         PROVIDERS.assignment_api.create_system_grant_for_user(
  148             self.user_id, self.role_id
  149         )
  150         token = self.get_system_scoped_token()
  151 
  152         # The type ec2 was chosen, instead of a random string,
  153         # because the type must be in the list of supported types
  154         ec2_credential = unit.new_credential_ref(user_id=uuid.uuid4().hex,
  155                                                  project_id=self.project_id,
  156                                                  type=CRED_TYPE_EC2)
  157 
  158         ec2_resp = PROVIDERS.credential_api.create_credential(
  159             ec2_credential['id'], ec2_credential)
  160 
  161         # The type cert was chosen for the same reason as ec2
  162         r = self.get('/credentials?type=cert', token=token)
  163 
  164         # Testing the filter for two different types
  165         self.assertValidCredentialListResponse(r, ref=self.credential)
  166         for cred in r.result['credentials']:
  167             self.assertEqual('cert', cred['type'])
  168 
  169         r_ec2 = self.get('/credentials?type=ec2', token=token)
  170         self.assertThat(r_ec2.result['credentials'], matchers.HasLength(1))
  171         cred_ec2 = r_ec2.result['credentials'][0]
  172 
  173         self.assertValidCredentialListResponse(r_ec2, ref=ec2_resp)
  174         self.assertEqual(CRED_TYPE_EC2, cred_ec2['type'])
  175         self.assertEqual(ec2_credential['id'], cred_ec2['id'])
  176 
  177     def test_list_credentials_filtered_by_type_and_user_id(self):
  178         """Call ``GET  /credentials?user_id={user_id}&type={type}``."""
  179         user1_id = uuid.uuid4().hex
  180         user2_id = uuid.uuid4().hex
  181 
  182         PROVIDERS.assignment_api.create_system_grant_for_user(
  183             self.user_id, self.role_id
  184         )
  185         token = self.get_system_scoped_token()
  186 
  187         # Creating credentials for two different users
  188         credential_user1_ec2 = unit.new_credential_ref(user_id=user1_id,
  189                                                        type=CRED_TYPE_EC2)
  190         credential_user1_cert = unit.new_credential_ref(user_id=user1_id)
  191         credential_user2_cert = unit.new_credential_ref(user_id=user2_id)
  192 
  193         PROVIDERS.credential_api.create_credential(
  194             credential_user1_ec2['id'], credential_user1_ec2)
  195         PROVIDERS.credential_api.create_credential(
  196             credential_user1_cert['id'], credential_user1_cert)
  197         PROVIDERS.credential_api.create_credential(
  198             credential_user2_cert['id'], credential_user2_cert)
  199 
  200         r = self.get(
  201             '/credentials?user_id=%s&type=ec2' % user1_id, token=token
  202         )
  203         self.assertValidCredentialListResponse(r, ref=credential_user1_ec2)
  204         self.assertThat(r.result['credentials'], matchers.HasLength(1))
  205         cred = r.result['credentials'][0]
  206         self.assertEqual(CRED_TYPE_EC2, cred['type'])
  207         self.assertEqual(user1_id, cred['user_id'])
  208 
  209     def test_create_credential(self):
  210         """Call ``POST /credentials``."""
  211         ref = unit.new_credential_ref(user_id=self.user['id'])
  212         r = self.post(
  213             '/credentials',
  214             body={'credential': ref})
  215         self.assertValidCredentialResponse(r, ref)
  216 
  217     def test_get_credential(self):
  218         """Call ``GET /credentials/{credential_id}``."""
  219         r = self.get(
  220             '/credentials/%(credential_id)s' % {
  221                 'credential_id': self.credential['id']})
  222         self.assertValidCredentialResponse(r, self.credential)
  223 
  224     def test_update_credential(self):
  225         """Call ``PATCH /credentials/{credential_id}``."""
  226         ref = unit.new_credential_ref(user_id=self.user['id'],
  227                                       project_id=self.project_id)
  228         del ref['id']
  229         r = self.patch(
  230             '/credentials/%(credential_id)s' % {
  231                 'credential_id': self.credential['id']},
  232             body={'credential': ref})
  233         self.assertValidCredentialResponse(r, ref)
  234 
  235     def test_update_credential_to_ec2_type(self):
  236         """Call ``PATCH /credentials/{credential_id}``."""
  237         # Create a credential without providing a project_id
  238         ref = unit.new_credential_ref(user_id=self.user['id'])
  239         r = self.post(
  240             '/credentials',
  241             body={'credential': ref})
  242         self.assertValidCredentialResponse(r, ref)
  243         credential_id = r.result.get('credential')['id']
  244 
  245         # Updating the credential to ec2 requires a project_id
  246         update_ref = {'type': 'ec2', 'project_id': self.project_id}
  247         self.patch(
  248             '/credentials/%(credential_id)s' % {
  249                 'credential_id': credential_id},
  250             body={'credential': update_ref})
  251 
  252     def test_update_credential_to_ec2_missing_project_id(self):
  253         """Call ``PATCH /credentials/{credential_id}``."""
  254         # Create a credential without providing a project_id
  255         ref = unit.new_credential_ref(user_id=self.user['id'])
  256         r = self.post(
  257             '/credentials',
  258             body={'credential': ref})
  259         self.assertValidCredentialResponse(r, ref)
  260         credential_id = r.result.get('credential')['id']
  261 
  262         # Updating such credential to ec2 type without providing a project_id
  263         # will fail
  264         update_ref = {'type': 'ec2'}
  265         self.patch(
  266             '/credentials/%(credential_id)s' % {
  267                 'credential_id': credential_id},
  268             body={'credential': update_ref},
  269             expected_status=http.client.BAD_REQUEST)
  270 
  271     def test_update_credential_to_ec2_with_previously_set_project_id(self):
  272         """Call ``PATCH /credentials/{credential_id}``."""
  273         # Create a credential providing a project_id
  274         ref = unit.new_credential_ref(user_id=self.user['id'],
  275                                       project_id=self.project_id)
  276         r = self.post(
  277             '/credentials',
  278             body={'credential': ref})
  279         self.assertValidCredentialResponse(r, ref)
  280         credential_id = r.result.get('credential')['id']
  281 
  282         # Since the created credential above already has a project_id, the
  283         # update request will not fail
  284         update_ref = {'type': 'ec2'}
  285         self.patch(
  286             '/credentials/%(credential_id)s' % {
  287                 'credential_id': credential_id},
  288             body={'credential': update_ref})
  289 
  290     def test_update_credential_non_owner(self):
  291         """Call ``PATCH /credentials/{credential_id}``."""
  292         alt_user = unit.create_user(
  293             PROVIDERS.identity_api, domain_id=self.domain_id)
  294         alt_user_id = alt_user['id']
  295         alt_project = unit.new_project_ref(domain_id=self.domain_id)
  296         alt_project_id = alt_project['id']
  297         PROVIDERS.resource_api.create_project(
  298             alt_project['id'], alt_project)
  299         alt_role = unit.new_role_ref(name='reader')
  300         alt_role_id = alt_role['id']
  301         PROVIDERS.role_api.create_role(alt_role_id, alt_role)
  302         PROVIDERS.assignment_api.add_role_to_user_and_project(
  303             alt_user_id, alt_project_id, alt_role_id)
  304         auth = self.build_authentication_request(
  305             user_id=alt_user_id,
  306             password=alt_user['password'],
  307             project_id=alt_project_id)
  308         ref = unit.new_credential_ref(user_id=alt_user_id,
  309                                       project_id=alt_project_id)
  310         r = self.post(
  311             '/credentials',
  312             auth=auth,
  313             body={'credential': ref})
  314         self.assertValidCredentialResponse(r, ref)
  315         credential_id = r.result.get('credential')['id']
  316 
  317         # Cannot change the credential to be owned by another user
  318         update_ref = {'user_id': self.user_id, 'project_id': self.project_id}
  319         self.patch(
  320             '/credentials/%(credential_id)s' % {
  321                 'credential_id': credential_id},
  322             expected_status=403,
  323             auth=auth,
  324             body={'credential': update_ref})
  325 
  326     def test_update_ec2_credential_change_trust_id(self):
  327         """Call ``PATCH /credentials/{credential_id}``."""
  328         blob, ref = unit.new_ec2_credential(user_id=self.user['id'],
  329                                             project_id=self.project_id)
  330         blob['trust_id'] = uuid.uuid4().hex
  331         ref['blob'] = json.dumps(blob)
  332         r = self.post(
  333             '/credentials',
  334             body={'credential': ref})
  335         self.assertValidCredentialResponse(r, ref)
  336         credential_id = r.result.get('credential')['id']
  337         # Try changing to a different trust
  338         blob['trust_id'] = uuid.uuid4().hex
  339         update_ref = {'blob': json.dumps(blob)}
  340         self.patch(
  341             '/credentials/%(credential_id)s' % {
  342                 'credential_id': credential_id},
  343             body={'credential': update_ref},
  344             expected_status=http.client.BAD_REQUEST)
  345         # Try removing the trust
  346         del blob['trust_id']
  347         update_ref = {'blob': json.dumps(blob)}
  348         self.patch(
  349             '/credentials/%(credential_id)s' % {
  350                 'credential_id': credential_id},
  351             body={'credential': update_ref},
  352             expected_status=http.client.BAD_REQUEST)
  353 
  354     def test_update_ec2_credential_change_app_cred_id(self):
  355         """Call ``PATCH /credentials/{credential_id}``."""
  356         blob, ref = unit.new_ec2_credential(user_id=self.user['id'],
  357                                             project_id=self.project_id)
  358         blob['app_cred_id'] = uuid.uuid4().hex
  359         ref['blob'] = json.dumps(blob)
  360         r = self.post(
  361             '/credentials',
  362             body={'credential': ref})
  363         self.assertValidCredentialResponse(r, ref)
  364         credential_id = r.result.get('credential')['id']
  365         # Try changing to a different app cred
  366         blob['app_cred_id'] = uuid.uuid4().hex
  367         update_ref = {'blob': json.dumps(blob)}
  368         self.patch(
  369             '/credentials/%(credential_id)s' % {
  370                 'credential_id': credential_id},
  371             body={'credential': update_ref},
  372             expected_status=http.client.BAD_REQUEST)
  373         # Try removing the app cred
  374         del blob['app_cred_id']
  375         update_ref = {'blob': json.dumps(blob)}
  376         self.patch(
  377             '/credentials/%(credential_id)s' % {
  378                 'credential_id': credential_id},
  379             body={'credential': update_ref},
  380             expected_status=http.client.BAD_REQUEST)
  381 
  382     def test_update_ec2_credential_change_access_token_id(self):
  383         """Call ``PATCH /credentials/{credential_id}``."""
  384         blob, ref = unit.new_ec2_credential(user_id=self.user['id'],
  385                                             project_id=self.project_id)
  386         blob['access_token_id'] = uuid.uuid4().hex
  387         ref['blob'] = json.dumps(blob)
  388         r = self.post(
  389             '/credentials',
  390             body={'credential': ref})
  391         self.assertValidCredentialResponse(r, ref)
  392         credential_id = r.result.get('credential')['id']
  393         # Try changing to a different access token
  394         blob['access_token_id'] = uuid.uuid4().hex
  395         update_ref = {'blob': json.dumps(blob)}
  396         self.patch(
  397             '/credentials/%(credential_id)s' % {
  398                 'credential_id': credential_id},
  399             body={'credential': update_ref},
  400             expected_status=http.client.BAD_REQUEST)
  401         # Try removing the access token
  402         del blob['access_token_id']
  403         update_ref = {'blob': json.dumps(blob)}
  404         self.patch(
  405             '/credentials/%(credential_id)s' % {
  406                 'credential_id': credential_id},
  407             body={'credential': update_ref},
  408             expected_status=http.client.BAD_REQUEST)
  409 
  410     def test_delete_credential(self):
  411         """Call ``DELETE /credentials/{credential_id}``."""
  412         self.delete(
  413             '/credentials/%(credential_id)s' % {
  414                 'credential_id': self.credential['id']})
  415 
  416     def test_delete_credential_retries_on_deadlock(self):
  417         patcher = mock.patch('sqlalchemy.orm.query.Query.delete',
  418                              autospec=True)
  419 
  420         class FakeDeadlock(object):
  421             def __init__(self, mock_patcher):
  422                 self.deadlock_count = 2
  423                 self.mock_patcher = mock_patcher
  424                 self.patched = True
  425 
  426             def __call__(self, *args, **kwargs):
  427                 if self.deadlock_count > 1:
  428                     self.deadlock_count -= 1
  429                 else:
  430                     self.mock_patcher.stop()
  431                     self.patched = False
  432                 raise oslo_db_exception.DBDeadlock
  433 
  434         sql_delete_mock = patcher.start()
  435         side_effect = FakeDeadlock(patcher)
  436         sql_delete_mock.side_effect = side_effect
  437 
  438         try:
  439             PROVIDERS.credential_api.delete_credentials_for_user(
  440                 user_id=self.user['id'])
  441         finally:
  442             if side_effect.patched:
  443                 patcher.stop()
  444 
  445         # initial attempt + 1 retry
  446         self.assertEqual(sql_delete_mock.call_count, 2)
  447 
  448     def test_create_ec2_credential(self):
  449         """Call ``POST /credentials`` for creating ec2 credential."""
  450         blob, ref = unit.new_ec2_credential(user_id=self.user['id'],
  451                                             project_id=self.project_id)
  452         r = self.post('/credentials', body={'credential': ref})
  453         self.assertValidCredentialResponse(r, ref)
  454         # Assert credential id is same as hash of access key id for
  455         # ec2 credentials
  456         access = blob['access'].encode('utf-8')
  457         self.assertEqual(hashlib.sha256(access).hexdigest(),
  458                          r.result['credential']['id'])
  459         # Create second ec2 credential with the same access key id and check
  460         # for conflict.
  461         self.post(
  462             '/credentials',
  463             body={'credential': ref}, expected_status=http.client.CONFLICT)
  464 
  465     def test_get_ec2_dict_blob(self):
  466         """Ensure non-JSON blob data is correctly converted."""
  467         expected_blob, credential_id = self._create_dict_blob_credential()
  468 
  469         r = self.get(
  470             '/credentials/%(credential_id)s' % {
  471                 'credential_id': credential_id})
  472 
  473         # use json.loads to transform the blobs back into Python dictionaries
  474         # to avoid problems with the keys being in different orders.
  475         self.assertEqual(json.loads(expected_blob),
  476                          json.loads(r.result['credential']['blob']))
  477 
  478     def test_list_ec2_dict_blob(self):
  479         """Ensure non-JSON blob data is correctly converted."""
  480         expected_blob, credential_id = self._create_dict_blob_credential()
  481 
  482         list_r = self.get('/credentials')
  483         list_creds = list_r.result['credentials']
  484         list_ids = [r['id'] for r in list_creds]
  485         self.assertIn(credential_id, list_ids)
  486         # use json.loads to transform the blobs back into Python dictionaries
  487         # to avoid problems with the keys being in different orders.
  488         for r in list_creds:
  489             if r['id'] == credential_id:
  490                 self.assertEqual(json.loads(expected_blob),
  491                                  json.loads(r['blob']))
  492 
  493     def test_create_non_ec2_credential(self):
  494         """Test creating non-ec2 credential.
  495 
  496         Call ``POST /credentials``.
  497         """
  498         blob, ref = unit.new_cert_credential(user_id=self.user['id'])
  499 
  500         r = self.post('/credentials', body={'credential': ref})
  501         self.assertValidCredentialResponse(r, ref)
  502         # Assert credential id is not same as hash of access key id for
  503         # non-ec2 credentials
  504         access = blob['access'].encode('utf-8')
  505         self.assertNotEqual(hashlib.sha256(access).hexdigest(),
  506                             r.result['credential']['id'])
  507 
  508     def test_create_ec2_credential_with_missing_project_id(self):
  509         """Test Creating ec2 credential with missing project_id.
  510 
  511         Call ``POST /credentials``.
  512         """
  513         _, ref = unit.new_ec2_credential(user_id=self.user['id'],
  514                                          project_id=None)
  515         # Assert bad request status when missing project_id
  516         self.post(
  517             '/credentials',
  518             body={'credential': ref}, expected_status=http.client.BAD_REQUEST)
  519 
  520     def test_create_ec2_credential_with_invalid_blob(self):
  521         """Test creating ec2 credential with invalid blob.
  522 
  523         Call ``POST /credentials``.
  524         """
  525         ref = unit.new_credential_ref(user_id=self.user['id'],
  526                                       project_id=self.project_id,
  527                                       blob='{"abc":"def"d}',
  528                                       type=CRED_TYPE_EC2)
  529         # Assert bad request status when request contains invalid blob
  530         response = self.post(
  531             '/credentials',
  532             body={'credential': ref}, expected_status=http.client.BAD_REQUEST)
  533         self.assertValidErrorResponse(response)
  534 
  535     def test_create_credential_with_admin_token(self):
  536         # Make sure we can create credential with the static admin token
  537         ref = unit.new_credential_ref(user_id=self.user['id'])
  538         r = self.post(
  539             '/credentials',
  540             body={'credential': ref},
  541             token=self.get_admin_token())
  542         self.assertValidCredentialResponse(r, ref)
  543 
  544 
  545 class TestCredentialTrustScoped(CredentialBaseTestCase):
  546     """Test credential with trust scoped token."""
  547 
  548     def setUp(self):
  549         super(TestCredentialTrustScoped, self).setUp()
  550 
  551         self.trustee_user = unit.new_user_ref(domain_id=self.domain_id)
  552         password = self.trustee_user['password']
  553         self.trustee_user = PROVIDERS.identity_api.create_user(
  554             self.trustee_user
  555         )
  556         self.trustee_user['password'] = password
  557         self.trustee_user_id = self.trustee_user['id']
  558         self.useFixture(
  559             ksfixtures.KeyRepository(
  560                 self.config_fixture,
  561                 'credential',
  562                 credential_fernet.MAX_ACTIVE_KEYS
  563             )
  564         )
  565 
  566     def config_overrides(self):
  567         super(TestCredentialTrustScoped, self).config_overrides()
  568         self.config_fixture.config(group='trust')
  569 
  570     def test_trust_scoped_ec2_credential(self):
  571         """Test creating trust scoped ec2 credential.
  572 
  573         Call ``POST /credentials``.
  574         """
  575         # Create the trust
  576         ref = unit.new_trust_ref(
  577             trustor_user_id=self.user_id,
  578             trustee_user_id=self.trustee_user_id,
  579             project_id=self.project_id,
  580             impersonation=True,
  581             expires=dict(minutes=1),
  582             role_ids=[self.role_id])
  583         del ref['id']
  584         r = self.post('/OS-TRUST/trusts', body={'trust': ref})
  585         trust = self.assertValidTrustResponse(r)
  586 
  587         # Get a trust scoped token
  588         auth_data = self.build_authentication_request(
  589             user_id=self.trustee_user['id'],
  590             password=self.trustee_user['password'],
  591             trust_id=trust['id'])
  592         r = self.v3_create_token(auth_data)
  593         self.assertValidProjectScopedTokenResponse(r, self.user)
  594         trust_id = r.result['token']['OS-TRUST:trust']['id']
  595         token_id = r.headers.get('X-Subject-Token')
  596 
  597         # Create the credential with the trust scoped token
  598         blob, ref = unit.new_ec2_credential(user_id=self.user_id,
  599                                             project_id=self.project_id)
  600         r = self.post('/credentials', body={'credential': ref}, token=token_id)
  601 
  602         # We expect the response blob to contain the trust_id
  603         ret_ref = ref.copy()
  604         ret_blob = blob.copy()
  605         ret_blob['trust_id'] = trust_id
  606         ret_ref['blob'] = json.dumps(ret_blob)
  607         self.assertValidCredentialResponse(r, ref=ret_ref)
  608 
  609         # Assert credential id is same as hash of access key id for
  610         # ec2 credentials
  611         access = blob['access'].encode('utf-8')
  612         self.assertEqual(hashlib.sha256(access).hexdigest(),
  613                          r.result['credential']['id'])
  614 
  615         # Create a role assignment to ensure that it is ignored and only the
  616         # trust-delegated roles are used
  617         role = unit.new_role_ref(name='reader')
  618         role_id = role['id']
  619         PROVIDERS.role_api.create_role(role_id, role)
  620         PROVIDERS.assignment_api.add_role_to_user_and_project(
  621             self.user_id, self.project_id, role_id)
  622 
  623         ret_blob = json.loads(r.result['credential']['blob'])
  624         ec2token = self._test_get_token(
  625             access=ret_blob['access'], secret=ret_blob['secret'])
  626         ec2_roles = [role['id'] for role in ec2token['roles']]
  627         self.assertIn(self.role_id, ec2_roles)
  628         self.assertNotIn(role_id, ec2_roles)
  629 
  630         # Create second ec2 credential with the same access key id and check
  631         # for conflict.
  632         self.post(
  633             '/credentials',
  634             body={'credential': ref},
  635             token=token_id,
  636             expected_status=http.client.CONFLICT)
  637 
  638 
  639 class TestCredentialAppCreds(CredentialBaseTestCase):
  640     """Test credential with application credential token."""
  641 
  642     def setUp(self):
  643         super(TestCredentialAppCreds, self).setUp()
  644         self.useFixture(
  645             ksfixtures.KeyRepository(
  646                 self.config_fixture,
  647                 'credential',
  648                 credential_fernet.MAX_ACTIVE_KEYS
  649             )
  650         )
  651 
  652     def test_app_cred_ec2_credential(self):
  653         """Test creating ec2 credential from an application credential.
  654 
  655         Call ``POST /credentials``.
  656         """
  657         # Create the app cred
  658         ref = unit.new_application_credential_ref(roles=[{'id': self.role_id}])
  659         del ref['id']
  660         r = self.post('/users/%s/application_credentials' % self.user_id,
  661                       body={'application_credential': ref})
  662         app_cred = r.result['application_credential']
  663 
  664         # Get an application credential token
  665         auth_data = self.build_authentication_request(
  666             app_cred_id=app_cred['id'],
  667             secret=app_cred['secret'])
  668         r = self.v3_create_token(auth_data)
  669         token_id = r.headers.get('X-Subject-Token')
  670 
  671         # Create the credential with the app cred token
  672         blob, ref = unit.new_ec2_credential(user_id=self.user_id,
  673                                             project_id=self.project_id)
  674         r = self.post('/credentials', body={'credential': ref}, token=token_id)
  675 
  676         # We expect the response blob to contain the app_cred_id
  677         ret_ref = ref.copy()
  678         ret_blob = blob.copy()
  679         ret_blob['app_cred_id'] = app_cred['id']
  680         ret_ref['blob'] = json.dumps(ret_blob)
  681         self.assertValidCredentialResponse(r, ref=ret_ref)
  682 
  683         # Assert credential id is same as hash of access key id for
  684         # ec2 credentials
  685         access = blob['access'].encode('utf-8')
  686         self.assertEqual(hashlib.sha256(access).hexdigest(),
  687                          r.result['credential']['id'])
  688 
  689         # Create a role assignment to ensure that it is ignored and only the
  690         # roles in the app cred are used
  691         role = unit.new_role_ref(name='reader')
  692         role_id = role['id']
  693         PROVIDERS.role_api.create_role(role_id, role)
  694         PROVIDERS.assignment_api.add_role_to_user_and_project(
  695             self.user_id, self.project_id, role_id)
  696 
  697         ret_blob = json.loads(r.result['credential']['blob'])
  698         ec2token = self._test_get_token(
  699             access=ret_blob['access'], secret=ret_blob['secret'])
  700         ec2_roles = [role['id'] for role in ec2token['roles']]
  701         self.assertIn(self.role_id, ec2_roles)
  702         self.assertNotIn(role_id, ec2_roles)
  703 
  704         # Create second ec2 credential with the same access key id and check
  705         # for conflict.
  706         self.post(
  707             '/credentials',
  708             body={'credential': ref},
  709             token=token_id,
  710             expected_status=http.client.CONFLICT)
  711 
  712 
  713 class TestCredentialAccessToken(CredentialBaseTestCase):
  714     """Test credential with access token."""
  715 
  716     def setUp(self):
  717         super(TestCredentialAccessToken, self).setUp()
  718         self.useFixture(
  719             ksfixtures.KeyRepository(
  720                 self.config_fixture,
  721                 'credential',
  722                 credential_fernet.MAX_ACTIVE_KEYS
  723             )
  724         )
  725         self.base_url = 'http://localhost/v3'
  726 
  727     def _urllib_parse_qs_text_keys(self, content):
  728         results = urllib.parse.parse_qs(content)
  729         return {key.decode('utf-8'): value for key, value in results.items()}
  730 
  731     def _create_single_consumer(self):
  732         endpoint = '/OS-OAUTH1/consumers'
  733 
  734         ref = {'description': uuid.uuid4().hex}
  735         resp = self.post(endpoint, body={'consumer': ref})
  736         return resp.result['consumer']
  737 
  738     def _create_request_token(self, consumer, project_id, base_url=None):
  739         endpoint = '/OS-OAUTH1/request_token'
  740         client = oauth1.Client(consumer['key'],
  741                                client_secret=consumer['secret'],
  742                                signature_method=oauth1.SIG_HMAC,
  743                                callback_uri="oob")
  744         headers = {'requested_project_id': project_id}
  745         if not base_url:
  746             base_url = self.base_url
  747         url, headers, body = client.sign(base_url + endpoint,
  748                                          http_method='POST',
  749                                          headers=headers)
  750         return endpoint, headers
  751 
  752     def _create_access_token(self, consumer, token, base_url=None):
  753         endpoint = '/OS-OAUTH1/access_token'
  754         client = oauth1.Client(consumer['key'],
  755                                client_secret=consumer['secret'],
  756                                resource_owner_key=token.key,
  757                                resource_owner_secret=token.secret,
  758                                signature_method=oauth1.SIG_HMAC,
  759                                verifier=token.verifier)
  760         if not base_url:
  761             base_url = self.base_url
  762         url, headers, body = client.sign(base_url + endpoint,
  763                                          http_method='POST')
  764         headers.update({'Content-Type': 'application/json'})
  765         return endpoint, headers
  766 
  767     def _get_oauth_token(self, consumer, token):
  768         client = oauth1.Client(consumer['key'],
  769                                client_secret=consumer['secret'],
  770                                resource_owner_key=token.key,
  771                                resource_owner_secret=token.secret,
  772                                signature_method=oauth1.SIG_HMAC)
  773         endpoint = '/auth/tokens'
  774         url, headers, body = client.sign(self.base_url + endpoint,
  775                                          http_method='POST')
  776         headers.update({'Content-Type': 'application/json'})
  777         ref = {'auth': {'identity': {'oauth1': {}, 'methods': ['oauth1']}}}
  778         return endpoint, headers, ref
  779 
  780     def _authorize_request_token(self, request_id):
  781         if isinstance(request_id, bytes):
  782             request_id = request_id.decode()
  783         return '/OS-OAUTH1/authorize/%s' % (request_id)
  784 
  785     def _get_access_token(self):
  786         consumer = self._create_single_consumer()
  787         consumer_id = consumer['id']
  788         consumer_secret = consumer['secret']
  789         consumer = {'key': consumer_id, 'secret': consumer_secret}
  790 
  791         url, headers = self._create_request_token(consumer, self.project_id)
  792         content = self.post(
  793             url, headers=headers,
  794             response_content_type='application/x-www-form-urlencoded')
  795         credentials = self._urllib_parse_qs_text_keys(content.result)
  796         request_key = credentials['oauth_token'][0]
  797         request_secret = credentials['oauth_token_secret'][0]
  798         request_token = oauth1.Token(request_key, request_secret)
  799 
  800         url = self._authorize_request_token(request_key)
  801         body = {'roles': [{'id': self.role_id}]}
  802         resp = self.put(url, body=body, expected_status=http.client.OK)
  803         verifier = resp.result['token']['oauth_verifier']
  804 
  805         request_token.set_verifier(verifier)
  806         url, headers = self._create_access_token(consumer, request_token)
  807         content = self.post(
  808             url, headers=headers,
  809             response_content_type='application/x-www-form-urlencoded')
  810         credentials = self._urllib_parse_qs_text_keys(content.result)
  811         access_key = credentials['oauth_token'][0]
  812         access_secret = credentials['oauth_token_secret'][0]
  813         access_token = oauth1.Token(access_key, access_secret)
  814 
  815         url, headers, body = self._get_oauth_token(consumer, access_token)
  816         content = self.post(url, headers=headers, body=body)
  817         return access_key, content.headers['X-Subject-Token']
  818 
  819     def test_access_token_ec2_credential(self):
  820         """Test creating ec2 credential from an oauth access token.
  821 
  822         Call ``POST /credentials``.
  823         """
  824         access_key, token_id = self._get_access_token()
  825 
  826         # Create the credential with the access token
  827         blob, ref = unit.new_ec2_credential(user_id=self.user_id,
  828                                             project_id=self.project_id)
  829         r = self.post('/credentials', body={'credential': ref}, token=token_id)
  830 
  831         # We expect the response blob to contain the access_token_id
  832         ret_ref = ref.copy()
  833         ret_blob = blob.copy()
  834         ret_blob['access_token_id'] = access_key.decode('utf-8')
  835         ret_ref['blob'] = json.dumps(ret_blob)
  836         self.assertValidCredentialResponse(r, ref=ret_ref)
  837 
  838         # Assert credential id is same as hash of access key id for
  839         # ec2 credentials
  840         access = blob['access'].encode('utf-8')
  841         self.assertEqual(hashlib.sha256(access).hexdigest(),
  842                          r.result['credential']['id'])
  843 
  844         # Create a role assignment to ensure that it is ignored and only the
  845         # roles in the access token are used
  846         role = unit.new_role_ref(name='reader')
  847         role_id = role['id']
  848         PROVIDERS.role_api.create_role(role_id, role)
  849         PROVIDERS.assignment_api.add_role_to_user_and_project(
  850             self.user_id, self.project_id, role_id)
  851 
  852         ret_blob = json.loads(r.result['credential']['blob'])
  853         ec2token = self._test_get_token(
  854             access=ret_blob['access'], secret=ret_blob['secret'])
  855         ec2_roles = [role['id'] for role in ec2token['roles']]
  856         self.assertIn(self.role_id, ec2_roles)
  857         self.assertNotIn(role_id, ec2_roles)
  858 
  859 
  860 class TestCredentialEc2(CredentialBaseTestCase):
  861     """Test v3 credential compatibility with ec2tokens."""
  862 
  863     def test_ec2_credential_signature_validate(self):
  864         """Test signature validation with a v3 ec2 credential."""
  865         blob, ref = unit.new_ec2_credential(user_id=self.user['id'],
  866                                             project_id=self.project_id)
  867         r = self.post('/credentials', body={'credential': ref})
  868         self.assertValidCredentialResponse(r, ref)
  869         # Assert credential id is same as hash of access key id
  870         access = blob['access'].encode('utf-8')
  871         self.assertEqual(hashlib.sha256(access).hexdigest(),
  872                          r.result['credential']['id'])
  873 
  874         cred_blob = json.loads(r.result['credential']['blob'])
  875         self.assertEqual(blob, cred_blob)
  876         self._test_get_token(access=cred_blob['access'],
  877                              secret=cred_blob['secret'])
  878 
  879     def test_ec2_credential_signature_validate_legacy(self):
  880         """Test signature validation with a legacy v3 ec2 credential."""
  881         cred_json, _ = self._create_dict_blob_credential()
  882         cred_blob = json.loads(cred_json)
  883         self._test_get_token(access=cred_blob['access'],
  884                              secret=cred_blob['secret'])
  885 
  886     def _get_ec2_cred_uri(self):
  887         return '/users/%s/credentials/OS-EC2' % self.user_id
  888 
  889     def _get_ec2_cred(self):
  890         uri = self._get_ec2_cred_uri()
  891         r = self.post(uri, body={'tenant_id': self.project_id})
  892         return r.result['credential']
  893 
  894     def test_ec2_create_credential(self):
  895         """Test ec2 credential creation."""
  896         ec2_cred = self._get_ec2_cred()
  897         self.assertEqual(self.user_id, ec2_cred['user_id'])
  898         self.assertEqual(self.project_id, ec2_cred['tenant_id'])
  899         self.assertIsNone(ec2_cred['trust_id'])
  900         self._test_get_token(access=ec2_cred['access'],
  901                              secret=ec2_cred['secret'])
  902         uri = '/'.join([self._get_ec2_cred_uri(), ec2_cred['access']])
  903         self.assertThat(ec2_cred['links']['self'],
  904                         matchers.EndsWith(uri))
  905 
  906     def test_ec2_get_credential(self):
  907         ec2_cred = self._get_ec2_cred()
  908         uri = '/'.join([self._get_ec2_cred_uri(), ec2_cred['access']])
  909         r = self.get(uri)
  910         self.assertDictEqual(ec2_cred, r.result['credential'])
  911         self.assertThat(ec2_cred['links']['self'],
  912                         matchers.EndsWith(uri))
  913 
  914     def test_ec2_cannot_get_non_ec2_credential(self):
  915         access_key = uuid.uuid4().hex
  916         cred_id = utils.hash_access_key(access_key)
  917         non_ec2_cred = unit.new_credential_ref(
  918             user_id=self.user_id,
  919             project_id=self.project_id)
  920         non_ec2_cred['id'] = cred_id
  921         PROVIDERS.credential_api.create_credential(cred_id, non_ec2_cred)
  922         uri = '/'.join([self._get_ec2_cred_uri(), access_key])
  923         # if access_key is not found, ec2 controller raises Unauthorized
  924         # exception
  925         self.get(uri, expected_status=http.client.UNAUTHORIZED)
  926 
  927     def test_ec2_list_credentials(self):
  928         """Test ec2 credential listing."""
  929         self._get_ec2_cred()
  930         uri = self._get_ec2_cred_uri()
  931         r = self.get(uri)
  932         cred_list = r.result['credentials']
  933         self.assertEqual(1, len(cred_list))
  934         self.assertThat(r.result['links']['self'],
  935                         matchers.EndsWith(uri))
  936 
  937         # non-EC2 credentials won't be fetched
  938         non_ec2_cred = unit.new_credential_ref(
  939             user_id=self.user_id,
  940             project_id=self.project_id)
  941         non_ec2_cred['type'] = uuid.uuid4().hex
  942         PROVIDERS.credential_api.create_credential(
  943             non_ec2_cred['id'], non_ec2_cred
  944         )
  945         r = self.get(uri)
  946         cred_list_2 = r.result['credentials']
  947         # still one element because non-EC2 credentials are not returned.
  948         self.assertEqual(1, len(cred_list_2))
  949         self.assertEqual(cred_list[0], cred_list_2[0])
  950 
  951     def test_ec2_delete_credential(self):
  952         """Test ec2 credential deletion."""
  953         ec2_cred = self._get_ec2_cred()
  954         uri = '/'.join([self._get_ec2_cred_uri(), ec2_cred['access']])
  955         cred_from_credential_api = (
  956             PROVIDERS.credential_api
  957             .list_credentials_for_user(self.user_id, type=CRED_TYPE_EC2))
  958         self.assertEqual(1, len(cred_from_credential_api))
  959         self.delete(uri)
  960         self.assertRaises(exception.CredentialNotFound,
  961                           PROVIDERS.credential_api.get_credential,
  962                           cred_from_credential_api[0]['id'])