-
Notifications
You must be signed in to change notification settings - Fork 173
feat(BA-4808): define RBAC scope-entity combination constants #9544
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d717f75
feat(BA-4808): define RBAC scope-entity combination constants
fregataa bbbdfe3
changelog: add news fragment for PR #9544
fregataa 6d0e174
feat(BA-4808): simplify scope-entity combinations and add entity-leve…
fregataa 7f0adf9
changelog: update news fragment for PR #9544
fregataa 89448ab
feat(BA-4808): add Session, ModelDeployment scopes and StorageHost to…
fregataa 6f8c894
refactor(BA-4808): use Mapping[..., frozenset] for immutable scope-en…
fregataa bf05bd8
fix(BA-4808): add AGENT, KERNEL, ROUTING to exhaustive match arms
fregataa 1fbd054
changelog: simplify news fragment wording
fregataa 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 @@ | ||
| Add RBAC scope-entity combination constants based on BEP-1048 entity-edge-catalog. |
60 changes: 60 additions & 0 deletions
60
src/ai/backend/common/data/permission/scope_entity_combinations.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,60 @@ | ||
| """Valid RBAC scope-entity combinations based on BEP-1048/entity-edge-catalog.md. | ||
|
|
||
| This module is the single source of truth for which entity types are valid | ||
| under each scope type. It is used for: | ||
| - Frontend UI filtering (which entity types to show for a given scope) | ||
| - Server-side validation (reject invalid combinations) | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
|
|
||
| from ai.backend.common.data.permission.types import RBACElementType | ||
|
|
||
| VALID_SCOPE_ENTITY_COMBINATIONS: Mapping[RBACElementType, frozenset[RBACElementType]] = { | ||
| RBACElementType.DOMAIN: frozenset({ | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.CONTAINER_REGISTRY, | ||
| RBACElementType.USER, | ||
| RBACElementType.PROJECT, | ||
| RBACElementType.NETWORK, | ||
| RBACElementType.STORAGE_HOST, | ||
| }), | ||
| RBACElementType.PROJECT: frozenset({ | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.CONTAINER_REGISTRY, | ||
| RBACElementType.SESSION, | ||
| RBACElementType.VFOLDER, | ||
| RBACElementType.DEPLOYMENT, | ||
| RBACElementType.NETWORK, | ||
| RBACElementType.USER, | ||
| RBACElementType.STORAGE_HOST, | ||
| }), | ||
| RBACElementType.USER: frozenset({ | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.SESSION, | ||
| RBACElementType.VFOLDER, | ||
| RBACElementType.DEPLOYMENT, | ||
| RBACElementType.KEYPAIR, | ||
| }), | ||
| RBACElementType.RESOURCE_GROUP: frozenset({ | ||
| RBACElementType.AGENT, | ||
| }), | ||
| RBACElementType.AGENT: frozenset({ | ||
| RBACElementType.KERNEL, | ||
| }), | ||
| RBACElementType.SESSION: frozenset({ | ||
| RBACElementType.KERNEL, | ||
| }), | ||
| RBACElementType.MODEL_DEPLOYMENT: frozenset({ | ||
| RBACElementType.ROUTING, | ||
| RBACElementType.SESSION, | ||
| }), | ||
| RBACElementType.CONTAINER_REGISTRY: frozenset({ | ||
| RBACElementType.IMAGE, | ||
| }), | ||
| RBACElementType.STORAGE_HOST: frozenset({ | ||
| RBACElementType.VFOLDER, | ||
| }), | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python_tests() |
115 changes: 115 additions & 0 deletions
115
tests/common/data/permission/test_scope_entity_combinations.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,115 @@ | ||
| """Tests for RBAC scope-entity combination constants.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from ai.backend.common.data.permission.scope_entity_combinations import ( | ||
| VALID_SCOPE_ENTITY_COMBINATIONS, | ||
| ) | ||
| from ai.backend.common.data.permission.types import RBACElementType | ||
|
|
||
|
|
||
| class TestValidScopeEntityCombinations: | ||
| """Tests for the VALID_SCOPE_ENTITY_COMBINATIONS constant.""" | ||
|
|
||
| def test_domain_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.DOMAIN] | ||
| assert entities == { | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.CONTAINER_REGISTRY, | ||
| RBACElementType.USER, | ||
| RBACElementType.PROJECT, | ||
| RBACElementType.NETWORK, | ||
| RBACElementType.STORAGE_HOST, | ||
| } | ||
|
|
||
| def test_project_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.PROJECT] | ||
| assert entities == { | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.CONTAINER_REGISTRY, | ||
| RBACElementType.SESSION, | ||
| RBACElementType.VFOLDER, | ||
| RBACElementType.DEPLOYMENT, | ||
| RBACElementType.NETWORK, | ||
| RBACElementType.USER, | ||
| RBACElementType.STORAGE_HOST, | ||
| } | ||
|
|
||
| def test_user_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.USER] | ||
| assert entities == { | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.SESSION, | ||
| RBACElementType.VFOLDER, | ||
| RBACElementType.DEPLOYMENT, | ||
| RBACElementType.KEYPAIR, | ||
| } | ||
|
|
||
| def test_resource_group_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.RESOURCE_GROUP] | ||
| assert entities == { | ||
| RBACElementType.AGENT, | ||
| } | ||
|
|
||
| def test_agent_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.AGENT] | ||
| assert entities == { | ||
| RBACElementType.KERNEL, | ||
| } | ||
|
|
||
| def test_session_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.SESSION] | ||
| assert entities == { | ||
| RBACElementType.KERNEL, | ||
| } | ||
|
|
||
| def test_model_deployment_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.MODEL_DEPLOYMENT] | ||
| assert entities == { | ||
| RBACElementType.ROUTING, | ||
| RBACElementType.SESSION, | ||
| } | ||
|
|
||
| def test_container_registry_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.CONTAINER_REGISTRY] | ||
| assert entities == { | ||
| RBACElementType.IMAGE, | ||
| } | ||
|
|
||
| def test_storage_host_scope_entities(self) -> None: | ||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS[RBACElementType.STORAGE_HOST] | ||
| assert entities == { | ||
| RBACElementType.VFOLDER, | ||
| } | ||
|
|
||
| def test_scope_keys(self) -> None: | ||
| expected_scopes = { | ||
| RBACElementType.DOMAIN, | ||
| RBACElementType.PROJECT, | ||
| RBACElementType.USER, | ||
| RBACElementType.RESOURCE_GROUP, | ||
| RBACElementType.AGENT, | ||
| RBACElementType.SESSION, | ||
| RBACElementType.MODEL_DEPLOYMENT, | ||
| RBACElementType.CONTAINER_REGISTRY, | ||
| RBACElementType.STORAGE_HOST, | ||
| } | ||
| assert set(VALID_SCOPE_ENTITY_COMBINATIONS.keys()) == expected_scopes | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("scope", "entity"), | ||
| [ | ||
| (RBACElementType.DOMAIN, RBACElementType.SESSION), | ||
| (RBACElementType.DOMAIN, RBACElementType.KEYPAIR), | ||
| (RBACElementType.PROJECT, RBACElementType.KEYPAIR), | ||
| (RBACElementType.USER, RBACElementType.PROJECT), | ||
| (RBACElementType.USER, RBACElementType.CONTAINER_REGISTRY), | ||
| (RBACElementType.SESSION, RBACElementType.VFOLDER), | ||
| (RBACElementType.SESSION, RBACElementType.SESSION), | ||
| ], | ||
| ) | ||
| def test_invalid_combinations(self, scope: RBACElementType, entity: RBACElementType) -> None: | ||
|
Member
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. I'm not sure whether parametrize is necessary for this test, since there can be virtually unlimited invalid combinations anyway. Unless there is a specific reason to test all combinations separately. |
||
| entities = VALID_SCOPE_ENTITY_COMBINATIONS.get(scope, frozenset()) | ||
| assert entity not in entities | ||
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.
Uh oh!
There was an error while loading. Please reload this page.