-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
157 lines (139 loc) · 3.35 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
_zanzi "github.com/sourcenetwork/zanzi"
"github.com/sourcenetwork/zanzi/pkg/api"
"github.com/sourcenetwork/zanzi/pkg/domain"
)
func main() {
// Init Zanzi
zanzi, err := _zanzi.New(
_zanzi.WithDevelopmentLogger(),
_zanzi.WithMemKVStore(),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
policyServ := zanzi.GetPolicyService()
relGraph := zanzi.GetRelationGraphService()
setupData(ctx, policyServ)
/*
res, err := relGraph.DumpRelationships(ctx, &api.DumpRelationshipsRequest{
PolicyId: "10",
Format: api.DumpRelationshipsRequest_DOT,
})
dot := res.Dump.(*api.DumpRelationshipResponse_Dot).Dot
fmt.Println(dot)
*/
/*
res, err := relGraph.Check(ctx, &api.CheckRequest{
PolicyId: "10",
AccessRequest: &domain.AccessRequest{
Object: domain.NewEntity("file", "readme"),
Relation: "read",
Subject: domain.NewEntity("user", "bob"),
},
})
fmt.Println(res.Result.Authorized)
*/
res, err := relGraph.Expand(ctx, &api.ExpandRequest{
PolicyId: "10",
Root: &domain.RelationNode{
Node: &domain.RelationNode_EntitySet{
EntitySet: &domain.EntitySetNode{
Object: domain.NewEntity("file", "readme"),
Relation: "read",
},
},
},
Format: api.ExplainFormat_DOT,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(res.GoalTree)
}
func setupData(ctx context.Context, service api.PolicyServiceServer) {
// Create Policy
createResponse, err := service.CreatePolicy(ctx, &api.CreatePolicyRequest{
PolicyDefinition: &api.PolicyDefinition{
Definition: &api.PolicyDefinition_PolicyYaml{
PolicyYaml: policyYaml,
},
},
})
if err != nil {
log.Fatal(err)
}
log.Printf("Created policy: %v", createResponse.Record.Policy)
// Set Relationships
for _, relationship := range relationships {
_, err = service.SetRelationship(ctx, &api.SetRelationshipRequest{
PolicyId: createResponse.Record.Policy.Id,
Relationship: &relationship,
})
if err != nil {
log.Fatal(err)
}
}
}
var policyYaml = `
id: 10
name: test
doc: test policy
resources:
file:
doc: file resource
relations:
owner:
expr: _this
types:
- '*'
parent:
expr: _this
types:
- directory
read:
expr: owner + parent->read
types: []
directory:
relations:
owner:
types:
- user
- group:member
- group:owner
expr: _this
reader:
expr: _this
types:
- user
- group:member
read:
expr: owner + reader
group:
relations:
member:
expr: _this + owner
types:
- user
owner:
expr: _this
types:
- user
user:
attributes:
foo: bar
`
var builder domain.RelationshipBuilder
var relationships []domain.Relationship = []domain.Relationship{
builder.Relationship("file", "readme", "owner", "user", "charlie"),
builder.Relationship("file", "readme", "parent", "directory", "proj"),
builder.EntitySet("directory", "proj", "owner", "group", "eng", "owner"),
builder.EntitySet("directory", "proj", "reader", "group", "eng", "member"),
builder.Relationship("group", "eng", "owner", "user", "alice"),
builder.Relationship("group", "eng", "member", "user", "bob"),
}