feat(audit): per-request loguru audit middleware with daily rotation

按需为每个 HTTP 接口写一条合规记录到
data/logs/audit/audit-YYYY-MM-DD.log,字段:时间 / 用户 /
METHOD path / 状态码。

设计:
* 复用全局 loguru logger,文件 sink 由 audit._DailyFileSink
  自管:缓存当天文件句柄、跨日重建。不走 loguru 的
  rotation=00:00(产物是 audit.log.YYYY-MM-DD_HH-MM-SS,
  不符合按天单文件的命名要求)。
* AuditMiddleware 只做 CPU 验签拿 user_id:cookie access_token
  优先,Authorization Bearer 兜底,无/坏 JWT 一律记 '-'。
  绝不查 DB(RequestContext 在路由解析后才注入)。
* 审计失败不拖死请求:所有异常捕获。
* 与 main.py 现有 access_log 严格分离:access_log 走 stderr
  诊断(method/path/status/耗时),audit 走独立文件合规
  (时间/用户/接口),并存。
* 启动时按 settings.audit_log_retention_days 清理过期文件
  (设 0 关闭)。
* 新增 settings.audit_log_dir(默认 data/logs/audit,相对 cwd)
  与 settings.audit_log_retention_days(默认 30)两个配置项;
  .env.example 同步。

新增 9 个 case:文件创建、行字段、未登录 '-'、坏 JWT、ULID
path、retention 清理/关闭、Bearer 头、幂等。
This commit is contained in:
tao.chen
2026-08-21 13:22:00 +08:00
parent 2b1f6b5303
commit 97825ca86d
5 changed files with 425 additions and 0 deletions
+7
View File
@@ -30,6 +30,13 @@ INITIAL_ADMIN_PASSWORD=admin12345
# runtime_client._jupyter_request.
LOG_LEVEL=INFO
# Audit log (backend HTTP interface compliance log). One line per HTTP
# request, written to data/logs/audit/audit-YYYY-MM-DD.log (one file per
# day). AUDIT_LOG_DIR is relative to the backend process cwd (/app in the
# container). AUDIT_LOG_RETENTION_DAYS=0 disables cleanup of old files.
AUDIT_LOG_DIR=
AUDIT_LOG_RETENTION_DAYS=30
# Object storage. Two modes are supported:
# STORAGE_BACKEND=s3 — connects to an S3-compatible service (MinIO,
# RustFS, SeaweedFS, AWS S3, …). Requires the