Make api-paste.ini config optional

* osprofiler.web.WsgiMiddleware now treats hmac_keys as optional arg
* add hmac_keys argument to enable() method to use it later in
  OpenStack services setup

Change-Id: Ib544d2732c7307bc5405b4336eda80120d2f43af
This commit is contained in:
Dina Belova 2015-11-11 15:51:01 +03:00
parent 9c18d7ef6b
commit cabf79d44c
3 changed files with 39 additions and 10 deletions

View File

@ -60,20 +60,20 @@ Assignee(s)
-----------
Primary assignee:
<launchpad-id or None>
dbelova
Work Items
----------
- Modify osprofiler.web.WsgiMiddleware to make ``hmac_keys`` optional
- Modify osprofiler.web.WsgiMiddleware to make ``hmac_keys`` optional (done)
- Add alternative way to setup osprofiler.web.WsgiMiddleware, e.g. extra
argument to enable() hmac_keys
argument hmac_keys to enable() method (done)
- Cut new release 0.3.1
- Cut new release 0.3.1 (tbd)
- Fix the code in all projects: remove api-paste.ini arguments and add
extra argument to osprofiler.web.enable
- Fix the code in all projects: remove api-paste.ini arguments and use
osprofiler.web.enable with extra argument (tbd)
Dependencies

View File

@ -40,6 +40,7 @@ def get_trace_id_headers():
_DISABLED = False
_HMAC_KEYS = None
def disable():
@ -52,16 +53,17 @@ def disable():
_DISABLED = True
def enable():
def enable(hmac_keys=None):
"""Enable middleware."""
global _DISABLED
global _DISABLED, _HMAC_KEYS
_DISABLED = False
_HMAC_KEYS = utils.split(hmac_keys or "")
class WsgiMiddleware(object):
"""WSGI Middleware that enables tracing for an application."""
def __init__(self, application, hmac_keys, enabled=False):
def __init__(self, application, hmac_keys=None, enabled=False):
"""Initialize middleware with api-paste.ini arguments.
:application: wsgi app
@ -100,7 +102,7 @@ class WsgiMiddleware(object):
trace_info = utils.signed_unpack(request.headers.get("X-Trace-Info"),
request.headers.get("X-Trace-HMAC"),
self.hmac_keys)
_HMAC_KEYS or self.hmac_keys)
if not self._trace_is_valid(trace_info):
return request.get_response(self.application)

View File

@ -270,6 +270,33 @@ class WebMiddlewareTestCase(test.TestCase):
self.assertEqual("yeah!", middleware(request))
self.assertEqual(mock_profiler_init.call_count, 0)
@mock.patch("osprofiler.web.profiler.init")
def test_wsgi_middleware_enable_via_python(self, mock_profiler_init):
request = mock.MagicMock()
request.get_response.return_value = "yeah!"
request.url = "someurl"
request.host_url = "someurl"
request.path = "path"
request.query_string = "query"
request.method = "method"
request.scheme = "scheme"
hmac_key = 'super_secret_key2'
pack = utils.signed_pack({"base_id": "1", "parent_id": "2"}, hmac_key)
request.headers = {
"a": "1",
"b": "2",
"X-Trace-Info": pack[0],
"X-Trace-HMAC": pack[1]
}
web.enable('super_secret_key1,super_secret_key2')
middleware = web.WsgiMiddleware("app", enabled=True)
self.assertEqual("yeah!", middleware(request))
mock_profiler_init.assert_called_once_with(hmac_key=hmac_key,
base_id="1",
parent_id="2")
def test_disable(self):
web.disable()
self.assertTrue(web._DISABLED)