-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzedcache.go
338 lines (290 loc) · 11.6 KB
/
zedcache.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package zedcache
import (
"context"
"fmt"
pb "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/hashicorp/go-multierror"
"google.golang.org/grpc"
"github.com/connylabs/zedcache/cache"
)
// Options allows to pass extra options to the PermissionsServiceClient implementation.
type Option func(*permissionClient)
// WithLogger can overwrite the default Noop logger.
// The logger implements "github.com/go-kit/log"'s Logger interface.
func WithLogger(l log.Logger) Option {
return func(pc *permissionClient) {
pc.l = l
}
}
// New is a helper function to add a cache to the authzed.Client's PermissionsServiceClient implementation.
func New(c *authzed.Client, ca cache.Cache, opts ...Option) *authzed.Client {
return &authzed.Client{
PermissionsServiceClient: NewPermissionServiceClient(c.PermissionsServiceClient, ca, opts...),
SchemaServiceClient: c.SchemaServiceClient,
WatchServiceClient: c.WatchServiceClient,
}
}
// NewPermissionServiceClient add a cache to the given PermissionsServiceClient.
// Note that unlike the original interface the default Consistency is FullyConsistent.
func NewPermissionServiceClient(c pb.PermissionsServiceClient, ca cache.Cache, opts ...Option) pb.PermissionsServiceClient {
pc := &permissionClient{
PermissionsServiceClient: c,
ca: ca,
l: log.NewNopLogger(),
}
for _, o := range opts {
o(pc)
}
return pc
}
type permissionClient struct {
pb.PermissionsServiceClient
ca cache.Cache
l log.Logger
}
type permissionsService_ReadRelationshipsClient struct {
pb.PermissionsService_ReadRelationshipsClient
c cache.Cache
recourceCacheKey string
cached bool
l log.Logger
}
func (rrc *permissionsService_ReadRelationshipsClient) Recv() (*pb.ReadRelationshipsResponse, error) {
ret, err := rrc.PermissionsService_ReadRelationshipsClient.Recv()
if err != nil || rrc.cached {
return ret, err
}
if ret.ReadAt != nil {
if err := rrc.c.Set(rrc.recourceCacheKey, ret.ReadAt.Token); err != nil {
level.Error(rrc.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
}
rrc.cached = true
return ret, err
}
// ReadRelationships reads a set of the relationships matching one or more
// filters.
func (c *permissionClient) ReadRelationships(ctx context.Context, in *pb.ReadRelationshipsRequest, opts ...grpc.CallOption) (pb.PermissionsService_ReadRelationshipsClient, error) {
if in.Consistency == nil {
in.Consistency = &pb.Consistency{}
}
if in.Consistency.Requirement == nil {
in.Consistency.Requirement = &pb.Consistency_FullyConsistent{FullyConsistent: true}
// Not sure how to cache zedtoken if we the caller does not specify a resource id.
if in.RelationshipFilter.OptionalResourceId != "" {
t, err := c.ca.Get(fmt.Sprintf("%s#%s", in.RelationshipFilter.ResourceType, in.RelationshipFilter.OptionalResourceId))
if err == nil {
in.Consistency.Requirement = &pb.Consistency_AtLeastAsFresh{AtLeastAsFresh: &pb.ZedToken{Token: t}}
}
}
}
ret, err := c.PermissionsServiceClient.ReadRelationships(ctx, in, opts...)
// Not sure how to cache zedtoken if we the caller does not specify a resource id.
if err != nil || in.RelationshipFilter.OptionalResourceId == "" {
return ret, err
}
return &permissionsService_ReadRelationshipsClient{
PermissionsService_ReadRelationshipsClient: ret,
c: c.ca,
recourceCacheKey: fmt.Sprintf("%s#%s", in.RelationshipFilter.ResourceType, in.RelationshipFilter.OptionalResourceId),
l: c.l,
}, err
}
// CheckPermission determines for a given resource whether a subject computes
// to having a permission or is a direct member of a particular relation.
func (c *permissionClient) CheckPermission(ctx context.Context, in *pb.CheckPermissionRequest, opts ...grpc.CallOption) (*pb.CheckPermissionResponse, error) {
if in.Consistency == nil {
in.Consistency = &pb.Consistency{}
}
if in.Consistency.Requirement == nil {
in.Consistency.Requirement = &pb.Consistency_FullyConsistent{FullyConsistent: true}
t, err := c.ca.Get(sprintObjectReference(in.Resource))
if err == nil {
in.Consistency.Requirement = &pb.Consistency_AtLeastAsFresh{AtLeastAsFresh: &pb.ZedToken{Token: t}}
}
}
ret, err := c.PermissionsServiceClient.CheckPermission(ctx, in, opts...)
if err != nil {
return ret, err
}
fmt.Println(sprintObjectReference(in.Resource))
if err := c.ca.Set(sprintObjectReference(in.Resource), ret.CheckedAt.Token); err != nil {
level.Error(c.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
return ret, err
}
// ExpandPermissionTree reveals the graph structure for a resource's
// permission or relation. This RPC does not recurse infinitely deep and may
// require multiple calls to fully unnest a deeply nested graph.
func (c *permissionClient) ExpandPermissionTree(ctx context.Context, in *pb.ExpandPermissionTreeRequest, opts ...grpc.CallOption) (*pb.ExpandPermissionTreeResponse, error) {
if in.Consistency == nil {
in.Consistency = &pb.Consistency{}
}
if in.Consistency.Requirement == nil {
in.Consistency.Requirement = &pb.Consistency_FullyConsistent{FullyConsistent: true}
t, err := c.ca.Get(sprintObjectReference(in.Resource))
if err == nil {
in.Consistency.Requirement = &pb.Consistency_AtLeastAsFresh{AtLeastAsFresh: &pb.ZedToken{Token: t}}
}
}
ret, err := c.PermissionsServiceClient.ExpandPermissionTree(ctx, in, opts...)
if err != nil {
return ret, err
}
if ret.ExpandedAt != nil {
if err := c.ca.Set(sprintObjectReference(in.Resource), ret.ExpandedAt.Token); err != nil {
level.Error(c.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
}
return ret, err
}
type permissionsService_LookupResourcesClient struct {
pb.PermissionsService_LookupResourcesClient
c cache.Cache
parentObjectCacheKey string
cached bool
l log.Logger
}
func (lrc *permissionsService_LookupResourcesClient) Recv() (*pb.LookupResourcesResponse, error) {
ret, err := lrc.PermissionsService_LookupResourcesClient.Recv()
if err != nil || lrc.cached {
return ret, err
}
if ret.LookedUpAt != nil {
if err := lrc.c.Set(lrc.parentObjectCacheKey, ret.LookedUpAt.Token); err != nil {
level.Error(lrc.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
}
lrc.cached = true
return ret, err
}
// LookupResources returns all the resources of a given type that a subject
// can access whether via a computed permission or relation membership.
func (c *permissionClient) LookupResources(ctx context.Context, in *pb.LookupResourcesRequest, opts ...grpc.CallOption) (pb.PermissionsService_LookupResourcesClient, error) {
if in.Consistency == nil {
in.Consistency = &pb.Consistency{}
}
if in.Consistency.Requirement == nil {
in.Consistency.Requirement = &pb.Consistency_FullyConsistent{FullyConsistent: true}
t, err := c.ca.Get(sprintSubjectReference(in.Subject))
if err == nil {
in.Consistency.Requirement = &pb.Consistency_AtLeastAsFresh{AtLeastAsFresh: &pb.ZedToken{Token: t}}
}
}
ret, err := c.PermissionsServiceClient.LookupResources(ctx, in, opts...)
if err != nil {
return ret, err
}
nr := &permissionsService_LookupResourcesClient{
PermissionsService_LookupResourcesClient: ret,
c: c.ca,
parentObjectCacheKey: sprintSubjectReference(in.Subject),
l: c.l,
}
return nr, err
}
type permissionsService_LookupSubjectsClient struct {
pb.PermissionsService_LookupSubjectsClient
c cache.Cache
parentResourceKey string
cached bool
l log.Logger
}
func (lsc *permissionsService_LookupSubjectsClient) Recv() (*pb.LookupSubjectsResponse, error) {
ret, err := lsc.PermissionsService_LookupSubjectsClient.Recv()
if err != nil {
return ret, err
}
// TODO: does it make sense to cache the token along the resource here?
if !lsc.cached {
if err := lsc.c.Set(lsc.parentResourceKey, ret.LookedUpAt.Token); err != nil {
level.Error(lsc.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
lsc.cached = true
}
return ret, err
}
// LookupSubjects returns all the subjects of a given type that
// have access whether via a computed permission or relation membership.
func (c *permissionClient) LookupSubjects(ctx context.Context, in *pb.LookupSubjectsRequest, opts ...grpc.CallOption) (pb.PermissionsService_LookupSubjectsClient, error) {
if in.Consistency == nil {
in.Consistency = &pb.Consistency{}
}
if in.Consistency.Requirement == nil {
in.Consistency.Requirement = &pb.Consistency_FullyConsistent{FullyConsistent: true}
t, err := c.ca.Get(sprintObjectReference(in.Resource))
if err == nil {
in.Consistency.Requirement = &pb.Consistency_AtLeastAsFresh{AtLeastAsFresh: &pb.ZedToken{Token: t}}
}
}
ret, err := c.PermissionsServiceClient.LookupSubjects(ctx, in, opts...)
if err != nil {
return ret, err
}
return &permissionsService_LookupSubjectsClient{
PermissionsService_LookupSubjectsClient: ret,
c: c.ca,
parentResourceKey: sprintObjectReference(in.Resource),
l: c.l,
}, err
}
// WriteRelationships atomically writes and/or deletes a set of specified
// relationships. An optional set of preconditions can be provided that must
// be satisfied for the operation to commit.
// The zedtoken will always be cached along the resource.
// This is not ideal in the case of adding or removing resources: https://authzed.com/docs/reference/zedtokens-and-zookies#when-adding-or-removing-a-resource
// The authors suggest to only save the zedtoken along the parent resource.
// However it is not clear how to determine whether a resource was added/removed or a relation was added/removed.
func (c *permissionClient) WriteRelationships(ctx context.Context, in *pb.WriteRelationshipsRequest, opts ...grpc.CallOption) (*pb.WriteRelationshipsResponse, error) {
{
// delete all relevant cached zed token to avoid the "New Enimy" problem.
keys := make([]string, 2*len(in.Updates))
for i, r := range in.Updates {
keys[i] = sprintObjectReference(r.Relationship.Resource)
keys[2*i] = sprintSubjectReference(r.Relationship.Subject)
}
if err := c.ca.Del(keys...); err != nil {
return nil, fmt.Errorf("failed to clear cache: %w", err)
}
}
res, err := c.PermissionsServiceClient.WriteRelationships(ctx, in, opts...)
if err != nil {
return res, err
}
g := multierror.Group{}
for _, r := range in.Updates {
rKey := sprintObjectReference(r.Relationship.Resource)
fmt.Println(rKey)
g.Go(func() error {
return c.ca.Set(rKey, res.WrittenAt.Token)
})
sKey := sprintSubjectReference(r.Relationship.Subject)
fmt.Println(sKey)
g.Go(func() error {
return c.ca.Set(sKey, res.WrittenAt.Token)
})
}
// We don't need block for writing the updated valued to the cache here, because we already deleted the relevant cache entries.
// But it would make the testing more difficult, so no async writes here at first.
// go func() {
if err := g.Wait().ErrorOrNil(); err != nil {
level.Error(c.l).Log("msg", "failed to write cache entry", "err", err.Error())
}
// }()
return res, nil
}
func sprintObjectReference(r *pb.ObjectReference) string {
if r == nil {
panic("can not print nil object reference")
}
return fmt.Sprintf("%s#%s", r.ObjectType, r.ObjectId)
}
func sprintSubjectReference(r *pb.SubjectReference) string {
if r == nil || r.Object == nil {
panic("can not print nil object reference")
}
return sprintObjectReference(r.Object)
}