From a55d2a93c9599766446a37d7cc508f9ad9f39531 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 8 Oct 2020 11:57:20 -0400 Subject: [PATCH 1/4] invalidate the cache when a fallback key is uploaded --- .../storage/databases/main/end_to_end_keys.py | 4 ++++ tests/handlers/test_e2e_keys.py | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/synapse/storage/databases/main/end_to_end_keys.py b/synapse/storage/databases/main/end_to_end_keys.py index 359dc6e968c6..44159094147e 100644 --- a/synapse/storage/databases/main/end_to_end_keys.py +++ b/synapse/storage/databases/main/end_to_end_keys.py @@ -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 diff --git a/tests/handlers/test_e2e_keys.py b/tests/handlers/test_e2e_keys.py index 4e9e3dcbc267..b0ed3cbf4d09 100644 --- a/tests/handlers/test_e2e_keys.py +++ b/tests/handlers/test_e2e_keys.py @@ -33,6 +33,7 @@ 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): @@ -40,6 +41,7 @@ def setUp(self): 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): @@ -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, @@ -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( @@ -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( From 1737c78d10798032393fe9d246741f2f1c2372e3 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 8 Oct 2020 12:05:43 -0400 Subject: [PATCH 2/4] add changelog --- changelog.d/8501.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8501.bugfix diff --git a/changelog.d/8501.bugfix b/changelog.d/8501.bugfix new file mode 100644 index 000000000000..0fb825281bb7 --- /dev/null +++ b/changelog.d/8501.bugfix @@ -0,0 +1 @@ +Invalidate the cache when a fallback key is uploaded. From 17e19692ec99a5f0482da201384611caac7c3a51 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 8 Oct 2020 12:12:16 -0400 Subject: [PATCH 3/4] black --- tests/handlers/test_e2e_keys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/handlers/test_e2e_keys.py b/tests/handlers/test_e2e_keys.py index b0ed3cbf4d09..e79d612f7ae2 100644 --- a/tests/handlers/test_e2e_keys.py +++ b/tests/handlers/test_e2e_keys.py @@ -33,7 +33,7 @@ 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 + self.store = None # type: synapse.storage.Storage @defer.inlineCallbacks def setUp(self): From c14a7a32856c82f674ff248cd3048f34aae25946 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 8 Oct 2020 12:31:41 -0400 Subject: [PATCH 4/4] fix changelog so it gets grouped with original fallback implementation --- changelog.d/8501.bugfix | 1 - changelog.d/8501.feature | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 changelog.d/8501.bugfix create mode 100644 changelog.d/8501.feature diff --git a/changelog.d/8501.bugfix b/changelog.d/8501.bugfix deleted file mode 100644 index 0fb825281bb7..000000000000 --- a/changelog.d/8501.bugfix +++ /dev/null @@ -1 +0,0 @@ -Invalidate the cache when a fallback key is uploaded. diff --git a/changelog.d/8501.feature b/changelog.d/8501.feature new file mode 100644 index 000000000000..5220ddd48252 --- /dev/null +++ b/changelog.d/8501.feature @@ -0,0 +1 @@ +Add support for olm fallback keys ([MSC2732](https://github.com/matrix-org/matrix-doc/pull/2732)).