예제 #1
0
    def test_signed_pack_unpack_many_keys(self):
        keys = ["secret", "secret2", "secret3"]
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, keys[-1])

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertEqual(keys[-1], process_data["hmac_key"])
예제 #2
0
    def test_signed_pack_unpack_many_keys(self):
        keys = ['secret', 'secret2', 'secret3']
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, keys[-1])

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertEqual(keys[-1], process_data['hmac_key'])
예제 #3
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        # do not profile ajax requests for now
        if not self.is_enabled(request) or request.is_ajax():
            return None

        trace_info = profiler_utils.signed_unpack(
            request.META.get('X-Trace-Info'),
            request.META.get('X-Trace-HMAC'),
            self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return None

        profiler.init(**trace_info)
        info = {
            'request': {
                'path': request.path,
                'query': request.GET.urlencode(),
                'method': request.method,
                'scheme': request.scheme
            }
        }
        with api.traced(request, view_func.__name__, info) as trace_id:
            response = view_func(request, *view_args, **view_kwargs)
            url = reverse('horizon:developer:profiler:index')
            message = safestring.mark_safe(
                _('Traced with id %(id)s. Go to <a href="%(url)s">page</a>') %
                {'id': trace_id, 'url': url})
            messages.info(request, message)
            return response
예제 #4
0
    def process_view(self, request, view_func, view_args, view_kwargs):

        if not self.is_enabled(request):
            return None

        trace_info = profiler_utils.signed_unpack(
            request.META.get('X-Trace-Info'), request.META.get('X-Trace-HMAC'),
            self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return None

        profiler.init(**trace_info)
        info = {
            'request': {
                'path': request.path,
                'query': request.GET.urlencode(),
                'method': request.method,
                'scheme': request.scheme
            }
        }
        with api.traced(request, view_func.__name__, info) as trace_id:
            request.META[api.ROOT_HEADER] = profiler.get().get_id()

            response = view_func(request, *view_args, **view_kwargs)
            url = reverse('horizon:developer:profiler:index')
            message = safestring.mark_safe(
                _('Traced with id %(id)s. Go to <a href="%(url)s">page</a>') %
                {
                    'id': trace_id,
                    'url': url
                })
            messages.info(request, message)
            return response
예제 #5
0
    def test_signed_pack_unpack_many_wrong_keys(self):
        keys = ['secret', 'secret2', 'secret3']
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, 'password')

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertIsNone(process_data)
예제 #6
0
    def test_signed_pack_unpack_many_wrong_keys(self):
        keys = ["secret", "secret2", "secret3"]
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, "password")

        process_data = utils.signed_unpack(packed_data, hmac_data, keys)
        self.assertIsNone(process_data)
