node acl service (wip), fixes
This commit is contained in:
1 parent
348acd6d84
commit
41a3f0502e
22 files changed
+1439
-491
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/account"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice/synchandler"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusclient"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CName = "node.acl"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type Service interface {
|
||||
app.Component
|
||||
}
|
||||
|
||||
type service struct {
|
||||
consService consensusclient.Service
|
||||
account account.Service
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.consService = a.MustComponent(consensusclient.CName).(consensusclient.Service)
|
||||
s.account = a.MustComponent(account.CName).(account.Service)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) CreateLog(ctx context.Context, aclId string, rec *aclrecordproto.RawACLRecordWithId) (err error) {
|
||||
logId, err := cidToByte(aclId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
recId, err := cidToByte(rec.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
acc := s.account.Account()
|
||||
rec.AcceptorIdentity = acc.Identity
|
||||
if rec.AcceptorSignature, err = acc.SignKey.Sign(rec.Payload); err != nil {
|
||||
return
|
||||
}
|
||||
recPayload, err := rec.Marshal()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return s.consService.AddLog(ctx, &consensusproto.Log{
|
||||
Id: logId,
|
||||
Records: []*consensusproto.Record{
|
||||
{
|
||||
Id: recId,
|
||||
Payload: recPayload,
|
||||
CreatedUnix: uint64(time.Now().Unix()),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) AddRecord(ctx context.Context, aclId string, rec *aclrecordproto.RawACLRecordWithId) (err error) {
|
||||
logId, err := cidToByte(aclId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
recId, err := cidToByte(rec.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
acc := s.account.Account()
|
||||
rec.AcceptorIdentity = acc.Identity
|
||||
if rec.AcceptorSignature, err = acc.SignKey.Sign(rec.Payload); err != nil {
|
||||
return
|
||||
}
|
||||
recPayload, err := rec.Marshal()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return s.consService.AddRecord(ctx, logId, &consensusproto.Record{
|
||||
Id: recId,
|
||||
PrevId: nil, //TODO:
|
||||
Payload: recPayload,
|
||||
CreatedUnix: uint64(time.Now().Unix()),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) Watch(ctx context.Context, spaceId, aclId string, h synchandler.SyncHandler) (err error) {
|
||||
w, err := newWatcher(spaceId, aclId, h)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = s.consService.Watch(w.logId, w); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Ready(ctx)
|
||||
}
|
||||
|
||||
func (s *service) UnWatch(aclId string) (err error) {
|
||||
logId, err := cidToByte(aclId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return s.consService.UnWatch(logId)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package acl
|
||||
|
||||
import "github.com/ipfs/go-cid"
|
||||
|
||||
func cidToString(b []byte) (s string, err error) {
|
||||
rcid, err := cid.Cast(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return rcid.String(), nil
|
||||
}
|
||||
|
||||
func cidToByte(s string) (b []byte, err error) {
|
||||
rcid, err := cid.Decode(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return rcid.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/cid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCIDLen(t *testing.T) {
|
||||
s, _ := cid.NewCIDFromBytes([]byte("some data"))
|
||||
t.Log(s, len(s))
|
||||
b, _ := cidToByte(s)
|
||||
t.Log(b, len(b))
|
||||
s2, _ := cidToString(b)
|
||||
assert.Equal(t, s, s2)
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice/synchandler"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto"
|
||||
"go.uber.org/zap"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func newWatcher(spaceId, aclId string, h synchandler.SyncHandler) (w *watcher, err error) {
|
||||
w = &watcher{
|
||||
aclId: aclId,
|
||||
spaceId: spaceId,
|
||||
handler: h,
|
||||
ready: make(chan struct{}),
|
||||
}
|
||||
if w.logId, err = cidToByte(aclId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type watcher struct {
|
||||
spaceId string
|
||||
aclId string
|
||||
logId []byte
|
||||
handler synchandler.SyncHandler
|
||||
ready chan struct{}
|
||||
isReady sync.Once
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *watcher) AddConsensusRecords(recs []*consensusproto.Record) {
|
||||
w.isReady.Do(func() {
|
||||
close(w.ready)
|
||||
})
|
||||
records := make([]*aclrecordproto.RawACLRecordWithId, 0, len(recs))
|
||||
|
||||
for _, rec := range recs {
|
||||
recId, err := cidToString(rec.Id)
|
||||
if err != nil {
|
||||
log.Error("received invalid id from consensus node", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
records = append(records, &aclrecordproto.RawACLRecordWithId{
|
||||
Payload: rec.Payload,
|
||||
Id: recId,
|
||||
})
|
||||
}
|
||||
|
||||
aclReq := &aclrecordproto.ACLSyncMessage{
|
||||
Content: &aclrecordproto.ACLSyncContentValue{
|
||||
Value: &aclrecordproto.ACLSyncContentValue_AddRecords{
|
||||
AddRecords: &aclrecordproto.ACLAddRecords{
|
||||
Records: records,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
payload, err := aclReq.Marshal()
|
||||
if err != nil {
|
||||
log.Error("acl payload marshal error", zap.Error(err))
|
||||
return
|
||||
}
|
||||
req := &spacesyncproto.ObjectSyncMessage{
|
||||
SpaceId: w.spaceId,
|
||||
Payload: payload,
|
||||
ObjectId: w.aclId,
|
||||
}
|
||||
|
||||
if err = w.handler.HandleMessage(context.TODO(), "", req); err != nil {
|
||||
log.Warn("handle message error", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (w *watcher) AddConsensusError(err error) {
|
||||
w.isReady.Do(func() {
|
||||
w.err = err
|
||||
close(w.ready)
|
||||
})
|
||||
}
|
||||
|
||||
func (w *watcher) Ready(ctx context.Context) (err error) {
|
||||
select {
|
||||
case <-w.ready:
|
||||
return w.err
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func createListStorage(db *pogreb.DB, root *aclrecordproto.RawACLRecordWithId) (
|
||||
return
|
||||
}
|
||||
|
||||
func (l *listStorage) ID() string {
|
||||
func (l *listStorage) Id() string {
|
||||
return l.id
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
|
||||
spacestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage"
|
||||
storage2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
storage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
"go.uber.org/zap"
|
||||
"path"
|
||||
"sync"
|
||||
@@ -22,7 +22,7 @@ type spaceStorage struct {
|
||||
spaceId string
|
||||
objDb *pogreb.DB
|
||||
keys spaceKeys
|
||||
aclStorage storage2.ListStorage
|
||||
aclStorage storage.ListStorage
|
||||
header *spacesyncproto.RawSpaceHeaderWithId
|
||||
mx sync.Mutex
|
||||
}
|
||||
@@ -88,8 +88,8 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
||||
}
|
||||
|
||||
defer func() {
|
||||
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
||||
if err != nil {
|
||||
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
||||
db.Close()
|
||||
}
|
||||
}()
|
||||
@@ -129,15 +129,15 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
||||
return
|
||||
}
|
||||
|
||||
func (s *spaceStorage) ID() string {
|
||||
func (s *spaceStorage) Id() string {
|
||||
return s.spaceId
|
||||
}
|
||||
|
||||
func (s *spaceStorage) TreeStorage(id string) (storage2.TreeStorage, error) {
|
||||
func (s *spaceStorage) TreeStorage(id string) (storage.TreeStorage, error) {
|
||||
return newTreeStorage(s.objDb, id)
|
||||
}
|
||||
|
||||
func (s *spaceStorage) CreateTreeStorage(payload storage2.TreeStorageCreatePayload) (ts storage2.TreeStorage, err error) {
|
||||
func (s *spaceStorage) CreateTreeStorage(payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) {
|
||||
// we have mutex here, so we prevent overwriting the heads of a tree on concurrent creation
|
||||
s.mx.Lock()
|
||||
defer s.mx.Unlock()
|
||||
@@ -145,7 +145,7 @@ func (s *spaceStorage) CreateTreeStorage(payload storage2.TreeStorageCreatePaylo
|
||||
return createTreeStorage(s.objDb, payload)
|
||||
}
|
||||
|
||||
func (s *spaceStorage) ACLStorage() (storage2.ListStorage, error) {
|
||||
func (s *spaceStorage) ACLStorage() (storage.ListStorage, error) {
|
||||
return s.aclStorage, nil
|
||||
}
|
||||
|
||||
@@ -156,13 +156,13 @@ func (s *spaceStorage) SpaceHeader() (header *spacesyncproto.RawSpaceHeaderWithI
|
||||
func (s *spaceStorage) StoredIds() (ids []string, err error) {
|
||||
index := s.objDb.Items()
|
||||
|
||||
key, _, err := index.Next()
|
||||
key, val, err := index.Next()
|
||||
for err == nil {
|
||||
strKey := string(key)
|
||||
if isRootIdKey(strKey) {
|
||||
ids = append(ids, getRootId(strKey))
|
||||
ids = append(ids, string(val))
|
||||
}
|
||||
key, _, err = index.Next()
|
||||
key, val, err = index.Next()
|
||||
}
|
||||
|
||||
if err != pogreb.ErrIterationDone {
|
||||
|
||||
@@ -3,7 +3,7 @@ package storage
|
||||
import (
|
||||
"context"
|
||||
"github.com/akrylysov/pogreb"
|
||||
storage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto"
|
||||
)
|
||||
|
||||
@@ -48,7 +48,7 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e
|
||||
}
|
||||
|
||||
func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) {
|
||||
keys := newTreeKeys(payload.RootRawChange.Id)
|
||||
keys := newTreeKeys(payload.TreeId)
|
||||
has, err := db.Has(keys.HeadsKey())
|
||||
if err != nil {
|
||||
return
|
||||
@@ -86,7 +86,7 @@ func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload)
|
||||
return
|
||||
}
|
||||
|
||||
func (t *treeStorage) ID() string {
|
||||
func (t *treeStorage) Id() string {
|
||||
return t.id
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user