Phase 3.1: Instance password AES-256-GCM encryption Phase 3.2: SQL audit rule engine (10 rules) Phase 3.3: Auto-audit on ticket creation Phase 3.4: Operation audit logs + GET /audit/logs Tests: 44 -> 85 (41 new), Frontend: AuditLogsPage
50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.core.security import get_current_user
|
|
from app.models.user import User
|
|
from app.models.instance import Instance
|
|
from app.models.audit_log import AuditLog
|
|
from app.schemas.query import QueryRequest, QueryResult
|
|
from app.services.query_service import QueryService
|
|
from app.core.crypto import decrypt_password
|
|
|
|
router = APIRouter(prefix="/query", tags=["SQL 查询"])
|
|
|
|
|
|
@router.post("", response_model=QueryResult)
|
|
async def execute_query(
|
|
req: QueryRequest,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
if current_user.role == "guest":
|
|
raise HTTPException(status_code=403, detail="访客无权执行查询")
|
|
|
|
result = await db.execute(select(Instance).where(Instance.id == req.instance_id))
|
|
instance = result.scalar_one_or_none()
|
|
if not instance:
|
|
raise HTTPException(status_code=404, detail="数据源不存在")
|
|
|
|
# Decrypt password for query execution
|
|
instance.password = decrypt_password(instance.password)
|
|
|
|
try:
|
|
query_result = await QueryService.execute_query(instance, req.database, req.sql, current_user.id)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
# Record audit log
|
|
log = AuditLog(
|
|
user_id=current_user.id,
|
|
action="QUERY",
|
|
target_type="query",
|
|
target_id=instance.id,
|
|
detail=req.sql[:500],
|
|
)
|
|
db.add(log)
|
|
|
|
return query_result
|