#!/usr/bin/env python3 """Sanitize Proxmox audit output into a terminal-replay transcript.""" from __future__ import annotations import json import os import re import sys from collections.abc import Callable IP = re.compile(r"\b(?:192\.168|10|100)\.\d{1,3}\.\d{1,3}\b") MAC = re.compile(r"\b(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}\b") UUID = re.compile(r"\b[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}\b") DISK = re.compile(r"\b(?:eui\.[A-Za-z0-9._-]+|nvme-[A-Za-z0-9._-]+|wwn-[A-Za-z0-9._-]+)\b", re.I) SERIAL = re.compile(r"(?i)\b(serial(?:_number)?\s*[=:]\s*)[^\s,]+") COMMENT = re.compile(r"""(?i)(["']?(?:comment|notes|description)["']?\s*[:=]\s*)(["'])(.*?)\2""") REPOID = re.compile(r"\s*\(repoid [0-9a-f]+\)") def replacement(prefix: str) -> Callable[[re.Match[str]], str]: values: dict[str, str] = {} def replace(match: re.Match[str]) -> str: value = match.group(0) if value not in values: values[value] = f"[{prefix}-{len(values) + 1}]" return values[value] return replace def sanitize(text: str) -> str: """Replace network and machine identifiers while retaining audit meaning.""" text = IP.sub(replacement("address"), text) text = MAC.sub(replacement("mac"), text) text = UUID.sub(replacement("uuid"), text) text = DISK.sub(replacement("disk-id"), text) text = SERIAL.sub(r"\1[serial]", text) text = COMMENT.sub(r"\1\2[note]\2", text) text = REPOID.sub("", text) for word in filter(None, (w.strip() for w in os.environ.get("SANITIZE_EXTRA", "").split(","))): text = re.sub(re.escape(word), "[name]", text, flags=re.I) return text def main() -> int: output = sanitize(sys.stdin.read()) transcript = {"title": "Proxmox cluster audit", "prompt": "$ ", "type_ms": 28, "line_ms": 30, "steps": [{"cmd": "PVE_HOST=[address] python pve_audit.py", "out": output, "pause": 900}]} json.dump(transcript, sys.stdout, indent=2) sys.stdout.write("\n") return 0 if __name__ == "__main__": raise SystemExit(main())