Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /api /routes /pqc_admin.py
| """ | |
| ACGS-2 Enhanced Agent Bus - PQC Admin Routes | |
| Constitutional Hash: 608508a9bd224290 | |
| Provides admin endpoints for managing the PQC enforcement mode: | |
| PATCH /api/v1/admin/pqc-enforcement - change mode (strict | permissive) | |
| GET /api/v1/admin/pqc-enforcement - read current mode | |
| Authorization: platform-operator or tenant-admin JWT role required. | |
| """ | |
| from __future__ import annotations | |
| from datetime import UTC, datetime | |
| from typing import TYPE_CHECKING, Annotated, Any, cast | |
| from fastapi import APIRouter, Depends, HTTPException, Query, Request, status | |
| from fastapi.security import HTTPAuthorizationCredentials | |
| from ...pqc_enforcement_config import EnforcementModeConfigService, StorageUnavailableError | |
| from ...pqc_enforcement_models import EnforcementModeRequest, EnforcementModeResponse | |
| from ..rate_limiting import limiter | |
| if TYPE_CHECKING: | |
| from enhanced_agent_bus._compat.security.auth import get_current_user | |
| else: | |
| try: | |
| from enhanced_agent_bus._compat.security.auth import get_current_user as _get_current_user | |
| except ImportError: # pragma: no cover - fallback for isolated test runs | |
| async def _get_current_user( | |
| credentials: HTTPAuthorizationCredentials = cast(HTTPAuthorizationCredentials, ...), | |
| ) -> Any: | |
| del credentials | |
| raise HTTPException(status_code=401, detail="Auth not configured") | |
| get_current_user = cast(Any, _get_current_user) | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| logger = get_logger(__name__) | |
| router = APIRouter(tags=["PQC Admin"]) | |
| # Roles authorised to change/read the enforcement mode | |
| _ADMIN_ROLES = frozenset({"platform-operator", "tenant-admin"}) | |
| PROPAGATION_DEADLINE_SECONDS = 60 | |
| # --------------------------------------------------------------------------- | |
| # Dependency: enforcement service singleton | |
| # --------------------------------------------------------------------------- | |
| def get_enforcement_service(request: Request) -> EnforcementModeConfigService: | |
| """FastAPI dependency that returns the shared enforcement-mode service.""" | |
| service = getattr(request.app.state, "pqc_enforcement_service", None) | |
| if isinstance(service, EnforcementModeConfigService): | |
| return service | |
| raise HTTPException( | |
| status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | |
| detail="PQC enforcement config service is not initialised", | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Auth helper | |
| # --------------------------------------------------------------------------- | |
| def _require_admin(user: Any) -> None: | |
| """Raise HTTP 403 if the user does not hold an admin role.""" | |
| if not any(role in _ADMIN_ROLES for role in (user.roles or [])): | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="platform-operator or tenant-admin role required", | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Endpoints | |
| # --------------------------------------------------------------------------- | |
| async def patch_pqc_enforcement( | |
| request: Request, | |
| body: EnforcementModeRequest, | |
| user: Annotated[Any, Depends(get_current_user)], | |
| svc: Annotated[EnforcementModeConfigService, Depends(get_enforcement_service)], | |
| ) -> EnforcementModeResponse: | |
| """Change the PQC enforcement mode (strict | permissive). | |
| Requires: platform-operator or tenant-admin JWT role. | |
| Returns the new mode with server-set activation timestamp and | |
| propagation deadline. | |
| """ | |
| _require_admin(user) | |
| if body.mode == "permissive": | |
| logger.warning( | |
| "PQC enforcement mode set to permissive - classical keys will be accepted", | |
| extra={"activated_by": user.sub, "scope": body.scope}, | |
| ) | |
| try: | |
| await svc.set_mode( | |
| mode=body.mode, | |
| scope=body.scope, | |
| activated_by=user.sub, | |
| ) | |
| except StorageUnavailableError as exc: | |
| logger.error( | |
| "Storage unavailable while changing PQC enforcement mode", | |
| extra={"error": str(exc), "activated_by": user.sub}, | |
| ) | |
| raise HTTPException( | |
| status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | |
| detail="Enforcement mode storage unavailable; change could not be persisted", | |
| ) from exc | |
| return EnforcementModeResponse( | |
| mode=body.mode, | |
| activated_at=datetime.now(tz=UTC), | |
| activated_by=user.sub, | |
| scope=body.scope, | |
| propagation_deadline_seconds=PROPAGATION_DEADLINE_SECONDS, | |
| ) | |
| async def get_pqc_enforcement( | |
| request: Request, | |
| user: Annotated[Any, Depends(get_current_user)], | |
| svc: Annotated[EnforcementModeConfigService, Depends(get_enforcement_service)], | |
| scope: str = Query(default="global", description="Enforcement scope"), | |
| ) -> EnforcementModeResponse: | |
| """Return the current PQC enforcement mode and metadata. | |
| Requires: platform-operator or tenant-admin JWT role. | |
| """ | |
| _require_admin(user) | |
| try: | |
| mode = await svc.get_mode(scope=scope) | |
| except StorageUnavailableError as exc: | |
| logger.error( | |
| "Storage unavailable while reading PQC enforcement mode", | |
| extra={"error": str(exc), "scope": scope}, | |
| ) | |
| raise HTTPException( | |
| status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | |
| detail="Enforcement mode storage unavailable", | |
| ) from exc | |
| return EnforcementModeResponse( | |
| mode=mode, | |
| activated_at=datetime.now(tz=UTC), | |
| activated_by="system", | |
| scope=scope, | |
| propagation_deadline_seconds=PROPAGATION_DEADLINE_SECONDS, | |
| ) | |
| __all__ = ["get_enforcement_service", "router"] | |
Xet Storage Details
- Size:
- 6.17 kB
- Xet hash:
- e988de343c02ff6f5a186e1c454c0d2a1616fb3f47eea153396f386b43913b2d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.