Skip to content

Commit de498d5

Browse files
committed
NAC gate SyncBranchableCollection
1 parent b471703 commit de498d5

File tree

16 files changed

+412
-34
lines changed

16 files changed

+412
-34
lines changed

acp/types/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const (
110110
NodeP2PDocumentDeletePerm
111111
NodeP2PDocumentListPerm
112112
NodeP2PSyncCollectionVersionsPerm
113+
NodeP2PSyncBranchableCollectionPerm
113114
NodeSignatureVerifyPerm
114115
NodeLensCreatePerm
115116
NodeLensListPerm
@@ -157,6 +158,7 @@ var RequiredResourcePermissionsForNode = []string{
157158
"p2p-document-delete",
158159
"p2p-document-list",
159160
"p2p-sync-collection-versions",
161+
"p2p-sync-branchable-collection",
160162
"signature-verify",
161163
"lens-create",
162164
"lens-list",
@@ -251,6 +253,8 @@ resources:
251253
expr: admin
252254
- name: p2p-sync-collection-versions
253255
expr: admin
256+
- name: p2p-sync-branchable-collection
257+
expr: admin
254258
255259
- name: signature-verify
256260
expr: admin

cbindings/wrapper.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,12 @@ func (w *CWrapper) SyncCollectionVersions(
376376
return nil
377377
}
378378

379-
func (w *CWrapper) SyncBranchableCollection(ctx context.Context, collectionID string) error {
379+
func (w *CWrapper) SyncBranchableCollection(
380+
ctx context.Context,
381+
collectionID string,
382+
opts ...options.Lister[options.SyncBranchableCollectionOptions],
383+
) error {
384+
opt := utils.NewOptions(opts...)
380385
cCollectionID := C.CString(collectionID)
381386
defer C.free(unsafe.Pointer(cCollectionID))
382387

@@ -388,8 +393,11 @@ func (w *CWrapper) SyncBranchableCollection(ctx context.Context, collectionID st
388393
cTimerStr := C.CString(timerStr)
389394
defer C.free(unsafe.Pointer(cTimerStr))
390395

396+
cIdentity := optionToUintptr(opt.GetIdentity())
397+
defer C.IdentityFree(cIdentity)
398+
391399
res := ConvertAndFreeCResult(
392-
C.P2PbranchableCollectionSync(C.uintptr_t(w.handle), cCollectionID, cTimerStr, C.uintptr_t(0)))
400+
C.P2PbranchableCollectionSync(C.uintptr_t(w.handle), cCollectionID, cTimerStr, cIdentity))
393401

394402
if res.Status != 0 {
395403
return errors.New(res.Error)

client/mocks/txn.go

Lines changed: 23 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/options/p2p.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,35 @@ func (b *SyncCollectionVersionsOptionsBuilder) SetIdentity(id identity.Identity)
360360
return b
361361
}
362362

363+
// SyncBranchableCollectionOptions contains options for SyncBranchableCollection operation.
364+
type SyncBranchableCollectionOptions struct {
365+
// Identity is the identity of the actor performing the operation.
366+
Identity immutable.Option[identity.Identity]
367+
}
368+
369+
// GetIdentity returns the identity for the operation.
370+
func (o *SyncBranchableCollectionOptions) GetIdentity() immutable.Option[identity.Identity] {
371+
return o.Identity
372+
}
373+
374+
// SyncBranchableCollectionOptionsBuilder is a builder for SyncBranchableCollectionOptions.
375+
type SyncBranchableCollectionOptionsBuilder struct {
376+
enumerableBuilder[SyncBranchableCollectionOptions]
377+
}
378+
379+
// SyncBranchableCollection creates a new SyncBranchableCollectionOptionsBuilder instance.
380+
func SyncBranchableCollection() *SyncBranchableCollectionOptionsBuilder {
381+
return &SyncBranchableCollectionOptionsBuilder{}
382+
}
383+
384+
// SetIdentity sets the identity for the operation.
385+
func (b *SyncBranchableCollectionOptionsBuilder) SetIdentity(id identity.Identity) *SyncBranchableCollectionOptionsBuilder {
386+
b.append(func(opts *SyncBranchableCollectionOptions) {
387+
opts.Identity = immutable.Some(id)
388+
})
389+
return b
390+
}
391+
363392
// DeleteP2PDocumentsOptions contains options for RemoveP2PDocuments operation.
364393
type DeleteP2PDocumentsOptions struct {
365394
// Identity is the identity of the actor performing the operation.

client/p2p.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ type P2P interface {
123123
// for branchable collections (collections marked with @branchable directive).
124124
// It doesn't automatically subscribe to the collection for future updates.
125125
// context.WithTimeout can be used to set a timeout for the operation.
126-
SyncBranchableCollection(ctx context.Context, collectionID string) error
126+
SyncBranchableCollection(
127+
ctx context.Context,
128+
collectionID string,
129+
opts ...options.Lister[options.SyncBranchableCollectionOptions],
130+
) error
127131
}
128132

129133
type StreamHandler = func(stream io.Reader, peerID string)

http/client_p2p.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@ func (c *Client) SyncCollectionVersions(
372372
return err
373373
}
374374

375-
func (c *Client) SyncBranchableCollection(ctx context.Context, collectionID string) error {
375+
func (c *Client) SyncBranchableCollection(
376+
ctx context.Context,
377+
collectionID string,
378+
opts ...options.Lister[options.SyncBranchableCollectionOptions],
379+
) error {
380+
376381
methodURL := c.http.apiURL.JoinPath("p2p", "collections", "sync-branchable")
377382

378383
req := map[string]any{
@@ -394,6 +399,8 @@ func (c *Client) SyncBranchableCollection(ctx context.Context, collectionID stri
394399
// This is necessary because the node handling this request will usually wait whole timeout
395400
// duration as it might receive responses from multiple peers.
396401
httpCtx := context.Background()
402+
opt := utils.NewOptions(opts...)
403+
ctx = identity.WithContext(ctx, opt.GetIdentity())
397404
if hasDeadline {
398405
var cancel context.CancelFunc
399406
httpCtx, cancel = context.WithTimeout(httpCtx, time.Until(deadline)+500*time.Millisecond)

internal/db/p2p.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,19 @@ func (db *DB) SyncCollectionVersions(
382382
// context.WithTimeout can be used to set a timeout for the operation.
383383
//
384384
// WARNING: This function does not respect transactions.
385-
func (db *DB) SyncBranchableCollection(ctx context.Context, collectionID string) error {
385+
func (db *DB) SyncBranchableCollection(
386+
ctx context.Context,
387+
collectionID string,
388+
opts ...options.Lister[options.SyncBranchableCollectionOptions],
389+
) error {
390+
opt := utils.NewOptions(opts...)
391+
392+
if err := db.checkNodeAccess(ctx, opt.Identity, acpTypes.NodeP2PSyncBranchableCollectionPerm); err != nil {
393+
return err
394+
}
395+
386396
if db.p2p == nil {
387397
return ErrNoP2P
388398
}
389-
return db.p2p.SyncBranchableCollection(ctx, collectionID)
399+
return db.p2p.SyncBranchableCollection(ctx, collectionID, opt)
390400
}

internal/db/p2p/p2p.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,17 @@ func (p *P2P) hasAccess(ctx context.Context, pid string, c cid.Cid) bool {
347347
return true
348348
}
349349

350-
cols, err := p.db.GetCollections(
351-
ctx,
352-
options.GetCollections().SetVersionID(block.Delta.GetCollectionVersionID()),
353-
)
350+
ident, err := p.db.GetNodeIdentity(p.ctx)
351+
if err != nil {
352+
log.ErrorE("Failed to get node identity", err)
353+
return false
354+
}
355+
getColOpts := options.GetCollections().SetCollectionID(block.Delta.GetCollectionVersionID())
356+
if ident.HasValue() {
357+
getColOpts = getColOpts.SetIdentity(identity.FromDID(ident.Value().DID))
358+
}
359+
360+
cols, err := p.db.GetCollections(ctx, getColOpts)
354361
if err != nil {
355362
log.ErrorE("Failed to get collections", err)
356363
return false

internal/db/p2p/sync_branchable_col.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/sourcenetwork/corelog"
2323

24+
"github.com/sourcenetwork/defradb/acp/identity"
2425
"github.com/sourcenetwork/defradb/client"
2526
"github.com/sourcenetwork/defradb/client/options"
2627
"github.com/sourcenetwork/defradb/errors"
@@ -50,11 +51,17 @@ type syncBranchableCollectionReply struct {
5051
//
5152
// This function call will block until there is a response for the collection.
5253
// It is the responsibility of the caller to set an appropriate timeout on the context.
53-
func (p *P2P) SyncBranchableCollection(ctx context.Context, collectionID string) error {
54-
cols, err := p.db.GetCollections(
55-
ctx,
56-
options.GetCollections().SetCollectionID(collectionID),
57-
)
54+
func (p *P2P) SyncBranchableCollection(
55+
ctx context.Context,
56+
collectionID string,
57+
opts *options.SyncBranchableCollectionOptions,
58+
) error {
59+
getColOpts := options.GetCollections().SetCollectionID(collectionID)
60+
if opts.Identity.HasValue() {
61+
getColOpts = getColOpts.SetIdentity(opts.Identity.Value())
62+
}
63+
64+
cols, err := p.db.GetCollections(ctx, getColOpts)
5865
if err != nil {
5966
return err
6067
}
@@ -262,10 +269,16 @@ func (p *P2P) syncBranchableCollectionMessageHandler(from string, topic string,
262269

263270
// processSyncBranchableCollection processes a branchable collection sync request and returns all head CIDs.
264271
func (p *P2P) processSyncBranchableCollection(collectionID string) ([][]byte, error) {
265-
cols, err := p.db.GetCollections(
266-
p.ctx,
267-
options.GetCollections().SetCollectionID(collectionID),
268-
)
272+
ident, err := p.db.GetNodeIdentity(p.ctx)
273+
if err != nil {
274+
return nil, err
275+
}
276+
getColOpts := options.GetCollections().SetCollectionID(collectionID)
277+
if ident.HasValue() {
278+
getColOpts = getColOpts.SetIdentity(identity.FromDID(ident.Value().DID))
279+
}
280+
281+
cols, err := p.db.GetCollections(p.ctx, getColOpts)
269282
if err != nil || len(cols) == 0 {
270283
return nil, err
271284
}

internal/db/txn.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ func (txn *Txn) SyncCollectionVersions(
428428
return txn.db.SyncCollectionVersions(ctx, versionIDs, opts...)
429429
}
430430

431-
func (txn *Txn) SyncBranchableCollection(ctx context.Context, collectionID string) error {
431+
func (txn *Txn) SyncBranchableCollection(
432+
ctx context.Context,
433+
collectionID string,
434+
opts ...options.Lister[options.SyncBranchableCollectionOptions],
435+
) error {
432436
ctx = InitContext(ctx, txn)
433-
return txn.db.SyncBranchableCollection(ctx, collectionID)
437+
return txn.db.SyncBranchableCollection(ctx, collectionID, opts...)
434438
}

0 commit comments

Comments
 (0)