2017-12-04 20:29:25 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2019 Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2017-12-04 20:29:25 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2022-07-21 08:01:52 -04:00
|
|
|
from typing import TYPE_CHECKING, Dict, Optional, cast
|
2021-12-14 12:46:47 -05:00
|
|
|
|
|
|
|
from typing_extensions import Literal
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2019-02-06 21:32:52 -05:00
|
|
|
from synapse.api.errors import (
|
|
|
|
Codes,
|
|
|
|
NotFoundError,
|
|
|
|
RoomKeysVersionError,
|
|
|
|
StoreError,
|
2019-02-06 22:18:41 -05:00
|
|
|
SynapseError,
|
2019-02-06 21:32:52 -05:00
|
|
|
)
|
2019-08-22 06:28:12 -04:00
|
|
|
from synapse.logging.opentracing import log_kv, trace
|
2021-12-14 12:46:47 -05:00
|
|
|
from synapse.storage.databases.main.e2e_room_keys import RoomKey
|
2021-01-28 08:34:19 -05:00
|
|
|
from synapse.types import JsonDict
|
2024-07-22 09:51:17 -04:00
|
|
|
from synapse.util.async_helpers import ReadWriteLock
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2021-01-28 08:34:19 -05:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2021-01-28 08:34:19 -05:00
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class E2eRoomKeysHandler:
|
2017-12-17 20:52:46 -05: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 08:34:19 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2017-12-17 20:52:46 -05: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.
|
2024-07-22 09:51:17 -04:00
|
|
|
self._upload_lock = ReadWriteLock()
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05: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 12:46:47 -05:00
|
|
|
) -> Dict[
|
|
|
|
Literal["rooms"], Dict[str, Dict[Literal["sessions"], Dict[str, RoomKey]]]
|
|
|
|
]:
|
2017-12-27 18:37:44 -05: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 06:26:18 -04:00
|
|
|
Args:
|
2021-01-28 08:34:19 -05: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 08:47:43 -04:00
|
|
|
sessions
|
2018-11-05 17:59:29 -05:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the backup version does not exist
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
2021-12-14 12:46:47 -05: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 18:37:44 -05:00
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
# we deliberately take the lock to get keys so that changing the version
|
|
|
|
# works atomically
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.read(user_id):
|
2018-11-05 17:59:29 -05:00
|
|
|
# make sure the backup version exists
|
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.get_e2e_room_keys_version_info(user_id, version)
|
2018-11-05 17:59:29 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
results = await self.store.get_e2e_room_keys(
|
2017-12-05 20:02:57 -05:00
|
|
|
user_id, version, room_id, session_id
|
|
|
|
)
|
2017-12-31 12:47:11 -05:00
|
|
|
|
2022-07-21 08:01:52 -04:00
|
|
|
log_kv(cast(JsonDict, results))
|
2019-07-23 09:00:55 -04:00
|
|
|
return results
|
2017-12-04 20:29:25 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05: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 18:37:44 -05: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 06:26:18 -04:00
|
|
|
Args:
|
2021-01-28 08:34:19 -05: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 08:47:43 -04:00
|
|
|
rooms
|
2021-01-28 08:34:19 -05:00
|
|
|
session_id: session ID to delete keys for, for None to delete keys
|
2018-10-12 08:47:43 -04:00
|
|
|
for all sessions
|
2019-11-27 16:14:44 -05:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the backup version does not exist
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
2019-11-27 16:14:44 -05:00
|
|
|
A dict containing the count and etag for the backup version
|
2017-12-27 18:37:44 -05:00
|
|
|
"""
|
|
|
|
|
2017-12-17 20:52:46 -05:00
|
|
|
# lock for consistency with uploading
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.write(user_id):
|
2019-11-27 16:14:44 -05:00
|
|
|
# make sure the backup version exists
|
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(
|
2019-11-27 16:14:44 -05:00
|
|
|
user_id, version
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.delete_e2e_room_keys(user_id, version, room_id, session_id)
|
2017-12-05 12:54:48 -05:00
|
|
|
|
2019-11-27 16:14:44 -05:00
|
|
|
version_etag = version_info["etag"] + 1
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-11-27 16:14:44 -05:00
|
|
|
user_id, version, None, version_etag
|
|
|
|
)
|
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
count = await self.store.count_e2e_room_keys(user_id, version)
|
2019-11-27 16:14:44 -05:00
|
|
|
return {"etag": str(version_etag), "count": count}
|
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05:00
|
|
|
async def upload_room_keys(
|
|
|
|
self, user_id: str, version: str, room_keys: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-12-27 18:37:44 -05: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 08:34:19 -05: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 18:37:44 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
"rooms": {
|
|
|
|
"!abc:matrix.org": {
|
|
|
|
"sessions": {
|
|
|
|
"c0ff33": {
|
|
|
|
"first_message_index": 1,
|
|
|
|
"forwarded_count": 1,
|
|
|
|
"is_verified": false,
|
|
|
|
"session_data": "SSBBTSBBIEZJU0gK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:14:44 -05:00
|
|
|
Returns:
|
|
|
|
A dict containing the count and etag for the backup version
|
|
|
|
|
2017-12-27 18:37:44 -05:00
|
|
|
Raises:
|
2018-11-05 17:59:29 -05:00
|
|
|
NotFoundError: if there are no versions defined
|
2017-12-27 18:37:44 -05:00
|
|
|
RoomKeysVersionError: if the uploaded version is not the current version
|
|
|
|
"""
|
|
|
|
|
2017-12-04 20:29:25 -05:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
2017-12-05 12:54:48 -05:00
|
|
|
# XXX: perhaps we should use a finer grained lock here?
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.write(user_id):
|
2017-12-17 20:52:46 -05:00
|
|
|
# Check that the version we're trying to upload is the current version
|
2017-12-27 18:42:08 -05:00
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(user_id)
|
2017-12-17 20:52:46 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 17:59:29 -05:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-17 20:52:46 -05:00
|
|
|
else:
|
2018-10-12 06:26:18 -04:00
|
|
|
raise
|
2017-12-17 20:52:46 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
if version_info["version"] != version:
|
2017-12-31 12:47:11 -05:00
|
|
|
# Check that the version we're trying to upload actually exists
|
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
version_info = await self.store.get_e2e_room_keys_version_info(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, version
|
2018-10-12 08:47:43 -04:00
|
|
|
)
|
2017-12-31 12:47:11 -05:00
|
|
|
# if we get this far, the version must exist
|
2019-06-20 05:32:02 -04:00
|
|
|
raise RoomKeysVersionError(current_version=version_info["version"])
|
2017-12-31 12:47:11 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
2018-11-05 17:59:29 -05:00
|
|
|
raise NotFoundError("Version '%s' not found" % (version,))
|
2017-12-31 12:47:11 -05:00
|
|
|
else:
|
2018-10-12 06:26:18 -04:00
|
|
|
raise
|
2017-12-17 20:52:46 -05:00
|
|
|
|
2019-11-27 16:14:44 -05: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 08:48:58 -04:00
|
|
|
existing_keys = await self.store.get_e2e_room_keys_multi(
|
2019-11-27 16:14:44 -05: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 07:03:36 -04:00
|
|
|
for room_id, room in room_keys["rooms"].items():
|
|
|
|
for session_id, room_key in room["sessions"].items():
|
2020-03-06 06:02:52 -05:00
|
|
|
if not isinstance(room_key["is_verified"], bool):
|
|
|
|
msg = (
|
2020-03-06 10:07:28 -05:00
|
|
|
"is_verified must be a boolean in keys for session %s in"
|
2020-03-06 10:11:13 -05:00
|
|
|
"room %s" % (session_id, room_id)
|
2020-03-06 06:02:52 -05:00
|
|
|
)
|
|
|
|
raise SynapseError(400, msg, Codes.INVALID_PARAM)
|
|
|
|
|
2019-11-27 16:14:44 -05:00
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Trying to upload room key",
|
|
|
|
"room_id": room_id,
|
|
|
|
"session_id": session_id,
|
|
|
|
"user_id": user_id,
|
|
|
|
}
|
2017-12-05 12:54:48 -05:00
|
|
|
)
|
2019-11-27 16:14:44 -05: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."})
|
2024-06-07 07:01:21 -04:00
|
|
|
logger.debug(
|
|
|
|
"Replacing room key. room=%s session=%s user=%s",
|
|
|
|
room_id,
|
|
|
|
session_id,
|
|
|
|
user_id,
|
|
|
|
)
|
2019-11-27 16:14:44 -05:00
|
|
|
# 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 08:48:58 -04:00
|
|
|
await self.store.update_e2e_room_key(
|
2019-11-27 16:14:44 -05:00
|
|
|
user_id, version, room_id, session_id, room_key
|
|
|
|
)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
log_kv({"message": "Not replacing room_key."})
|
2024-06-07 07:01:21 -04:00
|
|
|
logger.debug(
|
|
|
|
"Not replacing room key. room=%s session=%s user=%s",
|
|
|
|
room_id,
|
|
|
|
session_id,
|
|
|
|
user_id,
|
|
|
|
)
|
2019-11-27 16:14:44 -05:00
|
|
|
else:
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"message": "Room key not found.",
|
|
|
|
"room_id": room_id,
|
|
|
|
"user_id": user_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
log_kv({"message": "Replacing room key."})
|
2024-06-07 07:01:21 -04:00
|
|
|
logger.debug(
|
|
|
|
"Inserting new room key. room=%s session=%s user=%s",
|
|
|
|
room_id,
|
|
|
|
session_id,
|
|
|
|
user_id,
|
|
|
|
)
|
2019-11-27 16:14:44 -05:00
|
|
|
to_insert.append((room_id, session_id, room_key))
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if len(to_insert):
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.add_e2e_room_keys(user_id, version, to_insert)
|
2019-11-27 16:14:44 -05:00
|
|
|
|
|
|
|
version_etag = version_info["etag"]
|
|
|
|
if changed:
|
|
|
|
version_etag = version_etag + 1
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-11-27 16:14:44 -05:00
|
|
|
user_id, version, None, version_etag
|
2019-08-22 06:28:12 -04:00
|
|
|
)
|
2017-12-05 16:44:25 -05:00
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
count = await self.store.count_e2e_room_keys(user_id, version)
|
2019-11-27 16:14:44 -05:00
|
|
|
return {"etag": str(version_etag), "count": count}
|
2017-12-17 20:52:46 -05:00
|
|
|
|
2017-12-24 10:03:44 -05:00
|
|
|
@staticmethod
|
2021-01-28 08:34:19 -05:00
|
|
|
def _should_replace_room_key(
|
2021-12-14 12:46:47 -05:00
|
|
|
current_room_key: Optional[RoomKey], room_key: RoomKey
|
2021-01-28 08:34:19 -05:00
|
|
|
) -> bool:
|
2017-12-17 20:52:46 -05:00
|
|
|
"""
|
2017-12-24 10:03:44 -05:00
|
|
|
Determine whether to replace a given current_room_key (if any)
|
|
|
|
with a newly uploaded room_key backup
|
2017-12-17 20:52:46 -05:00
|
|
|
|
|
|
|
Args:
|
2021-01-28 08:34:19 -05: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 20:52:46 -05:00
|
|
|
replace the current_room_key
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if current_room_key should be replaced by room_key in the backup
|
|
|
|
"""
|
|
|
|
|
2017-12-05 16:44:25 -05:00
|
|
|
if current_room_key:
|
2017-12-17 20:52:46 -05:00
|
|
|
# spelt out with if/elifs rather than nested boolean expressions
|
|
|
|
# purely for legibility.
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
if room_key["is_verified"] and not current_room_key["is_verified"]:
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2017-12-05 16:44:25 -05:00
|
|
|
elif (
|
2019-06-20 05:32:02 -04:00
|
|
|
room_key["first_message_index"]
|
|
|
|
< current_room_key["first_message_index"]
|
2017-12-05 16:44:25 -05:00
|
|
|
):
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2019-06-20 05:32:02 -04:00
|
|
|
elif room_key["forwarded_count"] < current_room_key["forwarded_count"]:
|
2018-10-12 06:26:18 -04:00
|
|
|
return True
|
2017-12-05 16:44:25 -05:00
|
|
|
else:
|
2017-12-17 20:52:46 -05:00
|
|
|
return False
|
|
|
|
return True
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05:00
|
|
|
async def create_version(self, user_id: str, version_info: JsonDict) -> str:
|
2017-12-27 18:37:44 -05: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 18:58:51 -05:00
|
|
|
Args:
|
2021-01-28 08:34:19 -05:00
|
|
|
user_id: the user whose backup version we're creating
|
|
|
|
version_info: metadata about the new version being created
|
2017-12-27 18:37:44 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
|
|
|
|
Returns:
|
2021-01-28 08:34:19 -05:00
|
|
|
The new version number.
|
2017-12-27 18:37:44 -05:00
|
|
|
"""
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
# TODO: Validate the JSON to make sure it has the right keys.
|
|
|
|
|
|
|
|
# lock everyone out until we've switched version
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.write(user_id):
|
2020-07-15 08:48:58 -04:00
|
|
|
new_version = await self.store.create_e2e_room_keys_version(
|
2017-12-06 04:02:49 -05:00
|
|
|
user_id, version_info
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return new_version
|
2017-12-05 20:02:57 -05:00
|
|
|
|
2021-01-28 08:34:19 -05:00
|
|
|
async def get_version_info(
|
|
|
|
self, user_id: str, version: Optional[str] = None
|
|
|
|
) -> JsonDict:
|
2017-12-31 09:10:31 -05:00
|
|
|
"""Get the info about a given version of the user's backup
|
2017-12-27 18:37:44 -05:00
|
|
|
|
|
|
|
Args:
|
2021-01-28 08:34:19 -05:00
|
|
|
user_id: the user whose current backup version we're querying
|
|
|
|
version: Optional; if None gives the most recent version
|
2017-12-31 09:10:31 -05:00
|
|
|
otherwise a historical one.
|
2017-12-27 18:37:44 -05:00
|
|
|
Raises:
|
2019-02-06 17:47:22 -05:00
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
2017-12-27 18:37:44 -05:00
|
|
|
Returns:
|
2021-01-28 08:34:19 -05:00
|
|
|
A info dict that gives the info about the new version.
|
2017-12-27 18:37:44 -05:00
|
|
|
|
|
|
|
{
|
2017-12-31 09:10:31 -05:00
|
|
|
"version": "1234",
|
2017-12-27 18:37:44 -05:00
|
|
|
"algorithm": "m.megolm_backup.v1",
|
|
|
|
"auth_data": "dGhpcyBzaG91bGQgYWN0dWFsbHkgYmUgZW5jcnlwdGVkIGpzb24K"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.read(user_id):
|
2019-02-06 17:47:22 -05:00
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
res = await self.store.get_e2e_room_keys_version_info(user_id, version)
|
2019-02-06 17:47:22 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2019-11-27 16:14:44 -05:00
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
res["count"] = await self.store.count_e2e_room_keys(user_id, res["version"])
|
2020-06-15 13:42:44 -04:00
|
|
|
res["etag"] = str(res["etag"])
|
2019-07-23 09:00:55 -04:00
|
|
|
return res
|
2017-12-31 12:47:11 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05:00
|
|
|
async def delete_version(self, user_id: str, version: Optional[str] = None) -> None:
|
2017-12-27 18:37:44 -05:00
|
|
|
"""Deletes a given version of the user's e2e_room_keys backup
|
|
|
|
|
|
|
|
Args:
|
2022-11-16 10:25:24 -05:00
|
|
|
user_id: the user whose current backup version we're deleting
|
|
|
|
version: Optional. the version ID of the backup version we're deleting
|
|
|
|
If missing, we delete the current backup version info.
|
2017-12-27 18:37:44 -05:00
|
|
|
Raises:
|
2019-02-06 17:47:22 -05:00
|
|
|
NotFoundError: if this backup version doesn't exist
|
2017-12-27 18:37:44 -05:00
|
|
|
"""
|
|
|
|
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.write(user_id):
|
2019-02-06 17:47:22 -05:00
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.delete_e2e_room_keys_version(user_id, version)
|
2019-02-06 17:47:22 -05:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError("Unknown backup version")
|
|
|
|
else:
|
|
|
|
raise
|
2019-02-06 17:57:10 -05:00
|
|
|
|
2019-08-22 06:28:12 -04:00
|
|
|
@trace
|
2021-01-28 08:34:19 -05:00
|
|
|
async def update_version(
|
|
|
|
self, user_id: str, version: str, version_info: JsonDict
|
|
|
|
) -> JsonDict:
|
2019-02-06 17:57:10 -05:00
|
|
|
"""Update the info about a given version of the user's backup
|
|
|
|
|
|
|
|
Args:
|
2021-01-28 08:34:19 -05: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 17:57:10 -05:00
|
|
|
Raises:
|
|
|
|
NotFoundError: if the requested backup version doesn't exist
|
|
|
|
Returns:
|
2021-01-28 08:34:19 -05:00
|
|
|
An empty dict.
|
2019-02-06 17:57:10 -05:00
|
|
|
"""
|
2019-02-08 01:32:45 -05:00
|
|
|
if "version" not in version_info:
|
2019-10-09 17:54:03 -04:00
|
|
|
version_info["version"] = version
|
|
|
|
elif version_info["version"] != version:
|
2019-02-06 17:57:10 -05:00
|
|
|
raise SynapseError(
|
2019-06-20 05:32:02 -04:00
|
|
|
400, "Version in body does not match", Codes.INVALID_PARAM
|
2019-02-06 17:57:10 -05:00
|
|
|
)
|
2024-07-22 09:51:17 -04:00
|
|
|
async with self._upload_lock.write(user_id):
|
2019-02-08 01:32:45 -05:00
|
|
|
try:
|
2020-07-15 08:48:58 -04:00
|
|
|
old_info = await self.store.get_e2e_room_keys_version_info(
|
2019-02-08 01:32:45 -05: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"]:
|
2019-06-20 05:32:02 -04:00
|
|
|
raise SynapseError(400, "Algorithm does not match", Codes.INVALID_PARAM)
|
2019-02-06 17:57:10 -05:00
|
|
|
|
2020-07-15 08:48:58 -04:00
|
|
|
await self.store.update_e2e_room_keys_version(
|
2019-06-20 05:32:02 -04:00
|
|
|
user_id, version, version_info
|
|
|
|
)
|
2019-02-06 17:57:10 -05:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return {}
|