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_hiding_sensitive_fields_by_default():
log_record = RequestWebRecord({REQUEST_KEY: None, RESPONSE_KEY: None},
FRAMEWORK, 'name', logging.DEBUG, 'pathname', 1, 'msg', [], None)
_assert_sensitive_fields_redacted(log_record)
assert log_record.remote_user == defaults.REDACTED
assert log_record.referer == defaults.REDACTED
def test_init_cf_logger_web_log(mocker):
""" tests CfLogger creates SimpleLogRecord if extra contains request and response """
mocker.patch.object(RequestWebRecord, '__init__', lambda *a, **kwa: None)
record = _make_record(extra={REQUEST_KEY: {}, RESPONSE_KEY: {}})
assert isinstance(record, RequestWebRecord)
def makeRecord(self, name, level, fn, lno, msg, msgargs, exc_info,
func=None, extra=None, *args, **kwargs):
""" Returns SimpleLogMessage or a RequestWebRecord depending on the extra variable """
# check what record type this is
cls = None
if extra is not None and REQUEST_KEY in extra and RESPONSE_KEY in extra:
cls = RequestWebRecord
else:
cls = SimpleLogRecord
return cls(extra, FRAMEWORK, name, level, fn, lno, msg, msgargs, exc_info,
func, *args, **kwargs)
def _wrapper(response):
cf_logging.FRAMEWORK.context.set(
'response_sent_at', datetime.utcnow(), request)
extra = {REQUEST_KEY: request, RESPONSE_KEY: response}
logging.getLogger('cf.flask.logger').info('', extra=extra)
return wrapped(response)
return _wrapper
def _wrapper(request, response):
cf_logging.FRAMEWORK.context.set('response_sent_at', datetime.utcnow(), request)
extra = {REQUEST_KEY: request, RESPONSE_KEY: response}
logging.getLogger('cf.sanic.logger').info('', extra=extra)
return wrapped(request, response)
def process_response(self, request, response, resource, req_succeeded): # pylint: disable=unused-argument
"""Post-processing of the response (after routing).
:param request: - Falcon Request object
:param response: - Falcon Response object
:param resource: - Falcon Resource object to which the request was routed
:param req_succeeded: - True if no exceptions were raised while
the framework processed and routed the request
"""
cf_logging.FRAMEWORK.context.set(
'response_sent_at', datetime.utcnow(), request)
extra = {REQUEST_KEY: request, RESPONSE_KEY: response}
logging.getLogger(self._logger_name).info('', extra=extra)
def __init__(self, extra, framework, *args, **kwargs):
super(RequestWebRecord, self).__init__(extra, framework, *args, **kwargs)
context = framework.context
request_reader = framework.request_reader
response_reader = framework.response_reader
request = extra[REQUEST_KEY]
response = extra[RESPONSE_KEY]
props = dict((key, value) for key, value in extra.items()
if key not in [RESPONSE_KEY, REQUEST_KEY])
for key, value in props.items():
setattr(self, key, value)
length = request_reader.get_content_length(request)
remote_ip = request_reader.get_remote_ip(request)
self.type = 'request'
self.direction = 'IN'
self.remote_user = request_reader.get_remote_user(request)
self.request = request_reader.get_path(request)
self.referer = request_reader.get_http_header(
request, 'referer', defaults.UNKNOWN)
self.x_forwarded_for = request_reader.get_http_header(
request, 'x-forwarded-for', defaults.UNKNOWN)
self.protocol = request_reader.get_protocol(request)
self.method = request_reader.get_method(request)
import logging
import traceback
from datetime import datetime
from sap.cf_logging import defaults
from sap.cf_logging.core.constants import REQUEST_KEY, RESPONSE_KEY
from sap.cf_logging.record import application_info
from sap.cf_logging.record import util
from sap.cf_logging.formatters.stacktrace_formatter import format_stacktrace
_SKIP_ATTRIBUTES = ["type", "written_at", "written_ts", "correlation_id", "remote_user", "referer",
"x_forwarded_for", "protocol", "method", "remote_ip", "request_size_b",
"remote_host", "remote_port", "request_received_at", "direction",
"response_time_ms", "response_status", "response_size_b",
"response_content_type", "response_sent_at", REQUEST_KEY, RESPONSE_KEY]
class SimpleLogRecord(logging.LogRecord):
""" SimpleLogRecord class holds data for user logged messages """
# pylint: disable=too-many-arguments,too-many-locals
def __init__(self, extra, framework, *args, **kwargs):
super(SimpleLogRecord, self).__init__(*args, **kwargs)
utcnow = datetime.utcnow()
self.written_at = util.iso_time_format(utcnow)
self.written_ts = util.epoch_nano_second(utcnow)
request = extra[REQUEST_KEY] if extra and REQUEST_KEY in extra else None