Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_secrets(self):
self.assertTrue(hasattr(agent, 'secrets_matcher'))
self.assertEqual(agent.secrets_matcher, 'contains-ignore-case')
self.assertTrue(hasattr(agent, 'secrets_list'))
self.assertEqual(agent.secrets_list, ['key', 'pass', 'secret'])
async def stan_middleware(request, handler):
try:
ctx = async_tracer.extract(opentracing.Format.HTTP_HEADERS, request.headers)
request['scope'] = async_tracer.start_active_span('aiohttp-server', child_of=ctx)
scope = request['scope']
# Query param scrubbing
url = str(request.url)
parts = url.split('?')
if len(parts) > 1:
cleaned_qp = strip_secrets(parts[1], agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
scope.span.set_tag("http.url", parts[0])
scope.span.set_tag("http.method", request.method)
# Custom header tracking support
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in request.headers:
scope.span.set_tag("http.%s" % custom_header, request.headers[custom_header])
response = await handler(request)
if response is not None:
# Mark 500 responses as errored
if 500 <= response.status <= 511:
if args is not None and len(args) == 2:
kvs['method'] = args[0]
kvs['path'] = args[1]
else:
kvs['method'] = kwargs.get('method')
kvs['path'] = kwargs.get('path')
if kvs['path'] is None:
kvs['path'] = kwargs.get('url')
# Strip any secrets from potential query params
if kvs.get('path') is not None and ('?' in kvs['path']):
parts = kvs['path'].split('?')
kvs['path'] = parts[0]
if len(parts) == 2:
kvs['query'] = strip_secrets(parts[1], agent.secrets_matcher, agent.secrets_list)
if type(instance) is urllib3.connectionpool.HTTPSConnectionPool:
kvs['url'] = 'https://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
else:
kvs['url'] = 'http://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
except Exception:
logger.debug("urllib3 collect error", exc_info=True)
return kvs
else:
return kvs
try:
parent_span = async_tracer.active_span
# If we're not tracing, just return
if parent_span is None:
trace_config_ctx.scope = None
return
scope = async_tracer.start_active_span("aiohttp-client", child_of=parent_span)
trace_config_ctx.scope = scope
async_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, params.headers)
parts = str(params.url).split('?')
if len(parts) > 1:
cleaned_qp = strip_secrets(parts[1], agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
scope.span.set_tag("http.url", parts[0])
scope.span.set_tag('http.method', params.method)
except Exception:
logger.debug("stan_request_start", exc_info=True)
request = tornado.httpclient.HTTPRequest(url=request, **kwargs)
new_kwargs = {}
for param in ('callback', 'raise_error'):
# if not in instead and pop
if param in kwargs:
new_kwargs[param] = kwargs.pop(param)
kwargs = new_kwargs
scope = tornado_tracer.start_active_span('tornado-client', child_of=parent_span)
tornado_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, request.headers)
# Query param scrubbing
parts = request.url.split('?')
if len(parts) > 1:
cleaned_qp = strip_secrets(parts[1], agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
scope.span.set_tag("http.url", parts[0])
scope.span.set_tag("http.method", request.method)
future = wrapped(request, **kwargs)
if future is not None:
cb = functools.partial(finish_tracing, scope=scope)
future.add_done_callback(cb)
return future
except Exception:
logger.debug("tornado fetch", exc_info=True)
raise
ctx = tracer.extract(ot.Format.HTTP_HEADERS, env)
request.iscope = tracer.start_active_span('django', child_of=ctx)
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
django_header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if django_header in env:
request.iscope.span.set_tag("http.%s" % custom_header, env[django_header])
request.iscope.span.set_tag(ext.HTTP_METHOD, request.method)
if 'PATH_INFO' in env:
request.iscope.span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
request.iscope.span.set_tag("http.params", scrubbed_params)
if 'HTTP_HOST' in env:
request.iscope.span.set_tag("http.host", env['HTTP_HOST'])
except Exception:
logger.debug("Django middleware @ process_request", exc_info=True)
def execute_with_instana(wrapped, instance, argv, kwargs):
try:
with tracer_stack_context():
ctx = tornado_tracer.extract(opentracing.Format.HTTP_HEADERS, instance.request.headers)
scope = tornado_tracer.start_active_span('tornado-server', child_of=ctx)
# Query param scrubbing
if instance.request.query is not None and len(instance.request.query) > 0:
cleaned_qp = strip_secrets(instance.request.query, agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
url = "%s://%s%s" % (instance.request.protocol, instance.request.host, instance.request.path)
scope.span.set_tag("http.url", url)
scope.span.set_tag("http.method", instance.request.method)
scope.span.set_tag("handler", instance.__class__.__name__)
# Custom header tracking support
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in instance.request.headers:
scope.span.set_tag("http.%s" % custom_header, instance.request.headers[custom_header])
setattr(instance.request, "_instana", scope)
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
span = flask.g.scope.span
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if header in env:
span.set_tag("http.%s" % custom_header, env[header])
span.set_tag(ext.HTTP_METHOD, flask.request.method)
if 'PATH_INFO' in env:
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
span.set_tag("http.params", scrubbed_params)
if 'HTTP_HOST' in env:
span.set_tag("http.host", env['HTTP_HOST'])
if hasattr(flask.request.url_rule, 'rule') and \
path_tpl_re.search(flask.request.url_rule.rule) is not None:
path_tpl = flask.request.url_rule.rule.replace("<", "{")
path_tpl = path_tpl.replace(">", "}")
span.set_tag("http.path_tpl", path_tpl)
except:
logger.debug("Flask before_request", exc_info=True)
finally:
return None
return start_response(status, headers, exc_info)
ctx = tracer.extract(ot.Format.HTTP_HEADERS, env)
scope = env['stan_scope'] = tracer.start_active_span("wsgi", child_of=ctx)
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
wsgi_header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if wsgi_header in env:
scope.span.set_tag("http.%s" % custom_header, env[wsgi_header])
if 'PATH_INFO' in env:
scope.span.set_tag('http.path', env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", scrubbed_params)
if 'REQUEST_METHOD' in env:
scope.span.set_tag(tags.HTTP_METHOD, env['REQUEST_METHOD'])
if 'HTTP_HOST' in env:
scope.span.set_tag("http.host", env['HTTP_HOST'])
return wrapped(env, new_start_response)
except ImportError:
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
span = flask.g.scope.span
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if header in env:
span.set_tag("http.%s" % custom_header, env[header])
span.set_tag(ext.HTTP_METHOD, flask.request.method)
if 'PATH_INFO' in env:
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
span.set_tag("http.params", scrubbed_params)
if 'HTTP_HOST' in env:
span.set_tag("http.host", env['HTTP_HOST'])
if hasattr(flask.request.url_rule, 'rule') and \
path_tpl_re.search(flask.request.url_rule.rule) is not None:
path_tpl = flask.request.url_rule.rule.replace("<", "{")
path_tpl = path_tpl.replace(">", "}")
span.set_tag("http.path_tpl", path_tpl)
except:
logger.debug("Flask before_request", exc_info=True)