Coverage for /usr/local/lib/python3.12/site-packages/prefect/_experimental/plugins/apply.py: 38%
17 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
1"""
2Safe application of plugin setup results with secret redaction.
3"""
5from __future__ import annotations 1a
7import logging 1a
8import os 1a
10from prefect._experimental.plugins.spec import SetupResult 1a
12REDACT_KEYS = ("SECRET", "TOKEN", "PASSWORD", "KEY") 1a
15def redact(key: str, value: str) -> str: 1a
16 """
17 Redact sensitive values based on key name heuristics.
19 Args:
20 key: Environment variable name
21 value: Environment variable value
23 Returns:
24 Redacted value if the key appears sensitive, otherwise truncated value
25 """
26 k = key.upper()
27 if any(tag in k for tag in REDACT_KEYS):
28 return "••••••"
29 return value if len(value) <= 64 else value[:20] + "…"
32def apply_setup_result(result: SetupResult, logger: logging.Logger) -> None: 1a
33 """
34 Apply environment changes to the current process.
36 This function never logs secrets - all values are redacted based on key name
37 heuristics.
39 Args:
40 result: The SetupResult containing environment variables to set
41 logger: Logger to use for informational messages
42 """
43 for k, v in (result.env or {}).items():
44 os.environ[str(k)] = str(v)
45 note = result.note or ""
46 logger.info(
47 "plugin env applied%s",
48 f" — {note}" if note else "",
49 )
52def summarize_env(env: dict[str, str]) -> dict[str, str]: 1a
53 """
54 Create a safe summary of environment variables with redacted values.
56 Args:
57 env: Dictionary of environment variables
59 Returns:
60 Dictionary with same keys but redacted values
61 """
62 return {k: redact(k, v) for k, v in env.items()}