2015-04-15 20:44:32 +02:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied. See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2015-09-29 14:39:48 +02:00
|
|
|
from debtcollector import removals
|
2015-06-24 15:31:29 +00:00
|
|
|
from oslo_config import cfg
|
2015-04-15 20:44:32 +02:00
|
|
|
from oslo_middleware import base
|
|
|
|
|
|
|
|
|
|
|
|
OPTS = [
|
|
|
|
cfg.StrOpt('secure_proxy_ssl_header',
|
|
|
|
default='X-Forwarded-Proto',
|
2015-09-29 14:39:48 +02:00
|
|
|
deprecated_for_removal=True,
|
2015-04-15 20:44:32 +02:00
|
|
|
help="The HTTP Header that will be used to determine what "
|
|
|
|
"the original request protocol scheme was, even if it was "
|
2016-08-11 12:49:15 -05:00
|
|
|
"hidden by a SSL termination proxy.")
|
2015-04-15 20:44:32 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2015-08-20 07:52:59 +02:00
|
|
|
class SSLMiddleware(base.ConfigurableMiddleware):
|
2015-04-15 20:44:32 +02:00
|
|
|
"""SSL termination proxies middleware.
|
|
|
|
|
|
|
|
This middleware overloads wsgi.url_scheme with the one provided in
|
|
|
|
secure_proxy_ssl_header header. This is useful when behind a SSL
|
|
|
|
termination proxy.
|
|
|
|
"""
|
|
|
|
|
2015-08-20 07:52:59 +02:00
|
|
|
def __init__(self, application, *args, **kwargs):
|
2016-10-02 10:26:08 -04:00
|
|
|
removals.removed_module(__name__, "oslo_middleware.http_proxy_to_wsgi")
|
2015-08-20 07:52:59 +02:00
|
|
|
super(SSLMiddleware, self).__init__(application, *args, **kwargs)
|
2015-08-06 09:15:57 +02:00
|
|
|
self.oslo_conf.register_opts(OPTS, group='oslo_middleware')
|
2015-04-15 20:44:32 +02:00
|
|
|
|
|
|
|
def process_request(self, req):
|
2015-08-06 09:15:57 +02:00
|
|
|
self.header_name = 'HTTP_{0}'.format(
|
2015-08-06 10:53:48 +02:00
|
|
|
self._conf_get('secure_proxy_ssl_header').upper()
|
2015-08-06 09:15:57 +02:00
|
|
|
.replace('-', '_'))
|
2015-04-15 20:44:32 +02:00
|
|
|
req.environ['wsgi.url_scheme'] = req.environ.get(
|
|
|
|
self.header_name, req.environ['wsgi.url_scheme'])
|