diff --git a/doc/specs/in-progress/make_paste_ini_config_optional.rst b/doc/specs/in-progress/make_paste_ini_config_optional.rst index 0e06d20..a797924 100644 --- a/doc/specs/in-progress/make_paste_ini_config_optional.rst +++ b/doc/specs/in-progress/make_paste_ini_config_optional.rst @@ -60,20 +60,20 @@ Assignee(s) ----------- Primary assignee: - + 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 diff --git a/osprofiler/web.py b/osprofiler/web.py index 053c132..209cc27 100644 --- a/osprofiler/web.py +++ b/osprofiler/web.py @@ -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) diff --git a/tests/test_web.py b/tests/test_web.py index 578559c..94ab5c4 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -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)