-
Notifications
You must be signed in to change notification settings - Fork 36
feat: optimize aliased ListRelations checks #310
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
Siddhant-K-code
wants to merge
5
commits into
openfga:main
Choose a base branch
from
Siddhant-K-code:feat/optimize-list-relations-aliases
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.
+1,204
−3
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
487c9b3
feat: optimize aliased ListRelations checks
Siddhant-K-code 25d3201
test: cover ListRelations optimization edge cases
Siddhant-K-code 0de3ad5
fix: address ListRelations review feedback
Siddhant-K-code 34b6d90
fix: validate optimization and clean up model loads
Siddhant-K-code 8be9c7d
test: cover relation alias cache validation
Siddhant-K-code 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
Some comments aren't visible on the classic Files Changed page.
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
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,102 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from openfga_sdk.models.authorization_model import AuthorizationModel | ||
| from openfga_sdk.models.userset import Userset | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class RelationCheckGroup: | ||
| """Relations that can share a single authorization check.""" | ||
|
|
||
| relation: str | ||
| indexes: tuple[int, ...] | ||
|
|
||
|
|
||
| def is_concrete_user(user: str) -> bool: | ||
| """Return whether a user string represents one concrete object.""" | ||
| return user != "*" and not user.endswith(":*") and "#" not in user | ||
|
|
||
|
|
||
| def build_relation_aliases( | ||
| authorization_model: AuthorizationModel, | ||
| ) -> dict[str, dict[str, str]]: | ||
| """Build canonical targets for pure computed-userset relation aliases.""" | ||
| aliases_by_type: dict[str, dict[str, str]] = {} | ||
|
|
||
| for type_definition in authorization_model.type_definitions or []: | ||
| relations = type_definition.relations or {} | ||
| direct_aliases = { | ||
| relation: target | ||
| for relation, rewrite in relations.items() | ||
| if (target := _pure_computed_userset_target(rewrite)) is not None | ||
| and target in relations | ||
| } | ||
|
|
||
| canonical_aliases: dict[str, str] = {} | ||
| for relation in direct_aliases: | ||
| target = _resolve_alias(relation, direct_aliases) | ||
| if target is not None and target != relation: | ||
| canonical_aliases[relation] = target | ||
|
|
||
| aliases_by_type[type_definition.type] = canonical_aliases | ||
|
|
||
| return aliases_by_type | ||
|
|
||
|
|
||
| def group_relations( | ||
| relations: list[str], aliases: dict[str, str] | ||
| ) -> list[RelationCheckGroup]: | ||
| """Group requested relations by their canonical evaluation target.""" | ||
| indexes_by_target: dict[str, list[int]] = {} | ||
| for index, relation in enumerate(relations): | ||
| target = aliases.get(relation, relation) | ||
| indexes_by_target.setdefault(target, []).append(index) | ||
|
|
||
| groups = [] | ||
| for target, indexes in indexes_by_target.items(): | ||
| submitted_relation = target if len(indexes) > 1 else relations[indexes[0]] | ||
| groups.append( | ||
| RelationCheckGroup( | ||
| relation=submitted_relation, | ||
| indexes=tuple(indexes), | ||
| ) | ||
| ) | ||
| return groups | ||
|
|
||
|
|
||
| def _pure_computed_userset_target(rewrite: Userset) -> str | None: | ||
| """Return the target when a rewrite is only a same-object computed userset.""" | ||
| computed_userset = rewrite.computed_userset | ||
| if computed_userset is None or not computed_userset.relation: | ||
| return None | ||
|
|
||
| if any( | ||
| value is not None | ||
| for value in ( | ||
| rewrite.this, | ||
| rewrite.tuple_to_userset, | ||
| rewrite.union, | ||
| rewrite.intersection, | ||
| rewrite.difference, | ||
| ) | ||
| ): | ||
| return None | ||
|
|
||
| if computed_userset.object not in (None, ""): | ||
| return None | ||
|
|
||
| return computed_userset.relation | ||
|
|
||
|
|
||
| def _resolve_alias(relation: str, direct_aliases: dict[str, str]) -> str | None: | ||
| """Resolve an alias chain, returning none when it contains a cycle.""" | ||
| visited = set() | ||
| current = relation | ||
|
|
||
| while current in direct_aliases: | ||
| if current in visited: | ||
| return None | ||
| visited.add(current) | ||
| current = direct_aliases[current] | ||
|
|
||
| return current |
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.