2026-07-29 02:42:16 +02:00
|
|
|
"""ABAC policy routes — attribute-based access control management."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.db import get_db
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.deps import require_permission
|
2026-07-29 02:42:16 +02:00
|
|
|
from app.schemas.policy import PolicyCreate, PolicyUpdate
|
|
|
|
|
from app.services import policy_service
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/v1/policies", tags=["policies"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{entity_type}")
|
|
|
|
|
async def list_policies(
|
|
|
|
|
entity_type: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-08-23 18:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("policies:read")),
|
2026-07-29 02:42:16 +02:00
|
|
|
):
|
|
|
|
|
"""List all ABAC policies for a given entity type."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
try:
|
|
|
|
|
items = await policy_service.list_policies(db, tenant_id, entity_type)
|
|
|
|
|
except ValueError as e:
|
2026-08-16 01:17:18 +02:00
|
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
2026-07-29 02:42:16 +02:00
|
|
|
return {"items": items, "total": len(items)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", status_code=status.HTTP_201_CREATED)
|
|
|
|
|
async def create_policy(
|
|
|
|
|
body: PolicyCreate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-08-23 18:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("policies:write")),
|
2026-07-29 02:42:16 +02:00
|
|
|
):
|
|
|
|
|
"""Create a new ABAC policy."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
try:
|
|
|
|
|
return await policy_service.create_policy(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
name=body.name,
|
|
|
|
|
entity_type=body.entity_type,
|
|
|
|
|
principal_type=body.principal_type,
|
|
|
|
|
principal_id=body.principal_id,
|
|
|
|
|
effect=body.effect,
|
|
|
|
|
conditions=body.conditions,
|
|
|
|
|
priority=body.priority,
|
|
|
|
|
)
|
|
|
|
|
except ValueError as e:
|
2026-08-16 01:17:18 +02:00
|
|
|
raise HTTPException(status_code=400, detail=str(e)) from e
|
2026-07-29 02:42:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/{policy_id}")
|
|
|
|
|
async def update_policy(
|
|
|
|
|
policy_id: str,
|
|
|
|
|
body: PolicyUpdate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-08-23 18:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("policies:write")),
|
2026-07-29 02:42:16 +02:00
|
|
|
):
|
|
|
|
|
"""Update an existing ABAC policy."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
try:
|
|
|
|
|
return await policy_service.update_policy(
|
|
|
|
|
db,
|
|
|
|
|
tenant_id,
|
|
|
|
|
policy_id,
|
|
|
|
|
name=body.name,
|
|
|
|
|
entity_type=body.entity_type,
|
|
|
|
|
principal_type=body.principal_type,
|
|
|
|
|
principal_id=body.principal_id,
|
|
|
|
|
effect=body.effect,
|
|
|
|
|
conditions=body.conditions,
|
|
|
|
|
priority=body.priority,
|
|
|
|
|
enabled=body.enabled,
|
|
|
|
|
)
|
|
|
|
|
except ValueError as e:
|
2026-08-16 01:17:18 +02:00
|
|
|
raise HTTPException(status_code=404, detail=str(e)) from e
|
2026-07-29 02:42:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{policy_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
async def delete_policy(
|
|
|
|
|
policy_id: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-08-23 18:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("policies:write")),
|
2026-07-29 02:42:16 +02:00
|
|
|
):
|
|
|
|
|
"""Delete an ABAC policy."""
|
|
|
|
|
tenant_id = uuid.UUID(current_user["tenant_id"])
|
|
|
|
|
try:
|
|
|
|
|
await policy_service.delete_policy(db, tenant_id, policy_id)
|
|
|
|
|
except ValueError as e:
|
2026-08-16 01:17:18 +02:00
|
|
|
raise HTTPException(status_code=404, detail=str(e)) from e
|