From dbca8153e3a724fc428655c99310c24b58e8e093 Mon Sep 17 00:00:00 2001 From: Kevin_Zheng Date: Thu, 22 Mar 2018 19:32:13 +0800 Subject: [PATCH] Noauth should also use request_id from compute_req_id.py When using noauth midlleware, the context is generated here: https://github.com/openstack/nova/blob/ef6f4e4c8ec82e2c9f9988fe2e04591ee01220e6/nova/api/openstack/auth.py#L56 as the openstack.request_id from req.environ, previously created in: https://github.com/openstack/oslo.middleware/blob/master/oslo_middleware/request_id.py#L57 was not included in the initialize of the context obj, oslo_context.context.Context.__init__ will generate a new request_id for context obj. As the req.environ['openstack.request_id'] will later be returned to the user as the 'x-openstack-request-id' field in the response header, users may use this for searching etc. But the Nova workflow will all use the context.request_id, which will lead to an inconsistency problem. Change-Id: I047b5f66b33ac89b2b5e9170c12bc87d76d0946b Closes-Bug: #1758031 --- nova/api/openstack/auth.py | 9 ++-- nova/tests/unit/api/openstack/test_auth.py | 50 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 nova/tests/unit/api/openstack/test_auth.py diff --git a/nova/api/openstack/auth.py b/nova/api/openstack/auth.py index d5966b8ce6a4..36c0078ebec7 100644 --- a/nova/api/openstack/auth.py +++ b/nova/api/openstack/auth.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. +from oslo_middleware import request_id import webob.dec import webob.exc @@ -53,10 +54,10 @@ class NoAuthMiddlewareBase(base_wsgi.Middleware): if CONF.api.use_forwarded_for: remote_address = req.headers.get('X-Forwarded-For', remote_address) is_admin = always_admin or (user_id == 'admin') - ctx = context.RequestContext(user_id, - project_id, - is_admin=is_admin, - remote_address=remote_address) + ctx = context.RequestContext( + user_id, project_id, is_admin=is_admin, + remote_address=remote_address, + request_id=req.environ.get(request_id.ENV_REQUEST_ID)) req.environ['nova.context'] = ctx return self.application diff --git a/nova/tests/unit/api/openstack/test_auth.py b/nova/tests/unit/api/openstack/test_auth.py new file mode 100644 index 000000000000..06a38c5c452a --- /dev/null +++ b/nova/tests/unit/api/openstack/test_auth.py @@ -0,0 +1,50 @@ +# Copyright (c) 2018 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_middleware import request_id +from oslo_serialization import jsonutils +import webob +import webob.exc + +import nova.api.openstack.auth +import nova.conf +from nova import test + +CONF = nova.conf.CONF + + +class NoAuthMiddleware(test.NoDBTestCase): + + def setUp(self): + super(NoAuthMiddleware, self).setUp() + + @webob.dec.wsgify() + def fake_app(req): + self.context = req.environ['nova.context'] + return webob.Response() + + self.context = None + self.middleware = nova.api.openstack.auth.NoAuthMiddleware(fake_app) + self.request = webob.Request.blank('/') + self.request.headers['X_TENANT_ID'] = 'testtenantid' + self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken' + self.request.headers['X_SERVICE_CATALOG'] = jsonutils.dumps({}) + + def test_request_id_extracted_from_env(self): + req_id = 'dummy-request-id' + self.request.headers['X_PROJECT_ID'] = 'testtenantid' + self.request.headers['X_USER_ID'] = 'testuserid' + self.request.environ[request_id.ENV_REQUEST_ID] = req_id + self.request.get_response(self.middleware) + self.assertEqual(req_id, self.context.request_id)