Files
insight-updater/backend/app/services/audit.py
T
B0rbor4d cc4c3fcecb Hub-and-Spoke Umbau: Multi-Tenant Zentrale + Satellite-Agent
- Backend: Customer/Satellite Models, customer_id auf Server/Job/Audit
- Satellite-API: heartbeat, poll (atomares Claiming), logs, result,
  scan-result, health-report - Auth via X-Api-Key (SHA-256 gehasht)
- Job-Queue: pending/claimed/running/success/failed + Stale-Janitor
- Batch-Trigger: ein Job pro Server, Satellite arbeitet sequenziell ab
- Credentials bleiben lokal: nur symbolische credential_ref zentral
- Neues Paket satellite/: Pull-Loop, WinRM/SSH/CAU/Scanner, PyInstaller-tauglich
- Frontend: Kunden-Switcher, Satelliten-View, Polling statt WebSocket
- Entfernt: WebSocket/Socket.io, Redis, zentrale Credentials, JobRunner
- Docs: README/AGENTS/PROMPT auf neue Architektur aktualisiert
2026-08-07 03:42:06 +00:00

50 lines
1.3 KiB
Python

"""Audit service: write structured, immutable audit entries to DB."""
import json
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.logging import get_logger
from app.models.audit_log import AuditLog
logger = get_logger(__name__)
class AuditService:
"""Persists audit events. Every mutating API action should call this."""
def __init__(self, db: AsyncSession) -> None:
self.db = db
async def log(
self,
username: str,
action: str,
target: str | None = None,
result: str = "success",
details: dict[str, Any] | None = None,
ip_address: str | None = None,
customer_id: int | None = None,
) -> AuditLog:
entry = AuditLog(
username=username,
action=action,
target=target,
result=result,
details=json.dumps(details) if details else None,
ip_address=ip_address,
customer_id=customer_id,
)
self.db.add(entry)
await self.db.flush()
logger.info(
"audit",
username=username,
action=action,
target=target,
result=result,
customer_id=customer_id,
)
return entry