Coverage for /usr/local/lib/python3.12/site-packages/prefect/logging/filters.py: 33%
17 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
1import logging 1a
2from typing import Any 1a
4from prefect.utilities.collections import visit_collection 1a
5from prefect.utilities.names import obfuscate 1a
8def redact_substr(obj: Any, substr: str) -> Any: 1a
9 """
10 Redact a string from a potentially nested object.
12 Args:
13 obj: The object to redact the string from
14 substr: The string to redact.
16 Returns:
17 Any: The object with the API key redacted.
18 """
20 def redact_item(item: Any) -> Any:
21 if isinstance(item, str):
22 return item.replace(substr, obfuscate(substr))
23 return item
25 redacted_obj = visit_collection(obj, visit_fn=redact_item, return_data=True)
26 return redacted_obj
29class ObfuscateApiKeyFilter(logging.Filter): 1a
30 """
31 A logging filter that obfuscates any string that matches the obfuscate_string function.
32 """
34 def filter(self, record: logging.LogRecord) -> bool: 1a
35 # Need to import here to avoid circular imports
36 from prefect.settings import PREFECT_API_KEY
38 if PREFECT_API_KEY:
39 record.msg = redact_substr(record.msg, PREFECT_API_KEY.value())
41 return True