Change sym encryption logic in objecttree

This commit is contained in:
mcrakhman 2023-03-27 23:03:02 +02:00 committed by Mikhail Iudin
parent 8a4ed528b8
commit 61819f04a2
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 30 additions and 22 deletions

View File

@ -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
} }

View File

@ -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
@ -100,6 +101,7 @@ type objectTree struct {
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,16 +640,10 @@ 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()) {
for key, value := range state.UserReadKeys() {
treeKey, err := deriveTreeKey(value, ot.id)
if err != nil { if err != nil {
return err 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)
} }