feat: P0生产加固 — 认证安全+操作审计+错误提示
P0-1 认证安全: - JWT secret 改为环境变量配置, 未设置则随机生成(重启失效) - 新增 LoginRateLimitMiddleware: 5次/分钟登录频率限制(滑动窗口) - 429 响应 + 前端 toast 提示 P0-2 操作审计日志: - 新增 OperationLog 模型 (user/username/action/resource/detail/ip) - 新增 AuditLogMiddleware 自动记录 POST/PUT/DELETE/PATCH 操作 - 新增 GET /audit-logs API (分页/过滤/统计) - 前端 AuditLogsPage 对接真实 API (字段/路径修正) P0-3 实例密码加密: - 已确认 crypto.py AES-256-GCM 加密存储, 无需修改 P1 前端错误提示: - 新增 useToast hook + ToastContainer 组件 - request.ts 拦截器: 4xx/5xx 错误 toast 显示后端 detail 信息 - 422 验证错误提取第一条 msg - 网络失败显示通用提示 1182 tests passed
This commit is contained in:
1 parent
0576b253e4
commit
344c519209
15 files changed
+510
-97
No files matched your search
@@ -0,0 +1,94 @@
|
||||
"""操作审计日志 API。"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func, desc
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import require_role
|
||||
from app.models.user import User
|
||||
from app.models.operation_log import OperationLog
|
||||
|
||||
router = APIRouter(prefix="/audit-logs", tags=["audit-logs"])
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_audit_logs(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
action: Optional[str] = Query(None),
|
||||
resource_type: Optional[str] = Query(None),
|
||||
username: Optional[str] = Query(None),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""查询操作审计日志(仅管理员)。"""
|
||||
query = select(OperationLog)
|
||||
count_query = select(func.count(OperationLog.id))
|
||||
|
||||
if action:
|
||||
query = query.where(OperationLog.action == action)
|
||||
count_query = count_query.where(OperationLog.action == action)
|
||||
if resource_type:
|
||||
query = query.where(OperationLog.resource_type == resource_type)
|
||||
count_query = count_query.where(OperationLog.resource_type == resource_type)
|
||||
if username:
|
||||
query = query.where(OperationLog.username.contains(username))
|
||||
count_query = count_query.where(OperationLog.username.contains(username))
|
||||
|
||||
total = (await db.execute(count_query)).scalar() or 0
|
||||
query = query.order_by(desc(OperationLog.created_at))
|
||||
query = query.offset((page - 1) * page_size).limit(page_size)
|
||||
|
||||
result = await db.execute(query)
|
||||
logs = result.scalars().all()
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"items": [
|
||||
{
|
||||
"id": l.id,
|
||||
"user_id": l.user_id,
|
||||
"username": l.username,
|
||||
"action": l.action,
|
||||
"resource_type": l.resource_type,
|
||||
"resource_id": l.resource_id,
|
||||
"detail": l.detail,
|
||||
"ip_address": l.ip_address,
|
||||
"created_at": l.created_at.isoformat() if l.created_at else None,
|
||||
}
|
||||
for l in logs
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/stats")
|
||||
async def audit_log_stats(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""审计日志统计概览。"""
|
||||
# 最近7天操作分布
|
||||
from datetime import datetime, timedelta, timezone
|
||||
since = datetime.now(timezone.utc) - timedelta(days=7)
|
||||
|
||||
result = await db.execute(
|
||||
select(OperationLog.action, func.count(OperationLog.id))
|
||||
.where(OperationLog.created_at >= since)
|
||||
.group_by(OperationLog.action)
|
||||
)
|
||||
action_stats = {row[0]: row[1] for row in result.all()}
|
||||
|
||||
# 操作最多的用户
|
||||
result = await db.execute(
|
||||
select(OperationLog.username, func.count(OperationLog.id))
|
||||
.where(OperationLog.created_at >= since)
|
||||
.group_by(OperationLog.username)
|
||||
.order_by(desc(func.count(OperationLog.id)))
|
||||
.limit(5)
|
||||
)
|
||||
top_users = [{"username": row[0], "count": row[1]} for row in result.all()]
|
||||
|
||||
return {"action_stats": action_stats, "top_users": top_users}
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -12,11 +12,16 @@ router = APIRouter(prefix="/auth", tags=["认证"])
|
||||
|
||||
|
||||
@router.post("/login", response_model=TokenResponse)
|
||||
async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||
async def login(req: LoginRequest, request: Request, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(User).where(User.username == req.username))
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user or not verify_password(req.password, user.hashed_password):
|
||||
# 记录登录失败
|
||||
from app.services.audit_logger import log_operation
|
||||
await log_operation(db, action="login_failed", username=req.username,
|
||||
detail=f"登录失败: 用户名或密码错误", request=request)
|
||||
await db.commit()
|
||||
raise HTTPException(status_code=401, detail="用户名或密码错误")
|
||||
|
||||
if not user.is_active:
|
||||
@@ -26,6 +31,11 @@ async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||
raise HTTPException(status_code=403, detail="访客账户已禁用,请联系管理员")
|
||||
|
||||
user.last_login = datetime.now(timezone.utc)
|
||||
|
||||
# 记录登录成功
|
||||
from app.services.audit_logger import log_operation
|
||||
await log_operation(db, action="login", user_id=user.id, username=user.username,
|
||||
detail=f"登录成功", request=request)
|
||||
await db.commit()
|
||||
|
||||
token = create_access_token(data={"sub": str(user.id), "role": user.role})
|
||||
|
||||
@@ -7,7 +7,7 @@ from app.api.v1.endpoints import (
|
||||
instance_group, masking_rule, rollback,
|
||||
workflow, impact, schema_drift, pool,
|
||||
mongo, query_history, sql_assistant, db_types,
|
||||
settings, scheduled_task,
|
||||
settings, scheduled_task, audit_log,
|
||||
)
|
||||
|
||||
api_router = APIRouter(prefix="/api/v1")
|
||||
@@ -41,3 +41,4 @@ api_router.include_router(sql_assistant.router)
|
||||
api_router.include_router(db_types.router)
|
||||
api_router.include_router(settings.router, prefix="/settings", tags=["系统设置"])
|
||||
api_router.include_router(scheduled_task.router)
|
||||
api_router.include_router(audit_log.router)
|
||||
@@ -0,0 +1,129 @@
|
||||
"""操作审计中间件 — 自动记录所有 POST/PUT/DELETE/PATCH 请求。"""
|
||||
import time
|
||||
import json
|
||||
from fastapi import Request
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from app.core.database import async_session
|
||||
from app.models.operation_log import OperationLog
|
||||
|
||||
|
||||
# 不记录审计的路径(高频/非业务)
|
||||
_EXCLUDE_PREFIXES = (
|
||||
"/api/v1/audit-logs",
|
||||
"/api/v1/dashboard",
|
||||
"/docs",
|
||||
"/openapi",
|
||||
"/redoc",
|
||||
"/api/v1/query/execute", # 查询执行单独记录
|
||||
)
|
||||
|
||||
# 资源类型映射: URL前缀 -> 资源类型
|
||||
_RESOURCE_MAP = {
|
||||
"/api/v1/tickets": "ticket",
|
||||
"/api/v1/instances": "instance",
|
||||
"/api/v1/users": "user",
|
||||
"/api/v1/audit-rules": "audit_rule",
|
||||
"/api/v1/notifications": "notification",
|
||||
"/api/v1/auth": "auth",
|
||||
"/api/v1/workflows": "workflow",
|
||||
"/api/v1/settings": "system_setting",
|
||||
}
|
||||
|
||||
# action 映射
|
||||
_ACTION_MAP = {
|
||||
"POST": "create",
|
||||
"PUT": "update",
|
||||
"PATCH": "update",
|
||||
"DELETE": "delete",
|
||||
}
|
||||
|
||||
|
||||
class AuditLogMiddleware(BaseHTTPMiddleware):
|
||||
"""自动审计中间件 — 记录所有写操作。"""
|
||||
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
# 只审计写操作
|
||||
if request.method not in ("POST", "PUT", "PATCH", "DELETE"):
|
||||
return await call_next(request)
|
||||
|
||||
path = request.url.path
|
||||
# 跳过排除列表
|
||||
if any(path.startswith(p) for p in _EXCLUDE_PREFIXES):
|
||||
return await call_next(request)
|
||||
|
||||
# 特殊: POST /auth/login 在 auth.py 中单独记录
|
||||
if path == "/api/v1/auth/login" or path == "/api/v1/auth/logout":
|
||||
return await call_next(request)
|
||||
|
||||
response = await call_next(request)
|
||||
|
||||
# 异步记录审计日志(不阻塞响应)
|
||||
try:
|
||||
await self._record(request, response.status_code)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
async def _record(request: Request, status_code: int):
|
||||
path = request.url.path
|
||||
method = request.method
|
||||
|
||||
# 确定 action
|
||||
action = _ACTION_MAP.get(method, method.lower())
|
||||
if path.endswith("/review"):
|
||||
action = "approve" if status_code < 300 else "reject"
|
||||
elif path.endswith("/execute"):
|
||||
action = "execute"
|
||||
elif path.endswith("/cancel"):
|
||||
action = "cancel"
|
||||
|
||||
# 确定资源类型
|
||||
resource_type = "other"
|
||||
for prefix, rtype in _RESOURCE_MAP.items():
|
||||
if path.startswith(prefix):
|
||||
resource_type = rtype
|
||||
break
|
||||
|
||||
# 提取 resource_id
|
||||
resource_id = None
|
||||
parts = path.rstrip("/").split("/")
|
||||
for p in parts:
|
||||
if p.isdigit():
|
||||
resource_id = int(p)
|
||||
break
|
||||
|
||||
# 获取操作者信息
|
||||
user_id = None
|
||||
username = None
|
||||
try:
|
||||
from app.core.security import decode_token
|
||||
auth_header = request.headers.get("authorization", "")
|
||||
if auth_header.startswith("Bearer "):
|
||||
payload = decode_token(auth_header[7:])
|
||||
user_id = int(payload.get("sub", 0))
|
||||
username = payload.get("role", "unknown")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# IP
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
ip_address = forwarded.split(",")[0].strip() if forwarded else (
|
||||
request.client.host if request.client else None
|
||||
)
|
||||
|
||||
# 写入DB
|
||||
async with async_session() as db:
|
||||
log = OperationLog(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
resource_id=resource_id,
|
||||
detail=f"{method} {path} -> {status_code}",
|
||||
ip_address=ip_address,
|
||||
)
|
||||
db.add(log)
|
||||
await db.commit()
|
||||
@@ -2,12 +2,17 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
import secrets
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
ENV: str = "development"
|
||||
DEBUG: bool = True
|
||||
|
||||
SECRET_KEY: str = "dev-secret-key-change-in-production"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
||||
# JWT secret: 生产环境必须通过环境变量设置
|
||||
# 若未设置则每次启动随机生成(重启后旧 token 失效)
|
||||
SECRET_KEY: str = ""
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 480 # 8小时
|
||||
ALGORITHM: str = "HS256"
|
||||
|
||||
DATABASE_URL: str = "sqlite+aiosqlite:///./sqlite3.db"
|
||||
@@ -18,9 +23,18 @@ class Settings(BaseSettings):
|
||||
# CORS: production comma-separated origins (e.g. "https://sql.example.com,https://sql2.example.com")
|
||||
CORS_ORIGINS: str = ""
|
||||
|
||||
# 登录频率限制: 每IP每分钟最多尝试次数
|
||||
LOGIN_RATE_LIMIT: int = 10
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
s = Settings()
|
||||
if not s.SECRET_KEY:
|
||||
s.SECRET_KEY = secrets.token_urlsafe(32)
|
||||
if s.ENV == "development":
|
||||
import logging
|
||||
logging.getLogger(__name__).info("SECRET_KEY 未设置,已自动生成(重启后失效)")
|
||||
return s
|
||||
@@ -0,0 +1,50 @@
|
||||
"""登录频率限制 — 基于IP的滑动窗口计数器。"""
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
from fastapi import Request, HTTPException, status
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from app.core.config import get_settings
|
||||
|
||||
|
||||
class LoginRateLimitMiddleware(BaseHTTPMiddleware):
|
||||
"""限制每IP每分钟的登录尝试次数。"""
|
||||
|
||||
def __init__(self, app, **kwargs):
|
||||
super().__init__(app, **kwargs)
|
||||
self._attempts: Dict[str, List[float]] = defaultdict(list)
|
||||
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
if request.url.path == "/api/v1/auth/login" and request.method == "POST":
|
||||
client_ip = self._get_client_ip(request)
|
||||
settings = get_settings()
|
||||
max_attempts = settings.LOGIN_RATE_LIMIT
|
||||
|
||||
now = time.time()
|
||||
# 清理60秒前的记录
|
||||
self._attempts[client_ip] = [
|
||||
t for t in self._attempts[client_ip] if now - t < 60
|
||||
]
|
||||
|
||||
if len(self._attempts[client_ip]) >= max_attempts:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail=f"登录尝试过于频繁,请稍后再试(限制: {max_attempts}次/分钟)",
|
||||
)
|
||||
|
||||
response = await call_next(request)
|
||||
# 只在登录失败时记录(成功不计入限制)
|
||||
if response.status_code != 200:
|
||||
self._attempts[client_ip].append(now)
|
||||
return response
|
||||
|
||||
return await call_next(request)
|
||||
|
||||
@staticmethod
|
||||
def _get_client_ip(request: Request) -> str:
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
return forwarded.split(",")[0].strip()
|
||||
return request.client.host if request.client else "unknown"
|
||||
@@ -7,6 +7,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.config import get_settings
|
||||
from app.core.rate_limit import LoginRateLimitMiddleware
|
||||
from app.core.audit_middleware import AuditLogMiddleware
|
||||
from app.core.database import init_db, async_session
|
||||
from app.core.security import hash_password
|
||||
from app.models.user import User
|
||||
@@ -124,6 +126,8 @@ app = FastAPI(
|
||||
_cors_origins = ["*"] if settings.DEBUG else [
|
||||
o.strip() for o in settings.CORS_ORIGINS.split(",") if o.strip()
|
||||
]
|
||||
app.add_middleware(AuditLogMiddleware)
|
||||
app.add_middleware(LoginRateLimitMiddleware)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=_cors_origins,
|
||||
|
||||
@@ -2,3 +2,4 @@ from app.models.instance_group import InstanceGroup, user_groups, instance_group
|
||||
from app.models.system_setting import SystemSetting # noqa: F401
|
||||
from app.models.query_history import QueryHistory, QuickQueryModel # noqa: F401
|
||||
from app.models.ticket_sql_item import TicketSqlItem # noqa: F401
|
||||
from app.models.operation_log import OperationLog # noqa: F401
|
||||
@@ -0,0 +1,27 @@
|
||||
"""操作审计日志模型。"""
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, Index
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class OperationLog(Base):
|
||||
__tablename__ = "operation_logs"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id = Column(Integer, nullable=True, comment="操作者ID(系统操作为空)")
|
||||
username = Column(String(50), nullable=True, comment="操作者用户名")
|
||||
action = Column(String(50), nullable=False, comment="操作类型: login/create/update/delete/execute/approve/reject")
|
||||
resource_type = Column(String(50), nullable=True, comment="资源类型: ticket/instance/user/audit_rule/notification/workflow")
|
||||
resource_id = Column(Integer, nullable=True, comment="资源ID")
|
||||
detail = Column(Text, nullable=True, comment="操作详情")
|
||||
ip_address = Column(String(50), nullable=True, comment="操作者IP")
|
||||
user_agent = Column(String(255), nullable=True, comment="浏览器UA")
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_oplog_user_id", "user_id"),
|
||||
Index("ix_oplog_action", "action"),
|
||||
Index("ix_oplog_resource", "resource_type", "resource_id"),
|
||||
Index("ix_oplog_created_at", "created_at"),
|
||||
)
|
||||
@@ -0,0 +1,45 @@
|
||||
"""操作审计日志服务 — 轻量级,各 endpoint 直接调用。"""
|
||||
from typing import Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from fastapi import Request
|
||||
|
||||
from app.models.operation_log import OperationLog
|
||||
|
||||
|
||||
async def log_operation(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
action: str,
|
||||
resource_type: Optional[str] = None,
|
||||
resource_id: Optional[int] = None,
|
||||
detail: Optional[str] = None,
|
||||
user_id: Optional[int] = None,
|
||||
username: Optional[str] = None,
|
||||
request: Optional[Request] = None,
|
||||
) -> None:
|
||||
"""记录操作日志(fire-and-forget,不抛异常)。"""
|
||||
try:
|
||||
ip_address = None
|
||||
user_agent = None
|
||||
if request:
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
ip_address = forwarded.split(",")[0].strip() if forwarded else (
|
||||
request.client.host if request.client else None
|
||||
)
|
||||
user_agent = request.headers.get("user-agent", "")[:255]
|
||||
|
||||
log = OperationLog(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
resource_id=resource_id,
|
||||
detail=detail,
|
||||
ip_address=ip_address,
|
||||
user_agent=user_agent,
|
||||
)
|
||||
db.add(log)
|
||||
await db.flush()
|
||||
except Exception:
|
||||
# 审计日志不应影响业务流程
|
||||
pass
|
||||
Reference in new issue
Block a user