-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
187 lines (156 loc) · 5.85 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"log"
"slices"
"sort"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
parser "github.com/openfga/language/pkg/go/transformer"
"github.com/openfga/openfga/pkg/typesystem"
"gonum.org/v1/gonum/graph/encoding/dot"
"gonum.org/v1/gonum/graph/topo"
)
func buildGraph(model *openfgav1.AuthorizationModel) *dotEncodingGraph {
typesys := typesystem.New(model)
// sort type names to guarantee stable outcome
sort.SliceStable(model.GetTypeDefinitions(), func(i, j int) bool {
return slices.IsSorted([]string{model.GetTypeDefinitions()[i].Type, model.GetTypeDefinitions()[j].Type})
})
g := newDotEncodingGraph()
for _, typedef := range model.GetTypeDefinitions() {
typeName := typedef.GetType()
g.AddOrGetNode(typeName)
g.AddOrGetNode(typeName + ":*")
// sort relation names to guarantee stable outcome
sortedRelationNames := make([]string, 0, len(typedef.GetRelations()))
for key := range typedef.GetRelations() {
sortedRelationNames = append(sortedRelationNames, key)
}
sort.Strings(sortedRelationNames)
for _, relation := range sortedRelationNames {
g.AddOrGetNode(fmt.Sprintf("%s#%s", typeName, relation))
rewrite := typedef.GetRelations()[relation]
if _, err := typesystem.WalkUsersetRewrite(rewrite, rewriteHandler(typesys, g, typeName, relation)); err != nil {
panic(err)
}
}
}
return g
}
func rewriteHandler(typesys *typesystem.TypeSystem, g *dotEncodingGraph, typeName, relation string) typesystem.WalkUsersetRewriteHandler {
relationNodeName := fmt.Sprintf("%s#%s", typeName, relation)
return func(r *openfgav1.Userset) interface{} {
switch rw := r.Userset.(type) {
case *openfgav1.Userset_This:
assignableRelations, err := typesys.GetDirectlyRelatedUserTypes(typeName, relation)
if err != nil {
panic(err)
}
for _, assignableRelation := range assignableRelations {
assignableType := assignableRelation.GetType()
conditionName := assignableRelation.GetCondition()
if conditionName != "" {
assignableType = fmt.Sprintf(" %s[with %s]", assignableType, conditionName)
}
if assignableRelation.GetRelationOrWildcard() != nil {
assignableRelationRef := assignableRelation.GetRelation()
if assignableRelationRef != "" {
assignableRelationNodeName := fmt.Sprintf("%s#%s", assignableType, assignableRelationRef)
g.AddEdge(assignableRelationNodeName, relationNodeName, "", "")
}
wildcardRelationRef := assignableRelation.GetWildcard()
if wildcardRelationRef != nil {
wildcardRelationNodeName := fmt.Sprintf("%s:*", assignableType)
g.AddEdge(wildcardRelationNodeName, relationNodeName, "", "")
}
} else {
g.AddEdge(assignableType, relationNodeName, "", "")
}
}
case *openfgav1.Userset_ComputedUserset:
rewrittenRelation := rw.ComputedUserset.GetRelation()
rewritten, err := typesys.GetRelation(typeName, rewrittenRelation)
if err != nil {
panic(err)
}
rewrittenNodeName := fmt.Sprintf("%s#%s", typeName, rewritten.GetName())
g.AddEdge(rewrittenNodeName, relationNodeName, "", "dashed")
case *openfgav1.Userset_TupleToUserset:
tupleset := rw.TupleToUserset.GetTupleset().GetRelation()
rewrittenRelation := rw.TupleToUserset.GetComputedUserset().GetRelation()
tuplesetRel, err := typesys.GetRelation(typeName, tupleset)
if err != nil {
panic(err)
}
directlyRelatedTypes := tuplesetRel.GetTypeInfo().GetDirectlyRelatedUserTypes()
for _, relatedType := range directlyRelatedTypes {
assignableType := relatedType.GetType()
conditionName := relatedType.GetCondition()
if conditionName != "" {
assignableType = fmt.Sprintf(" %s[with %s]", assignableType, conditionName)
}
rewrittenNodeName := fmt.Sprintf("%s#%s", assignableType, rewrittenRelation)
conditionedOnNodeName := fmt.Sprintf("(%s#%s)", typeName, tuplesetRel.GetName())
g.AddEdge(rewrittenNodeName, relationNodeName, conditionedOnNodeName, "")
}
case *openfgav1.Userset_Union:
case *openfgav1.Userset_Intersection:
case *openfgav1.Userset_Difference:
default:
panic("unexpected userset rewrite type encountered")
}
return nil
}
}
type CycleInformation struct {
// cycles that have at least one edge that is NOT a computed relation
// They are dangerous to call Check API on.
possibleCycles int
// cycles that involve computed relations only.
// They should be forbidden when calling WriteAuthorizationModel API.
definitiveCycles int
cycles [][]string
}
func parseCycleInformation(g *dotEncodingGraph) *CycleInformation {
result := &CycleInformation{}
pathsInCycles := topo.DirectedCyclesIn(g)
// convertedCycles has nicely formatted nodes, like "document#viewer"
convertedCycles := make([][]string, 0)
for _, nodesInCycle := range pathsInCycles {
inner := make([]string, 0)
for i, node := range nodesInCycle {
from := node.ID()
inner = append(inner, g.reverseMapping[node.ID()])
if i != len(nodesInCycle)-1 {
to := nodesInCycle[i+1].ID()
lines := g.Lines(from, to)
for {
if !lines.Next() {
break
}
l := lines.Line()
if g.lines[fmt.Sprintf("%v-%v-%v", from, to, l.ID())].attrs["style"] != "dashed" {
// it's not a computed userset, so it's a possible cycle, not a definitive one
result.possibleCycles++
break
}
}
}
}
convertedCycles = append(convertedCycles, inner)
}
result.cycles = convertedCycles
result.definitiveCycles = len(result.cycles) - result.possibleCycles
return result
}
// Writer returns the DOT of the model and information about cycles in the model
func Writer(modelString string) (string, *CycleInformation) {
model := parser.MustTransformDSLToProto(modelString)
g := buildGraph(model)
g.RemoveNodesWithNoEdges()
multi, err := dot.MarshalMulti(g, "", "", "")
if err != nil {
log.Fatalf("failed to render graph: %v", err)
}
return string(multi), parseCycleInformation(g)
}