-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathbridge.go
More file actions
360 lines (318 loc) · 7.72 KB
/
Copy pathbridge.go
File metadata and controls
360 lines (318 loc) · 7.72 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2025 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package dac
import (
"context"
protoTypes "github.com/cosmos/gogoproto/types"
"github.com/sourcenetwork/corelog"
"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/acp/identity"
acpTypes "github.com/sourcenetwork/defradb/acp/types"
)
var _ acp.ACPSystemClient = (*RemoteDocumentACP)(nil)
var _ DocumentACP = (*bridgeDocumentACP)(nil)
// bridgeDocumentACP wraps an [ACPSystemClient], hosting the DefraDB specific logic away
// from ACP client specific code.
type bridgeDocumentACP struct {
clientACP acp.ACPSystemClient
// documentACPType must only be set to [acpTypes.LocalDocumentACP] or
// [acpTypes.RemoteDocumentACP]. [acpTypes.NodeACP] and future NAC types are invalid.
documentACPType acpTypes.ACPSystemType
}
func (a *bridgeDocumentACP) Start(ctx context.Context) error {
return a.clientACP.Start(ctx)
}
func (a *bridgeDocumentACP) AddPolicy(ctx context.Context, creator identity.Identity, policy string) (string, error) {
// Having a creator identity is a MUST requirement for adding a policy.
if creator == nil || creator.DID() == "" {
return "", acp.ErrPolicyCreatorMustNotBeEmpty
}
if policy == "" {
return "", acp.ErrPolicyDataMustNotBeEmpty
}
marshalType := acpTypes.PolicyMarshalType_YAML
policyID, err := a.clientACP.AddPolicy(
ctx,
creator,
policy,
marshalType,
protoTypes.TimestampNow(),
)
if err != nil {
return "", acp.NewErrFailedToAddPolicy(err, a.documentACPType.String(), creator.DID())
}
log.InfoContext(ctx, "Created Policy", corelog.Any("PolicyID", policyID))
return policyID, nil
}
func (a *bridgeDocumentACP) ValidateResourceInterface(
ctx context.Context,
policyID string,
resourceName string,
) error {
if a.documentACPType != acpTypes.LocalDocumentACP &&
a.documentACPType != acpTypes.RemoteDocumentACP {
return acp.ErrInvalidACPSystem
}
return acp.ValidateResourceInterface(
ctx,
policyID,
resourceName,
a.documentACPType,
a.clientACP,
)
}
func (a *bridgeDocumentACP) RegisterDocObject(
ctx context.Context,
identity identity.Identity,
policyID string,
resourceName string,
docID string,
) error {
err := a.clientACP.RegisterObject(
ctx,
identity,
policyID,
resourceName,
docID,
protoTypes.TimestampNow(),
)
if err != nil {
// RegisterObject is effectively idempotent if the object already has the requested owner.
// This lets the same collection be registered on multiple nodes that share an acp instance.
owner, ownerErr := a.clientACP.ObjectOwner(ctx, policyID, resourceName, docID)
if ownerErr == nil && owner.HasValue() && owner.Value() == identity.DID() {
return nil
}
return acp.NewErrFailedToRegisterDoc(
err,
a.documentACPType.String(),
policyID,
identity.DID(),
resourceName,
docID,
)
}
return nil
}
func (a *bridgeDocumentACP) IsDocRegistered(
ctx context.Context,
policyID string,
resourceName string,
docID string,
) (bool, error) {
maybeActor, err := a.clientACP.ObjectOwner(
ctx,
policyID,
resourceName,
docID,
)
if err != nil {
return false, acp.NewErrFailedToCheckIfDocIsRegistered(
err,
a.documentACPType.String(),
policyID,
resourceName,
docID,
)
}
return maybeActor.HasValue(), nil
}
func (a *bridgeDocumentACP) CheckDocAccess(
ctx context.Context,
permission acpTypes.DocumentResourcePermission,
actorID string,
policyID string,
resourceName string,
docID string,
) (bool, error) {
// We grant "read" access even if the identity does not explicitly have the "read" permission,
// as long as they have any of the permissions that imply read access.
if permission == acpTypes.DocumentReadPerm {
var canRead bool = false
var err error
for _, permissionThatImpliesRead := range acpTypes.ImplyDocumentReadPerm {
canRead, err = a.clientACP.VerifyAccessRequest(
ctx,
permissionThatImpliesRead,
actorID,
policyID,
resourceName,
docID,
)
if err != nil {
return false, acp.NewErrFailedToVerifyDocAccess(
err,
a.documentACPType.String(),
permissionThatImpliesRead.String(),
policyID,
actorID,
resourceName,
docID,
)
}
if canRead {
break
}
}
return canRead, nil
}
hasAccess, err := a.clientACP.VerifyAccessRequest(
ctx,
permission,
actorID,
policyID,
resourceName,
docID,
)
if err != nil {
return false, acp.NewErrFailedToVerifyDocAccess(
err,
a.documentACPType.String(),
permission.String(),
policyID,
actorID,
resourceName,
docID,
)
}
return hasAccess, nil
}
func (a *bridgeDocumentACP) AddDocActorRelationship(
ctx context.Context,
policyID string,
resourceName string,
docID string,
relation string,
requestActor identity.Identity,
targetActor string,
) (bool, error) {
if policyID == "" ||
resourceName == "" ||
docID == "" ||
relation == "" ||
requestActor == nil ||
targetActor == "" {
var requestActorDID string
if requestActor != nil {
requestActorDID = requestActor.DID()
}
return false, acp.NewErrMissingRequiredArgToAddDocActorRelationship(
policyID,
resourceName,
docID,
relation,
requestActorDID,
targetActor,
)
}
exists, err := a.clientACP.AddActorRelationship(
ctx,
policyID,
resourceName,
docID,
relation,
requestActor,
targetActor,
protoTypes.TimestampNow(),
)
if err != nil {
return false, acp.NewErrFailedToAddDocActorRelationship(
err,
a.documentACPType.String(),
policyID,
resourceName,
docID,
relation,
requestActor.DID(),
targetActor,
)
}
log.InfoContext(
ctx,
"Document and actor relationship set",
corelog.Any("PolicyID", policyID),
corelog.Any("ResourceName", resourceName),
corelog.Any("DocID", docID),
corelog.Any("Relation", relation),
corelog.Any("RequestActor", requestActor.DID()),
corelog.Any("TargetActor", targetActor),
corelog.Any("Existed", exists),
)
return exists, nil
}
func (a *bridgeDocumentACP) DeleteDocActorRelationship(
ctx context.Context,
policyID string,
resourceName string,
docID string,
relation string,
requestActor identity.Identity,
targetActor string,
) (bool, error) {
if policyID == "" ||
resourceName == "" ||
docID == "" ||
relation == "" ||
requestActor == nil ||
targetActor == "" {
var requestActorDID string
if requestActor != nil {
requestActorDID = requestActor.DID()
}
return false, acp.NewErrMissingRequiredArgToDeleteDocActorRelationship(
policyID,
resourceName,
docID,
relation,
requestActorDID,
targetActor,
)
}
recordFound, err := a.clientACP.DeleteActorRelationship(
ctx,
policyID,
resourceName,
docID,
relation,
requestActor,
targetActor,
protoTypes.TimestampNow(),
)
if err != nil {
return false, acp.NewErrFailedToDeleteDocActorRelationship(
err,
a.documentACPType.String(),
policyID,
resourceName,
docID,
relation,
requestActor.DID(),
targetActor,
)
}
log.InfoContext(
ctx,
"Document and actor relationship delete",
corelog.Any("PolicyID", policyID),
corelog.Any("ResourceName", resourceName),
corelog.Any("DocID", docID),
corelog.Any("Relation", relation),
corelog.Any("RequestActor", requestActor.DID()),
corelog.Any("TargetActor", targetActor),
corelog.Any("RecordFound", recordFound),
)
return recordFound, nil
}
func (a *bridgeDocumentACP) Close() error {
return a.clientACP.Close()
}
func (a *bridgeDocumentACP) ResetState(ctx context.Context) error {
return a.clientACP.ResetState(ctx)
}