-
Notifications
You must be signed in to change notification settings - Fork 178
feat(BA-6859): bind app_config fragments to their RBAC scope on write (repository layer) #12801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jopemachine
wants to merge
1
commit into
main
Choose a base branch
from
feat/app-config-fragment-rbac-write-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Bind AppConfig fragments to their RBAC scope on write (repository layer): a `user` / `domain` fragment is associated to its RBAC scope on create and unbound on purge (`public` maps to GLOBAL and carries none), so fragment write authorization can be enforced by RBAC. The allow list becomes a read allowlist (registration + `rank`) while writes move to RBAC (BEP-1052). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from ai.backend.common.data.permission.types import RBACElementType | ||
| from ai.backend.common.exception import BackendAIError | ||
| from ai.backend.common.identifier.app_config_fragment import AppConfigFragmentID | ||
| from ai.backend.common.metrics.metric import DomainType, LayerType | ||
|
|
@@ -25,19 +26,26 @@ | |
| from ai.backend.manager.models.app_config_fragment.conditions import AppConfigFragmentConditions | ||
| from ai.backend.manager.models.app_config_fragment.row import AppConfigFragmentRow | ||
| from ai.backend.manager.models.scopes import SearchScope | ||
| from ai.backend.manager.repositories.app_config_fragment.creators import ( | ||
| AppConfigFragmentCreatorSpec, | ||
| ) | ||
| from ai.backend.manager.repositories.app_config_fragment.scope_binders import ( | ||
| AppConfigFragmentScopeUnbinder, | ||
| fragment_rbac_scope_ref, | ||
| ) | ||
| from ai.backend.manager.repositories.app_config_fragment.types import ( | ||
| AppConfigScopeArguments, | ||
| ) | ||
| from ai.backend.manager.repositories.base import ( | ||
| BatchQuerier, | ||
| BulkCreator, | ||
| Creator, | ||
| NoPagination, | ||
| Purger, | ||
| Querier, | ||
| Updater, | ||
| ) | ||
| from ai.backend.manager.repositories.ops import DBOpsProvider | ||
| from ai.backend.manager.repositories.base.rbac.entity_creator import RBACEntityCreator | ||
| from ai.backend.manager.repositories.ops.rbac.provider import RBACOpsProvider | ||
|
|
||
| __all__ = ("AppConfigFragmentDBSource",) | ||
|
|
||
|
|
@@ -64,19 +72,27 @@ | |
| class AppConfigFragmentDBSource: | ||
| """Database source for app config fragment operations.""" | ||
|
|
||
| _ops: DBOpsProvider | ||
| _ops: RBACOpsProvider | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _rbac_ops_provider |
||
|
|
||
| def __init__(self, ops_provider: DBOpsProvider) -> None: | ||
| def __init__(self, ops_provider: RBACOpsProvider) -> None: | ||
| self._ops = ops_provider | ||
|
|
||
| @app_config_fragment_db_source_resilience.apply() | ||
| async def create(self, creator: Creator[AppConfigFragmentRow]) -> AppConfigFragmentData: | ||
| # The FK to the allow-list is the gate: inserting a fragment with no | ||
| # allow-list row for its ``(config_name, scope_type)`` raises | ||
| # ``AppConfigFragmentWriteNotAllowed`` (see the spec's integrity checks). | ||
| async def create(self, spec: AppConfigFragmentCreatorSpec) -> AppConfigFragmentData: | ||
| # The FK to the allow-list gates *existence* (a missing allow-list row for the | ||
| # ``(config_name, scope_type)`` raises ``AppConfigFragmentWriteNotAllowed``). Create | ||
| # also binds the fragment to its owning RBAC scope via | ||
| # ``association_scopes_entities`` (same tx) so a later update/purge can resolve its | ||
| # scope for the RBAC write check; a ``public`` fragment is global-scoped | ||
| # (``scope_ref`` is ``None``) and carries no association. | ||
| rbac_creator = RBACEntityCreator( | ||
| spec=spec, | ||
| element_type=RBACElementType.APP_CONFIG_FRAGMENT, | ||
| scope_ref=fragment_rbac_scope_ref(spec.scope_type, spec.scope_id), | ||
| ) | ||
| async with self._ops.write_ops() as w: | ||
| created = await w.create(creator) | ||
| return created.row.to_data() | ||
| created = await w.bulk_create_scoped([rbac_creator]) | ||
| return created.rows[0].to_data() | ||
|
|
||
| @app_config_fragment_db_source_resilience.apply() | ||
| async def get_by_id(self, fragment_id: AppConfigFragmentID) -> AppConfigFragmentData: | ||
|
|
@@ -99,12 +115,23 @@ async def update(self, updater: Updater[AppConfigFragmentRow]) -> AppConfigFragm | |
|
|
||
| @app_config_fragment_db_source_resilience.apply() | ||
| async def purge(self, purger: Purger[AppConfigFragmentRow]) -> AppConfigFragmentData: | ||
| # No write-gate here — see ``update``. | ||
| # A fragment is a config bound at a scope, so purging is an RBAC *unbind*: the row | ||
| # and its scope association are deleted atomically (the association table has no FK | ||
| # cascade). A public fragment is global-scoped and simply has no association. The | ||
| # row is fetched first to resolve its scope and to return its data. | ||
| async with self._ops.write_ops() as w: | ||
| result = await w.purge(purger) | ||
| if result is None: | ||
| found = await w.query(Querier(row_class=AppConfigFragmentRow, pk_value=purger.pk_value)) | ||
| if found is None: | ||
| raise AppConfigFragmentNotFound(f"App config fragment {purger.pk_value} not found") | ||
| return result.row.to_data() | ||
| data = found.row.to_data() | ||
| await w.unbind_scope_entities( | ||
| AppConfigFragmentScopeUnbinder( | ||
| fragment_id=data.id, | ||
| fragment_scope_type=data.scope_type, | ||
| fragment_scope_id=data.scope_id, | ||
| ) | ||
| ) | ||
|
Comment on lines
+127
to
+133
|
||
| return data | ||
|
|
||
| @app_config_fragment_db_source_resilience.apply() | ||
| async def bulk_create( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/ai/backend/manager/repositories/app_config_fragment/scope_binders.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """RBAC scope binding helpers for app config fragments.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from dataclasses import dataclass | ||
| from typing import override | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from ai.backend.common.data.app_config.types import AppConfigScopeType | ||
| from ai.backend.common.data.permission.types import RBACElementType | ||
| from ai.backend.common.identifier.app_config_fragment import AppConfigFragmentID | ||
| from ai.backend.manager.data.permission.types import RBACElementRef | ||
| from ai.backend.manager.models.app_config_fragment.row import AppConfigFragmentRow | ||
| from ai.backend.manager.repositories.base.purger import BatchPurgerSpec | ||
| from ai.backend.manager.repositories.base.rbac.scope_unbinder import RBACScopeEntityUnbinder | ||
|
|
||
|
|
||
| def fragment_rbac_scope_ref(scope_type: AppConfigScopeType, scope_id: str) -> RBACElementRef | None: | ||
| """The RBAC scope a fragment belongs to; ``None`` for global-scoped (public) fragments.""" | ||
| element = scope_type.to_rbac_element_type() | ||
| if element is None: | ||
| return None | ||
| return RBACElementRef(element, scope_id) | ||
|
|
||
|
|
||
| @dataclass | ||
| class AppConfigFragmentByIdPurgerSpec(BatchPurgerSpec[AppConfigFragmentRow]): | ||
| """Select a single fragment row (by id) for deletion.""" | ||
|
|
||
| fragment_id: AppConfigFragmentID | ||
|
|
||
| @override | ||
| def build_subquery(self) -> sa.sql.Select[tuple[AppConfigFragmentRow]]: | ||
| return sa.select(AppConfigFragmentRow).where(AppConfigFragmentRow.id == self.fragment_id) | ||
|
|
||
|
|
||
| @dataclass | ||
| class AppConfigFragmentScopeUnbinder(RBACScopeEntityUnbinder[AppConfigFragmentRow]): | ||
| """Unbind (purge) one fragment from its owning scope. | ||
|
|
||
| A fragment is a config bound at a scope, so purging it is an unbind: the fragment row | ||
| and its RBAC scope association are deleted atomically. A ``public`` fragment is | ||
| global-scoped (``scope_ref`` is ``None``), so only its row is deleted — it never had | ||
| an association. | ||
| """ | ||
|
|
||
| fragment_id: AppConfigFragmentID | ||
| fragment_scope_type: AppConfigScopeType | ||
| fragment_scope_id: str | ||
|
|
||
| @override | ||
| def build_purger_spec(self) -> BatchPurgerSpec[AppConfigFragmentRow]: | ||
| return AppConfigFragmentByIdPurgerSpec(fragment_id=self.fragment_id) | ||
|
|
||
| @property | ||
| @override | ||
| def entity_type(self) -> RBACElementType: | ||
| return RBACElementType.APP_CONFIG_FRAGMENT | ||
|
|
||
| @property | ||
| @override | ||
| def scope_ref(self) -> RBACElementRef | None: | ||
| return fragment_rbac_scope_ref(self.fragment_scope_type, self.fragment_scope_id) | ||
|
|
||
| @property | ||
| @override | ||
| def entity_ids(self) -> Sequence[str] | None: | ||
| return [str(self.fragment_id)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too verbose