Add module event producer coverage
This commit is contained in:
@@ -7,7 +7,15 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from govoplan_core.auth import ApiPrincipal
|
||||
from govoplan_core.core.change_sequence import record_change
|
||||
from govoplan_core.core.events import current_event_trace, normalize_trace_id
|
||||
from govoplan_core.core.events import (
|
||||
EventActorRef,
|
||||
EventObjectRef,
|
||||
EventTenantRef,
|
||||
PlatformEvent,
|
||||
current_event_trace,
|
||||
normalize_trace_id,
|
||||
publish_platform_event,
|
||||
)
|
||||
from govoplan_core.privacy.retention import sanitize_audit_details_for_policy
|
||||
from govoplan_audit.backend.db.models import AuditLog
|
||||
|
||||
@@ -34,6 +42,27 @@ SENSITIVE_DETAIL_KEYS = {
|
||||
"build_token",
|
||||
}
|
||||
|
||||
_ACTION_MODULE_PREFIXES = {
|
||||
"api_key": "access",
|
||||
"configuration_change": "access",
|
||||
"configuration_package": "access",
|
||||
"group": "access",
|
||||
"profile": "access",
|
||||
"role": "access",
|
||||
"system_access": "access",
|
||||
"system_account": "access",
|
||||
"system_memberships": "access",
|
||||
"system_role": "access",
|
||||
"user": "access",
|
||||
"tenant": "tenancy",
|
||||
"privacy_retention": "policy",
|
||||
"retention_policy": "policy",
|
||||
"files": "files",
|
||||
"mail": "mail",
|
||||
"campaign": "campaigns",
|
||||
"report": "campaigns",
|
||||
}
|
||||
|
||||
|
||||
def _optional_text(value: object | None) -> str | None:
|
||||
if value is None:
|
||||
@@ -135,6 +164,34 @@ def _restore_trace_after_policy(details: dict[str, Any], trace: dict[str, str])
|
||||
return restored
|
||||
|
||||
|
||||
def _module_id_for_audit_action(action: str) -> str:
|
||||
prefix = action.split(".", 1)[0].strip().casefold()
|
||||
return _ACTION_MODULE_PREFIXES.get(prefix, prefix or AUDIT_MODULE_ID)
|
||||
|
||||
|
||||
def _publish_audit_platform_event(item: AuditLog) -> None:
|
||||
trace = _compact_trace(item.details.get("_trace") if isinstance(item.details, Mapping) else None)
|
||||
publish_platform_event(
|
||||
PlatformEvent(
|
||||
type=item.action,
|
||||
module_id=_module_id_for_audit_action(item.action),
|
||||
payload={
|
||||
"audit_log_id": item.id,
|
||||
"scope": item.scope,
|
||||
"details": dict(item.details or {}),
|
||||
},
|
||||
correlation_id=trace.get("correlation_id"),
|
||||
causation_id=trace.get("causation_id"),
|
||||
actor=EventActorRef(type="user", id=item.user_id) if item.user_id else (
|
||||
EventActorRef(type="api_key", id=item.api_key_id) if item.api_key_id else None
|
||||
),
|
||||
tenant=EventTenantRef(id=item.tenant_id) if item.tenant_id else None,
|
||||
resource=EventObjectRef(type=item.object_type, id=item.object_id) if item.object_type else None,
|
||||
classification="internal",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def audit_event(
|
||||
session: Session,
|
||||
*,
|
||||
@@ -194,6 +251,7 @@ def audit_event(
|
||||
actor_id=user_id or api_key_id,
|
||||
payload={"scope": scope, "action": action, "object_type": object_type, "object_id": object_id},
|
||||
)
|
||||
_publish_audit_platform_event(item)
|
||||
if commit:
|
||||
session.commit()
|
||||
return item
|
||||
|
||||
Reference in New Issue
Block a user