|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Iterable |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Generic, TypeVar |
| 6 | +from uuid import UUID |
| 7 | + |
| 8 | +import sqlalchemy as sa |
| 9 | +from sqlalchemy.ext.asyncio import AsyncSession as SASession |
| 10 | +from sqlalchemy.orm import ( |
| 11 | + contains_eager, |
| 12 | + selectinload, |
| 13 | + with_loader_criteria, |
| 14 | +) |
| 15 | + |
| 16 | +from ai.backend.manager.data.permission.id import ( |
| 17 | + ObjectId, |
| 18 | + ScopeId, |
| 19 | +) |
| 20 | +from ai.backend.manager.errors.repository import UnsupportedCompositePrimaryKeyError |
| 21 | +from ai.backend.manager.models.base import Base |
| 22 | +from ai.backend.manager.models.rbac_models.association_scopes_entities import ( |
| 23 | + AssociationScopesEntitiesRow, |
| 24 | +) |
| 25 | +from ai.backend.manager.models.rbac_models.permission.object_permission import ObjectPermissionRow |
| 26 | +from ai.backend.manager.models.rbac_models.permission.permission import PermissionRow |
| 27 | +from ai.backend.manager.models.rbac_models.permission.permission_group import PermissionGroupRow |
| 28 | +from ai.backend.manager.models.rbac_models.role import RoleRow |
| 29 | + |
| 30 | +TRow = TypeVar("TRow", bound=Base) |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class Purger(Generic[TRow]): |
| 35 | + """Single-row delete by primary key. |
| 36 | +
|
| 37 | + Attributes: |
| 38 | + row_class: ORM class for table access and PK detection. |
| 39 | + pk_value: Primary key value to identify the target row. |
| 40 | + """ |
| 41 | + |
| 42 | + row_class: type[TRow] |
| 43 | + pk_value: UUID | str | int |
| 44 | + entity_id: ObjectId |
| 45 | + |
| 46 | + |
| 47 | +@dataclass |
| 48 | +class PurgerResult(Generic[TRow]): |
| 49 | + """Result of executing a single-row delete operation.""" |
| 50 | + |
| 51 | + row: TRow |
| 52 | + |
| 53 | + |
| 54 | +async def _get_association_rows( |
| 55 | + db_sess: SASession, |
| 56 | + object_id: ObjectId, |
| 57 | +) -> list[AssociationScopesEntitiesRow]: |
| 58 | + assoc_scalars = await db_sess.scalars( |
| 59 | + sa.select(AssociationScopesEntitiesRow).where( |
| 60 | + sa.and_( |
| 61 | + AssociationScopesEntitiesRow.entity_id == object_id.entity_id, |
| 62 | + AssociationScopesEntitiesRow.entity_type == object_id.entity_type, |
| 63 | + ) |
| 64 | + ) |
| 65 | + ) |
| 66 | + return assoc_scalars.all() |
| 67 | + |
| 68 | + |
| 69 | +async def _get_related_roles( |
| 70 | + db_sess: SASession, |
| 71 | + object_id: ObjectId, |
| 72 | + scopes: list[ScopeId], |
| 73 | +) -> list[RoleRow]: |
| 74 | + role_scalars = await db_sess.scalars( |
| 75 | + sa.select(RoleRow) |
| 76 | + .select_from( |
| 77 | + sa.join(RoleRow, ObjectPermissionRow, RoleRow.id == ObjectPermissionRow.role_id) |
| 78 | + ) |
| 79 | + .where( |
| 80 | + sa.and_( |
| 81 | + ObjectPermissionRow.entity_id == object_id.entity_id, |
| 82 | + ObjectPermissionRow.entity_type == object_id.entity_type, |
| 83 | + ) |
| 84 | + ) |
| 85 | + .options( |
| 86 | + contains_eager(RoleRow.object_permission_rows), |
| 87 | + selectinload(RoleRow.permission_group_rows), |
| 88 | + with_loader_criteria( |
| 89 | + PermissionGroupRow, |
| 90 | + sa.and_( |
| 91 | + sa.not_( |
| 92 | + sa.exists( |
| 93 | + sa.select(PermissionRow.id).where( |
| 94 | + PermissionRow.permission_group_id == PermissionGroupRow.id |
| 95 | + ) |
| 96 | + ) |
| 97 | + ), |
| 98 | + PermissionGroupRow.scope_id.in_([scope.scope_id for scope in scopes]), # type: ignore[attr-defined] |
| 99 | + PermissionGroupRow.scope_type.in_([scope.scope_type for scope in scopes]), # type: ignore[attr-defined] |
| 100 | + ), |
| 101 | + ), |
| 102 | + ) |
| 103 | + ) |
| 104 | + return role_scalars.all() |
| 105 | + |
| 106 | + |
| 107 | +async def _purge_related_rows( |
| 108 | + db_sess: SASession, |
| 109 | + object_permission_ids: Iterable[UUID], |
| 110 | + permission_group_ids: Iterable[UUID], |
| 111 | + association_ids: Iterable[UUID], |
| 112 | +) -> None: |
| 113 | + await db_sess.execute( |
| 114 | + sa.delete(ObjectPermissionRow).where(ObjectPermissionRow.id.in_(object_permission_ids)) # type: ignore[attr-defined] |
| 115 | + ) |
| 116 | + await db_sess.execute( |
| 117 | + sa.delete(PermissionGroupRow).where(PermissionGroupRow.id.in_(permission_group_ids)) # type: ignore[attr-defined] |
| 118 | + ) |
| 119 | + await db_sess.execute( |
| 120 | + sa.delete(AssociationScopesEntitiesRow).where( |
| 121 | + AssociationScopesEntitiesRow.id.in_(association_ids) # type: ignore[attr-defined] |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +async def execute_purger( |
| 127 | + db_sess: SASession, |
| 128 | + purger: Purger[TRow], |
| 129 | +) -> PurgerResult[TRow] | None: |
| 130 | + row_class = purger.row_class |
| 131 | + table = row_class.__table__ # type: ignore[attr-defined] |
| 132 | + pk_columns = list(table.primary_key.columns) |
| 133 | + |
| 134 | + if len(pk_columns) != 1: |
| 135 | + raise UnsupportedCompositePrimaryKeyError( |
| 136 | + f"Purger only supports single-column primary keys (table: {table.name})", |
| 137 | + ) |
| 138 | + |
| 139 | + scopes: list[ScopeId] = [] |
| 140 | + object_id = purger.entity_id |
| 141 | + object_permission_ids: list[UUID] = [] |
| 142 | + permission_group_ids: list[UUID] = [] |
| 143 | + association_ids: list[UUID] = [] |
| 144 | + |
| 145 | + assoc_rows = await _get_association_rows(db_sess, object_id) |
| 146 | + for assoc_row in assoc_rows: |
| 147 | + association_ids.append(assoc_row.id) |
| 148 | + scopes.append(assoc_row.parsed_scope_id()) |
| 149 | + |
| 150 | + # Check all roles associated with the entity as object permission |
| 151 | + role_rows = await _get_related_roles(db_sess, object_id, scopes) |
| 152 | + for role_row in role_rows: |
| 153 | + for obj_perm_row in role_row.object_permission_rows: |
| 154 | + object_permission_ids.append(obj_perm_row.id) |
| 155 | + for perm_group_row in role_row.permission_group_rows: |
| 156 | + permission_group_ids.append(perm_group_row.id) |
| 157 | + await _purge_related_rows( |
| 158 | + db_sess, |
| 159 | + object_permission_ids, |
| 160 | + permission_group_ids, |
| 161 | + association_ids, |
| 162 | + ) |
| 163 | + stmt = sa.delete(table).where(pk_columns[0] == purger.pk_value).returning(*table.columns) |
| 164 | + |
| 165 | + result = await db_sess.execute(stmt) |
| 166 | + row_data = result.fetchone() |
| 167 | + |
| 168 | + if row_data is None: |
| 169 | + return None |
| 170 | + |
| 171 | + deleted_row: TRow = row_class(**dict(row_data._mapping)) |
| 172 | + return PurgerResult(row=deleted_row) |
0 commit comments