Convert Normalizing filter to flask native Middleware

Normalizing filter has been converted to a flask-native style
middleware instead of leaning on the old application logic from
Webob. We also now strip all trailing slashes, not just a single
traling slash.

Test Changes:

* test_url_middleware now tests the new middleware directly instead
  of leaning on webob and fake requests.

Change-Id: I5f82817b61a9284b97cf6443105107150d4a1757
Partial-Bug: #1776504
This commit is contained in:
Morgan Fainberg 2018-10-11 13:47:18 -07:00
parent 18d597f8e8
commit 848c8fa638
6 changed files with 71 additions and 58 deletions

View File

@ -13,4 +13,3 @@
# under the License.
from keystone.middleware.auth import * # noqa
from keystone.middleware.core import * # noqa

View File

@ -1,34 +0,0 @@
# Copyright 2012 OpenStack Foundation
#
# 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.
from oslo_log import log
from keystone.common import wsgi
LOG = log.getLogger(__name__)
class NormalizingFilter(wsgi.Middleware):
"""Middleware filter to handle URL normalization."""
def process_request(self, request):
"""Normalize URLs."""
# Removes a trailing slash from the given path, if any.
if (len(request.environ['PATH_INFO']) > 1 and
request.environ['PATH_INFO'][-1] == '/'):
request.environ['PATH_INFO'] = request.environ['PATH_INFO'][:-1]
# Rewrites path to root if no path is given.
elif not request.environ['PATH_INFO']:
request.environ['PATH_INFO'] = '/'

View File

@ -30,6 +30,7 @@ import keystone.conf
import keystone.middleware
import keystone.server
from keystone.server.flask import application
from keystone.server.flask.request_processing.middleware import url_normalize
# NOTE(morgan): Middleware Named Tuple with the following values:
# * "namespace": namespace for the entry_point
@ -67,7 +68,7 @@ _APP_MIDDLEWARE = (
# middleware defined in _APP_MIDDLEWARE. AuthContextMiddleware should always
# be the last element here as long as it is an actual Middleware.
_KEYSTONE_MIDDLEWARE = (
keystone.middleware.NormalizingFilter,
url_normalize.URLNormalizingMiddleware,
keystone.middleware.AuthContextMiddleware,
)

View File

@ -0,0 +1,35 @@
# 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.
# Flask Native URL Normalizing Middleware
class URLNormalizingMiddleware(object):
"""Middleware filter to handle URL normalization."""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
"""Normalize URLs."""
# TODO(morgan): evaluate collapsing multiple slashes in this middleware
# e.g. '/v3//auth/tokens -> /v3/auth/tokens
# Removes a trailing slashes from the given path, if any.
if len(environ['PATH_INFO']) > 1 and environ['PATH_INFO'][-1] == '/':
environ['PATH_INFO'] = environ['PATH_INFO'].rstrip('/')
# Rewrites path to root if no path is given
if not environ['PATH_INFO']:
environ['PATH_INFO'] = '/'
return self.app(environ, start_response)

View File

@ -12,43 +12,55 @@
# License for the specific language governing permissions and limitations
# under the License.
import webob
from keystone import middleware
from keystone.server.flask.request_processing.middleware import url_normalize
from keystone.tests import unit
class FakeApp(object):
"""Fakes a WSGI app URL normalized."""
def __init__(self):
self.env = {}
def __call__(self, env, start_response):
resp = webob.Response()
resp.body = 'SUCCESS'
return resp(env, start_response)
self.env = env
return
class UrlMiddlewareTest(unit.TestCase):
def setUp(self):
self.middleware = middleware.NormalizingFilter(FakeApp())
self.response_status = None
self.response_headers = None
super(UrlMiddlewareTest, self).setUp()
def start_fake_response(self, status, headers):
self.response_status = int(status.split(' ', 1)[0])
self.response_headers = dict(headers)
self.fake_app = FakeApp()
self.middleware = url_normalize.URLNormalizingMiddleware(self.fake_app)
def test_trailing_slash_normalization(self):
"""Test /v3/auth/tokens & /v3/auth/tokens/ normalized URLs match."""
req1 = webob.Request.blank('/v3/auth/tokens')
req2 = webob.Request.blank('/v3/auth/tokens/')
self.middleware(req1.environ, self.start_fake_response)
self.middleware(req2.environ, self.start_fake_response)
self.assertEqual(req1.path_url, req2.path_url)
self.assertEqual('http://localhost/v3/auth/tokens', req1.path_url)
expected = '/v3/auth/tokens'
no_slash = {'PATH_INFO': expected}
with_slash = {'PATH_INFO': '/v3/auth/tokens/'}
with_many_slash = {'PATH_INFO': '/v3/auth/tokens////'}
# Run with a URL that doesn't need stripping and ensure nothing else is
# added to the environ
self.middleware(no_slash, None)
self.assertEqual(expected, self.fake_app.env['PATH_INFO'])
self.assertEqual(1, len(self.fake_app.env.keys()))
# Run with a URL that needs a single slash stripped and nothing else is
# added to the environ
self.middleware(with_slash, None)
self.assertEqual(expected, self.fake_app.env['PATH_INFO'])
self.assertEqual(1, len(self.fake_app.env.keys()))
# Run with a URL that needs multiple slashes stripped and ensure
# nothing else is added to the environ
self.middleware(with_many_slash, None)
self.assertEqual(expected, self.fake_app.env['PATH_INFO'])
self.assertEqual(1, len(self.fake_app.env.keys()))
def test_rewrite_empty_path(self):
"""Test empty path is rewritten to root."""
req = webob.Request.blank('')
self.middleware(req.environ, self.start_fake_response)
self.assertEqual('http://localhost/', req.path_url)
environ = {'PATH_INFO': ''}
self.middleware(environ, None)
self.assertEqual('/', self.fake_app.env['PATH_INFO'])
self.assertEqual(1, len(self.fake_app.env.keys()))