18
18
from keystoneclient.v2_0 import client as keystone_client
19
19
from keystoneclient import exceptions as keystone_exceptions
20
20
from oslo.config import cfg
21
from webob.exc import HTTPBadRequest
21
22
from webob.exc import HTTPUnauthorized
23
24
from heat.openstack.common import importutils
34
35
def __init__(self, app, conf):
37
if 'auth_uri' in self.conf:
38
auth_url = self.conf['auth_uri']
40
# Import auth_token to have keystone_authtoken settings setup.
41
importutils.import_module('keystoneclient.middleware.auth_token')
42
auth_url = cfg.CONF.keystone_authtoken['auth_uri']
39
if not cfg.CONF.auth_password.multi_cloud:
40
if 'auth_uri' in self.conf:
41
auth_url = self.conf['auth_uri']
43
# Import auth_token to have keystone_authtoken settings setup.
44
importutils.import_module(
45
'keystoneclient.middleware.auth_token')
46
auth_url = cfg.CONF.keystone_authtoken['auth_uri']
43
47
self.auth_url = auth_url
45
49
def __call__(self, env, start_response):
48
52
password = env.get('HTTP_X_AUTH_KEY')
49
53
# Determine tenant id from path.
50
54
tenant = env.get('PATH_INFO').split('/')[1]
55
auth_url = self.auth_url
56
if cfg.CONF.auth_password.multi_cloud:
57
auth_url = env.get('HTTP_X_AUTH_URL')
58
error = self._validate_auth_url(env, start_response, auth_url)
52
return self._reject_request(env, start_response)
62
return self._reject_request(env, start_response, auth_url)
54
64
client = keystone_client.Client(
55
65
username=username, password=password, tenant_id=tenant,
56
auth_url=self.auth_url)
57
67
except (keystone_exceptions.Unauthorized,
58
68
keystone_exceptions.Forbidden,
59
69
keystone_exceptions.NotFound,
60
70
keystone_exceptions.AuthorizationFailure):
61
return self._reject_request(env, start_response)
71
return self._reject_request(env, start_response, auth_url)
62
72
env['keystone.token_info'] = client.auth_ref
63
env.update(self._build_user_headers(client.auth_ref))
73
env.update(self._build_user_headers(client.auth_ref, auth_url))
64
74
return self.app(env, start_response)
66
def _reject_request(self, env, start_response):
76
def _reject_request(self, env, start_response, auth_url):
67
77
"""Redirect client to auth server."""
68
headers = [('WWW-Authenticate', 'Keystone uri=\'%s\'' % self.auth_url)]
78
headers = [('WWW-Authenticate', 'Keystone uri=\'%s\'' % auth_url)]
69
79
resp = HTTPUnauthorized('Authentication required', headers)
70
80
return resp(env, start_response)
72
def _build_user_headers(self, token_info):
82
def _build_user_headers(self, token_info, auth_url):
73
83
"""Build headers that represent authenticated user from auth token."""
74
84
tenant_id = token_info['token']['tenant']['id']
75
85
tenant_name = token_info['token']['tenant']['name']
89
99
'HTTP_X_ROLES': roles,
90
100
'HTTP_X_SERVICE_CATALOG': service_catalog,
91
101
'HTTP_X_AUTH_TOKEN': auth_token,
92
'HTTP_X_AUTH_URL': self.auth_url,
102
'HTTP_X_AUTH_URL': auth_url,
94
104
'HTTP_X_USER': user_name,
95
105
'HTTP_X_TENANT_ID': tenant_id,
113
def _validate_auth_url(self, env, start_response, auth_url):
114
"""Validate auth_url to ensure it can be used."""
116
resp = HTTPBadRequest(_('Request missing required header '
118
return resp(env, start_response)
119
allowed = cfg.CONF.auth_password.allowed_auth_uris
120
if auth_url not in allowed:
121
resp = HTTPUnauthorized(_('Header X-Auth-Url "%s" not an allowed '
124
return resp(env, start_response)
104
128
def filter_factory(global_conf, **local_conf):
105
129
"""Returns a WSGI filter app for use with paste.deploy."""