예제 #7
0
파일: web.py 프로젝트: openstack/osprofiler
    def __call__(self, request):
        if (_ENABLED is not None and not _ENABLED or
                _ENABLED is None and not self.enabled):
            return request.get_response(self.application)

        trace_info = utils.signed_unpack(request.headers.get(X_TRACE_INFO),
                                         request.headers.get(X_TRACE_HMAC),
                                         _HMAC_KEYS or self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return request.get_response(self.application)

        profiler.init(**trace_info)
        info = {
            "request": {
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        try:
            with profiler.Trace(self.name, info=info):
                return request.get_response(self.application)
        finally:
            profiler.clean()
예제 #8
0
    def __call__(self, request):
        if (_ENABLED is not None and not _ENABLED
                or _ENABLED is None and not self.enabled):
            return request.get_response(self.application)

        trace_info = utils.signed_unpack(request.headers.get(X_TRACE_INFO),
                                         request.headers.get(X_TRACE_HMAC),
                                         _HMAC_KEYS or self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return request.get_response(self.application)

        profiler.init(**trace_info)
        info = {
            "request": {
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        try:
            with profiler.Trace(self.name, info=info):
                return request.get_response(self.application)
        finally:
            profiler._clean()
예제 #9
0
파일: web.py 프로젝트: docc-lab/osprofiler
    def __call__(self, request):
        if (_ENABLED is not None and not _ENABLED
                or _ENABLED is None and not self.enabled):
            return request.get_response(self.application)

        trace_info = utils.signed_unpack(request.headers.get(X_TRACE_INFO),
                                         request.headers.get(X_TRACE_HMAC),
                                         _HMAC_KEYS or self.hmac_keys)

        if not self.trace_is_valid(trace_info):
            return request.get_response(self.application)

        osprofiler.profiler.init(**trace_info)
        info = {
            "request": {
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        tracepoint_id = '%s%s:%s' % (getpass.getuser(), request.path,
                                     request.method)
        info['tracepoint_id'] = self.uuid.sub('/UUID', tracepoint_id)
        # # This gets the entire stack as the tracepoint_id
        # curframe = inspect.currentframe()
        # for parframe in inspect.getouterframes(curframe):
        #     info['tracepoint_id'] += '%s:%d:%s,' % parframe[1:4]
        try:
            with osprofiler.profiler.Trace(self.name, info=info):
                return request.get_response(self.application)
        finally:
            osprofiler.profiler.clean()
예제 #10
0
    def test_get_trace_id_headers(self):
        profiler.init("key", base_id="y", parent_id="z")
        headers = web.get_trace_id_headers()
        self.assertEqual(sorted(headers.keys()), sorted(["X-Trace-Info", "X-Trace-HMAC"]))

        trace_info = utils.signed_unpack(headers["X-Trace-Info"], headers["X-Trace-HMAC"], ["key"])
        self.assertIn("hmac_key", trace_info)
        self.assertEqual("key", trace_info.pop("hmac_key"))
        self.assertEqual({"parent_id": "z", "base_id": "y"}, trace_info)
예제 #11
0
def update_trace_headers(keys, **kwargs):
    trace_headers = web.get_trace_id_headers()
    trace_info = utils.signed_unpack(
        trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC],
        keys)
    trace_info.update(kwargs)
    p = profiler.get()
    trace_data = utils.signed_pack(trace_info, p.hmac_key)
    return json.dumps({web.X_TRACE_INFO: trace_data[0],
                       web.X_TRACE_HMAC: trace_data[1]})
예제 #12
0
    def test_signed_pack_unpack(self):
        hmac = "secret"
        data = {"some": "data"}

        packed_data, hmac_data = utils.signed_pack(data, hmac)

        process_data = utils.signed_unpack(packed_data, hmac_data, [hmac])
        self.assertIn("hmac_key", process_data)
        process_data.pop("hmac_key")
        self.assertEqual(data, process_data)
예제 #13
0
    def test_get_trace_id_headers(self):
        profiler.init("key", base_id="y", parent_id="z")
        headers = web.get_trace_id_headers()
        self.assertEqual(sorted(headers.keys()),
                         sorted(["X-Trace-Info", "X-Trace-HMAC"]))

        trace_info = utils.signed_unpack(headers["X-Trace-Info"],
                                         headers["X-Trace-HMAC"], ["key"])
        self.assertIn("hmac_key", trace_info)
        self.assertEqual("key", trace_info.pop("hmac_key"))
        self.assertEqual({"parent_id": "z", "base_id": "y"}, trace_info)
예제 #14
0
def update_trace_headers(keys, **kwargs):
    trace_headers = web.get_trace_id_headers()
    trace_info = utils.signed_unpack(
        trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC],
        keys)
    trace_info.update(kwargs)
    p = profiler.get()
    trace_data = utils.signed_pack(trace_info, p.hmac_key)
    trace_data = [key.decode() if isinstance(key, bytes)
                  else key for key in trace_data]
    return json.dumps({web.X_TRACE_INFO: trace_data[0],
                       web.X_TRACE_HMAC: trace_data[1]})
예제 #15
0
파일: api.py 프로젝트: CCI-MOC/horizon
def update_trace_headers(keys, **kwargs):
    trace_headers = web.get_trace_id_headers()
    trace_info = utils.signed_unpack(
        trace_headers[web.X_TRACE_INFO], trace_headers[web.X_TRACE_HMAC],
        keys)
    trace_info.update(kwargs)
    p = profiler.get()
    trace_data = utils.signed_pack(trace_info, p.hmac_key)
    if six.PY3:
        trace_data = [key.decode() if isinstance(key, six.binary_type)
                      else key for key in trace_data]
    return json.dumps({web.X_TRACE_INFO: trace_data[0],
                       web.X_TRACE_HMAC: trace_data[1]})
예제 #16
0
파일: web.py 프로젝트: docc-lab/osprofiler
def init_from_headers(headers, hmac_keys=None):
    """Init osprofiler using the headers"""
    if isinstance(hmac_keys, str):
        hmac_keys = [hmac_keys]

    trace_info = utils.signed_unpack(headers.get('HTTP_X_TRACE_INFO'),
                                     headers.get('HTTP_X_TRACE_HMAC'),
                                     _HMAC_KEYS or hmac_keys)

    if not WsgiMiddleware.trace_is_valid(trace_info):
        return

    osprofiler.profiler.init(**trace_info)
    return
예제 #17
0
파일: profile.py 프로젝트: openstack/zaqar
    def __call__(self, environ, start_response):
        request = webob.Request(environ)
        trace_info = utils.signed_unpack(request.headers.get(web.X_TRACE_INFO),
                                         request.headers.get(web.X_TRACE_HMAC),
                                         self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return self.application(environ, start_response)

        profiler.init(**trace_info)
        info = {
            "request": {
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        with profiler.Trace(self.name, info=info):
            return self.application(environ, start_response)
예제 #18
0
파일: profile.py 프로젝트: weizai118/zaqar
    def __call__(self, environ, start_response):
        request = webob.Request(environ)
        trace_info = utils.signed_unpack(request.headers.get(web.X_TRACE_INFO),
                                         request.headers.get(web.X_TRACE_HMAC),
                                         self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return self.application(environ, start_response)

        profiler.init(**trace_info)
        info = {
            "request": {
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        with profiler.Trace(self.name, info=info):
            return self.application(environ, start_response)
예제 #19
0
    def __call__(self, request):
        if _DISABLED or not self.enabled:
            return request.get_response(self.application)

        trace_info = utils.signed_unpack(request.headers.get("X-Trace-Info"),
                                         request.headers.get("X-Trace-HMAC"),
                                         self.hmac_keys)

        if not self._trace_is_valid(trace_info):
            return request.get_response(self.application)

        profiler.init(**trace_info)
        info = {
            "request": {
                "host_url": request.host_url,
                "path": request.path,
                "query": request.query_string,
                "method": request.method,
                "scheme": request.scheme
            }
        }
        with profiler.Trace(self.name, info=info):
            return request.get_response(self.application)
예제 #20
0
 def test_signed_unpack_no_key_or_hmac_data(self):
     data = {"some": "data"}
     packed_data, hmac_data = utils.signed_pack(data, "secret")
     self.assertIsNone(utils.signed_unpack(packed_data, hmac_data, None))
     self.assertIsNone(utils.signed_unpack(packed_data, None, "secret"))
     self.assertIsNone(utils.signed_unpack(packed_data, " ", "secret"))
예제 #21
0
    def test_signed_unpack_wrong_key(self):
        data = {"some": "data"}
        packed_data, hmac_data = utils.signed_pack(data, "secret")

        self.assertIsNone(utils.signed_unpack(packed_data, hmac_data, "wrong"))
예제 #22
0
 def test_singed_unpack_generate_hmac_failed(self, mock_generate_hmac):
     mock_generate_hmac.side_effect = Exception
     self.assertIsNone(utils.signed_unpack("data", "hmac_data", "hmac_key"))
예제 #23
0
    def test_signed_unpack_invalid_json(self):
        hmac = "secret"
        data = base64.urlsafe_b64encode(utils.binary_encode("not_a_json"))
        hmac_data = utils.generate_hmac(data, hmac)

        self.assertIsNone(utils.signed_unpack(data, hmac_data, hmac))