Source code for s365b.core.audit
"""Writing the audit log.
Every security-relevant action (sign-in, settings change, restore, export)
is recorded with who did it, from where, and what changed. Rows are only ever
inserted.
"""
from __future__ import annotations
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from s365b.db.models import AuditLog
[docs]
async def audit(
s: AsyncSession,
actor: str,
action: str,
*,
target: str = "",
details: dict[str, Any] | None = None,
ip: str | None = None,
) -> None:
"""Append an audit record.
Args:
s: Open session; the caller commits.
actor: Username, ``system`` or ``anonymous``.
action: Dotted action name, e.g. ``auth.login.local``.
target: What was acted on.
details: Extra structured details. Never include secrets.
ip: Client IP address.
"""
s.add(AuditLog(actor=actor, action=action, target=target, details=details or {}, source_ip=ip))