infrasynth-backend-kit/infrasynth/audit/views.py
jcv-dev 551b42eab5 feat: production-hardening pass across the kit
Close the gaps between the documented contract (API-STANDARD, TENANCY,
ENTITLEMENTS) and the implementation, and remove committed build artifacts.

Security:
- verify + process inbound webhooks (HMAC/handler verify, size limit,
  timestamp tolerance, idempotency via InboundEvent.external_id)
- real 2FA login flow (pre-auth challenge; tokens only after verify/recovery)
- wire HybridPermission into security/audit views; add API-key rotate and
  users/<id>/permissions|roles endpoints
- tenant-scoped throttling on by default; webhook replay protection
- verify MercadoPago webhook signatures
- login brute-force guard, configurable password policy, real ALTCHA PoW

Correctness:
- apply verified billing webhooks idempotently (subscription/entitlement/
  invoice/PaymentTransaction); scheduled payment lifecycle jobs
- capture audit update diffs automatically; add audit retention purge
- working notification retries, per-channel rate limits, log retention
- pluggable virus scanner, upload-size limit, pipeline toggle
- feature rollout %/environment targeting; settings-driven registrations
- workflow guards (instance cap, route depth, self-assignment, clone on re-entry)
- wire every previously-dead INFRASYNTH_* setting; drop truly dead ones

Delivery:
- README + CHANGELOG; CI format check + coverage gate
- keep test media out of the tree; untrack .coverage, __pycache__,
  egg-info, docs/ and invoice artifacts
2026-09-24 10:41:21 -05:00

65 lines
2.3 KiB
Python

from rest_framework import mixins, viewsets
from infrasynth.security.permissions import IsAuthenticatedAndPermitted
from infrasynth.tenancy.context import get_current_tenant
from .filters import APIInteractionLogFilter, ModelChangeLogFilter, SecurityEventFilter
from .models import APIInteractionLog, ModelChangeLog, SecurityEvent
from .serializers import (
APIInteractionLogSerializer,
ModelChangeLogSerializer,
SecurityEventSerializer,
)
class _AuditScopedViewSet:
"""Audit rows are tenant-filtered for tenants; platform staff see all."""
def _scoped(self, model):
from infrasynth.tenancy.services import TenantService
qs = model.objects.select_related("actor").order_by("-timestamp")
user = getattr(self.request, "user", None)
if user is not None and getattr(user, "is_authenticated", False):
if getattr(user, "is_superuser", False) or TenantService().is_platform_staff(user):
return qs
tenant = get_current_tenant()
if tenant is None:
return qs.none()
return qs.filter(tenant_id=tenant.pk)
class ModelChangeLogViewSet(
_AuditScopedViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
):
serializer_class = ModelChangeLogSerializer
permission_classes = [IsAuthenticatedAndPermitted]
required_permissions = ["audit.view_model_changes"]
filterset_class = ModelChangeLogFilter
def get_queryset(self):
return self._scoped(ModelChangeLog)
class APIInteractionLogViewSet(
_AuditScopedViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
):
serializer_class = APIInteractionLogSerializer
permission_classes = [IsAuthenticatedAndPermitted]
required_permissions = ["audit.view_api_logs"]
filterset_class = APIInteractionLogFilter
def get_queryset(self):
return self._scoped(APIInteractionLog)
class SecurityEventViewSet(
_AuditScopedViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
):
serializer_class = SecurityEventSerializer
permission_classes = [IsAuthenticatedAndPermitted]
required_permissions = ["audit.view_security_events"]
filterset_class = SecurityEventFilter
def get_queryset(self):
return self._scoped(SecurityEvent)