2018-10-12 04:26:18 -06:00
|
|
|
# Copyright 2017, 2018 New Vector Ltd
|
2019-11-27 14:14:44 -07:00
|
|
|
# Copyright 2019 Matrix.org Foundation C.I.C.
|
2017-12-04 18:29:25 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
2022-07-21 06:01:52 -06:00
|
|
|
from typing import TYPE_CHECKING, Dict, Optional, cast
|
2021-12-14 10:46:47 -07:00
|
|
|
|
|
|
|
from typing_extensions import Literal
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2019-02-06 19:32:52 -07:00
|
|
|
from synapse.api.errors import (
|
|
|
|
Codes,
|
|
|
|
NotFoundError,
|
|
|
|
RoomKeysVersionError,
|
|
|
|
StoreError,
|
2019-02-06 20:18:41 -07:00
|
|
|
SynapseError,
|
2019-02-06 19:32:52 -07:00
|
|
|
)
|
2019-08-22 04:28:12 -06:00
|
|
|
from synapse.logging.opentracing import log_kv, trace
|
2021-12-14 10:46:47 -07:00
|
|
|
from synapse.storage.databases.main.e2e_room_keys import RoomKey
|
2021-01-28 06:34:19 -07:00
|
|
|
from synapse.types import JsonDict
|
2018-08-24 20:51:25 -06:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2021-01-28 06:34:19 -07:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2021-01-28 06:34:19 -07:00
|
|
|
|
2017-12-04 18:29:25 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class E2eRoomKeysHandler:
|
2017-12-17 18:52:46 -07:00
|
|
|
"""
|
|
|
|
Implements an optional realtime backup mechanism for encrypted E2E megolm room keys.
|
|
|
|
This gives a way for users to store and recover their megolm keys if they lose all
|
|
|
|
their clients. It should also extend easily to future room key mechanisms.
|
|
|
|
The actual payload of the encrypted keys is completely opaque to the handler.
|
|
|
|
"""
|
|
|
|
|
2021-01-28 06:34:19 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2017-12-17 18:52:46 -07:00
|
|
|
|
|
|
|
# Used to lock whenever a client is uploading key data. This prevents collisions
|
|
|
|
# between clients trying to upload the details of a new session, given all
|
|
|
|
# clients belonging to a user will receive and try to upload a new session at
|
|
|
|
# roughly the same time. Also used to lock out uploads when the key is being
|
|
|
|
# changed.
|
2017-12-05 14:44:25 -07:00
|
|
|
self._upload_linearizer = Linearizer("upload_room_keys_lock")
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def get_room_keys(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
2021-12-14 10:46:47 -07:00
|
|
|
) -> Dict[
|
|
|
|
Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
|
|
|
|
]:
|
2017-12-27 16:37:44 -07:00
|
|
|
"""Bulk get the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room, or a given session.
|
|
|
|
See EndToEndRoomKeyStore.get_e2e_room_keys for full details.
|
|
|
|
|
2018-10-12 04:26:18 -06:00
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose keys we're getting
|
|
|
|
version: the version ID of the backup we're getting keys from
|
|
|
|
room_id: room ID to get keys for, for None to get keys for all rooms
|
|
|
|
session_id: session ID to get keys for, for None to get keys for all
|
2018-10-12 06:47:43 -06:00
|
|
|
sessions
|
2018-11-05 15:59:29 -07:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the backup version does not exist
|
2017-12-27 16:37:44 -07:00
|
|
|
Returns:
|
2021-12-14 10:46:47 -07:00
|
|
|
A dict giving the session_data and message metadata for these room keys.
|
|
|
|
`{"rooms": {room_id: {"sessions": {session_id: room_key}}}}`
|
2017-12-27 16:37:44 -07:00
|
|
|
"""
|
|
|
|
|
2017-12-05 18:02:57 -07:00
|
|
|
# we deliberately take the lock to get keys so that changing the version
|
|
|
|
# works atomically
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2018-11-05 15:59:29 -07:00
|
|
|
# make sure the backup version exists
|
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.get_e2e_room_keys_version_info(user_id, version)
|
2018-11-05 15:59:29 -07:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
results = await self.store.get_e2e_room_keys(
|
2017-12-05 18:02:57 -07:00
|
|
|
user_id, version, room_id, session_id
|
|
|
|
)
|
2017-12-31 10:47:11 -07:00
|
|
|
|
2022-07-21 06:01:52 -06:00
|
|
|
log_kv(cast(JsonDict, results))
|
2019-07-23 07:00:55 -06:00
|
|
|
return results
|
2017-12-04 18:29:25 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def delete_room_keys(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
version: str,
|
|
|
|
room_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> JsonDict:
|
2017-12-27 16:37:44 -07:00
|
|
|
"""Bulk delete the E2E room keys for a given backup, optionally filtered to a given
|
|
|
|
room or a given session.
|
|
|
|
See EndToEndRoomKeyStore.delete_e2e_room_keys for full details.
|
|
|
|
|
2018-10-12 04:26:18 -06:00
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose backup we're deleting
|
|
|
|
version: the version ID of the backup we're deleting
|
|
|
|
room_id: room ID to delete keys for, for None to delete keys for all
|
2018-10-12 06:47:43 -06:00
|
|
|
rooms
|
2021-01-28 06:34:19 -07:00
|
|
|
session_id: session ID to delete keys for, for None to delete keys
|
2018-10-12 06:47:43 -06:00
|
|
|
for all sessions
|
2019-11-27 14:14:44 -07:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the backup version does not exist
|
2017-12-27 16:37:44 -07:00
|
|
|
Returns:
|
2019-11-27 14:14:44 -07:00
|
|
|
A dict containing the count and etag for the backup version
|
2017-12-27 16:37:44 -07:00
|
|
|
"""
|
|
|
|
|
2017-12-17 18:52:46 -07:00
|
|
|
# lock for consistency with uploading
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2019-11-27 14:14:44 -07:00
|
|
|
# make sure the backup version exists
|
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(
|
2019-11-27 14:14:44 -07:00
|
|
|
user_id, version
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.delete_e2e_room_keys(user_id, version, room_id, session_id)
|
2017-12-05 10:54:48 -07:00
|
|
|
|
2019-11-27 14:14:44 -07:00
|
|
|
version_etag = version_info["etag"] + 1
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-11-27 14:14:44 -07:00
|
|
|
user_id, version, None, version_etag
|
|
|
|
)
|
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
count = await self.store.count_e2e_room_keys(user_id, version)
|
2019-11-27 14:14:44 -07:00
|
|
|
return {"etag": str(version_etag), "count": count}
|
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def upload_room_keys(
|
|
|
|
self, user_id: str, version: str, room_keys: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-12-27 16:37:44 -07:00
|
|
|
"""Bulk upload a list of room keys into a given backup version, asserting
|
|
|
|
that the given version is the current backup version. room_keys are merged
|
|
|
|
into the current backup as described in RoomKeysServlet.on_PUT().
|
|
|
|
|
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose backup we're setting
|
|
|
|
version: the version ID of the backup we're updating
|
|
|
|
room_keys: a nested dict describing the room_keys we're setting:
|
2017-12-27 16:37:44 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
"rooms": {
|
|
|
|
"!abc:matrix.org": {
|
|
|
|
"sessions": {
|
|
|
|
"c0ff33": {
|
|
|
|
"first_message_index": 1,
|
|
|
|
"forwarded_count": 1,
|
|
|
|
"is_verified": false,
|
|
|
|
"session_data": "SSBBTSBBIEZJU0gK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:14:44 -07:00
|
|
|
Returns:
|
|
|
|
A dict containing the count and etag for the backup version
|
|
|
|
|
2017-12-27 16:37:44 -07:00
|
|
|
Raises:
|
2018-11-05 15:59:29 -07:00
|
|
|
NotFoundError: if there are no versions defined
|
2017-12-27 16:37:44 -07:00
|
|
|
RoomKeysVersionError: if the uploaded version is not the current version
|
|
|
|
"""
|
|
|
|
|
2017-12-04 18:29:25 -07:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
2017-12-05 10:54:48 -07:00
|
|
|
# XXX: perhaps we should use a finer grained lock here?
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2017-12-31 10:47:11 -07:00
|
|
|
|
2017-12-17 18:52:46 -07:00
|
|
|
# Check that the version we're trying to upload is the current version
|
2017-12-27 16:42:08 -07:00
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(user_id)
|
2017-12-17 18:52:46 -07:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 15:59:29 -07:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-17 18:52:46 -07:00
|
|
|
else:
|
2018-10-12 04:26:18 -06:00
|
|
|
raise
|
2017-12-17 18:52:46 -07:00
|
|
|
|
|
|
|
if version_info["version"] != version:
|
2017-12-31 10:47:11 -07:00
|
|
|
# Check that the version we're trying to upload actually exists
|
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(
|
2018-10-12 06:47:43 -06:00
|
|
|
user_id, version
|
|
|
|
)
|
2017-12-31 10:47:11 -07:00
|
|
|
# if we get this far, the version must exist
|
|
|
|
raise RoomKeysVersionError(current_version=version_info["version"])
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 15:59:29 -07:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-31 10:47:11 -07:00
|
|
|
else:
|
2018-10-12 04:26:18 -06:00
|
|
|
raise
|
2017-12-17 18:52:46 -07:00
|
|
|
|
2019-11-27 14:14:44 -07:00
|
|
|
# Fetch any existing room keys for the sessions that have been
|
|
|
|
# submitted. Then compare them with the submitted keys. If the
|
|
|
|
# key is new, insert it; if the key should be updated, then update
|
|
|
|
# it; otherwise, drop it.
|
2020-07-15 06:48:58 -06:00
|
|
|
existing_keys = await self.store.get_e2e_room_keys_multi(
|
2019-11-27 14:14:44 -07:00
|
|
|
user_id, version, room_keys["rooms"]
|
|
|
|
)
|
|
|
|
to_insert = [] # batch the inserts together
|
|
|
|
changed = False # if anything has changed, we need to update the etag
|
2020-06-15 05:03:36 -06:00
|
|
|
for room_id, room in room_keys["rooms"].items():
|
|
|
|
for session_id, room_key in room["sessions"].items():
|
2020-03-06 04:02:52 -07:00
|
|
|
if not isinstance(room_key["is_verified"], bool):
|
|
|
|
msg = (
|
2020-03-06 08:07:28 -07:00
|
|
|
"is_verified must be a boolean in keys for session %s in"
|
2020-03-06 08:11:13 -07:00
|
|
|
"room %s" % (session_id, room_id)
|
2020-03-06 04:02:52 -07:00
|
|
|
)
|
|
|
|
raise SynapseError(400, msg, Codes.INVALID_PARAM)
|
|
|
|
|
2019-11-27 14:14:44 -07:00
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Trying to upload room key",
|
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
"user_id": user_id,
|
|
|
|
}
|
2017-12-05 10:54:48 -07:00
|
|
|
)
|
2019-11-27 14:14:44 -07:00
|
|
|
current_room_key = existing_keys.get(room_id, {}).get(session_id)
|
|
|
|
if current_room_key:
|
|
|
|
if self._should_replace_room_key(current_room_key, room_key):
|
|
|
|
log_kv({"message": "Replacing room key."})
|
|
|
|
# updates are done one at a time in the DB, so send
|
|
|
|
# updates right away rather than batching them up,
|
|
|
|
# like we do with the inserts
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.update_e2e_room_key(
|
2019-11-27 14:14:44 -07:00
|
|
|
user_id, version, room_id, session_id, room_key
|
|
|
|
)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
log_kv({"message": "Not replacing room_key."})
|
|
|
|
else:
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Room key not found.",
|
|
|
|
"room_id": room_id,
|
|
|
|
"user_id": user_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
log_kv({"message": "Replacing room key."})
|
|
|
|
to_insert.append((room_id, session_id, room_key))
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if len(to_insert):
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.add_e2e_room_keys(user_id, version, to_insert)
|
2019-11-27 14:14:44 -07:00
|
|
|
|
|
|
|
version_etag = version_info["etag"]
|
|
|
|
if changed:
|
|
|
|
version_etag = version_etag + 1
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-11-27 14:14:44 -07:00
|
|
|
user_id, version, None, version_etag
|
2019-08-22 04:28:12 -06:00
|
|
|
)
|
2017-12-05 14:44:25 -07:00
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
count = await self.store.count_e2e_room_keys(user_id, version)
|
2019-11-27 14:14:44 -07:00
|
|
|
return {"etag": str(version_etag), "count": count}
|
2017-12-17 18:52:46 -07:00
|
|
|
|
2017-12-24 08:03:44 -07:00
|
|
|
@staticmethod
|
2021-01-28 06:34:19 -07:00
|
|
|
def _should_replace_room_key(
|
2021-12-14 10:46:47 -07:00
|
|
|
current_room_key: Optional[RoomKey], room_key: RoomKey
|
2021-01-28 06:34:19 -07:00
|
|
|
) -> bool:
|
2017-12-17 18:52:46 -07:00
|
|
|
"""
|
2017-12-24 08:03:44 -07:00
|
|
|
Determine whether to replace a given current_room_key (if any)
|
|
|
|
with a newly uploaded room_key backup
|
2017-12-17 18:52:46 -07:00
|
|
|
|
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
current_room_key: Optional, the current room_key dict if any
|
|
|
|
room_key : The new room_key dict which may or may not be fit to
|
2017-12-17 18:52:46 -07:00
|
|
|
replace the current_room_key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if current_room_key should be replaced by room_key in the backup
|
|
|
|
"""
|
|
|
|
|
2017-12-05 14:44:25 -07:00
|
|
|
if current_room_key:
|
2017-12-17 18:52:46 -07:00
|
|
|
# spelt out with if/elifs rather than nested boolean expressions
|
|
|
|
# purely for legibility.
|
|
|
|
|
2017-12-05 14:44:25 -07:00
|
|
|
if room_key["is_verified"] and not current_room_key["is_verified"]:
|
2018-10-12 04:26:18 -06:00
|
|
|
return True
|
2017-12-05 14:44:25 -07:00
|
|
|
elif (
|
|
|
|
room_key["first_message_index"]
|
|
|
|
< current_room_key["first_message_index"]
|
|
|
|
):
|
2018-10-12 04:26:18 -06:00
|
|
|
return True
|
2017-12-17 18:52:46 -07:00
|
|
|
elif room_key["forwarded_count"] < current_room_key["forwarded_count"]:
|
2018-10-12 04:26:18 -06:00
|
|
|
return True
|
2017-12-05 14:44:25 -07:00
|
|
|
else:
|
2017-12-17 18:52:46 -07:00
|
|
|
return False
|
|
|
|
return True
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def create_version(self, user_id: str, version_info: JsonDict) -> str:
|
2017-12-27 16:37:44 -07:00
|
|
|
"""Create a new backup version. This automatically becomes the new
|
|
|
|
backup version for the user's keys; previous backups will no longer be
|
|
|
|
writeable to.
|
|
|
|
|
2017-12-27 16:58:51 -07:00
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose backup version we're creating
|
|
|
|
version_info: metadata about the new version being created
|
2017-12-27 16:37:44 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
|
|
|
|
Returns:
|
2021-01-28 06:34:19 -07:00
|
|
|
The new version number.
|
2017-12-27 16:37:44 -07:00
|
|
|
"""
|
|
|
|
|
2017-12-05 18:02:57 -07:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
|
|
|
# lock everyone out until we've switched version
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2020-07-15 06:48:58 -06:00
|
|
|
new_version = await self.store.create_e2e_room_keys_version(
|
2017-12-06 02:02:49 -07:00
|
|
|
user_id, version_info
|
2017-12-05 18:02:57 -07:00
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
return new_version
|
2017-12-05 18:02:57 -07:00
|
|
|
|
2021-01-28 06:34:19 -07:00
|
|
|
async def get_version_info(
|
|
|
|
self, user_id: str, version: Optional[str] = None
|
|
|
|
) -> JsonDict:
|
2017-12-31 07:10:31 -07:00
|
|
|
"""Get the info about a given version of the user's backup
|
2017-12-27 16:37:44 -07:00
|
|
|
|
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose current backup version we're querying
|
|
|
|
version: Optional; if None gives the most recent version
|
2017-12-31 07:10:31 -07:00
|
|
|
otherwise a historical one.
|
2017-12-27 16:37:44 -07:00
|
|
|
Raises:
|
2019-02-06 15:47:22 -07:00
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
2017-12-27 16:37:44 -07:00
|
|
|
Returns:
|
2021-01-28 06:34:19 -07:00
|
|
|
A info dict that gives the info about the new version.
|
2017-12-27 16:37:44 -07:00
|
|
|
|
|
|
|
{
|
2017-12-31 07:10:31 -07:00
|
|
|
"version": "1234",
|
2017-12-27 16:37:44 -07:00
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2019-02-06 15:47:22 -07:00
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
res = await self.store.get_e2e_room_keys_version_info(user_id, version)
|
2019-02-06 15:47:22 -07:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2019-11-27 14:14:44 -07:00
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
res["count"] = await self.store.count_e2e_room_keys(user_id, res["version"])
|
2020-06-15 11:42:44 -06:00
|
|
|
res["etag"] = str(res["etag"])
|
2019-07-23 07:00:55 -06:00
|
|
|
return res
|
2017-12-31 10:47:11 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def delete_version(self, user_id: str, version: Optional[str] = None) -> None:
|
2017-12-27 16:37:44 -07:00
|
|
|
"""Deletes a given version of the user's e2e_room_keys backup
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user whose current backup version we're deleting
|
|
|
|
version(str): the version id of the backup being deleted
|
|
|
|
Raises:
|
2019-02-06 15:47:22 -07:00
|
|
|
NotFoundError: if this backup version doesn't exist
|
2017-12-27 16:37:44 -07:00
|
|
|
"""
|
|
|
|
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2019-02-06 15:47:22 -07:00
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.delete_e2e_room_keys_version(user_id, version)
|
2019-02-06 15:47:22 -07:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2019-02-06 15:57:10 -07:00
|
|
|
|
2019-08-22 04:28:12 -06:00
|
|
|
@trace
|
2021-01-28 06:34:19 -07:00
|
|
|
async def update_version(
|
|
|
|
self, user_id: str, version: str, version_info: JsonDict
|
|
|
|
) -> JsonDict:
|
2019-02-06 15:57:10 -07:00
|
|
|
"""Update the info about a given version of the user's backup
|
|
|
|
|
|
|
|
Args:
|
2021-01-28 06:34:19 -07:00
|
|
|
user_id: the user whose current backup version we're updating
|
|
|
|
version: the backup version we're updating
|
|
|
|
version_info: the new information about the backup
|
2019-02-06 15:57:10 -07:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
|
|
|
Returns:
|
2021-01-28 06:34:19 -07:00
|
|
|
An empty dict.
|
2019-02-06 15:57:10 -07:00
|
|
|
"""
|
2019-02-07 23:32:45 -07:00
|
|
|
if "version" not in version_info:
|
2019-10-09 15:54:03 -06:00
|
|
|
version_info["version"] = version
|
|
|
|
elif version_info["version"] != version:
|
2019-02-06 15:57:10 -07:00
|
|
|
raise SynapseError(
|
2019-02-07 23:32:45 -07:00
|
|
|
400, "Version in body does not match", Codes.INVALID_PARAM
|
2019-02-06 15:57:10 -07:00
|
|
|
)
|
2022-04-05 08:43:52 -06:00
|
|
|
async with self._upload_linearizer.queue(user_id):
|
2019-02-07 23:32:45 -07:00
|
|
|
try:
|
2020-07-15 06:48:58 -06:00
|
|
|
old_info = await self.store.get_e2e_room_keys_version_info(
|
2019-02-07 23:32:45 -07:00
|
|
|
user_id, version
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
if old_info["algorithm"] != version_info["algorithm"]:
|
|
|
|
raise SynapseError(400, "Algorithm does not match", Codes.INVALID_PARAM)
|
2019-02-06 15:57:10 -07:00
|
|
|
|
2020-07-15 06:48:58 -06:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-02-07 23:32:45 -07:00
|
|
|
user_id, version, version_info
|
|
|
|
)
|
2019-02-06 15:57:10 -07:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {}
|