diff options
Diffstat (limited to 'pyvyos/utils')
| -rw-r--r-- | pyvyos/utils/__init__.py | 8 | ||||
| -rw-r--r-- | pyvyos/utils/ids.py | 14 | ||||
| -rw-r--r-- | pyvyos/utils/json.py | 47 | ||||
| -rw-r--r-- | pyvyos/utils/paths.py | 30 |
4 files changed, 99 insertions, 0 deletions
diff --git a/pyvyos/utils/__init__.py b/pyvyos/utils/__init__.py new file mode 100644 index 0000000..adff044 --- /dev/null +++ b/pyvyos/utils/__init__.py @@ -0,0 +1,8 @@ +"""Utility functions for PyVyOS.""" + +from .json import redact_key, safe_dumps +from .ids import request_id +from .paths import build_path + +__all__ = ["redact_key", "safe_dumps", "request_id", "build_path"] + diff --git a/pyvyos/utils/ids.py b/pyvyos/utils/ids.py new file mode 100644 index 0000000..5807ddf --- /dev/null +++ b/pyvyos/utils/ids.py @@ -0,0 +1,14 @@ +"""ID generation utilities.""" + +import uuid + + +def request_id() -> str: + """ + Generate a unique request ID for tracing. + + Returns: + UUID4 string formatted for logging + """ + return str(uuid.uuid4()) + 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) + diff --git a/pyvyos/utils/paths.py b/pyvyos/utils/paths.py new file mode 100644 index 0000000..b591b09 --- /dev/null +++ b/pyvyos/utils/paths.py @@ -0,0 +1,30 @@ +"""Path building utilities for VyOS configuration paths.""" + +from typing import List, Union + + +def build_path(*segments: Union[str, List[str]]) -> List[str]: + """ + Build a configuration path from segments. + + Args: + *segments: Path segments (strings or lists of strings) + + Returns: + Flattened list of path segments + + Examples: + build_path("interfaces", "ethernet", "eth0") + -> ["interfaces", "ethernet", "eth0"] + + build_path(["interfaces", "ethernet"], "address", "192.168.1.1/24") + -> ["interfaces", "ethernet", "address", "192.168.1.1/24"] + """ + result = [] + for segment in segments: + if isinstance(segment, list): + result.extend(segment) + else: + result.append(str(segment)) + return result + |
