Skip to content
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

deduplicate edges when conditions exist and add conditions to the edges #422

Merged
merged 6 commits into from
Feb 20, 2025

Conversation

yissellokta
Copy link
Contributor

Inconsistent behavior in the parser was deduplicating the edges if directly related to the terminal type but not in the case of a userset, causing the algorithms in the type system to not identify models that should have fast path

Description

The parser was deduplicating cases like the one below, where it only constructs one edge from group#member to user:
type group
define member: [user, user with condX]
In the case of a userset with conditions it was not behaving in the same way, and it was creating two edges from group#member to folder#viewer
type group
define member: [folder#viewer, folder#viewer with condX]
type folder
define viewer: [user]

This PR covers the following changes:
1- Add the deduplication for the userset case
2- rename conditionOn on the edge to have a name that represents its value, tupleUserset, in a ttu (X from Y), it is the Y
3- add conditions to the edge, to add more info required to substitute in the future the type system and use the graph
4- Add tests that cover all these changes

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@yissellokta yissellokta requested a review from a team as a code owner February 20, 2025 16:07
@elbuo8 elbuo8 enabled auto-merge February 20, 2025 22:16
@elbuo8 elbuo8 added this pull request to the merge queue Feb 20, 2025
@elbuo8 elbuo8 removed this pull request from the merge queue due to a manual request Feb 20, 2025
@elbuo8 elbuo8 added this pull request to the merge queue Feb 20, 2025
Merged via the queue into main with commit ed0cfba Feb 20, 2025
10 checks passed
@elbuo8 elbuo8 deleted the feat-add-conditions-to-graph branch February 20, 2025 22:30
@@ -83,7 +83,8 @@ func (g *AuthorizationModelGraph) Reversed() (*AuthorizationModelGraph, error) {
if !ok {
return nil, fmt.Errorf("%w: could not cast to AuthorizationModelEdge", ErrBuildingGraph)
}
graphBuilder.AddEdge(nextLine.To(), nextLine.From(), casted.edgeType, casted.conditionedOn)
newEdge := graphBuilder.AddEdge(nextLine.To(), nextLine.From(), casted.edgeType, casted.tuplesetRelation, "")
newEdge.conditions = casted.conditions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not graphBuilder.AddEdge(nextLine.To(), nextLine.From(), casted.edgeType, casted.tuplesetRelation, casted.conditions) ?

if graphBuilder.hasEdge(nodeSource, parentNode, TTUEdge, typeTuplesetRelation) {
// de-dup types that are conditioned, e.g. if define viewer: [user, user with condX]
// we only draw one edge from user to x#viewer
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call upsert? Otherwise, will be lose the condition?

type permission
relations
define assignee: [role#assignee, role#assignee with condX]
define member: [user, permission#member, permission#member with condX]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be easier to read if they line up :)

require.Len(t, edges, 1)
conditions := edges[0].conditions
require.Empty(t, edges[0].tuplesetRelation)
require.Len(t, conditions, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we assert on the conditions itself? (i.e., check the individual array item)

yissellokta added a commit that referenced this pull request Feb 21, 2025
github-merge-queue bot pushed a commit that referenced this pull request Feb 24, 2025
* feedback from github.com//pull/422

* Update pkg/go/graph/graph_builder.go

Co-authored-by: Adrian Tam <[email protected]>

---------

Co-authored-by: Adrian Tam <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants