-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1cf8952
deduplicate edges when conditions exist and add conditions to the edges
yissellokta e64637f
add more comments
yissellokta 51a589d
Update pkg/go/graph/graph_builder.go
yissellokta e1de71c
Update pkg/go/graph/weighted_graph_edge.go
yissellokta 5773124
Update pkg/go/graph/weighted_graph.go
yissellokta b6c7161
fix lint errors
yissellokta 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 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 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 |
---|---|---|
|
@@ -46,7 +46,7 @@ func parseModel(model *openfgav1.AuthorizationModel) (*multi.DirectedGraph, Node | |
}) | ||
|
||
for _, typeDef := range sortedTypeDefs { | ||
graphBuilder.GetOrAddNode(typeDef.GetType(), typeDef.GetType(), SpecificType) | ||
graphBuilder.getOrAddNode(typeDef.GetType(), typeDef.GetType(), SpecificType) | ||
|
||
// sort relations by name to guarantee stable output | ||
sortedRelations := make([]string, 0, len(typeDef.GetRelations())) | ||
|
@@ -58,7 +58,7 @@ func parseModel(model *openfgav1.AuthorizationModel) (*multi.DirectedGraph, Node | |
|
||
for _, relation := range sortedRelations { | ||
uniqueLabel := fmt.Sprintf("%s#%s", typeDef.GetType(), relation) | ||
parentNode := graphBuilder.GetOrAddNode(uniqueLabel, uniqueLabel, SpecificTypeAndRelation) | ||
parentNode := graphBuilder.getOrAddNode(uniqueLabel, uniqueLabel, SpecificTypeAndRelation) | ||
rewrite := typeDef.GetRelations()[relation] | ||
checkRewrite(graphBuilder, parentNode, model, rewrite, typeDef, relation) | ||
} | ||
|
@@ -107,11 +107,11 @@ func checkRewrite(graphBuilder *AuthorizationModelGraphBuilder, parentNode *Auth | |
} | ||
|
||
operatorNode := fmt.Sprintf("%s:%s", operator, ulid.Make().String()) | ||
operatorNodeParent := graphBuilder.GetOrAddNode(operatorNode, operator, OperatorNode) | ||
operatorNodeParent := graphBuilder.getOrAddNode(operatorNode, operator, OperatorNode) | ||
|
||
// add one edge "operator" -> "relation that defined the operator" | ||
// Note: if this is a composition of operators, operationNode will be nil and this edge won't be added. | ||
graphBuilder.AddEdge(operatorNodeParent, parentNode, RewriteEdge, "") | ||
graphBuilder.AddEdge(operatorNodeParent, parentNode, RewriteEdge, "", "") | ||
for _, child := range children { | ||
checkRewrite(graphBuilder, operatorNodeParent, model, child, typeDef, relation) | ||
} | ||
|
@@ -129,42 +129,38 @@ func parseThis(graphBuilder *AuthorizationModelGraphBuilder, parentNode graph.No | |
if directlyRelatedDef.GetRelationOrWildcard() == nil { | ||
// direct assignment to concrete type | ||
assignableType := directlyRelatedDef.GetType() | ||
curNode = graphBuilder.GetOrAddNode(assignableType, assignableType, SpecificType) | ||
curNode = graphBuilder.getOrAddNode(assignableType, assignableType, SpecificType) | ||
} | ||
|
||
if directlyRelatedDef.GetWildcard() != nil { | ||
// direct assignment to wildcard | ||
assignableWildcard := directlyRelatedDef.GetType() + ":*" | ||
curNode = graphBuilder.GetOrAddNode(assignableWildcard, assignableWildcard, SpecificTypeWildcard) | ||
curNode = graphBuilder.getOrAddNode(assignableWildcard, assignableWildcard, SpecificTypeWildcard) | ||
} | ||
|
||
if directlyRelatedDef.GetRelation() != "" { | ||
// direct assignment to userset | ||
assignableUserset := directlyRelatedDef.GetType() + "#" + directlyRelatedDef.GetRelation() | ||
curNode = graphBuilder.GetOrAddNode(assignableUserset, assignableUserset, SpecificTypeAndRelation) | ||
curNode = graphBuilder.getOrAddNode(assignableUserset, assignableUserset, SpecificTypeAndRelation) | ||
} | ||
|
||
if graphBuilder.HasEdge(curNode, parentNode, DirectEdge, "") { | ||
// 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 | ||
} | ||
|
||
graphBuilder.AddEdge(curNode, parentNode, DirectEdge, "") | ||
// 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, but with two conditions: none and condX | ||
graphBuilder.upsertEdge(curNode, parentNode, DirectEdge, "", directlyRelatedDef.GetCondition()) | ||
} | ||
} | ||
|
||
func parseComputed(graphBuilder *AuthorizationModelGraphBuilder, parentNode *AuthorizationModelNode, typeDef *openfgav1.TypeDefinition, relation string) { | ||
nodeType := RewriteEdge | ||
// e.g. define x: y. Here y is the rewritten relation | ||
rewrittenNodeName := fmt.Sprintf("%s#%s", typeDef.GetType(), relation) | ||
newNode := graphBuilder.GetOrAddNode(rewrittenNodeName, rewrittenNodeName, SpecificTypeAndRelation) | ||
newNode := graphBuilder.getOrAddNode(rewrittenNodeName, rewrittenNodeName, SpecificTypeAndRelation) | ||
// new edge from y to x | ||
|
||
if parentNode.nodeType == SpecificTypeAndRelation && newNode.nodeType == SpecificTypeAndRelation { | ||
nodeType = ComputedEdge | ||
} | ||
graphBuilder.AddEdge(newNode, parentNode, nodeType, "") | ||
graphBuilder.AddEdge(newNode, parentNode, nodeType, "", "") | ||
} | ||
|
||
func parseTupleToUserset(graphBuilder *AuthorizationModelGraphBuilder, parentNode graph.Node, model *openfgav1.AuthorizationModel, typeDef *openfgav1.TypeDefinition, rewrite *openfgav1.TupleToUserset) { | ||
|
@@ -188,16 +184,22 @@ func parseTupleToUserset(graphBuilder *AuthorizationModelGraphBuilder, parentNod | |
} | ||
|
||
rewrittenNodeName := fmt.Sprintf("%s#%s", tuplesetType, computedRelation) | ||
nodeSource := graphBuilder.GetOrAddNode(rewrittenNodeName, rewrittenNodeName, SpecificTypeAndRelation) | ||
conditionedOnNodeName := fmt.Sprintf("%s#%s", typeDef.GetType(), tuplesetRelation) | ||
nodeSource := graphBuilder.getOrAddNode(rewrittenNodeName, rewrittenNodeName, SpecificTypeAndRelation) | ||
typeTuplesetRelation := fmt.Sprintf("%s#%s", typeDef.GetType(), tuplesetRelation) | ||
|
||
// new edge from "xxx#admin" to "yyy#viewer" conditioned on "yyy#parent" | ||
graphBuilder.AddEdge(nodeSource, parentNode, TTUEdge, conditionedOnNodeName) | ||
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 | ||
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. Do we need to call upsert? Otherwise, will be lose the condition? |
||
} | ||
|
||
// new edge from "xxx#admin" to "yyy#viewer" tuplesetRelation on "yyy#parent" | ||
graphBuilder.upsertEdge(nodeSource, parentNode, TTUEdge, typeTuplesetRelation, relatedType.GetCondition()) | ||
} | ||
} | ||
|
||
func (g *AuthorizationModelGraphBuilder) GetOrAddNode(uniqueLabel, label string, nodeType NodeType) *AuthorizationModelNode { | ||
if existingNode := g.GetNodeFor(uniqueLabel); existingNode != nil { | ||
func (g *AuthorizationModelGraphBuilder) getOrAddNode(uniqueLabel, label string, nodeType NodeType) *AuthorizationModelNode { | ||
if existingNode := g.getNodeByLabel(uniqueLabel); existingNode != nil { | ||
return existingNode | ||
} | ||
|
||
|
@@ -215,7 +217,7 @@ func (g *AuthorizationModelGraphBuilder) GetOrAddNode(uniqueLabel, label string, | |
return newNode | ||
} | ||
|
||
func (g *AuthorizationModelGraphBuilder) GetNodeFor(uniqueLabel string) *AuthorizationModelNode { | ||
func (g *AuthorizationModelGraphBuilder) getNodeByLabel(uniqueLabel string) *AuthorizationModelNode { | ||
id, ok := g.ids[uniqueLabel] | ||
if !ok { | ||
return nil | ||
|
@@ -229,31 +231,55 @@ func (g *AuthorizationModelGraphBuilder) GetNodeFor(uniqueLabel string) *Authori | |
return authModelNode | ||
} | ||
|
||
func (g *AuthorizationModelGraphBuilder) AddEdge(from, to graph.Node, edgeType EdgeType, conditionedOn string) *AuthorizationModelEdge { | ||
func (g *AuthorizationModelGraphBuilder) AddEdge(from, to graph.Node, edgeType EdgeType, tuplesetRelation string, condition string) *AuthorizationModelEdge { | ||
elbuo8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if from == nil || to == nil { | ||
return nil | ||
} | ||
if condition == "" { | ||
condition = NoCond | ||
} | ||
conditions := []string{condition} | ||
|
||
l := g.NewLine(from, to) | ||
newLine := &AuthorizationModelEdge{Line: l, edgeType: edgeType, conditionedOn: conditionedOn} | ||
newLine := &AuthorizationModelEdge{Line: l, edgeType: edgeType, tuplesetRelation: tuplesetRelation, conditions: conditions} | ||
g.SetLine(newLine) | ||
|
||
return newLine | ||
} | ||
|
||
func (g *AuthorizationModelGraphBuilder) HasEdge(from, to graph.Node, edgeType EdgeType, conditionedOn string) bool { | ||
func (g *AuthorizationModelGraphBuilder) upsertEdge(from, to graph.Node, edgeType EdgeType, tuplesetRelation string, condition string) { | ||
if from == nil || to == nil { | ||
return false | ||
return | ||
} | ||
|
||
iter := g.Lines(from.ID(), to.ID()) | ||
for iter.Next() { | ||
l := iter.Line() | ||
edge, ok := l.(*AuthorizationModelEdge) | ||
if !ok { | ||
return false | ||
edge, _ := l.(*AuthorizationModelEdge) | ||
if edge.edgeType == edgeType && edge.tuplesetRelation == tuplesetRelation { | ||
for _, cond := range edge.conditions { | ||
if cond == condition { | ||
return | ||
} | ||
} | ||
edge.conditions = append(edge.conditions, condition) | ||
return | ||
} | ||
if edge.edgeType == edgeType && edge.conditionedOn == conditionedOn { | ||
} | ||
|
||
g.AddEdge(from, to, edgeType, tuplesetRelation, condition) | ||
} | ||
|
||
func (g *AuthorizationModelGraphBuilder) hasEdge(from, to graph.Node, edgeType EdgeType, tuplesetRelation string) bool { | ||
if from == nil || to == nil { | ||
return false | ||
} | ||
|
||
iter := g.Lines(from.ID(), to.ID()) | ||
for iter.Next() { | ||
l := iter.Line() | ||
edge, _ := l.(*AuthorizationModelEdge) | ||
if edge.edgeType == edgeType && edge.tuplesetRelation == tuplesetRelation { | ||
return true | ||
} | ||
} | ||
|
This file contains 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 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 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 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 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.
why not
graphBuilder.AddEdge(nextLine.To(), nextLine.From(), casted.edgeType, casted.tuplesetRelation, casted.conditions)
?