-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_permission.go
145 lines (117 loc) · 3.57 KB
/
check_permission.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
package ds
import (
"context"
"github.com/aserto-dev/azm/cache"
"github.com/aserto-dev/azm/model"
dsc3 "github.com/aserto-dev/go-directory/aserto/directory/common/v3"
dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3"
"github.com/aserto-dev/go-directory/pkg/pb"
"github.com/aserto-dev/go-edge-ds/pkg/bdb"
"github.com/samber/lo"
bolt "go.etcd.io/bbolt"
)
type checkPermission struct {
*dsr3.CheckPermissionRequest
}
func CheckPermission(i *dsr3.CheckPermissionRequest) *checkPermission {
return &checkPermission{i}
}
func (i *checkPermission) Object() *dsc3.ObjectIdentifier {
return &dsc3.ObjectIdentifier{
ObjectType: i.ObjectType,
ObjectId: i.ObjectId,
}
}
func (i *checkPermission) Subject() *dsc3.ObjectIdentifier {
return &dsc3.ObjectIdentifier{
ObjectType: i.SubjectType,
ObjectId: i.SubjectId,
}
}
func (i *checkPermission) Validate(mc *cache.Cache) error {
if i == nil || i.CheckPermissionRequest == nil {
return ErrInvalidRequest.Msg("check_permission")
}
if err := ObjectIdentifier(i.Object()).Validate(mc); err != nil {
return err
}
if err := ObjectIdentifier(i.Subject()).Validate(mc); err != nil {
return err
}
if !mc.PermissionExists(model.ObjectName(i.ObjectType), model.RelationName(i.Permission)) {
return ErrPermissionNotFound.Msgf("%s%s%s", i.ObjectType, RelationSeparator, i.Permission)
}
return nil
}
func (i *checkPermission) Exec(ctx context.Context, tx *bolt.Tx, mc *cache.Cache) (*dsr3.CheckPermissionResponse, error) {
resp := &dsr3.CheckPermissionResponse{Check: false, Trace: []string{}}
check, err := i.newChecker(ctx, tx, bdb.RelationsObjPath, mc)
if err != nil {
return resp, err
}
match, err := check.check(i.Object())
return &dsr3.CheckPermissionResponse{Check: match}, err
}
func (i *checkPermission) newChecker(ctx context.Context, tx *bolt.Tx, path []string, mc *cache.Cache) (*permissionChecker, error) {
relations := mc.ExpandPermission(
model.ObjectName(i.GetObjectType()),
model.RelationName(i.GetPermission()))
userSet, err := CreateUserSet(ctx, tx, i.Subject())
if err != nil {
return nil, err
}
return &permissionChecker{
ctx: ctx,
tx: tx,
path: path,
anchor: i,
userSet: userSet,
filter: relations,
trace: [][]*dsc3.Relation{},
visited: map[ot]bool{},
}, nil
}
type permissionChecker struct {
ctx context.Context
tx *bolt.Tx
path []string
anchor *checkPermission
userSet []*dsc3.ObjectIdentifier
filter []model.RelationName
trace [][]*dsc3.Relation
visited map[ot]bool
}
func (c *permissionChecker) check(root *dsc3.ObjectIdentifier) (bool, error) {
// relations associated to object instance.
filter := ObjectIdentifier(root).Key() + InstanceSeparator
relations, err := bdb.Scan[dsc3.Relation](c.ctx, c.tx, c.path, filter)
if err != nil {
return false, err
}
c.visited[ot{root.ObjectType, root.ObjectId}] = true
for _, r := range relations {
if c.isMatch(r) {
return true, nil
}
}
for _, r := range relations {
if c.isCandidate(r) {
match, err := c.check(Relation(r).Subject())
if err != nil {
return false, err
}
if match {
return match, nil
}
}
}
return false, nil
}
func (c *permissionChecker) isMatch(relation *dsc3.Relation) bool {
return lo.Contains(c.filter, model.RelationName(relation.Relation)) &&
pb.Contains[*dsc3.ObjectIdentifier](c.userSet, Relation(relation).Subject())
}
func (c *permissionChecker) isCandidate(r *dsc3.Relation) bool {
return (lo.Contains(c.filter, model.RelationName(r.Relation)) || r.Relation == "parent") &&
!c.visited[ot{r.SubjectType, r.SubjectId}]
}