summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDina Belova <dbelova@mirantis.com>2015-11-11 15:51:01 +0300
committerDina Belova <dbelova@mirantis.com>2015-11-12 09:17:45 +0000
commitcabf79d44c77a9259481cabfa888e99730fb40b0 (patch)
tree5a4c5f05e5e7b50a349241f378a88fd8e8046051
parent9c18d7ef6b886a9e320937117c0b1e57b154eeeb (diff)
downloadosprofiler-0.3.1.tar.gz
Make api-paste.ini config optional0.3.1
* 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
-rw-r--r--doc/specs/in-progress/make_paste_ini_config_optional.rst12
-rw-r--r--osprofiler/web.py10
-rw-r--r--tests/test_web.py27
3 files changed, 39 insertions, 10 deletions
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:
- <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
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)