diff options
| author | Roberto Bertó <463349+robertoberto@users.noreply.github.com> | 2025-11-02 19:55:55 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-02 19:55:55 -0300 |
| commit | 6ca1269b138696b69c33d2db4bcf7dd03ea4caeb (patch) | |
| tree | 4c0a634730df2123ff506252cbec050de006dac8 /pyvyos/utils/json.py | |
| parent | 1ada32975f0bb559ec7526e0dd545700dde1cb94 (diff) | |
| parent | 6b4e9015744ab8c9f17a6b8e23387cd676b1d827 (diff) | |
| download | pyvyos-6ca1269b138696b69c33d2db4bcf7dd03ea4caeb.tar.gz pyvyos-6ca1269b138696b69c33d2db4bcf7dd03ea4caeb.zip | |
Merge pull request #27 from vyos-contrib/feat/architecture-and-quality-improvements
feat: v0.4.0 - Architecture refactor, bug fixes, and quality improvem…
Diffstat (limited to 'pyvyos/utils/json.py')
| -rw-r--r-- | pyvyos/utils/json.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/pyvyos/utils/json.py b/pyvyos/utils/json.py new file mode 100644 index 0000000..6d9a7c0 --- /dev/null +++ b/pyvyos/utils/json.py @@ -0,0 +1,47 @@ +"""JSON utilities with security (redaction) support.""" + +import json +from typing import Any, Dict, List + + +def redact_key(data: Dict[str, Any], keys: List[str] = None) -> Dict[str, Any]: + """ + Redact sensitive keys from a dictionary. + + Args: + data: Dictionary to redact + keys: List of keys to redact (default: ["key", "apikey", "password"]) + + Returns: + Copy of data with specified keys redacted as "***REDACTED***" + """ + if keys is None: + keys = ["key", "apikey", "password"] + + if not isinstance(data, dict): + return data + + redacted = data.copy() + for key in keys: + if key in redacted: + redacted[key] = "***REDACTED***" + + return redacted + + +def safe_dumps(obj: Any, redact_keys: List[str] = None) -> str: + """ + Safely serialize object to JSON with key redaction. + + Args: + obj: Object to serialize + redact_keys: Keys to redact if obj is a dict + + Returns: + JSON string with sensitive data redacted + """ + if isinstance(obj, dict): + obj = redact_key(obj, redact_keys) + + return json.dumps(obj, default=str) + |
