Check OCCI version in user agent

We only support OCCI/1.1, so if we have another version we have to raise
a 501 not implemented error.
This commit is contained in:
Alvaro Lopez Garcia 2015-04-16 17:49:50 +02:00
parent 86329b79f3
commit 2675a8c3d8
2 changed files with 34 additions and 2 deletions

View File

@ -97,6 +97,27 @@ class TestMiddleware(base.TestCase):
self.assertEqual(404, result.status_code)
self.assertDefaults(result)
def test_good_user_agent(self):
req = self._build_req("/", "tenant")
req.user_agent = "foo OCCI/1.1 bar"
result = req.get_response(self.get_app())
self.assertEqual(404, result.status_code)
self.assertDefaults(result)
def test_bad_user_agent(self):
req = self._build_req("/", "tenant")
req.user_agent = "foo OCCI/2.2 bar"
result = req.get_response(self.get_app())
self.assertEqual(501, result.status_code)
self.assertDefaults(result)
def test_ugly_user_agent(self):
req = self._build_req("/", "tenant")
req.user_agent = "fooOCCI/1.1bar"
result = req.get_response(self.get_app())
self.assertEqual(404, result.status_code)
self.assertDefaults(result)
def test_400_from_openstack(self):
@webob.dec.wsgify()
def _fake_app(req):

View File

@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
from oslo_log import log as logging
import routes
import routes.middleware
@ -72,6 +74,7 @@ class Request(webob.Request):
class OCCIMiddleware(object):
occi_version = "1.1"
occi_string = "OCCI/%s" % occi_version
@classmethod
def factory(cls, global_conf, **local_conf):
@ -157,6 +160,14 @@ class OCCIMiddleware(object):
return self.process_response(response)
def process_request(self, req):
if req.user_agent:
# FIXME(aloga): review the regexp, since it will only match the
# first string
match = re.search(r"\bOCCI/\d\.\d\b", req.user_agent)
if match and self.occi_string != match.group():
return Fault(webob.exc.HTTPNotImplemented(
explanation="%s not supported" % match.group()))
match = self.mapper.match(req.path_info, req.environ)
if not match:
return Fault(webob.exc.HTTPNotFound())
@ -165,8 +176,8 @@ class OCCIMiddleware(object):
def process_response(self, response):
"""Process a response by adding our headers."""
server_string = "ooi/%s OCCI/%s" % (ooi.__version__,
self.occi_version)
server_string = "ooi/%s %s" % (ooi.__version__,
self.occi_string)
headers = (("server", server_string),)
if isinstance(response, Fault):