mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-10 01:51:11 +09:00
Change sym encryption logic in objecttree
This commit is contained in:
parent
f4bc677d02
commit
13f30aa60f
2 changed files with 30 additions and 22 deletions
|
@ -43,14 +43,5 @@ func (h *historyTree) rebuildFromStorage(beforeId string, include bool) (err err
|
||||||
defer ot.aclList.RUnlock()
|
defer ot.aclList.RUnlock()
|
||||||
state := ot.aclList.AclState()
|
state := ot.aclList.AclState()
|
||||||
|
|
||||||
if len(ot.keys) != len(state.UserReadKeys()) {
|
return ot.readKeysFromAclState(state)
|
||||||
for key, value := range state.UserReadKeys() {
|
|
||||||
treeKey, err := deriveTreeKey(value, h.id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ot.keys[key] = treeKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ var (
|
||||||
ErrHasInvalidChanges = errors.New("the change is invalid")
|
ErrHasInvalidChanges = errors.New("the change is invalid")
|
||||||
ErrNoCommonSnapshot = errors.New("trees doesn't have a common snapshot")
|
ErrNoCommonSnapshot = errors.New("trees doesn't have a common snapshot")
|
||||||
ErrNoChangeInTree = errors.New("no such change in tree")
|
ErrNoChangeInTree = errors.New("no such change in tree")
|
||||||
|
ErrMissingKey = errors.New("missing current read key")
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddResultSummary int
|
type AddResultSummary int
|
||||||
|
@ -99,7 +100,8 @@ type objectTree struct {
|
||||||
root *Change
|
root *Change
|
||||||
tree *Tree
|
tree *Tree
|
||||||
|
|
||||||
keys map[string]crypto.SymKey
|
keys map[string]crypto.SymKey
|
||||||
|
currentReadKey crypto.SymKey
|
||||||
|
|
||||||
// buffers
|
// buffers
|
||||||
difSnapshotBuf []*treechangeproto.RawTreeChangeWithId
|
difSnapshotBuf []*treechangeproto.RawTreeChangeWithId
|
||||||
|
@ -238,10 +240,11 @@ func (ot *objectTree) prepareBuilderContent(content SignableChangeContent) (cnt
|
||||||
|
|
||||||
if content.IsEncrypted {
|
if content.IsEncrypted {
|
||||||
readKeyId = state.CurrentReadKeyId()
|
readKeyId = state.CurrentReadKeyId()
|
||||||
readKey, err = state.CurrentReadKey()
|
if ot.currentReadKey == nil {
|
||||||
if err != nil {
|
err = ErrMissingKey
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
readKey = ot.currentReadKey
|
||||||
}
|
}
|
||||||
cnt = BuilderContent{
|
cnt = BuilderContent{
|
||||||
TreeHeadIds: ot.tree.Heads(),
|
TreeHeadIds: ot.tree.Heads(),
|
||||||
|
@ -637,15 +640,9 @@ func (ot *objectTree) validateTree(newChanges []*Change) error {
|
||||||
defer ot.aclList.RUnlock()
|
defer ot.aclList.RUnlock()
|
||||||
state := ot.aclList.AclState()
|
state := ot.aclList.AclState()
|
||||||
|
|
||||||
// just not to take lock many times, updating the key map from aclList
|
err := ot.readKeysFromAclState(state)
|
||||||
if len(ot.keys) != len(state.UserReadKeys()) {
|
if err != nil {
|
||||||
for key, value := range state.UserReadKeys() {
|
return err
|
||||||
treeKey, err := deriveTreeKey(value, ot.id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ot.keys[key] = treeKey
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(newChanges) == 0 {
|
if len(newChanges) == 0 {
|
||||||
return ot.validator.ValidateFullTree(ot.tree, ot.aclList)
|
return ot.validator.ValidateFullTree(ot.tree, ot.aclList)
|
||||||
|
@ -654,6 +651,26 @@ func (ot *objectTree) validateTree(newChanges []*Change) error {
|
||||||
return ot.validator.ValidateNewChanges(ot.tree, ot.aclList, newChanges)
|
return ot.validator.ValidateNewChanges(ot.tree, ot.aclList, newChanges)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ot *objectTree) readKeysFromAclState(state *list.AclState) (err error) {
|
||||||
|
// just not to take lock many times, updating the key map from aclList
|
||||||
|
if len(ot.keys) == len(state.UserReadKeys()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for key, value := range state.UserReadKeys() {
|
||||||
|
treeKey, err := deriveTreeKey(value, ot.id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ot.keys[key] = treeKey
|
||||||
|
}
|
||||||
|
curKey, err := state.CurrentReadKey()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ot.currentReadKey, err = deriveTreeKey(curKey, ot.id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (ot *objectTree) Debug(parser DescriptionParser) (DebugInfo, error) {
|
func (ot *objectTree) Debug(parser DescriptionParser) (DebugInfo, error) {
|
||||||
return objectTreeDebug{}.debugInfo(ot, parser)
|
return objectTreeDebug{}.debugInfo(ot, parser)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue