douyinWEB/backend/app/settings_audit.py
eric b4e07d792e feat: add dashboard, oauth health, ops alerts, readonly role, and S3 archive replay
New pages and APIs for cross-platform jobs, YouTube OAuth health, mobile ops,
settings audit, optional DB backup, quota/disk alerts, and clip archive replay.
All new behavior defaults off to avoid changing production pipelines.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-23 08:02:58 -05:00

66 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
"""Audit trail for settings changes — append-only events, no data mutation."""
from __future__ import annotations
from typing import Any
_SENSITIVE_KEYS = frozenset(
{
"admin_password_hash",
"readonly_password_hash",
"shorts_oauth_client_secret",
"tiktok_oauth_client_secret",
"dy_cookie",
"yt_studio_cookies_by_profile",
"shorts_upload_socks_profiles",
"tiktok_upload_socks_profiles",
}
)
def _mask_value(key: str, value: Any) -> Any:
if key in _SENSITIVE_KEYS:
return "***"
if key == "shorts_oauth_profiles" and isinstance(value, list):
out = []
for row in value:
if not isinstance(row, dict):
continue
r = dict(row)
if r.get("client_secret"):
r["client_secret"] = "***"
out.append(r)
return out
if isinstance(value, str) and len(value) > 120:
return value[:80] + "…"
return value
def log_settings_patch(patch: dict[str, Any], *, actor: str = "") -> None:
if not patch:
return
try:
from . import db
if not db.get_settings_map().get("settings_audit_enabled", True):
return
except Exception:
pass
safe = {k: _mask_value(k, v) for k, v in patch.items() if v is not None}
if not safe:
return
keys = ", ".join(sorted(safe.keys())[:12])
more = len(safe) - 12
msg = f"设置变更:{keys}" + (f" 等 {more} 项" if more > 0 else "")
try:
from . import db
db.add_event(
kind="settings_audit",
level="info",
message=msg,
meta={"actor": actor or "system", "patch": safe},
)
except Exception:
pass