caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

84 lines
3.0 KiB
Python

"""Append-only audit trail service (Enterprise Access & Governance, Epic 1).
Two operations, no more:
* :func:`record` — INSERT one immutable audit row. Never updates or deletes.
* :func:`query` — read a workspace's trail, but FIRST enforce that the requester
holds ``audit.read`` in that workspace (via :mod:`app.services.rbac`). The read
is then strictly tenant-scoped, so a member of workspace *B* can never read
workspace *A*'s trail.
The append-only discipline is a hard invariant: there is deliberately no update
or delete function anywhere in this module or in
:class:`~app.models.db.repositories.AuditLogRepository`, so a governance trail is
tamper-evident by construction.
"""
from __future__ import annotations
from sqlalchemy.orm import Session
from app.models.db.models import AuditLog
from app.models.db.repositories import AuditLogRepository
from app.services import rbac
def record(
session: Session,
*,
actor_user_id: str,
action: str,
workspace_id: str | None = None,
target_type: str = "",
target_id: str | None = None,
metadata: dict | None = None,
ip: str | None = None,
) -> AuditLog:
"""Append one immutable audit row and return it.
``actor_user_id`` (who acted) and ``action`` (a stable dotted verb, e.g.
``members.role_changed``) are required; the rest describe the target and
context. ``workspace_id`` is optional so a platform-level action (e.g. a
staff impersonation not bound to one tenant) can be recorded without a
tenant. ``metadata`` is copied at the repository boundary so a later mutation
of the caller's dict cannot bleed into the persisted row. This is an INSERT
only — the row is never mutated or deleted afterward.
"""
return AuditLogRepository(session).create(
actor_user_id=actor_user_id,
action=action,
workspace_id=workspace_id,
target_type=target_type,
target_id=target_id,
metadata=metadata,
ip=ip,
)
def query(
session: Session,
workspace_id: str,
requester_user_id: str,
limit: int = 50,
offset: int = 0,
) -> list[AuditLog]:
"""Return a workspace's audit rows (newest first) — ``audit.read`` gated.
Authorization runs FIRST: :func:`rbac.require_permission` verifies the
requester is a member of ``workspace_id`` (tenant isolation) AND that their
role grants ``audit.read`` — a non-member or an under-privileged member
raises :class:`PermissionError` (mapped to HTTP ``403`` upstream) before any
row is read. Only then are that workspace's rows returned, paginated. Because
the query filters strictly on ``workspace_id``, a member of another tenant
can never receive these rows even if authorized elsewhere.
"""
rbac.require_permission(
session, requester_user_id, workspace_id, rbac.PERM_AUDIT_READ
)
return AuditLogRepository(session).list_for_workspace(
workspace_id, limit=limit, offset=offset
)
__all__ = ["query", "record"]