Skip to content

Commit

Permalink
Pass serviceCatalog in auth_token middleware
Browse files Browse the repository at this point in the history
 * This will allow  for chained requests (novaclient -> nova -> cinder)
 * Fixes bug 1010237

Change-Id: Iab126cb1f2fb01ca7da24fa9fe97ec81ee96e455
  • Loading branch information
sleepsonthefloor authored and dolph committed Jun 19, 2012
1 parent 720b764 commit cc91786
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
14 changes: 13 additions & 1 deletion keystone/middleware/auth_token.py
Expand Up @@ -76,6 +76,9 @@
HTTP_X_ROLES
Comma delimited list of case-sensitive Roles
HTTP_X_SERVICE_CATALOG
json encoded keystone service catalog (optional).
HTTP_X_TENANT
*Deprecated* in favor of HTTP_X_TENANT_ID and HTTP_X_TENANT_NAME
Keystone-assigned unique identifier, deprecated
Expand Down Expand Up @@ -394,6 +397,7 @@ def _build_user_headers(self, token_info):
* X_USER_ID: id of user
* X_USER_NAME: name of user
* X_ROLES: list of roles
* X_SERVICE_CATALOG: service catalog
Additional (deprecated) headers include:
* X_USER: name of user
Expand Down Expand Up @@ -435,7 +439,7 @@ def default_tenant():
user_id = user['id']
user_name = user['name']

return {
rval = {
'X-Identity-Status': 'Confirmed',
'X-Tenant-Id': tenant_id,
'X-Tenant-Name': tenant_name,
Expand All @@ -448,6 +452,14 @@ def default_tenant():
'X-Role': roles,
}

try:
catalog = token_info['access']['serviceCatalog']
rval['X-Service-Catalog'] = json.dumps(catalog)
except KeyError:
pass

return rval

def _header_to_env_var(self, key):
"""Convert header to wsgi env variable.
Expand Down
4 changes: 2 additions & 2 deletions keystone/service.py
Expand Up @@ -414,10 +414,10 @@ def validate_token(self, context, token_id):
for role_id in metadata_ref.get('roles', []):
roles_ref.append(self.identity_api.get_role(context, role_id))

# Get a service catalog if belongs_to is not none
# Get a service catalog if possible
# This is needed for on-behalf-of requests
catalog_ref = None
if belongs_to is not None:
if token_ref.get('tenant'):
catalog_ref = self.catalog_api.get_catalog(
context=context,
user_id=token_ref['user']['id'],
Expand Down
2 changes: 2 additions & 0 deletions tests/test_auth_token_middleware.py
Expand Up @@ -43,6 +43,7 @@
{'name': 'role2'},
],
},
'serviceCatalog': {}
},
},
'default-tenant-token': {
Expand Down Expand Up @@ -244,6 +245,7 @@ def test_valid_request(self):
req.headers['X-Auth-Token'] = 'valid-token'
body = self.middleware(req.environ, self.start_fake_response)
self.assertEqual(self.response_status, 200)
self.assertTrue(req.headers.get('X-Service-Catalog'))
self.assertEqual(body, ['SUCCESS'])

def test_default_tenant_token(self):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_content_types.py
Expand Up @@ -375,6 +375,13 @@ def test_validate_token_belongs_to(self):
self.assertValidAuthenticationResponse(r,
require_service_catalog=True)

def test_validate_token_no_belongs_to_still_returns_catalog(self):
token = self.get_scoped_token()
path = ('/v2.0/tokens/%s' % token)
r = self.admin_request(path=path, token=token)
self.assertValidAuthenticationResponse(r,
require_service_catalog=True)

def test_validate_token_head(self):
"""The same call as above, except using HEAD.
Expand Down

0 comments on commit cc91786

Please sign in to comment.