protondrive: don't fail a key lookup because another caller gave up
A caller waiting for another's fetch of the same public keys got that caller's context error if it was cancelled; it now fetches them itself. Also: - build each own address's public key ring once at sign-in rather than on every lookup, 2-3 times per decrypted node - a listing caching a node under a new parent forgets the old parent's listing, as caching a single node already did Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d934016ba2
commit
272fbe5ccb
5 files changed
+99
-30
No files matched your search
@@ -22,6 +22,7 @@ type address struct {
|
||||
ID string
|
||||
Email string
|
||||
keyRing *crypto.KeyRing // unlocked address keys in the server's order, primary first
|
||||
publicKR *crypto.KeyRing // the public parts of keyRing
|
||||
primaryKeyID string // ID of the first key of keyRing
|
||||
}
|
||||
|
||||
@@ -80,6 +81,10 @@ func loadAccount(ctx context.Context, s *session, keyPassword []byte) (*account,
|
||||
}
|
||||
for i, addr := range addrs.Addresses {
|
||||
kr, primaryKeyID, err := unlockAddressKeys(addr.Keys, keyPassword, a.userKR, verifyTime)
|
||||
var publicKR *crypto.KeyRing
|
||||
if err == nil {
|
||||
publicKR, err = publicKeyRing(kr)
|
||||
}
|
||||
if err != nil {
|
||||
if i == 0 {
|
||||
a.close()
|
||||
@@ -88,7 +93,7 @@ func loadAccount(ctx context.Context, s *session, keyPassword []byte) (*account,
|
||||
fs.Debugf(nil, "protondrive: skipping address %s: %v", addr.ID, err)
|
||||
continue
|
||||
}
|
||||
a.addresses = append(a.addresses, &address{ID: addr.ID, Email: addr.Email, keyRing: kr, primaryKeyID: primaryKeyID})
|
||||
a.addresses = append(a.addresses, &address{ID: addr.ID, Email: addr.Email, keyRing: kr, publicKR: publicKR, primaryKeyID: primaryKeyID})
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -276,31 +281,48 @@ type apiCaller func(ctx context.Context, opts *rest.Opts, request, response any)
|
||||
// a Proton address has an empty key ring.
|
||||
func (a *account) publicKeys(ctx context.Context, call apiCaller, email string) (*crypto.KeyRing, error) {
|
||||
if addr := a.byEmail(email); addr != nil {
|
||||
return publicKeyRing(addr.keyRing)
|
||||
return addr.publicKR, nil
|
||||
}
|
||||
key := emailKey(email)
|
||||
a.mu.Lock()
|
||||
l := a.foreign[key]
|
||||
if l == nil {
|
||||
l = &publicKeyLookup{done: make(chan struct{})}
|
||||
a.foreign[key] = l
|
||||
a.mu.Unlock()
|
||||
l.keyRing, l.err = fetchPublicKeys(ctx, call, email)
|
||||
if l.err != nil {
|
||||
// Let the next caller try again.
|
||||
a.mu.Lock()
|
||||
delete(a.foreign, key)
|
||||
for {
|
||||
a.mu.Lock()
|
||||
l := a.foreign[key]
|
||||
fetcher := l == nil
|
||||
if fetcher {
|
||||
l = &publicKeyLookup{done: make(chan struct{})}
|
||||
a.foreign[key] = l
|
||||
a.mu.Unlock()
|
||||
l.keyRing, l.err = fetchPublicKeys(ctx, call, email)
|
||||
if l.err != nil {
|
||||
// Let the next caller try again.
|
||||
a.forgetLookup(key, l)
|
||||
}
|
||||
close(l.done)
|
||||
} else {
|
||||
a.mu.Unlock()
|
||||
}
|
||||
close(l.done)
|
||||
} else {
|
||||
a.mu.Unlock()
|
||||
}
|
||||
select {
|
||||
case <-l.done:
|
||||
select {
|
||||
case <-l.done:
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
if !fetcher && ctx.Err() == nil && (errors.Is(l.err, context.Canceled) || errors.Is(l.err, context.DeadlineExceeded)) {
|
||||
// The fetching caller gave up, which says nothing of
|
||||
// this one.
|
||||
a.forgetLookup(key, l)
|
||||
continue
|
||||
}
|
||||
return l.keyRing, l.err
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// forgetLookup forgets the lookup l of the keys of key, if it is still
|
||||
// the one in use.
|
||||
func (a *account) forgetLookup(key string, l *publicKeyLookup) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if a.foreign[key] == l {
|
||||
delete(a.foreign, key)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -238,11 +238,16 @@ func TestPublicKeysCached(t *testing.T) {
|
||||
assert.Equal(t, 1, srv.Requests()[route])
|
||||
|
||||
srv.ResetRequests()
|
||||
var first *crypto.KeyRing
|
||||
for range 5 {
|
||||
kr, err := a.publicKeys(context.Background(), sessionCaller(s), u.Email())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, kr.CountEntities())
|
||||
assert.False(t, kr.GetKeys()[0].IsPrivate())
|
||||
if first == nil {
|
||||
first = kr
|
||||
}
|
||||
assert.Same(t, first, kr, "the own address's public keys are built again")
|
||||
}
|
||||
assert.Zero(t, srv.Requests()[route])
|
||||
|
||||
@@ -298,3 +303,28 @@ func TestSameEmail(t *testing.T) {
|
||||
assert.Equal(t, tc.same, emailKey(tc.a) == emailKey(tc.b), "%q %q", tc.a, tc.b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublicKeysWaiterRetries checks a caller waiting for another's
|
||||
// fetch of the same keys fetches them itself if the other gave up,
|
||||
// rather than failing with the other's context error.
|
||||
func TestPublicKeysWaiterRetries(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
other := srv.AddUser("other", "pass")
|
||||
s := newAccountSession(srv, u)
|
||||
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
|
||||
require.NoError(t, err)
|
||||
defer a.close()
|
||||
|
||||
// The lookup as a waiter sees it once the fetching caller's
|
||||
// context ended.
|
||||
l := &publicKeyLookup{done: make(chan struct{}), err: context.Canceled}
|
||||
close(l.done)
|
||||
a.mu.Lock()
|
||||
a.foreign[emailKey(other.Email())] = l
|
||||
a.mu.Unlock()
|
||||
kr, err := a.publicKeys(context.Background(), sessionCaller(s), other.Email())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, kr.CountEntities())
|
||||
}
|
||||
@@ -125,7 +125,8 @@ func (c *nodeCache) children(parentID string) ([]*node, bool) {
|
||||
}
|
||||
|
||||
// setChildren caches nodes as the complete listing of folder parentID,
|
||||
// and each of the nodes.
|
||||
// and each of the nodes. The listings of other parents a node was
|
||||
// cached under are forgotten.
|
||||
func (c *nodeCache) setChildren(parentID string, nodes []*node) {
|
||||
if c == nil {
|
||||
return
|
||||
@@ -142,6 +143,9 @@ func (c *nodeCache) setChildrenLocked(parentID string, nodes []*node) {
|
||||
ids := make([]string, len(nodes))
|
||||
for i, n := range nodes {
|
||||
ids[i] = n.ID
|
||||
if old := c.nodes[n.ID]; old != nil && old.ParentID != parentID {
|
||||
c.childrenChangedLocked(old.ParentID)
|
||||
}
|
||||
c.nodes[n.ID] = n
|
||||
}
|
||||
c.listings[parentID] = ids
|
||||
|
||||
@@ -271,3 +271,21 @@ func TestNodeCacheEmpty(t *testing.T) {
|
||||
assert.True(t, c.empty())
|
||||
c.endFetch("root", token, nil, false)
|
||||
}
|
||||
|
||||
// TestNodeCacheSetChildrenMoved checks a listing holding a node cached
|
||||
// under another parent forgets the other parent's listing, as put does.
|
||||
func TestNodeCacheSetChildrenMoved(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := newNodeCache(true)
|
||||
c.setChildren("a", []*node{{ID: "x", ParentID: "a"}, {ID: "y", ParentID: "a"}})
|
||||
token := c.startFetch("a")
|
||||
c.setChildren("b", []*node{{ID: "x", ParentID: "b"}})
|
||||
_, ok := c.children("a")
|
||||
assert.False(t, ok, "the old parent's listing still holds the node")
|
||||
got, ok := c.children("b")
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "b", got[0].ParentID)
|
||||
c.endFetch("a", token, []*node{{ID: "x", ParentID: "a"}}, true)
|
||||
_, ok = c.children("a")
|
||||
assert.False(t, ok, "a listing of the old parent begun before is cached")
|
||||
}
|
||||
+3
-8
@@ -269,15 +269,10 @@ workspace) not fixed in its fix wave. Paths are under `backend/protondrive/`.
|
||||
read the server-supplied `CreateTime`, so a server can back-date a node
|
||||
to exempt it (commented in `nodes.go`, as in the SDK).
|
||||
- [ ] Earlier deferred crypto minors still present: the node passphrase
|
||||
isn't checked as UTF-8 (`crypto.go` `decryptNodeKeys`); `publicKeys`
|
||||
waiters get the fetching caller's ctx error (`account.go`);
|
||||
`setChildrenLocked` overwrites a cached node without `put`'s
|
||||
changed-parent rule (`cache.go`); Move
|
||||
isn't checked as UTF-8 (`crypto.go` `decryptNodeKeys`); Move
|
||||
detects an anonymous node by `SignatureEmail == ""` (`nodes.go`
|
||||
`encryptForNewParent`).
|
||||
- [ ] `publicKeyRing` rebuilds the own-address public ring on every
|
||||
lookup, 2-3 times per decrypted node (`account.go`); build it once in
|
||||
`loadAccount`.
|
||||
`encryptForNewParent`). Both follow the SDK; changing either is a
|
||||
protocol judgement for the owner.
|
||||
- [ ] Two processes sharing one config (a mount and a cron job) can
|
||||
delete each other's in-progress draft of the same new name, since the
|
||||
draft client ID is per remote (`upload.go` `resolveConflict`).
|
||||
|
||||
Reference in new issue
Block a user