Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

invalidate the cache when a fallback key is uploaded #8501

Merged
merged 4 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8501.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for olm fallback keys ([MSC2732](https://github.com/matrix-org/matrix-doc/pull/2732)).
4 changes: 4 additions & 0 deletions synapse/storage/databases/main/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ async def set_e2e_fallback_keys(
desc="set_e2e_fallback_key",
)

await self.invalidate_cache_and_stream(
"get_e2e_unused_fallback_key_types", (user_id, device_id)
)

@cached(max_entries=10000)
async def get_e2e_unused_fallback_key_types(
self, user_id: str, device_id: str
Expand Down
20 changes: 20 additions & 0 deletions tests/handlers/test_e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.hs = None # type: synapse.server.HomeServer
self.handler = None # type: synapse.handlers.e2e_keys.E2eKeysHandler
self.store = None # type: synapse.storage.Storage

@defer.inlineCallbacks
def setUp(self):
self.hs = yield utils.setup_test_homeserver(
self.addCleanup, handlers=None, federation_client=mock.Mock()
)
self.handler = synapse.handlers.e2e_keys.E2eKeysHandler(self.hs)
self.store = self.hs.get_datastore()

@defer.inlineCallbacks
def test_query_local_devices_no_devices(self):
Expand Down Expand Up @@ -178,6 +180,12 @@ def test_fallback_key(self):
fallback_key = {"alg1:k1": "key1"}
otk = {"alg1:k2": "key2"}

# we shouldn't have any unused fallback keys yet
res = yield defer.ensureDeferred(
self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
)
self.assertEqual(res, [])

yield defer.ensureDeferred(
self.handler.upload_keys_for_user(
local_user,
Expand All @@ -186,6 +194,12 @@ def test_fallback_key(self):
)
)

# we should now have an unused alg1 key
res = yield defer.ensureDeferred(
self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
)
self.assertEqual(res, ["alg1"])

# claiming an OTK when no OTKs are available should return the fallback
# key
res = yield defer.ensureDeferred(
Expand All @@ -198,6 +212,12 @@ def test_fallback_key(self):
{"failures": {}, "one_time_keys": {local_user: {device_id: fallback_key}}},
)

# we shouldn't have any unused fallback keys again
res = yield defer.ensureDeferred(
self.store.get_e2e_unused_fallback_key_types(local_user, device_id)
)
self.assertEqual(res, [])

# claiming an OTK again should return the same fallback key
res = yield defer.ensureDeferred(
self.handler.claim_one_time_keys(
Expand Down