"""Support access codes for Xenia sites (APPS_PLATFORM D-16). Run on the BUILDER's machine. python tools/support_code.py --init create the master key (once; never overwrites) python tools/support_code.py --site-secret the SUPPORT_SECRET value for that site's .env python tools/support_code.py the response code for a challenge the site shows The master key lives in %USERPROFILE%\\.xenia\\support_master_key.txt (XENIA_SUPPORT_MASTER_KEY_FILE overrides the path). It is outside the repo: back it up. Losing it means every site's SUPPORT_SECRET has to be replaced; leaking it gives support access to every site. The derivation is local_backend/core/support_codes.py (loaded by path; stdlib only). """ from __future__ import annotations import importlib.util import os import secrets import sys from pathlib import Path _HERE = Path(__file__).resolve().parent _spec = importlib.util.spec_from_file_location("support_codes", _HERE.parent / "local_backend" / "core" / "support_codes.py") codes = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(codes) def key_file() -> Path: return Path(os.environ.get("XENIA_SUPPORT_MASTER_KEY_FILE") or Path.home() / ".xenia" / "support_master_key.txt") def master_key() -> str: p = key_file() if not p.exists(): sys.exit(f"No master key at {p}. Create it once with: python tools/support_code.py --init") key = p.read_text(encoding="utf-8").strip() if len(key) < 32: sys.exit(f"{p} looks wrong (too short).") return key def main(argv: list[str]) -> int: if argv == ["--init"]: p = key_file() if p.exists(): print(f"Master key already exists at {p}; not touching it.") return 0 p.parent.mkdir(parents=True, exist_ok=True) p.write_text(secrets.token_hex(32) + "\n", encoding="utf-8") print(f"Created {p}. Back it up somewhere safe.") return 0 if len(argv) == 2 and argv[0] == "--site-secret": print(codes.site_secret(master_key(), argv[1])) return 0 if len(argv) == 2 and not argv[0].startswith("-"): site_id, challenge = argv secret = codes.site_secret(master_key(), site_id) print(codes.group(codes.code_for(secret, site_id, challenge), 5)) return 0 print(__doc__) return 2 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))