feat: Phase 3 - AES encryption, SQL audit engine, audit logs (85 tests)

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
This commit is contained in:
logan committed 2026-05-22 13:08:55 +08:00
1 parent 27989274f5
commit 66b09e4d85
16 files changed
+1146 -6

No files matched your search

+28 -2
View File
@@ -10,6 +10,8 @@ from app.models.user import User
from app.models.instance import Instance
from app.schemas.instance import InstanceCreate, InstanceUpdate, InstanceResponse, ConnectionTestResult
from app.services.instance_service import InstanceService
from app.core.crypto import encrypt_password, decrypt_password
from app.services.audit_log_service import AuditLogService
router = APIRouter(prefix="/instances", tags=["数据源管理"])
@@ -42,12 +44,18 @@ async def create_instance(
host=data.host,
port=data.port,
username=data.username,
password=data.password,
password=encrypt_password(data.password),
database=data.database,
env=data.env,
description=data.description,
)
db.add(instance)
await db.flush()
await AuditLogService.log(
db, user_id=current_user.id, action="CREATE_INSTANCE",
target_type="instance", target_id=instance.id,
detail=f"创建数据源: {data.name} ({data.db_type}@{data.host}:{data.port})",
)
await db.commit()
await db.refresh(instance)
return instance
@@ -79,6 +87,8 @@ async def update_instance(
raise HTTPException(status_code=404, detail="数据源不存在")
update_data = data.model_dump(exclude_unset=True)
if "password" in update_data:
update_data["password"] = encrypt_password(update_data["password"])
for key, value in update_data.items():
setattr(instance, key, value)
await db.commit()
@@ -97,6 +107,11 @@ async def delete_instance(
if not instance:
raise HTTPException(status_code=404, detail="数据源不存在")
await AuditLogService.log(
db, user_id=current_user.id, action="DELETE_INSTANCE",
target_type="instance", target_id=instance.id,
detail=f"删除数据源: {instance.name}",
)
await db.delete(instance)
await db.commit()
return {"message": "数据源已删除"}
@@ -113,7 +128,18 @@ async def test_connection(
if not instance:
raise HTTPException(status_code=404, detail="数据源不存在")
return await InstanceService.test_connection(instance)
# Decrypt password for connection test
from app.models.instance import Instance as InstModel
decrypted = instance.__class__(
name=instance.name, db_type=instance.db_type, host=instance.host,
port=instance.port, username=instance.username,
password=decrypt_password(instance.password),
database=instance.database, env=instance.env,
description=instance.description, status=instance.status,
version=instance.version,
)
decrypted.id = instance.id
return await InstanceService.test_connection(decrypted)
@router.get("/{instance_id}/databases")