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_logging_sensitive_connection_data():
os.environ['LOG_SENSITIVE_CONNECTION_DATA'] = 'true'
log_record = RequestWebRecord({REQUEST_KEY: None, RESPONSE_KEY: None},
FRAMEWORK, 'name', logging.DEBUG, 'pathname', 1, 'msg', [], None)
assert log_record.remote_ip == '1.2.3.4'
assert log_record.remote_host == '1.2.3.4'
assert log_record.remote_port == '1234'
assert log_record.x_forwarded_for == 'some.host'
assert log_record.remote_user == defaults.REDACTED
assert log_record.referer == defaults.REDACTED
def test_logging_remote_user():
os.environ['LOG_REMOTE_USER'] = 'true'
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 == 'user'
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 test_logging_referer():
os.environ['LOG_REFERER'] = 'true'
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 == 'referer'
def test_incorrect_env_var_value():
os.environ['LOG_SENSITIVE_CONNECTION_DATA'] = 'false'
os.environ['LOG_REMOTE_USER'] = 'some-string'
os.environ['LOG_REFERER'] = ''
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 format(self):
record = super(RequestWebRecord, self).format_cf_attributes()
request_properties = dict(
(key, value) for key, value in self.__dict__.items() if key in PROPS)
record.update(request_properties)
return record