Buckets:

download
raw
30 kB
from __future__ import annotations
import os
import re
from pathlib import Path
from typing import Any
import pytest
from dirty_equals import IsJson, IsPartialDict
from inline_snapshot import snapshot
from opentelemetry._logs import LogRecord, get_logger
from opentelemetry.sdk._logs.export import SimpleLogRecordProcessor
from opentelemetry.sdk.environment_variables import OTEL_RESOURCE_ATTRIBUTES
from opentelemetry.trace.propagation import get_current_span
import logfire
from logfire._internal.scrubbing import DEFAULT_PATTERNS, NoopScrubber, Scrubber
from logfire.testing import TestExporter, TestLogExporter
DEFAULT_PATTERN_EXAMPLES = {
'password': 'has_password_value',
'passwd': 'has_passwd_value',
'mysql_pwd': 'has_mysql_pwd_value',
'secret': 'has_secret_value',
r'auth(?!ors?\b)': 'has_authorization_value',
'credential': 'has_credential_value',
'private[._ -]?key': 'has_private-key_value',
'api[._ -]?key': 'has_api.key_value',
'session': 'has_session_value',
'cookie': 'has_cookie_value',
'social[._ -]?security': 'has_social security_value',
'credit[._ -]?card': 'has_credit_card_value',
'logfire[._ -]?token': 'has_logfire-token_value',
r'pylf_v\d+_': 'has_pylf_v12_token',
r'(?:\b|_)csrf(?:\b|_)': 'has csrf value',
r'(?:\b|_)xsrf(?:\b|_)': 'has xsrf value',
r'(?:\b|_)jwt(?:\b|_)': 'has jwt value',
r'(?:\b|_)ssn(?:\b|_)': 'has ssn value',
}
def test_optimized_default_patterns_match_naive_pattern():
"""The optimized prefilter must not change which default pattern matches first."""
assert DEFAULT_PATTERN_EXAMPLES.keys() == set(DEFAULT_PATTERNS)
naive_pattern = re.compile('|'.join(DEFAULT_PATTERNS), re.IGNORECASE | re.DOTALL)
for value in [*DEFAULT_PATTERN_EXAMPLES.values(), 'has \u017fecret value', 'has credent\u0131al value']:
expected = naive_pattern.search(value)
assert expected is not None
scrub_matches: list[logfire.ScrubMatch] = []
def callback(match: logfire.ScrubMatch):
scrub_matches.append(match)
return match.value
result, scrubbed_notes = Scrubber(None, callback).scrub_value(('attributes', 'value'), value)
assert result == value
assert scrubbed_notes == []
assert len(scrub_matches) == 1
actual = scrub_matches[0].pattern_match
assert (actual.span(), actual.group(0)) == (expected.span(), expected.group(0))
@pytest.mark.parametrize(
('extra_pattern', 'value', 'expected_match'),
[
('custom', 'custom before password', 'custom'),
('pass', 'passwords', 'password'),
(r'\d+', '123 before password', '123'),
],
)
def test_optimized_default_patterns_preserve_extra_pattern_order(extra_pattern: str, value: str, expected_match: str):
matches: list[str] = []
def callback(match: logfire.ScrubMatch):
matches.append(match.pattern_match.group(0))
return match.value
result, scrubbed_notes = Scrubber([extra_pattern], callback).scrub_value(('attributes', 'value'), value)
assert result == value
assert scrubbed_notes == []
assert matches == [expected_match]
def test_default_pattern_start_chars_cover_each_pattern():
"""The prefilter must still find each default pattern's own first match.
Compare against that pattern in isolation, not the combined leftmost match,
so a new pattern cannot be silently disabled if its example also contains
an earlier default match, e.g. adding `token` with `has_secret_token_value`.
"""
assert DEFAULT_PATTERN_EXAMPLES.keys() == set(DEFAULT_PATTERNS)
isolated_examples = [
*DEFAULT_PATTERN_EXAMPLES.items(),
(r'(?:\b|_)csrf(?:\b|_)', 'has_csrf_value'),
(r'(?:\b|_)xsrf(?:\b|_)', 'has_xsrf_value'),
(r'(?:\b|_)jwt(?:\b|_)', 'has_jwt_value'),
(r'(?:\b|_)ssn(?:\b|_)', 'has_ssn_value'),
('secret', 'has \u017fecret value'),
('credential', 'has credent\u0131al value'),
]
def make_callback(bucket: list[logfire.ScrubMatch]):
def callback(match: logfire.ScrubMatch):
bucket.append(match)
return match.value
return callback
for pattern, value in isolated_examples:
expected = re.compile(pattern, re.IGNORECASE | re.DOTALL).search(value)
assert expected is not None, pattern
scrub_matches: list[logfire.ScrubMatch] = []
result, scrubbed_notes = Scrubber(None, make_callback(scrub_matches)).scrub_value(
('attributes', 'value'), value
)
assert result == value
assert scrubbed_notes == []
assert len(scrub_matches) == 1, (pattern, value)
actual = scrub_matches[0].pattern_match
assert (actual.span(), actual.group(0)) == (expected.span(), expected.group(0))
def test_scrub_attribute(exporter: TestExporter):
logfire.info(
'Password: {user_password}',
user_password=['hunter2'],
mode='password',
modes='passwords',
Author='Alice1',
authors='Alice2',
authr='Alice3',
authorization='Alice4',
)
# We redact:
# - The `user_password` attribute.
# - The `modes` attribute.
# - `authr` and `authorization` because they contain 'auth' but don't look like 'author(s)'.
# Things intentionally not redacted even though they contain "password":
# - The `mode` attribute, because the value 'password' is a full match.
# - 'Author' and 'authors': special cases in the regex that looks for 'auth'.
# - logfire.msg_template
# - The span name, which is the same as msg_template and shouldn't contain data.
# - logfire.json_schema
# - code.filepath (contains 'secret' - this test filename is itself part of the test)
# - logfire.msg: while `{user_password}` is obviously sensitive, the user clearly explicitly wanted it to be logged.
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'Password: {user_password}',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'Password: {user_password}',
'logfire.msg': "Password: ['hunter2']",
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_scrub_attribute',
'code.lineno': 123,
'user_password': "[Scrubbed due to 'password']",
'mode': 'password',
'modes': "[Scrubbed due to 'password']",
'Author': 'Alice1',
'authors': 'Alice2',
'authr': "[Scrubbed due to 'auth']",
'authorization': "[Scrubbed due to 'auth']",
'logfire.json_schema': '{"type":"object","properties":{"user_password":{"type":"array"},"mode":{},"modes":{},"Author":{},"authors":{},"authr":{},"authorization":{}}}',
'logfire.scrubbed': IsJson(
[
{'path': ['attributes', 'user_password'], 'matched_substring': 'password'},
{'path': ['attributes', 'modes'], 'matched_substring': 'password'},
{'path': ['attributes', 'authr'], 'matched_substring': 'auth'},
{'path': ['attributes', 'authorization'], 'matched_substring': 'auth'},
]
),
},
}
]
)
def test_scrub_log_event_attribute(logs_exporter: TestLogExporter):
get_logger(__name__).emit(
LogRecord(
attributes={
'event.name': 'Password: {user_password}',
'user_password': ['hunter2'],
'mode': 'password',
'modes': 'passwords',
'Author': 'Alice1',
'authors': 'Alice2',
'authr': 'Alice3',
'authorization': 'Alice4',
},
)
)
# We redact:
# - The `user_password` attribute.
# - The `modes` attribute.
# - `authr` and `authorization` because they contain 'auth' but don't look like 'author(s)'.
# Things intentionally not redacted even though they contain "password":
# - The `mode` attribute, because the value 'password' is a full match.
# - 'Author' and 'authors': special cases in the regex that looks for 'auth'.
# - event.name
assert logs_exporter.exported_logs_as_dicts() == snapshot(
[
{
'body': None,
'severity_number': None,
'severity_text': None,
'attributes': {
'user_password': ("[Scrubbed due to 'password']",),
'mode': 'password',
'modes': "[Scrubbed due to 'password']",
'Author': 'Alice1',
'authors': 'Alice2',
'authr': "[Scrubbed due to 'auth']",
'authorization': "[Scrubbed due to 'auth']",
'event.name': 'Password: {user_password}',
'logfire.scrubbed': IsJson(
snapshot(
[
{'path': ['attributes', 'user_password'], 'matched_substring': 'password'},
{'path': ['attributes', 'modes'], 'matched_substring': 'password'},
{'path': ['attributes', 'authr'], 'matched_substring': 'auth'},
{'path': ['attributes', 'authorization'], 'matched_substring': 'auth'},
]
)
),
},
'timestamp': 1000000000,
'observed_timestamp': 2000000000,
'trace_id': 0,
'span_id': 0,
'trace_flags': 0,
}
]
)
def test_scrub_message(exporter: TestExporter):
logfire.info('User: {user}', user=[{'name': 'John', 'password': 'hunter2'}])
# Only the sensitive part of the `user` attribute is redacted.
# The full formatted value is redacted from the message,
# because the formatting code only sees the full `str(user)`.
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'User: {user}',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'User: {user}',
'logfire.msg': "User: [Scrubbed due to 'password']",
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_scrub_message',
'code.lineno': 123,
'user': '[{"name": "John", "password": "[Scrubbed due to \'password\']"}]',
'logfire.scrubbed': IsJson(
[
{'path': ['message', 'user'], 'matched_substring': 'password'},
{'path': ['attributes', 'user', 0, 'password'], 'matched_substring': 'password'},
]
),
'logfire.json_schema': '{"type":"object","properties":{"user":{"type":"array"}}}',
},
}
]
)
class PasswordError(Exception):
pass
def test_scrub_events(exporter: TestExporter):
def get_password():
with logfire.span('get_password'):
get_current_span().add_event('password', {'password': 'hunter2', 'other': 'safe'})
try:
raise PasswordError('Password: hunter2')
except Exception as e:
get_current_span().record_exception(e, attributes={'exception.stacktrace': 'wrong and secret'})
raise
with pytest.raises(PasswordError):
get_password()
# We redact:
# - The `password` event attribute.
# We don't redact (despite containing "password"):
# - The event name.
# - The exception stuff.
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'get_password',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 5000000000,
'attributes': {
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'get_password',
'code.lineno': 123,
'logfire.msg_template': 'get_password',
'logfire.msg': 'get_password',
'logfire.span_type': 'span',
'logfire.level_num': 17,
'logfire.exception.fingerprint': '0000000000000000000000000000000000000000000000000000000000000000',
'logfire.scrubbed': IsJson(
[
{
'path': ['otel_events', 0, 'attributes', 'password'],
'matched_substring': 'password',
},
]
),
},
'events': [
{
'name': 'password',
'timestamp': 2000000000,
'attributes': {
'password': "[Scrubbed due to 'password']",
'other': 'safe',
},
},
{
'name': 'exception',
'timestamp': 3000000000,
'attributes': {
'exception.type': 'tests.test_secret_scrubbing.PasswordError',
'exception.message': 'Password: hunter2',
'exception.stacktrace': 'wrong and secret',
'exception.escaped': 'False',
},
},
{
'name': 'exception',
'timestamp': 4000000000,
'attributes': {
'exception.type': 'tests.test_secret_scrubbing.PasswordError',
'exception.message': 'Password: hunter2',
'exception.stacktrace': 'tests.test_secret_scrubbing.PasswordError: Password: hunter2',
'exception.escaped': 'True',
},
},
],
}
]
)
def test_scrubbing_config(exporter: TestExporter, logs_exporter: TestLogExporter, config_kwargs: dict[str, Any]):
def callback(match: logfire.ScrubMatch):
if match.path[-1] == 'my_password':
return str(match)
elif match.path[-1] == 'bad_value':
# This is not a valid OTEL attribute value, so it will be removed completely.
return match
config_kwargs['advanced'].log_record_processors = [SimpleLogRecordProcessor(logs_exporter)]
logfire.configure(
scrubbing=logfire.ScrubbingOptions(
extra_patterns=['my_pattern'],
callback=callback,
),
**config_kwargs,
)
# Note the values (or lack thereof) of each of these attributes in the exported span.
logfire.info('hi', my_password='hunter2', other='matches_my_pattern', bad_value='the_password')
get_logger(__name__).emit(
LogRecord(attributes={'my_password': 'hunter2', 'bad_value': 'the_password', 'event.name': 'hi'})
)
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'hi',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'hi',
'logfire.msg': 'hi',
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_scrubbing_config',
'code.lineno': 123,
'my_password': (
'ScrubMatch('
"path=('attributes', 'my_password'), "
"value='hunter2', "
"pattern_match=<re.Match object; span=(3, 11), match='password'>"
')'
),
'other': "[Scrubbed due to 'my_pattern']",
'logfire.json_schema': '{"type":"object","properties":{"my_password":{},"other":{},"bad_value":{}}}',
'logfire.scrubbed': '[{"path": ["attributes", "other"], "matched_substring": "my_pattern"}]',
},
}
]
)
assert logs_exporter.exported_logs_as_dicts() == snapshot(
[
{
'body': None,
'severity_number': None,
'severity_text': None,
'attributes': {
'my_password': (
'ScrubMatch('
"path=('attributes', 'my_password'), "
"value='hunter2', "
"pattern_match=<re.Match object; span=(3, 11), match='password'>"
')'
),
'event.name': 'hi',
},
'timestamp': 2000000000,
'observed_timestamp': 3000000000,
'trace_id': 0,
'span_id': 0,
'trace_flags': 0,
}
]
)
def test_dont_scrub_resource(exporter: TestExporter, config_kwargs: dict[str, Any]):
os.environ[OTEL_RESOURCE_ATTRIBUTES] = 'my_password=hunter2,yours=your_password,other=safe=good'
logfire.configure(**config_kwargs)
logfire.info('hi')
assert dict(exporter.exported_spans[0].resource.attributes) == IsPartialDict(
{
'telemetry.sdk.language': 'python',
'telemetry.sdk.name': 'opentelemetry',
'my_password': 'hunter2',
'yours': 'your_password',
'other': 'safe=good',
}
)
def test_disable_scrubbing(exporter: TestExporter, logs_exporter: TestLogExporter, config_kwargs: dict[str, Any]):
config_kwargs['advanced'].log_record_processors = [SimpleLogRecordProcessor(logs_exporter)]
logfire.configure(**config_kwargs, scrubbing=False)
config = logfire.DEFAULT_LOGFIRE_INSTANCE.config
assert config.scrubbing is False
assert isinstance(config.scrubber, NoopScrubber)
logfire.info('Password: {user_password}', user_password='my secret password')
get_logger(__name__).emit(
LogRecord(attributes={'user_password': 'my secret password', 'event.name': 'Password: {user_password}'})
)
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'Password: {user_password}',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'Password: {user_password}',
'logfire.msg': 'Password: my secret password',
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_disable_scrubbing',
'code.lineno': 123,
'user_password': 'my secret password',
'logfire.json_schema': '{"type":"object","properties":{"user_password":{}}}',
},
}
]
)
assert logs_exporter.exported_logs_as_dicts() == snapshot(
[
{
'body': None,
'severity_number': None,
'severity_text': None,
'attributes': {
'user_password': 'my secret password',
'event.name': 'Password: {user_password}',
},
'timestamp': 2000000000,
'observed_timestamp': 3000000000,
'trace_id': 0,
'span_id': 0,
'trace_flags': 0,
}
]
)
def test_do_not_scrub(exporter: TestExporter):
# do_not_scrub is a safe key to provide a crude workaround, but it only works if the matched value is *inside*
logfire.info(
'hi',
x=[
{'do_not_scrub': 'not_secret'}, # only this works
{'not_secret': 'do_not_scrub'},
{'not_secret': {'do_not_scrub': 'foo'}},
],
)
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
[
{
'name': 'hi',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'hi',
'logfire.msg': 'hi',
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_do_not_scrub',
'code.lineno': 123,
'x': [
{'do_not_scrub': 'not_secret'},
{'not_secret': "[Scrubbed due to 'secret']"},
{'not_secret': "[Scrubbed due to 'secret']"},
],
'logfire.json_schema': {'type': 'object', 'properties': {'x': {'type': 'array'}}},
'logfire.scrubbed': [
{'path': ['attributes', 'x', 1, 'not_secret'], 'matched_substring': 'secret'},
{'path': ['attributes', 'x', 2, 'not_secret'], 'matched_substring': 'secret'},
],
},
}
]
)
def test_fstring_magic_scrubbing(exporter: TestExporter):
password = 'secret-password'
name = 'John'
logfire.info(f'User: {name}, password: {password}', foo=1234)
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'User: {name}, password: {password}',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'User: {name}, password: {password}',
'logfire.msg': "User: John, password: [Scrubbed due to 'secret']",
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_fstring_magic_scrubbing',
'code.lineno': 123,
'foo': 1234,
'name': 'John',
'password': "[Scrubbed due to 'password']",
'logfire.json_schema': '{"type":"object","properties":{"foo":{},"name":{},"password":{}}}',
'logfire.scrubbed': IsJson(
[
{'path': ['message', 'password'], 'matched_substring': 'secret'},
{'path': ['attributes', 'password'], 'matched_substring': 'password'},
]
),
},
}
]
)
def test_word_boundaries(exporter: TestExporter):
logfire.info(
'hi',
x=[
'abcjwt',
'abc_jwt',
'abc-jwt',
'csrf123',
'csrf_123',
'csrf/123',
'456/csrf/123',
],
)
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
[
{
'name': 'hi',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'hi',
'logfire.msg': 'hi',
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_word_boundaries',
'code.lineno': 123,
'x': [
'abcjwt',
"[Scrubbed due to '_jwt']",
"[Scrubbed due to 'jwt']",
'csrf123',
"[Scrubbed due to 'csrf_']",
"[Scrubbed due to 'csrf']",
"[Scrubbed due to 'csrf']",
],
'logfire.json_schema': {'type': 'object', 'properties': {'x': {'type': 'array'}}},
'logfire.scrubbed': [
{'path': ['attributes', 'x', 1], 'matched_substring': '_jwt'},
{'path': ['attributes', 'x', 2], 'matched_substring': 'jwt'},
{'path': ['attributes', 'x', 4], 'matched_substring': 'csrf_'},
{'path': ['attributes', 'x', 5], 'matched_substring': 'csrf'},
{'path': ['attributes', 'x', 6], 'matched_substring': 'csrf'},
],
},
}
]
)
def test_default_patterns_match_docs():
"""The default scrubbing patterns are documented, so the docs must be kept in sync with the code.
The docs list is generated from `DEFAULT_PATTERNS`. If it has drifted, this test rewrites
the docs to match (so a local re-run passes) and then fails, like inline-snapshot's fix mode.
"""
from logfire._internal.scrubbing import DEFAULT_PATTERNS
docs = Path(__file__).parent.parent / 'docs' / 'how-to-guides' / 'scrubbing.md'
content = docs.read_text()
# `repr` of each pattern reproduces exactly how it's written in the docs code block.
expected_block = '[\n' + ''.join(f' {pattern!r},\n' for pattern in DEFAULT_PATTERNS) + ']'
# Match the ```python [...] ``` block that follows the "default scrubbing patterns" sentence.
match = re.search(
r'(Here are the default scrubbing patterns:\n+```python\n)(\[.*?\])(\n```)',
content,
re.DOTALL,
)
assert match, 'Could not find the default scrubbing patterns code block in the docs'
if match.group(2) != expected_block:
docs.write_text(content[: match.start(2)] + expected_block + content[match.end(2) :])
pytest.fail(
f'The scrubbing patterns documented in {docs} were out of sync with `DEFAULT_PATTERNS` '
'in logfire/_internal/scrubbing.py. The docs have been updated to match; re-run to confirm.'
)
def test_logfire_token_prefix_scrubbing(exporter: TestExporter):
logfire.info(
'hi',
x=[
'pylf_v1_abc123xyz',
'pylf_v2_sometoken',
'pylf_v10_longerversion',
'not_pylf_v1_token',
'pylf_v1_',
],
)
# pylf_v\d+_ pattern matches Logfire API token prefixes.
# Tokens like 'pylf_v1_abc123xyz' get scrubbed, but the prefix alone is kept.
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
[
{
'name': 'hi',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'hi',
'logfire.msg': 'hi',
'code.filepath': 'test_secret_scrubbing.py',
'code.function': 'test_logfire_token_prefix_scrubbing',
'code.lineno': 123,
'x': [
"[Scrubbed due to 'pylf_v1_']",
"[Scrubbed due to 'pylf_v2_']",
"[Scrubbed due to 'pylf_v10_']",
"[Scrubbed due to 'pylf_v1_']",
'pylf_v1_',
],
'logfire.json_schema': {'type': 'object', 'properties': {'x': {'type': 'array'}}},
'logfire.scrubbed': [
{'path': ['attributes', 'x', 0], 'matched_substring': 'pylf_v1_'},
{'path': ['attributes', 'x', 1], 'matched_substring': 'pylf_v2_'},
{'path': ['attributes', 'x', 2], 'matched_substring': 'pylf_v10_'},
{'path': ['attributes', 'x', 3], 'matched_substring': 'pylf_v1_'},
],
},
}
]
)

Xet Storage Details

Size:
30 kB
·
Xet hash:
2c49d478adafaa86dafd8d97c2056ef51d81036e4500b77caa7319377f2a6c90

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.