Use split_path from oslo.utils
We added method split_path in version 3.11 of oslo.utils, so don't maintain it by sahara. Change-Id: Ifd9fea2689fef508af5acba2e457f9ebd392b2fe
This commit is contained in:
parent
7116a3ec34
commit
e50c21186d
@ -15,12 +15,12 @@
|
|||||||
|
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_middleware import base
|
from oslo_middleware import base
|
||||||
|
from oslo_utils import strutils
|
||||||
import webob
|
import webob
|
||||||
import webob.exc as ex
|
import webob.exc as ex
|
||||||
|
|
||||||
from sahara.i18n import _
|
from sahara.i18n import _
|
||||||
from sahara.i18n import _LW
|
from sahara.i18n import _LW
|
||||||
import sahara.openstack.commons as commons
|
|
||||||
|
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
@ -48,8 +48,10 @@ class AuthValidator(base.Middleware):
|
|||||||
|
|
||||||
path = req.environ['PATH_INFO']
|
path = req.environ['PATH_INFO']
|
||||||
if path != '/':
|
if path != '/':
|
||||||
version, url_tenant, rest = commons.split_path(path, 3, 3, True)
|
try:
|
||||||
if not version or not url_tenant or not rest:
|
version, url_tenant, rest = strutils.split_path(path, 3, 3,
|
||||||
|
True)
|
||||||
|
except ValueError:
|
||||||
LOG.warning(_LW("Incorrect path: {path}").format(path=path))
|
LOG.warning(_LW("Incorrect path: {path}").format(path=path))
|
||||||
raise ex.HTTPNotFound(_("Incorrect path"))
|
raise ex.HTTPNotFound(_("Incorrect path"))
|
||||||
|
|
||||||
@ -84,14 +86,15 @@ class AuthValidatorV2(base.Middleware):
|
|||||||
LOG.warning(_LW("Can't get tenant_id from env"))
|
LOG.warning(_LW("Can't get tenant_id from env"))
|
||||||
raise ex.HTTPServiceUnavailable()
|
raise ex.HTTPServiceUnavailable()
|
||||||
|
|
||||||
if path.startswith('/v2'):
|
try:
|
||||||
version, rest = commons.split_path(path, 2, 2, True)
|
if path.startswith('/v2'):
|
||||||
requested_tenant = req.headers.get('OpenStack-Project-ID')
|
version, rest = strutils.split_path(path, 2, 2, True)
|
||||||
else:
|
requested_tenant = req.headers.get('OpenStack-Project-ID')
|
||||||
version, requested_tenant, rest = commons.split_path(
|
else:
|
||||||
path, 3, 3, True)
|
|
||||||
|
|
||||||
if not version or not requested_tenant or not rest:
|
version, requested_tenant, rest = strutils.split_path(
|
||||||
|
path, 3, 3, True)
|
||||||
|
except ValueError:
|
||||||
LOG.warning(_LW("Incorrect path: {path}").format(path=path))
|
LOG.warning(_LW("Incorrect path: {path}").format(path=path))
|
||||||
raise ex.HTTPNotFound(_("Incorrect path"))
|
raise ex.HTTPNotFound(_("Incorrect path"))
|
||||||
|
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
# pylint: disable-all
|
|
||||||
|
|
||||||
# Copyright (c) 2010-2012 OpenStack, LLC.
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
# stolen from the OpenStack Swift
|
|
||||||
|
|
||||||
|
|
||||||
"""Miscellaneous utility functions for use with Swift."""
|
|
||||||
|
|
||||||
|
|
||||||
def split_path(path, minsegs=1, maxsegs=None, rest_with_last=False):
|
|
||||||
"""Validate and split the given HTTP request path.
|
|
||||||
|
|
||||||
**Examples**::
|
|
||||||
|
|
||||||
['a'] = split_path('/a')
|
|
||||||
['a', None] = split_path('/a', 1, 2)
|
|
||||||
['a', 'c'] = split_path('/a/c', 1, 2)
|
|
||||||
['a', 'c', 'o/r'] = split_path('/a/c/o/r', 1, 3, True)
|
|
||||||
|
|
||||||
:param path: HTTP Request path to be split
|
|
||||||
:param minsegs: Minimum number of segments to be extracted
|
|
||||||
:param maxsegs: Maximum number of segments to be extracted
|
|
||||||
:param rest_with_last: If True, trailing data will be returned as part
|
|
||||||
of last segment. If False, and there is
|
|
||||||
trailing data, raises ValueError.
|
|
||||||
:returns: list of segments with a length of maxsegs (non-existent
|
|
||||||
segments will return as None)
|
|
||||||
:raises: ValueError if given an invalid path
|
|
||||||
"""
|
|
||||||
if not maxsegs:
|
|
||||||
maxsegs = minsegs
|
|
||||||
if minsegs > maxsegs:
|
|
||||||
raise ValueError('minsegs > maxsegs: %d > %d' % (minsegs, maxsegs))
|
|
||||||
if rest_with_last:
|
|
||||||
segs = path.split('/', maxsegs)
|
|
||||||
minsegs += 1
|
|
||||||
maxsegs += 1
|
|
||||||
count = len(segs)
|
|
||||||
if (segs[0] or count < minsegs or count > maxsegs or
|
|
||||||
'' in segs[1:minsegs]):
|
|
||||||
return None, None, None
|
|
||||||
else:
|
|
||||||
minsegs += 1
|
|
||||||
maxsegs += 1
|
|
||||||
segs = path.split('/', maxsegs)
|
|
||||||
count = len(segs)
|
|
||||||
if (segs[0] or count < minsegs or count > maxsegs + 1 or
|
|
||||||
'' in segs[1:minsegs] or
|
|
||||||
(count == maxsegs + 1 and segs[maxsegs])):
|
|
||||||
raise ValueError('Invalid path: %s' % path)
|
|
||||||
segs = segs[1:maxsegs]
|
|
||||||
segs.extend([None] * (maxsegs - 1 - len(segs)))
|
|
||||||
return segs
|
|
Loading…
Reference in New Issue
Block a user